Merge pull request #199 from harvesthq/abstract-chosen
Introduce abstract-chosen, try to start sharing more code between platforms
This commit is contained in:
commit
4a48fbcd11
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1 +1,2 @@
|
|||
.DS_Store
|
||||
.DS_Store
|
||||
node_modules
|
||||
|
|
6
Cakefile
6
Cakefile
|
@ -11,12 +11,14 @@ CoffeeScript = require 'coffee-script'
|
|||
|
||||
javascripts = {
|
||||
'chosen/chosen.jquery.js': [
|
||||
'coffee/chosen.jquery.coffee'
|
||||
'coffee/lib/select-parser.coffee'
|
||||
'coffee/lib/abstract-chosen.coffee'
|
||||
'coffee/chosen.jquery.coffee'
|
||||
]
|
||||
'chosen/chosen.proto.js': [
|
||||
'coffee/chosen.proto.coffee'
|
||||
'coffee/lib/select-parser.coffee'
|
||||
'coffee/lib/abstract-chosen.coffee'
|
||||
'coffee/chosen.proto.coffee'
|
||||
]
|
||||
}
|
||||
|
||||
|
|
|
@ -7,40 +7,101 @@
|
|||
|
||||
// MIT License, https://github.com/harvesthq/chosen/blob/master/LICENSE.md
|
||||
// This file is generated by `cake build`, do not edit it by hand.
|
||||
(function() {
|
||||
var SelectParser;
|
||||
SelectParser = (function() {
|
||||
function SelectParser() {
|
||||
this.options_index = 0;
|
||||
this.parsed = [];
|
||||
}
|
||||
SelectParser.prototype.add_node = function(child) {
|
||||
if (child.nodeName === "OPTGROUP") {
|
||||
return this.add_group(child);
|
||||
} else {
|
||||
return this.add_option(child);
|
||||
}
|
||||
};
|
||||
SelectParser.prototype.add_group = function(group) {
|
||||
var group_position, option, _i, _len, _ref, _results;
|
||||
group_position = this.parsed.length;
|
||||
this.parsed.push({
|
||||
array_index: group_position,
|
||||
group: true,
|
||||
label: group.label,
|
||||
children: 0,
|
||||
disabled: group.disabled
|
||||
});
|
||||
_ref = group.childNodes;
|
||||
_results = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
option = _ref[_i];
|
||||
_results.push(this.add_option(option, group_position, group.disabled));
|
||||
}
|
||||
return _results;
|
||||
};
|
||||
SelectParser.prototype.add_option = function(option, group_position, group_disabled) {
|
||||
if (option.nodeName === "OPTION") {
|
||||
if (option.text !== "") {
|
||||
if (group_position != null) {
|
||||
this.parsed[group_position].children += 1;
|
||||
}
|
||||
this.parsed.push({
|
||||
array_index: this.parsed.length,
|
||||
options_index: this.options_index,
|
||||
value: option.value,
|
||||
text: option.text,
|
||||
html: option.innerHTML,
|
||||
selected: option.selected,
|
||||
disabled: group_disabled === true ? group_disabled : option.disabled,
|
||||
group_array_index: group_position,
|
||||
classes: option.className,
|
||||
style: option.style.cssText
|
||||
});
|
||||
} else {
|
||||
this.parsed.push({
|
||||
array_index: this.parsed.length,
|
||||
options_index: this.options_index,
|
||||
empty: true
|
||||
});
|
||||
}
|
||||
return this.options_index += 1;
|
||||
}
|
||||
};
|
||||
return SelectParser;
|
||||
})();
|
||||
SelectParser.select_to_array = function(select) {
|
||||
var child, parser, _i, _len, _ref;
|
||||
parser = new SelectParser();
|
||||
_ref = select.childNodes;
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
child = _ref[_i];
|
||||
parser.add_node(child);
|
||||
}
|
||||
return parser.parsed;
|
||||
};
|
||||
this.SelectParser = SelectParser;
|
||||
}).call(this);
|
||||
(function() {
|
||||
/*
|
||||
Chosen source: generate output using 'cake build'
|
||||
Copyright (c) 2011 by Harvest
|
||||
*/ var $, Chosen, get_side_border_padding, root;
|
||||
*/
|
||||
var AbstractChosen, root;
|
||||
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
||||
root = this;
|
||||
$ = jQuery;
|
||||
$.fn.extend({
|
||||
chosen: function(options) {
|
||||
if ($.browser === "msie" && ($.browser.version === "6.0" || $.browser.version === "7.0")) {
|
||||
return this;
|
||||
}
|
||||
return $(this).each(function(input_field) {
|
||||
if (!($(this)).hasClass("chzn-done")) {
|
||||
return new Chosen(this, options);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
Chosen = (function() {
|
||||
function Chosen(form_field, options) {
|
||||
AbstractChosen = (function() {
|
||||
function AbstractChosen(form_field, options) {
|
||||
this.form_field = form_field;
|
||||
this.options = options != null ? options : {};
|
||||
this.set_default_values();
|
||||
this.form_field_jq = $(this.form_field);
|
||||
this.is_multiple = this.form_field.multiple;
|
||||
this.is_rtl = this.form_field_jq.hasClass("chzn-rtl");
|
||||
this.default_text_default = this.form_field.multiple ? "Select Some Options" : "Select an Option";
|
||||
this.default_text_default = this.is_multiple ? "Select Some Options" : "Select an Option";
|
||||
this.setup();
|
||||
this.set_up_html();
|
||||
this.register_observers();
|
||||
this.form_field_jq.addClass("chzn-done");
|
||||
this.finish_setup();
|
||||
}
|
||||
Chosen.prototype.set_default_values = function() {
|
||||
AbstractChosen.prototype.set_default_values = function() {
|
||||
this.click_test_action = __bind(function(evt) {
|
||||
return this.test_active_click(evt);
|
||||
}, this);
|
||||
|
@ -57,6 +118,157 @@
|
|||
this.choices = 0;
|
||||
return this.results_none_found = this.options.no_results_text || "No results match";
|
||||
};
|
||||
AbstractChosen.prototype.mouse_enter = function() {
|
||||
return this.mouse_on_container = true;
|
||||
};
|
||||
AbstractChosen.prototype.mouse_leave = function() {
|
||||
return this.mouse_on_container = false;
|
||||
};
|
||||
AbstractChosen.prototype.input_focus = function(evt) {
|
||||
if (!this.active_field) {
|
||||
return setTimeout((__bind(function() {
|
||||
return this.container_mousedown();
|
||||
}, this)), 50);
|
||||
}
|
||||
};
|
||||
AbstractChosen.prototype.input_blur = function(evt) {
|
||||
if (!this.mouse_on_container) {
|
||||
this.active_field = false;
|
||||
return setTimeout((__bind(function() {
|
||||
return this.blur_test();
|
||||
}, this)), 100);
|
||||
}
|
||||
};
|
||||
AbstractChosen.prototype.result_add_option = function(option) {
|
||||
var classes, style;
|
||||
if (!option.disabled) {
|
||||
option.dom_id = this.container_id + "_o_" + option.array_index;
|
||||
classes = option.selected && this.is_multiple ? [] : ["active-result"];
|
||||
if (option.selected) {
|
||||
classes.push("result-selected");
|
||||
}
|
||||
if (option.group_array_index != null) {
|
||||
classes.push("group-option");
|
||||
}
|
||||
if (option.classes !== "") {
|
||||
classes.push(option.classes);
|
||||
}
|
||||
style = option.style.cssText !== "" ? " style=\"" + option.style + "\"" : "";
|
||||
return '<li id="' + option.dom_id + '" class="' + classes.join(' ') + '"' + style + '>' + option.html + '</li>';
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
};
|
||||
AbstractChosen.prototype.results_update_field = function() {
|
||||
this.result_clear_highlight();
|
||||
this.result_single_selected = null;
|
||||
return this.results_build();
|
||||
};
|
||||
AbstractChosen.prototype.results_toggle = function() {
|
||||
if (this.results_showing) {
|
||||
return this.results_hide();
|
||||
} else {
|
||||
return this.results_show();
|
||||
}
|
||||
};
|
||||
AbstractChosen.prototype.results_search = function(evt) {
|
||||
if (this.results_showing) {
|
||||
return this.winnow_results();
|
||||
} else {
|
||||
return this.results_show();
|
||||
}
|
||||
};
|
||||
AbstractChosen.prototype.keyup_checker = function(evt) {
|
||||
var stroke, _ref;
|
||||
stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
|
||||
this.search_field_scale();
|
||||
switch (stroke) {
|
||||
case 8:
|
||||
if (this.is_multiple && this.backstroke_length < 1 && this.choices > 0) {
|
||||
return this.keydown_backstroke();
|
||||
} else if (!this.pending_backstroke) {
|
||||
this.result_clear_highlight();
|
||||
return this.results_search();
|
||||
}
|
||||
break;
|
||||
case 13:
|
||||
evt.preventDefault();
|
||||
if (this.results_showing) {
|
||||
return this.result_select(evt);
|
||||
}
|
||||
break;
|
||||
case 27:
|
||||
if (this.results_showing) {
|
||||
return this.results_hide();
|
||||
}
|
||||
break;
|
||||
case 9:
|
||||
case 38:
|
||||
case 40:
|
||||
case 16:
|
||||
case 91:
|
||||
case 17:
|
||||
break;
|
||||
default:
|
||||
return this.results_search();
|
||||
}
|
||||
};
|
||||
AbstractChosen.prototype.generate_field_id = function() {
|
||||
var new_id;
|
||||
new_id = this.generate_random_id();
|
||||
this.form_field.id = new_id;
|
||||
return new_id;
|
||||
};
|
||||
AbstractChosen.prototype.generate_random_char = function() {
|
||||
var chars, newchar, rand;
|
||||
chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ";
|
||||
rand = Math.floor(Math.random() * chars.length);
|
||||
return newchar = chars.substring(rand, rand + 1);
|
||||
};
|
||||
return AbstractChosen;
|
||||
})();
|
||||
root.AbstractChosen = AbstractChosen;
|
||||
}).call(this);
|
||||
(function() {
|
||||
/*
|
||||
Chosen source: generate output using 'cake build'
|
||||
Copyright (c) 2011 by Harvest
|
||||
*/
|
||||
var $, Chosen, get_side_border_padding, root;
|
||||
var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
|
||||
for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
|
||||
function ctor() { this.constructor = child; }
|
||||
ctor.prototype = parent.prototype;
|
||||
child.prototype = new ctor;
|
||||
child.__super__ = parent.prototype;
|
||||
return child;
|
||||
}, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
||||
root = this;
|
||||
$ = jQuery;
|
||||
$.fn.extend({
|
||||
chosen: function(options) {
|
||||
if ($.browser === "msie" && ($.browser.version === "6.0" || $.browser.version === "7.0")) {
|
||||
return this;
|
||||
}
|
||||
return $(this).each(function(input_field) {
|
||||
if (!($(this)).hasClass("chzn-done")) {
|
||||
return new Chosen(this, options);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
Chosen = (function() {
|
||||
__extends(Chosen, AbstractChosen);
|
||||
function Chosen() {
|
||||
Chosen.__super__.constructor.apply(this, arguments);
|
||||
}
|
||||
Chosen.prototype.setup = function() {
|
||||
this.form_field_jq = $(this.form_field);
|
||||
return this.is_rtl = this.form_field_jq.hasClass("chzn-rtl");
|
||||
};
|
||||
Chosen.prototype.finish_setup = function() {
|
||||
return this.form_field_jq.addClass("chzn-done");
|
||||
};
|
||||
Chosen.prototype.set_up_html = function() {
|
||||
var container_div, dd_top, dd_width, sf_width;
|
||||
this.container_id = this.form_field.id.length ? this.form_field.id.replace(/(:|\.)/g, '_') : this.generate_field_id();
|
||||
|
@ -193,27 +405,6 @@
|
|||
return this.results_reset(evt);
|
||||
}
|
||||
};
|
||||
Chosen.prototype.mouse_enter = function() {
|
||||
return this.mouse_on_container = true;
|
||||
};
|
||||
Chosen.prototype.mouse_leave = function() {
|
||||
return this.mouse_on_container = false;
|
||||
};
|
||||
Chosen.prototype.input_focus = function(evt) {
|
||||
if (!this.active_field) {
|
||||
return setTimeout((__bind(function() {
|
||||
return this.container_mousedown();
|
||||
}, this)), 50);
|
||||
}
|
||||
};
|
||||
Chosen.prototype.input_blur = function(evt) {
|
||||
if (!this.mouse_on_container) {
|
||||
this.active_field = false;
|
||||
return setTimeout((__bind(function() {
|
||||
return this.blur_test();
|
||||
}, this)), 100);
|
||||
}
|
||||
};
|
||||
Chosen.prototype.blur_test = function(evt) {
|
||||
if (!this.active_field && this.container.hasClass("chzn-container-active")) {
|
||||
return this.close_field();
|
||||
|
@ -293,31 +484,6 @@
|
|||
return "";
|
||||
}
|
||||
};
|
||||
Chosen.prototype.result_add_option = function(option) {
|
||||
var classes, style;
|
||||
if (!option.disabled) {
|
||||
option.dom_id = this.container_id + "_o_" + option.array_index;
|
||||
classes = option.selected && this.is_multiple ? [] : ["active-result"];
|
||||
if (option.selected) {
|
||||
classes.push("result-selected");
|
||||
}
|
||||
if (option.group_array_index != null) {
|
||||
classes.push("group-option");
|
||||
}
|
||||
if (option.classes !== "") {
|
||||
classes.push(option.classes);
|
||||
}
|
||||
style = option.style.cssText !== "" ? " style=\"" + option.style + "\"" : "";
|
||||
return '<li id="' + option.dom_id + '" class="' + classes.join(' ') + '"' + style + '>' + option.html + '</li>';
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
};
|
||||
Chosen.prototype.results_update_field = function() {
|
||||
this.result_clear_highlight();
|
||||
this.result_single_selected = null;
|
||||
return this.results_build();
|
||||
};
|
||||
Chosen.prototype.result_do_highlight = function(el) {
|
||||
var high_bottom, high_top, maxHeight, visible_bottom, visible_top;
|
||||
if (el.length) {
|
||||
|
@ -342,13 +508,6 @@
|
|||
}
|
||||
return this.result_highlight = null;
|
||||
};
|
||||
Chosen.prototype.results_toggle = function() {
|
||||
if (this.results_showing) {
|
||||
return this.results_hide();
|
||||
} else {
|
||||
return this.results_show();
|
||||
}
|
||||
};
|
||||
Chosen.prototype.results_show = function() {
|
||||
var dd_top;
|
||||
if (!this.is_multiple) {
|
||||
|
@ -514,13 +673,6 @@
|
|||
this.form_field_jq.trigger("change");
|
||||
return this.search_field_scale();
|
||||
};
|
||||
Chosen.prototype.results_search = function(evt) {
|
||||
if (this.results_showing) {
|
||||
return this.winnow_results();
|
||||
} else {
|
||||
return this.results_show();
|
||||
}
|
||||
};
|
||||
Chosen.prototype.winnow_results = function() {
|
||||
var found, option, part, parts, regex, result_id, results, searchText, startTime, startpos, text, zregex, _i, _j, _len, _len2, _ref;
|
||||
startTime = new Date();
|
||||
|
@ -662,41 +814,6 @@
|
|||
}
|
||||
return this.pending_backstroke = null;
|
||||
};
|
||||
Chosen.prototype.keyup_checker = function(evt) {
|
||||
var stroke, _ref;
|
||||
stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
|
||||
this.search_field_scale();
|
||||
switch (stroke) {
|
||||
case 8:
|
||||
if (this.is_multiple && this.backstroke_length < 1 && this.choices > 0) {
|
||||
return this.keydown_backstroke();
|
||||
} else if (!this.pending_backstroke) {
|
||||
this.result_clear_highlight();
|
||||
return this.results_search();
|
||||
}
|
||||
break;
|
||||
case 13:
|
||||
evt.preventDefault();
|
||||
if (this.results_showing) {
|
||||
return this.result_select(evt);
|
||||
}
|
||||
break;
|
||||
case 27:
|
||||
if (this.results_showing) {
|
||||
return this.results_hide();
|
||||
}
|
||||
break;
|
||||
case 9:
|
||||
case 38:
|
||||
case 40:
|
||||
case 16:
|
||||
case 91:
|
||||
case 17:
|
||||
break;
|
||||
default:
|
||||
return this.results_search();
|
||||
}
|
||||
};
|
||||
Chosen.prototype.keydown_checker = function(evt) {
|
||||
var stroke, _ref;
|
||||
stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
|
||||
|
@ -753,12 +870,6 @@
|
|||
});
|
||||
}
|
||||
};
|
||||
Chosen.prototype.generate_field_id = function() {
|
||||
var new_id;
|
||||
new_id = this.generate_random_id();
|
||||
this.form_field.id = new_id;
|
||||
return new_id;
|
||||
};
|
||||
Chosen.prototype.generate_random_id = function() {
|
||||
var string;
|
||||
string = "sel" + this.generate_random_char() + this.generate_random_char() + this.generate_random_char();
|
||||
|
@ -767,12 +878,6 @@
|
|||
}
|
||||
return string;
|
||||
};
|
||||
Chosen.prototype.generate_random_char = function() {
|
||||
var chars, newchar, rand;
|
||||
chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ";
|
||||
rand = Math.floor(Math.random() * chars.length);
|
||||
return newchar = chars.substring(rand, rand + 1);
|
||||
};
|
||||
return Chosen;
|
||||
})();
|
||||
get_side_border_padding = function(elmt) {
|
||||
|
@ -781,77 +886,3 @@
|
|||
};
|
||||
root.get_side_border_padding = get_side_border_padding;
|
||||
}).call(this);
|
||||
(function() {
|
||||
var SelectParser;
|
||||
SelectParser = (function() {
|
||||
function SelectParser() {
|
||||
this.options_index = 0;
|
||||
this.parsed = [];
|
||||
}
|
||||
SelectParser.prototype.add_node = function(child) {
|
||||
if (child.nodeName === "OPTGROUP") {
|
||||
return this.add_group(child);
|
||||
} else {
|
||||
return this.add_option(child);
|
||||
}
|
||||
};
|
||||
SelectParser.prototype.add_group = function(group) {
|
||||
var group_position, option, _i, _len, _ref, _results;
|
||||
group_position = this.parsed.length;
|
||||
this.parsed.push({
|
||||
array_index: group_position,
|
||||
group: true,
|
||||
label: group.label,
|
||||
children: 0,
|
||||
disabled: group.disabled
|
||||
});
|
||||
_ref = group.childNodes;
|
||||
_results = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
option = _ref[_i];
|
||||
_results.push(this.add_option(option, group_position, group.disabled));
|
||||
}
|
||||
return _results;
|
||||
};
|
||||
SelectParser.prototype.add_option = function(option, group_position, group_disabled) {
|
||||
if (option.nodeName === "OPTION") {
|
||||
if (option.text !== "") {
|
||||
if (group_position != null) {
|
||||
this.parsed[group_position].children += 1;
|
||||
}
|
||||
this.parsed.push({
|
||||
array_index: this.parsed.length,
|
||||
options_index: this.options_index,
|
||||
value: option.value,
|
||||
text: option.text,
|
||||
html: option.innerHTML,
|
||||
selected: option.selected,
|
||||
disabled: group_disabled === true ? group_disabled : option.disabled,
|
||||
group_array_index: group_position,
|
||||
classes: option.className,
|
||||
style: option.style.cssText
|
||||
});
|
||||
} else {
|
||||
this.parsed.push({
|
||||
array_index: this.parsed.length,
|
||||
options_index: this.options_index,
|
||||
empty: true
|
||||
});
|
||||
}
|
||||
return this.options_index += 1;
|
||||
}
|
||||
};
|
||||
return SelectParser;
|
||||
})();
|
||||
SelectParser.select_to_array = function(select) {
|
||||
var child, parser, _i, _len, _ref;
|
||||
parser = new SelectParser();
|
||||
_ref = select.childNodes;
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
child = _ref[_i];
|
||||
parser.add_node(child);
|
||||
}
|
||||
return parser.parsed;
|
||||
};
|
||||
this.SelectParser = SelectParser;
|
||||
}).call(this);
|
||||
|
|
2
chosen/chosen.jquery.min.js
vendored
2
chosen/chosen.jquery.min.js
vendored
File diff suppressed because one or more lines are too long
|
@ -7,26 +7,101 @@
|
|||
|
||||
// MIT License, https://github.com/harvesthq/chosen/blob/master/LICENSE.md
|
||||
// This file is generated by `cake build`, do not edit it by hand.
|
||||
(function() {
|
||||
var SelectParser;
|
||||
SelectParser = (function() {
|
||||
function SelectParser() {
|
||||
this.options_index = 0;
|
||||
this.parsed = [];
|
||||
}
|
||||
SelectParser.prototype.add_node = function(child) {
|
||||
if (child.nodeName === "OPTGROUP") {
|
||||
return this.add_group(child);
|
||||
} else {
|
||||
return this.add_option(child);
|
||||
}
|
||||
};
|
||||
SelectParser.prototype.add_group = function(group) {
|
||||
var group_position, option, _i, _len, _ref, _results;
|
||||
group_position = this.parsed.length;
|
||||
this.parsed.push({
|
||||
array_index: group_position,
|
||||
group: true,
|
||||
label: group.label,
|
||||
children: 0,
|
||||
disabled: group.disabled
|
||||
});
|
||||
_ref = group.childNodes;
|
||||
_results = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
option = _ref[_i];
|
||||
_results.push(this.add_option(option, group_position, group.disabled));
|
||||
}
|
||||
return _results;
|
||||
};
|
||||
SelectParser.prototype.add_option = function(option, group_position, group_disabled) {
|
||||
if (option.nodeName === "OPTION") {
|
||||
if (option.text !== "") {
|
||||
if (group_position != null) {
|
||||
this.parsed[group_position].children += 1;
|
||||
}
|
||||
this.parsed.push({
|
||||
array_index: this.parsed.length,
|
||||
options_index: this.options_index,
|
||||
value: option.value,
|
||||
text: option.text,
|
||||
html: option.innerHTML,
|
||||
selected: option.selected,
|
||||
disabled: group_disabled === true ? group_disabled : option.disabled,
|
||||
group_array_index: group_position,
|
||||
classes: option.className,
|
||||
style: option.style.cssText
|
||||
});
|
||||
} else {
|
||||
this.parsed.push({
|
||||
array_index: this.parsed.length,
|
||||
options_index: this.options_index,
|
||||
empty: true
|
||||
});
|
||||
}
|
||||
return this.options_index += 1;
|
||||
}
|
||||
};
|
||||
return SelectParser;
|
||||
})();
|
||||
SelectParser.select_to_array = function(select) {
|
||||
var child, parser, _i, _len, _ref;
|
||||
parser = new SelectParser();
|
||||
_ref = select.childNodes;
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
child = _ref[_i];
|
||||
parser.add_node(child);
|
||||
}
|
||||
return parser.parsed;
|
||||
};
|
||||
this.SelectParser = SelectParser;
|
||||
}).call(this);
|
||||
(function() {
|
||||
/*
|
||||
Chosen source: generate output using 'cake build'
|
||||
Copyright (c) 2011 by Harvest
|
||||
*/ var Chosen, get_side_border_padding, root;
|
||||
*/
|
||||
var AbstractChosen, root;
|
||||
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
||||
root = this;
|
||||
Chosen = (function() {
|
||||
function Chosen(form_field, options) {
|
||||
AbstractChosen = (function() {
|
||||
function AbstractChosen(form_field, options) {
|
||||
this.form_field = form_field;
|
||||
this.options = options != null ? options : {};
|
||||
this.set_default_values();
|
||||
this.is_multiple = this.form_field.multiple;
|
||||
this.is_rtl = this.form_field.hasClassName("chzn-rtl");
|
||||
this.default_text_default = this.form_field.multiple ? "Select Some Options" : "Select an Option";
|
||||
this.default_text_default = this.is_multiple ? "Select Some Options" : "Select an Option";
|
||||
this.setup();
|
||||
this.set_up_html();
|
||||
this.register_observers();
|
||||
this.form_field.addClassName("chzn-done");
|
||||
this.finish_setup();
|
||||
}
|
||||
Chosen.prototype.set_default_values = function() {
|
||||
AbstractChosen.prototype.set_default_values = function() {
|
||||
this.click_test_action = __bind(function(evt) {
|
||||
return this.test_active_click(evt);
|
||||
}, this);
|
||||
|
@ -41,7 +116,147 @@
|
|||
this.allow_single_deselect = (this.options.allow_single_deselect != null) && this.form_field.options[0].text === "" ? this.options.allow_single_deselect : false;
|
||||
this.disable_search_threshold = this.options.disable_search_threshold || 0;
|
||||
this.choices = 0;
|
||||
this.results_none_found = this.options.no_results_text || "No results match";
|
||||
return this.results_none_found = this.options.no_results_text || "No results match";
|
||||
};
|
||||
AbstractChosen.prototype.mouse_enter = function() {
|
||||
return this.mouse_on_container = true;
|
||||
};
|
||||
AbstractChosen.prototype.mouse_leave = function() {
|
||||
return this.mouse_on_container = false;
|
||||
};
|
||||
AbstractChosen.prototype.input_focus = function(evt) {
|
||||
if (!this.active_field) {
|
||||
return setTimeout((__bind(function() {
|
||||
return this.container_mousedown();
|
||||
}, this)), 50);
|
||||
}
|
||||
};
|
||||
AbstractChosen.prototype.input_blur = function(evt) {
|
||||
if (!this.mouse_on_container) {
|
||||
this.active_field = false;
|
||||
return setTimeout((__bind(function() {
|
||||
return this.blur_test();
|
||||
}, this)), 100);
|
||||
}
|
||||
};
|
||||
AbstractChosen.prototype.result_add_option = function(option) {
|
||||
var classes, style;
|
||||
if (!option.disabled) {
|
||||
option.dom_id = this.container_id + "_o_" + option.array_index;
|
||||
classes = option.selected && this.is_multiple ? [] : ["active-result"];
|
||||
if (option.selected) {
|
||||
classes.push("result-selected");
|
||||
}
|
||||
if (option.group_array_index != null) {
|
||||
classes.push("group-option");
|
||||
}
|
||||
if (option.classes !== "") {
|
||||
classes.push(option.classes);
|
||||
}
|
||||
style = option.style.cssText !== "" ? " style=\"" + option.style + "\"" : "";
|
||||
return '<li id="' + option.dom_id + '" class="' + classes.join(' ') + '"' + style + '>' + option.html + '</li>';
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
};
|
||||
AbstractChosen.prototype.results_update_field = function() {
|
||||
this.result_clear_highlight();
|
||||
this.result_single_selected = null;
|
||||
return this.results_build();
|
||||
};
|
||||
AbstractChosen.prototype.results_toggle = function() {
|
||||
if (this.results_showing) {
|
||||
return this.results_hide();
|
||||
} else {
|
||||
return this.results_show();
|
||||
}
|
||||
};
|
||||
AbstractChosen.prototype.results_search = function(evt) {
|
||||
if (this.results_showing) {
|
||||
return this.winnow_results();
|
||||
} else {
|
||||
return this.results_show();
|
||||
}
|
||||
};
|
||||
AbstractChosen.prototype.keyup_checker = function(evt) {
|
||||
var stroke, _ref;
|
||||
stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
|
||||
this.search_field_scale();
|
||||
switch (stroke) {
|
||||
case 8:
|
||||
if (this.is_multiple && this.backstroke_length < 1 && this.choices > 0) {
|
||||
return this.keydown_backstroke();
|
||||
} else if (!this.pending_backstroke) {
|
||||
this.result_clear_highlight();
|
||||
return this.results_search();
|
||||
}
|
||||
break;
|
||||
case 13:
|
||||
evt.preventDefault();
|
||||
if (this.results_showing) {
|
||||
return this.result_select(evt);
|
||||
}
|
||||
break;
|
||||
case 27:
|
||||
if (this.results_showing) {
|
||||
return this.results_hide();
|
||||
}
|
||||
break;
|
||||
case 9:
|
||||
case 38:
|
||||
case 40:
|
||||
case 16:
|
||||
case 91:
|
||||
case 17:
|
||||
break;
|
||||
default:
|
||||
return this.results_search();
|
||||
}
|
||||
};
|
||||
AbstractChosen.prototype.generate_field_id = function() {
|
||||
var new_id;
|
||||
new_id = this.generate_random_id();
|
||||
this.form_field.id = new_id;
|
||||
return new_id;
|
||||
};
|
||||
AbstractChosen.prototype.generate_random_char = function() {
|
||||
var chars, newchar, rand;
|
||||
chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ";
|
||||
rand = Math.floor(Math.random() * chars.length);
|
||||
return newchar = chars.substring(rand, rand + 1);
|
||||
};
|
||||
return AbstractChosen;
|
||||
})();
|
||||
root.AbstractChosen = AbstractChosen;
|
||||
}).call(this);
|
||||
(function() {
|
||||
/*
|
||||
Chosen source: generate output using 'cake build'
|
||||
Copyright (c) 2011 by Harvest
|
||||
*/
|
||||
var Chosen, get_side_border_padding, root;
|
||||
var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
|
||||
for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
|
||||
function ctor() { this.constructor = child; }
|
||||
ctor.prototype = parent.prototype;
|
||||
child.prototype = new ctor;
|
||||
child.__super__ = parent.prototype;
|
||||
return child;
|
||||
}, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
||||
root = this;
|
||||
Chosen = (function() {
|
||||
__extends(Chosen, AbstractChosen);
|
||||
function Chosen() {
|
||||
Chosen.__super__.constructor.apply(this, arguments);
|
||||
}
|
||||
Chosen.prototype.setup = function() {
|
||||
return this.is_rtl = this.form_field.hasClassName("chzn-rtl");
|
||||
};
|
||||
Chosen.prototype.finish_setup = function() {
|
||||
return this.form_field.addClassName("chzn-done");
|
||||
};
|
||||
Chosen.prototype.set_default_values = function() {
|
||||
Chosen.__super__.set_default_values.call(this);
|
||||
this.single_temp = new Template('<a href="javascript:void(0)" class="chzn-single"><span>#{default}</span><div><b></b></div></a><div class="chzn-drop" style="left:-9000px;"><div class="chzn-search"><input type="text" autocomplete="off" /></div><ul class="chzn-results"></ul></div>');
|
||||
this.multi_temp = new Template('<ul class="chzn-choices"><li class="search-field"><input type="text" value="#{default}" class="default" autocomplete="off" style="width:25px;" /></li></ul><div class="chzn-drop" style="left:-9000px;"><ul class="chzn-results"></ul></div>');
|
||||
this.choice_temp = new Template('<li class="search-choice" id="#{id}"><span>#{choice}</span><a href="javascript:void(0)" class="search-choice-close" rel="#{position}"></a></li>');
|
||||
|
@ -183,23 +398,6 @@
|
|||
return this.results_reset(evt);
|
||||
}
|
||||
};
|
||||
Chosen.prototype.mouse_enter = function() {
|
||||
return this.mouse_on_container = true;
|
||||
};
|
||||
Chosen.prototype.mouse_leave = function() {
|
||||
return this.mouse_on_container = false;
|
||||
};
|
||||
Chosen.prototype.input_focus = function(evt) {
|
||||
if (!this.active_field) {
|
||||
return setTimeout(this.container_mousedown.bind(this), 50);
|
||||
}
|
||||
};
|
||||
Chosen.prototype.input_blur = function(evt) {
|
||||
if (!this.mouse_on_container) {
|
||||
this.active_field = false;
|
||||
return setTimeout(this.blur_test.bind(this), 100);
|
||||
}
|
||||
};
|
||||
Chosen.prototype.blur_test = function(evt) {
|
||||
if (!this.active_field && this.container.hasClassName("chzn-container-active")) {
|
||||
return this.close_field();
|
||||
|
@ -281,31 +479,6 @@
|
|||
return "";
|
||||
}
|
||||
};
|
||||
Chosen.prototype.result_add_option = function(option) {
|
||||
var classes, style;
|
||||
if (!option.disabled) {
|
||||
option.dom_id = this.container_id + "_o_" + option.array_index;
|
||||
classes = option.selected && this.is_multiple ? [] : ["active-result"];
|
||||
if (option.selected) {
|
||||
classes.push("result-selected");
|
||||
}
|
||||
if (option.group_array_index != null) {
|
||||
classes.push("group-option");
|
||||
}
|
||||
if (option.classes !== "") {
|
||||
classes.push(option.classes);
|
||||
}
|
||||
style = option.style.cssText !== "" ? " style=\"" + option.style + "\"" : "";
|
||||
return '<li id="' + option.dom_id + '" class="' + classes.join(' ') + '"' + style + '>' + option.html + '</li>';
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
};
|
||||
Chosen.prototype.results_update_field = function() {
|
||||
this.result_clear_highlight();
|
||||
this.result_single_selected = null;
|
||||
return this.results_build();
|
||||
};
|
||||
Chosen.prototype.result_do_highlight = function(el) {
|
||||
var high_bottom, high_top, maxHeight, visible_bottom, visible_top;
|
||||
this.result_clear_highlight();
|
||||
|
@ -328,13 +501,6 @@
|
|||
}
|
||||
return this.result_highlight = null;
|
||||
};
|
||||
Chosen.prototype.results_toggle = function() {
|
||||
if (this.results_showing) {
|
||||
return this.results_hide();
|
||||
} else {
|
||||
return this.results_show();
|
||||
}
|
||||
};
|
||||
Chosen.prototype.results_show = function() {
|
||||
var dd_top;
|
||||
if (!this.is_multiple) {
|
||||
|
@ -511,13 +677,6 @@
|
|||
}
|
||||
return this.search_field_scale();
|
||||
};
|
||||
Chosen.prototype.results_search = function(evt) {
|
||||
if (this.results_showing) {
|
||||
return this.winnow_results();
|
||||
} else {
|
||||
return this.results_show();
|
||||
}
|
||||
};
|
||||
Chosen.prototype.winnow_results = function() {
|
||||
var found, option, part, parts, regex, result_id, results, searchText, startTime, startpos, text, zregex, _i, _j, _len, _len2, _ref;
|
||||
startTime = new Date();
|
||||
|
@ -670,41 +829,6 @@
|
|||
}
|
||||
return this.pending_backstroke = null;
|
||||
};
|
||||
Chosen.prototype.keyup_checker = function(evt) {
|
||||
var stroke, _ref;
|
||||
stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
|
||||
this.search_field_scale();
|
||||
switch (stroke) {
|
||||
case 8:
|
||||
if (this.is_multiple && this.backstroke_length < 1 && this.choices > 0) {
|
||||
return this.keydown_backstroke();
|
||||
} else if (!this.pending_backstroke) {
|
||||
this.result_clear_highlight();
|
||||
return this.results_search();
|
||||
}
|
||||
break;
|
||||
case 13:
|
||||
evt.preventDefault();
|
||||
if (this.results_showing) {
|
||||
return this.result_select(evt);
|
||||
}
|
||||
break;
|
||||
case 27:
|
||||
if (this.results_showing) {
|
||||
return this.results_hide();
|
||||
}
|
||||
break;
|
||||
case 9:
|
||||
case 38:
|
||||
case 40:
|
||||
case 16:
|
||||
case 91:
|
||||
case 17:
|
||||
break;
|
||||
default:
|
||||
return this.results_search();
|
||||
}
|
||||
};
|
||||
Chosen.prototype.keydown_checker = function(evt) {
|
||||
var stroke, _ref;
|
||||
stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
|
||||
|
@ -770,77 +894,3 @@
|
|||
};
|
||||
root.get_side_border_padding = get_side_border_padding;
|
||||
}).call(this);
|
||||
(function() {
|
||||
var SelectParser;
|
||||
SelectParser = (function() {
|
||||
function SelectParser() {
|
||||
this.options_index = 0;
|
||||
this.parsed = [];
|
||||
}
|
||||
SelectParser.prototype.add_node = function(child) {
|
||||
if (child.nodeName === "OPTGROUP") {
|
||||
return this.add_group(child);
|
||||
} else {
|
||||
return this.add_option(child);
|
||||
}
|
||||
};
|
||||
SelectParser.prototype.add_group = function(group) {
|
||||
var group_position, option, _i, _len, _ref, _results;
|
||||
group_position = this.parsed.length;
|
||||
this.parsed.push({
|
||||
array_index: group_position,
|
||||
group: true,
|
||||
label: group.label,
|
||||
children: 0,
|
||||
disabled: group.disabled
|
||||
});
|
||||
_ref = group.childNodes;
|
||||
_results = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
option = _ref[_i];
|
||||
_results.push(this.add_option(option, group_position, group.disabled));
|
||||
}
|
||||
return _results;
|
||||
};
|
||||
SelectParser.prototype.add_option = function(option, group_position, group_disabled) {
|
||||
if (option.nodeName === "OPTION") {
|
||||
if (option.text !== "") {
|
||||
if (group_position != null) {
|
||||
this.parsed[group_position].children += 1;
|
||||
}
|
||||
this.parsed.push({
|
||||
array_index: this.parsed.length,
|
||||
options_index: this.options_index,
|
||||
value: option.value,
|
||||
text: option.text,
|
||||
html: option.innerHTML,
|
||||
selected: option.selected,
|
||||
disabled: group_disabled === true ? group_disabled : option.disabled,
|
||||
group_array_index: group_position,
|
||||
classes: option.className,
|
||||
style: option.style.cssText
|
||||
});
|
||||
} else {
|
||||
this.parsed.push({
|
||||
array_index: this.parsed.length,
|
||||
options_index: this.options_index,
|
||||
empty: true
|
||||
});
|
||||
}
|
||||
return this.options_index += 1;
|
||||
}
|
||||
};
|
||||
return SelectParser;
|
||||
})();
|
||||
SelectParser.select_to_array = function(select) {
|
||||
var child, parser, _i, _len, _ref;
|
||||
parser = new SelectParser();
|
||||
_ref = select.childNodes;
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
child = _ref[_i];
|
||||
parser.add_node(child);
|
||||
}
|
||||
return parser.parsed;
|
||||
};
|
||||
this.SelectParser = SelectParser;
|
||||
}).call(this);
|
||||
|
|
2
chosen/chosen.proto.min.js
vendored
2
chosen/chosen.proto.min.js
vendored
File diff suppressed because one or more lines are too long
|
@ -11,37 +11,17 @@ $.fn.extend({
|
|||
$(this).each((input_field) ->
|
||||
new Chosen(this, options) unless ($ this).hasClass "chzn-done"
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
class Chosen
|
||||
class Chosen extends AbstractChosen
|
||||
|
||||
constructor: (@form_field, @options={}) ->
|
||||
this.set_default_values()
|
||||
|
||||
setup: ->
|
||||
@form_field_jq = $ @form_field
|
||||
@is_multiple = @form_field.multiple
|
||||
@is_rtl = @form_field_jq.hasClass "chzn-rtl"
|
||||
|
||||
@default_text_default = if @form_field.multiple then "Select Some Options" else "Select an Option"
|
||||
|
||||
this.set_up_html()
|
||||
this.register_observers()
|
||||
finish_setup: ->
|
||||
@form_field_jq.addClass "chzn-done"
|
||||
|
||||
set_default_values: ->
|
||||
|
||||
@click_test_action = (evt) => this.test_active_click(evt)
|
||||
@activate_action = (evt) => this.activate_field(evt)
|
||||
@active_field = false
|
||||
@mouse_on_container = false
|
||||
@results_showing = false
|
||||
@result_highlighted = null
|
||||
@result_single_selected = null
|
||||
@allow_single_deselect = if @options.allow_single_deselect? and @form_field.options[0].text == "" then @options.allow_single_deselect else false
|
||||
@disable_search_threshold = @options.disable_search_threshold || 0
|
||||
@choices = 0
|
||||
@results_none_found = @options.no_results_text or "No results match"
|
||||
|
||||
set_up_html: ->
|
||||
@container_id = if @form_field.id.length then @form_field.id.replace(/(:|\.)/g, '_') else this.generate_field_id()
|
||||
@container_id += "_chzn"
|
||||
|
@ -144,17 +124,6 @@ class Chosen
|
|||
container_mouseup: (evt) ->
|
||||
this.results_reset(evt) if evt.target.nodeName is "ABBR"
|
||||
|
||||
mouse_enter: -> @mouse_on_container = true
|
||||
mouse_leave: -> @mouse_on_container = false
|
||||
|
||||
input_focus: (evt) ->
|
||||
setTimeout (=> this.container_mousedown()), 50 unless @active_field
|
||||
|
||||
input_blur: (evt) ->
|
||||
if not @mouse_on_container
|
||||
@active_field = false
|
||||
setTimeout (=> this.blur_test()), 100
|
||||
|
||||
blur_test: (evt) ->
|
||||
this.close_field() if not @active_field and @container.hasClass "chzn-container-active"
|
||||
|
||||
|
@ -231,26 +200,6 @@ class Chosen
|
|||
else
|
||||
""
|
||||
|
||||
result_add_option: (option) ->
|
||||
if not option.disabled
|
||||
option.dom_id = @container_id + "_o_" + option.array_index
|
||||
|
||||
classes = if option.selected and @is_multiple then [] else ["active-result"]
|
||||
classes.push "result-selected" if option.selected
|
||||
classes.push "group-option" if option.group_array_index?
|
||||
classes.push option.classes if option.classes != ""
|
||||
|
||||
style = if option.style.cssText != "" then " style=\"#{option.style}\"" else ""
|
||||
|
||||
'<li id="' + option.dom_id + '" class="' + classes.join(' ') + '"'+style+'>' + option.html + '</li>'
|
||||
else
|
||||
""
|
||||
|
||||
results_update_field: ->
|
||||
this.result_clear_highlight()
|
||||
@result_single_selected = null
|
||||
this.results_build()
|
||||
|
||||
result_do_highlight: (el) ->
|
||||
if el.length
|
||||
this.result_clear_highlight()
|
||||
|
@ -274,12 +223,6 @@ class Chosen
|
|||
@result_highlight.removeClass "highlighted" if @result_highlight
|
||||
@result_highlight = null
|
||||
|
||||
results_toggle: ->
|
||||
if @results_showing
|
||||
this.results_hide()
|
||||
else
|
||||
this.results_show()
|
||||
|
||||
results_show: ->
|
||||
if not @is_multiple
|
||||
@selected_item.addClass "chzn-single-with-drop"
|
||||
|
@ -426,12 +369,6 @@ class Chosen
|
|||
@form_field_jq.trigger "change"
|
||||
this.search_field_scale()
|
||||
|
||||
results_search: (evt) ->
|
||||
if @results_showing
|
||||
this.winnow_results()
|
||||
else
|
||||
this.results_show()
|
||||
|
||||
winnow_results: ->
|
||||
startTime = new Date()
|
||||
this.no_results_clear()
|
||||
|
@ -545,27 +482,6 @@ class Chosen
|
|||
@pending_backstroke.removeClass "search-choice-focus" if @pending_backstroke
|
||||
@pending_backstroke = null
|
||||
|
||||
keyup_checker: (evt) ->
|
||||
stroke = evt.which ? evt.keyCode
|
||||
this.search_field_scale()
|
||||
|
||||
switch stroke
|
||||
when 8
|
||||
if @is_multiple and @backstroke_length < 1 and @choices > 0
|
||||
this.keydown_backstroke()
|
||||
else if not @pending_backstroke
|
||||
this.result_clear_highlight()
|
||||
this.results_search()
|
||||
when 13
|
||||
evt.preventDefault()
|
||||
this.result_select(evt) if this.results_showing
|
||||
when 27
|
||||
this.results_hide() if @results_showing
|
||||
when 9, 38, 40, 16, 91, 17
|
||||
# don't do anything on these keys
|
||||
else this.results_search()
|
||||
|
||||
|
||||
keydown_checker: (evt) ->
|
||||
stroke = evt.which ? evt.keyCode
|
||||
this.search_field_scale()
|
||||
|
@ -616,22 +532,12 @@ class Chosen
|
|||
dd_top = @container.height()
|
||||
@dropdown.css({"top": dd_top + "px"})
|
||||
|
||||
generate_field_id: ->
|
||||
new_id = this.generate_random_id()
|
||||
@form_field.id = new_id
|
||||
new_id
|
||||
|
||||
generate_random_id: ->
|
||||
string = "sel" + this.generate_random_char() + this.generate_random_char() + this.generate_random_char()
|
||||
while $("#" + string).length > 0
|
||||
string += this.generate_random_char()
|
||||
string
|
||||
|
||||
generate_random_char: ->
|
||||
chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ";
|
||||
rand = Math.floor(Math.random() * chars.length)
|
||||
newchar = chars.substring rand, rand+1
|
||||
|
||||
get_side_border_padding = (elmt) ->
|
||||
side_border_padding = elmt.outerWidth() - elmt.width()
|
||||
|
||||
|
|
|
@ -4,44 +4,23 @@ Copyright (c) 2011 by Harvest
|
|||
###
|
||||
root = this
|
||||
|
||||
class Chosen
|
||||
class Chosen extends AbstractChosen
|
||||
|
||||
constructor: (@form_field, @options={}) ->
|
||||
|
||||
this.set_default_values()
|
||||
|
||||
@is_multiple = @form_field.multiple
|
||||
setup: ->
|
||||
@is_rtl = @form_field.hasClassName "chzn-rtl"
|
||||
|
||||
@default_text_default = if @form_field.multiple then "Select Some Options" else "Select an Option"
|
||||
|
||||
this.set_up_html()
|
||||
this.register_observers()
|
||||
finish_setup: ->
|
||||
@form_field.addClassName "chzn-done"
|
||||
|
||||
|
||||
set_default_values: ->
|
||||
super()
|
||||
|
||||
@click_test_action = (evt) => this.test_active_click(evt)
|
||||
@activate_action = (evt) => this.activate_field(evt)
|
||||
@active_field = false
|
||||
@mouse_on_container = false
|
||||
@results_showing = false
|
||||
@result_highlighted = null
|
||||
@result_single_selected = null
|
||||
@allow_single_deselect = if @options.allow_single_deselect? and @form_field.options[0].text == "" then @options.allow_single_deselect else false
|
||||
@disable_search_threshold = @options.disable_search_threshold || 0
|
||||
@choices = 0
|
||||
|
||||
@results_none_found = @options.no_results_text or "No results match"
|
||||
|
||||
# HTML Templates
|
||||
@single_temp = new Template('<a href="javascript:void(0)" class="chzn-single"><span>#{default}</span><div><b></b></div></a><div class="chzn-drop" style="left:-9000px;"><div class="chzn-search"><input type="text" autocomplete="off" /></div><ul class="chzn-results"></ul></div>')
|
||||
@multi_temp = new Template('<ul class="chzn-choices"><li class="search-field"><input type="text" value="#{default}" class="default" autocomplete="off" style="width:25px;" /></li></ul><div class="chzn-drop" style="left:-9000px;"><ul class="chzn-results"></ul></div>')
|
||||
@choice_temp = new Template('<li class="search-choice" id="#{id}"><span>#{choice}</span><a href="javascript:void(0)" class="search-choice-close" rel="#{position}"></a></li>')
|
||||
@no_results_temp = new Template('<li class="no-results">' + @results_none_found + ' "<span>#{terms}</span>"</li>')
|
||||
|
||||
|
||||
set_up_html: ->
|
||||
@container_id = @form_field.identify().replace(/(:|\.)/g, '_') + "_chzn"
|
||||
|
||||
|
@ -85,7 +64,6 @@ class Chosen
|
|||
this.results_build()
|
||||
this.set_tab_index()
|
||||
|
||||
|
||||
register_observers: ->
|
||||
@container.observe "mousedown", (evt) => this.container_mousedown(evt)
|
||||
@container.observe "mouseup", (evt) => this.container_mouseup(evt)
|
||||
|
@ -138,17 +116,6 @@ class Chosen
|
|||
container_mouseup: (evt) ->
|
||||
this.results_reset(evt) if evt.target.nodeName is "ABBR"
|
||||
|
||||
mouse_enter: -> @mouse_on_container = true
|
||||
mouse_leave: -> @mouse_on_container = false
|
||||
|
||||
input_focus: (evt) ->
|
||||
setTimeout this.container_mousedown.bind(this), 50 unless @active_field
|
||||
|
||||
input_blur: (evt) ->
|
||||
if not @mouse_on_container
|
||||
@active_field = false
|
||||
setTimeout this.blur_test.bind(this), 100
|
||||
|
||||
blur_test: (evt) ->
|
||||
this.close_field() if not @active_field and @container.hasClassName("chzn-container-active")
|
||||
|
||||
|
@ -225,26 +192,6 @@ class Chosen
|
|||
else
|
||||
""
|
||||
|
||||
result_add_option: (option) ->
|
||||
if not option.disabled
|
||||
option.dom_id = @container_id + "_o_" + option.array_index
|
||||
|
||||
classes = if option.selected and @is_multiple then [] else ["active-result"]
|
||||
classes.push "result-selected" if option.selected
|
||||
classes.push "group-option" if option.group_array_index?
|
||||
classes.push option.classes if option.classes != ""
|
||||
|
||||
style = if option.style.cssText != "" then " style=\"#{option.style}\"" else ""
|
||||
|
||||
'<li id="' + option.dom_id + '" class="' + classes.join(' ') + '"'+style+'>' + option.html + '</li>'
|
||||
else
|
||||
""
|
||||
|
||||
results_update_field: ->
|
||||
this.result_clear_highlight()
|
||||
@result_single_selected = null
|
||||
this.results_build()
|
||||
|
||||
result_do_highlight: (el) ->
|
||||
this.result_clear_highlight()
|
||||
|
||||
|
@ -267,12 +214,6 @@ class Chosen
|
|||
@result_highlight.removeClassName('highlighted') if @result_highlight
|
||||
@result_highlight = null
|
||||
|
||||
results_toggle: ->
|
||||
if @results_showing
|
||||
this.results_hide()
|
||||
else
|
||||
this.results_show()
|
||||
|
||||
results_show: ->
|
||||
if not @is_multiple
|
||||
@selected_item.addClassName('chzn-single-with-drop')
|
||||
|
@ -419,12 +360,6 @@ class Chosen
|
|||
@form_field.simulate("change") if typeof Event.simulate is 'function'
|
||||
this.search_field_scale()
|
||||
|
||||
results_search: (evt) ->
|
||||
if @results_showing
|
||||
this.winnow_results()
|
||||
else
|
||||
this.results_show()
|
||||
|
||||
winnow_results: ->
|
||||
startTime = new Date()
|
||||
this.no_results_clear()
|
||||
|
@ -543,27 +478,6 @@ class Chosen
|
|||
@pending_backstroke.removeClassName("search-choice-focus") if @pending_backstroke
|
||||
@pending_backstroke = null
|
||||
|
||||
keyup_checker: (evt) ->
|
||||
stroke = evt.which ? evt.keyCode
|
||||
this.search_field_scale()
|
||||
|
||||
switch stroke
|
||||
when 8
|
||||
if @is_multiple and @backstroke_length < 1 and @choices > 0
|
||||
this.keydown_backstroke()
|
||||
else if not @pending_backstroke
|
||||
this.result_clear_highlight()
|
||||
this.results_search()
|
||||
when 13
|
||||
evt.preventDefault()
|
||||
this.result_select(evt) if this.results_showing
|
||||
when 27
|
||||
this.results_hide() if @results_showing
|
||||
when 9, 38, 40, 16, 91, 17
|
||||
# don't do anything on these keys
|
||||
else this.results_search()
|
||||
|
||||
|
||||
keydown_checker: (evt) ->
|
||||
stroke = evt.which ? evt.keyCode
|
||||
this.search_field_scale()
|
||||
|
@ -583,7 +497,6 @@ class Chosen
|
|||
when 40
|
||||
this.keydown_arrow()
|
||||
|
||||
|
||||
search_field_scale: ->
|
||||
if @is_multiple
|
||||
h = 0
|
||||
|
|
108
coffee/lib/abstract-chosen.coffee
Normal file
108
coffee/lib/abstract-chosen.coffee
Normal file
|
@ -0,0 +1,108 @@
|
|||
###
|
||||
Chosen source: generate output using 'cake build'
|
||||
Copyright (c) 2011 by Harvest
|
||||
###
|
||||
root = this
|
||||
|
||||
class AbstractChosen
|
||||
|
||||
constructor: (@form_field, @options={}) ->
|
||||
this.set_default_values()
|
||||
|
||||
@is_multiple = @form_field.multiple
|
||||
@default_text_default = if @is_multiple then "Select Some Options" else "Select an Option"
|
||||
|
||||
this.setup()
|
||||
|
||||
this.set_up_html()
|
||||
this.register_observers()
|
||||
|
||||
this.finish_setup()
|
||||
|
||||
set_default_values: ->
|
||||
@click_test_action = (evt) => this.test_active_click(evt)
|
||||
@activate_action = (evt) => this.activate_field(evt)
|
||||
@active_field = false
|
||||
@mouse_on_container = false
|
||||
@results_showing = false
|
||||
@result_highlighted = null
|
||||
@result_single_selected = null
|
||||
@allow_single_deselect = if @options.allow_single_deselect? and @form_field.options[0].text == "" then @options.allow_single_deselect else false
|
||||
@disable_search_threshold = @options.disable_search_threshold || 0
|
||||
@choices = 0
|
||||
@results_none_found = @options.no_results_text or "No results match"
|
||||
|
||||
mouse_enter: -> @mouse_on_container = true
|
||||
mouse_leave: -> @mouse_on_container = false
|
||||
|
||||
input_focus: (evt) ->
|
||||
setTimeout (=> this.container_mousedown()), 50 unless @active_field
|
||||
|
||||
input_blur: (evt) ->
|
||||
if not @mouse_on_container
|
||||
@active_field = false
|
||||
setTimeout (=> this.blur_test()), 100
|
||||
|
||||
result_add_option: (option) ->
|
||||
if not option.disabled
|
||||
option.dom_id = @container_id + "_o_" + option.array_index
|
||||
|
||||
classes = if option.selected and @is_multiple then [] else ["active-result"]
|
||||
classes.push "result-selected" if option.selected
|
||||
classes.push "group-option" if option.group_array_index?
|
||||
classes.push option.classes if option.classes != ""
|
||||
|
||||
style = if option.style.cssText != "" then " style=\"#{option.style}\"" else ""
|
||||
|
||||
'<li id="' + option.dom_id + '" class="' + classes.join(' ') + '"'+style+'>' + option.html + '</li>'
|
||||
else
|
||||
""
|
||||
|
||||
results_update_field: ->
|
||||
this.result_clear_highlight()
|
||||
@result_single_selected = null
|
||||
this.results_build()
|
||||
|
||||
results_toggle: ->
|
||||
if @results_showing
|
||||
this.results_hide()
|
||||
else
|
||||
this.results_show()
|
||||
|
||||
results_search: (evt) ->
|
||||
if @results_showing
|
||||
this.winnow_results()
|
||||
else
|
||||
this.results_show()
|
||||
|
||||
keyup_checker: (evt) ->
|
||||
stroke = evt.which ? evt.keyCode
|
||||
this.search_field_scale()
|
||||
|
||||
switch stroke
|
||||
when 8
|
||||
if @is_multiple and @backstroke_length < 1 and @choices > 0
|
||||
this.keydown_backstroke()
|
||||
else if not @pending_backstroke
|
||||
this.result_clear_highlight()
|
||||
this.results_search()
|
||||
when 13
|
||||
evt.preventDefault()
|
||||
this.result_select(evt) if this.results_showing
|
||||
when 27
|
||||
this.results_hide() if @results_showing
|
||||
when 9, 38, 40, 16, 91, 17
|
||||
# don't do anything on these keys
|
||||
else this.results_search()
|
||||
|
||||
generate_field_id: ->
|
||||
new_id = this.generate_random_id()
|
||||
@form_field.id = new_id
|
||||
new_id
|
||||
|
||||
generate_random_char: ->
|
||||
chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ"
|
||||
rand = Math.floor(Math.random() * chars.length)
|
||||
newchar = chars.substring rand, rand+1
|
||||
|
||||
root.AbstractChosen = AbstractChosen
|
Loading…
Reference in a new issue