Introduce abstract-chosen, try to start sharing more code between platforms

This commit is contained in:
Matthew Beale 2011-08-19 12:23:03 -04:00
parent df6345465a
commit 4e463669d2
8 changed files with 555 additions and 542 deletions

View file

@ -11,12 +11,14 @@ CoffeeScript = require 'coffee-script'
javascripts = { javascripts = {
'chosen/chosen.jquery.js': [ 'chosen/chosen.jquery.js': [
'coffee/chosen.jquery.coffee'
'coffee/lib/select-parser.coffee' 'coffee/lib/select-parser.coffee'
'coffee/lib/abstract-chosen.coffee'
'coffee/chosen.jquery.coffee'
] ]
'chosen/chosen.proto.js': [ 'chosen/chosen.proto.js': [
'coffee/chosen.proto.coffee'
'coffee/lib/select-parser.coffee' 'coffee/lib/select-parser.coffee'
'coffee/lib/abstract-chosen.coffee'
'coffee/chosen.proto.coffee'
] ]
} }

View file

@ -7,12 +7,227 @@
// MIT License, https://github.com/harvesthq/chosen/blob/master/LICENSE.md // MIT License, https://github.com/harvesthq/chosen/blob/master/LICENSE.md
// This file is generated by `cake build`, do not edit it by hand. // 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
});
} 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 AbstractChosen, root;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
root = this;
AbstractChosen = (function() {
function AbstractChosen(elmn) {
this.set_default_values();
this.form_field = elmn;
this.is_multiple = this.form_field.multiple;
this.default_text_default = this.is_multiple ? "Select Some Options" : "Select an Option";
this.setup();
this.set_up_html();
this.register_observers();
this.finish_setup();
}
AbstractChosen.prototype.set_default_values = function() {
this.click_test_action = __bind(function(evt) {
return this.test_active_click(evt);
}, this);
this.active_field = false;
this.mouse_on_container = false;
this.results_showing = false;
this.result_highlighted = null;
this.result_single_selected = null;
return this.choices = 0;
};
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;
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");
}
return '<li id="' + option.dom_id + '" class="' + classes.join(' ') + '">' + 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() { (function() {
/* /*
Chosen source: generate output using 'cake build' Chosen source: generate output using 'cake build'
Copyright (c) 2011 by Harvest Copyright (c) 2011 by Harvest
*/ var $, Chosen, get_side_border_padding, root; */ var $, Chosen, get_side_border_padding, root;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; 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; root = this;
$ = jQuery; $ = jQuery;
$.fn.extend({ $.fn.extend({
@ -28,27 +243,16 @@
} }
}); });
Chosen = (function() { Chosen = (function() {
function Chosen(elmn) { __extends(Chosen, AbstractChosen);
this.set_default_values(); function Chosen() {
this.form_field = elmn; Chosen.__super__.constructor.apply(this, arguments);
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.set_up_html();
this.register_observers();
this.form_field_jq.addClass("chzn-done");
} }
Chosen.prototype.set_default_values = function() { Chosen.prototype.setup = function() {
this.click_test_action = __bind(function(evt) { this.form_field_jq = $(this.form_field);
return this.test_active_click(evt); return this.is_rtl = this.form_field_jq.hasClass("chzn-rtl");
}, this); };
this.active_field = false; Chosen.prototype.finish_setup = function() {
this.mouse_on_container = false; return this.form_field_jq.addClass("chzn-done");
this.results_showing = false;
this.result_highlighted = null;
this.result_single_selected = null;
return this.choices = 0;
}; };
Chosen.prototype.set_up_html = function() { Chosen.prototype.set_up_html = function() {
var container_div, dd_top, dd_width, sf_width; var container_div, dd_top, dd_width, sf_width;
@ -158,27 +362,6 @@
return this.pending_destroy_click = false; return this.pending_destroy_click = false;
} }
}; };
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) { Chosen.prototype.blur_test = function(evt) {
if (!this.active_field && this.container.hasClass("chzn-container-active")) { if (!this.active_field && this.container.hasClass("chzn-container-active")) {
return this.close_field(); return this.close_field();
@ -254,27 +437,6 @@
return ""; return "";
} }
}; };
Chosen.prototype.result_add_option = function(option) {
var classes;
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");
}
return '<li id="' + option.dom_id + '" class="' + classes.join(' ') + '">' + 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) { Chosen.prototype.result_do_highlight = function(el) {
var high_bottom, high_top, maxHeight, visible_bottom, visible_top; var high_bottom, high_top, maxHeight, visible_bottom, visible_top;
if (el.length) { if (el.length) {
@ -299,13 +461,6 @@
} }
return this.result_highlight = null; 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() { Chosen.prototype.results_show = function() {
var dd_top; var dd_top;
if (!this.is_multiple) { if (!this.is_multiple) {
@ -453,13 +608,6 @@
this.form_field_jq.trigger("change"); this.form_field_jq.trigger("change");
return this.search_field_scale(); 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() { Chosen.prototype.winnow_results = function() {
var found, option, part, parts, regex, result_id, results, searchText, startTime, startpos, text, zregex, _i, _j, _len, _len2, _ref; var found, option, part, parts, regex, result_id, results, searchText, startTime, startpos, text, zregex, _i, _j, _len, _len2, _ref;
startTime = new Date(); startTime = new Date();
@ -601,41 +749,6 @@
} }
return this.pending_backstroke = null; 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) { Chosen.prototype.keydown_checker = function(evt) {
var stroke, _ref; var stroke, _ref;
stroke = (_ref = evt.which) != null ? _ref : evt.keyCode; stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
@ -692,12 +805,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() { Chosen.prototype.generate_random_id = function() {
var string; var string;
string = "sel" + this.generate_random_char() + this.generate_random_char() + this.generate_random_char(); string = "sel" + this.generate_random_char() + this.generate_random_char() + this.generate_random_char();
@ -706,12 +813,6 @@
} }
return string; 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; return Chosen;
})(); })();
get_side_border_padding = function(elmt) { get_side_border_padding = function(elmt) {
@ -720,75 +821,3 @@
}; };
root.get_side_border_padding = get_side_border_padding; root.get_side_border_padding = get_side_border_padding;
}).call(this); }).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
});
} 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);

File diff suppressed because one or more lines are too long

View file

@ -7,25 +7,97 @@
// MIT License, https://github.com/harvesthq/chosen/blob/master/LICENSE.md // MIT License, https://github.com/harvesthq/chosen/blob/master/LICENSE.md
// This file is generated by `cake build`, do not edit it by hand. // 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
});
} 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() { (function() {
/* /*
Chosen source: generate output using 'cake build' Chosen source: generate output using 'cake build'
Copyright (c) 2011 by Harvest 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); }; }; var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
root = this; root = this;
Chosen = (function() { AbstractChosen = (function() {
function Chosen(elmn) { function AbstractChosen(elmn) {
this.set_default_values(); this.set_default_values();
this.form_field = elmn; this.form_field = elmn;
this.is_multiple = this.form_field.multiple; this.is_multiple = this.form_field.multiple;
this.is_rtl = this.form_field.hasClassName("chzn-rtl"); this.default_text_default = this.is_multiple ? "Select Some Options" : "Select an Option";
this.default_text_default = this.form_field.multiple ? "Select Some Options" : "Select an Option"; this.setup();
this.set_up_html(); this.set_up_html();
this.register_observers(); 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) { this.click_test_action = __bind(function(evt) {
return this.test_active_click(evt); return this.test_active_click(evt);
}, this); }, this);
@ -34,7 +106,142 @@
this.results_showing = false; this.results_showing = false;
this.result_highlighted = null; this.result_highlighted = null;
this.result_single_selected = null; this.result_single_selected = null;
this.choices = 0; return this.choices = 0;
};
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;
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");
}
return '<li id="' + option.dom_id + '" class="' + classes.join(' ') + '">' + 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.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.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>'); 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>');
@ -148,23 +355,6 @@
return this.pending_destroy_click = false; return this.pending_destroy_click = false;
} }
}; };
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) { Chosen.prototype.blur_test = function(evt) {
if (!this.active_field && this.container.hasClassName("chzn-container-active")) { if (!this.active_field && this.container.hasClassName("chzn-container-active")) {
return this.close_field(); return this.close_field();
@ -240,27 +430,6 @@
return ""; return "";
} }
}; };
Chosen.prototype.result_add_option = function(option) {
var classes;
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");
}
return '<li id="' + option.dom_id + '" class="' + classes.join(' ') + '">' + 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) { Chosen.prototype.result_do_highlight = function(el) {
var high_bottom, high_top, maxHeight, visible_bottom, visible_top; var high_bottom, high_top, maxHeight, visible_bottom, visible_top;
this.result_clear_highlight(); this.result_clear_highlight();
@ -283,13 +452,6 @@
} }
return this.result_highlight = null; 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() { Chosen.prototype.results_show = function() {
var dd_top; var dd_top;
if (!this.is_multiple) { if (!this.is_multiple) {
@ -446,13 +608,6 @@
} }
return this.search_field_scale(); 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() { Chosen.prototype.winnow_results = function() {
var found, option, part, parts, regex, result_id, results, searchText, startTime, startpos, text, zregex, _i, _j, _len, _len2, _ref; var found, option, part, parts, regex, result_id, results, searchText, startTime, startpos, text, zregex, _i, _j, _len, _len2, _ref;
startTime = new Date(); startTime = new Date();
@ -605,41 +760,6 @@
} }
return this.pending_backstroke = null; 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) { Chosen.prototype.keydown_checker = function(evt) {
var stroke, _ref; var stroke, _ref;
stroke = (_ref = evt.which) != null ? _ref : evt.keyCode; stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
@ -718,75 +838,3 @@
}; };
root.get_side_border_padding = get_side_border_padding; root.get_side_border_padding = get_side_border_padding;
}).call(this); }).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
});
} 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);

File diff suppressed because one or more lines are too long

View file

@ -14,32 +14,15 @@ $.fn.extend({
) )
}) })
class Chosen class Chosen extends AbstractChosen
constructor: (elmn) -> setup: ->
this.set_default_values()
@form_field = elmn
@form_field_jq = $ @form_field @form_field_jq = $ @form_field
@is_multiple = @form_field.multiple
@is_rtl = @form_field_jq.hasClass "chzn-rtl" @is_rtl = @form_field_jq.hasClass "chzn-rtl"
@default_text_default = if @form_field.multiple then "Select Some Options" else "Select an Option" finish_setup: ->
this.set_up_html()
this.register_observers()
@form_field_jq.addClass "chzn-done" @form_field_jq.addClass "chzn-done"
set_default_values: ->
@click_test_action = (evt) => this.test_active_click(evt)
@active_field = false
@mouse_on_container = false
@results_showing = false
@result_highlighted = null
@result_single_selected = null
@choices = 0
set_up_html: -> set_up_html: ->
@container_id = if @form_field.id.length then @form_field.id.replace(/(:|\.)/g, '_') else this.generate_field_id() @container_id = if @form_field.id.length then @form_field.id.replace(/(:|\.)/g, '_') else this.generate_field_id()
@container_id += "_chzn" @container_id += "_chzn"
@ -125,17 +108,6 @@ class Chosen
else else
@pending_destroy_click = false @pending_destroy_click = false
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) -> blur_test: (evt) ->
this.close_field() if not @active_field and @container.hasClass "chzn-container-active" this.close_field() if not @active_field and @container.hasClass "chzn-container-active"
@ -210,23 +182,6 @@ class Chosen
else 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?
'<li id="' + option.dom_id + '" class="' + classes.join(' ') + '">' + option.html + '</li>'
else
""
results_update_field: ->
this.result_clear_highlight()
@result_single_selected = null
this.results_build()
result_do_highlight: (el) -> result_do_highlight: (el) ->
if el.length if el.length
this.result_clear_highlight() this.result_clear_highlight()
@ -250,12 +205,6 @@ class Chosen
@result_highlight.removeClass "highlighted" if @result_highlight @result_highlight.removeClass "highlighted" if @result_highlight
@result_highlight = null @result_highlight = null
results_toggle: ->
if @results_showing
this.results_hide()
else
this.results_show()
results_show: -> results_show: ->
if not @is_multiple if not @is_multiple
@selected_item.addClass "chzn-single-with-drop" @selected_item.addClass "chzn-single-with-drop"
@ -389,12 +338,6 @@ class Chosen
@form_field_jq.trigger "change" @form_field_jq.trigger "change"
this.search_field_scale() this.search_field_scale()
results_search: (evt) ->
if @results_showing
this.winnow_results()
else
this.results_show()
winnow_results: -> winnow_results: ->
startTime = new Date() startTime = new Date()
this.no_results_clear() this.no_results_clear()
@ -508,27 +451,6 @@ class Chosen
@pending_backstroke.removeClass "search-choice-focus" if @pending_backstroke @pending_backstroke.removeClass "search-choice-focus" if @pending_backstroke
@pending_backstroke = null @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) -> keydown_checker: (evt) ->
stroke = evt.which ? evt.keyCode stroke = evt.which ? evt.keyCode
this.search_field_scale() this.search_field_scale()
@ -580,22 +502,12 @@ class Chosen
dd_top = @container.height() dd_top = @container.height()
@dropdown.css({"top": dd_top + "px"}) @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: -> generate_random_id: ->
string = "sel" + this.generate_random_char() + this.generate_random_char() + this.generate_random_char() string = "sel" + this.generate_random_char() + this.generate_random_char() + this.generate_random_char()
while $("#" + string).length > 0 while $("#" + string).length > 0
string += this.generate_random_char() string += this.generate_random_char()
string string
generate_random_char: ->
chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ";
rand = Math.floor(Math.random() * chars.length)
newchar = chars.substring rand, rand+1
get_side_border_padding = (elmt) -> get_side_border_padding = (elmt) ->
side_border_padding = elmt.outerWidth() - elmt.width() side_border_padding = elmt.outerWidth() - elmt.width()

View file

@ -4,31 +4,16 @@ Copyright (c) 2011 by Harvest
### ###
root = this root = this
class Chosen class Chosen extends AbstractChosen
constructor: (elmn) -> setup: ->
this.set_default_values()
@form_field = elmn
@is_multiple = @form_field.multiple
@is_rtl = @form_field.hasClassName "chzn-rtl" @is_rtl = @form_field.hasClassName "chzn-rtl"
@default_text_default = if @form_field.multiple then "Select Some Options" else "Select an Option" finish_setup: ->
this.set_up_html()
this.register_observers()
@form_field.addClassName "chzn-done" @form_field.addClassName "chzn-done"
set_default_values: -> set_default_values: ->
super()
@click_test_action = (evt) => this.test_active_click(evt)
@active_field = false
@mouse_on_container = false
@results_showing = false
@result_highlighted = null
@result_single_selected = null
@choices = 0
# HTML Templates # 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>') @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>')
@ -36,7 +21,6 @@ class Chosen
@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>') @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">No results match "<span>#{terms}</span>"</li>') @no_results_temp = new Template('<li class="no-results">No results match "<span>#{terms}</span>"</li>')
set_up_html: -> set_up_html: ->
@container_id = @form_field.identify().replace(/(:|\.)/g, '_') + "_chzn" @container_id = @form_field.identify().replace(/(:|\.)/g, '_') + "_chzn"
@ -79,7 +63,6 @@ class Chosen
this.results_build() this.results_build()
this.set_tab_index() this.set_tab_index()
register_observers: -> register_observers: ->
@container.observe "mousedown", (evt) => this.container_mousedown(evt) @container.observe "mousedown", (evt) => this.container_mousedown(evt)
@container.observe "mouseenter", (evt) => this.mouse_enter(evt) @container.observe "mouseenter", (evt) => this.mouse_enter(evt)
@ -101,7 +84,6 @@ class Chosen
else else
@selected_item.observe "focus", (evt) => this.activate_field(evt) @selected_item.observe "focus", (evt) => this.activate_field(evt)
container_mousedown: (evt) -> container_mousedown: (evt) ->
if evt and evt.type is "mousedown" if evt and evt.type is "mousedown"
evt.stop() evt.stop()
@ -117,17 +99,6 @@ class Chosen
else else
@pending_destroy_click = false @pending_destroy_click = false
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) -> blur_test: (evt) ->
this.close_field() if not @active_field and @container.hasClassName("chzn-container-active") this.close_field() if not @active_field and @container.hasClassName("chzn-container-active")
@ -202,23 +173,6 @@ class Chosen
else 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?
'<li id="' + option.dom_id + '" class="' + classes.join(' ') + '">' + option.html + '</li>'
else
""
results_update_field: ->
this.result_clear_highlight()
@result_single_selected = null
this.results_build()
result_do_highlight: (el) -> result_do_highlight: (el) ->
this.result_clear_highlight() this.result_clear_highlight()
@ -241,12 +195,6 @@ class Chosen
@result_highlight.removeClassName('highlighted') if @result_highlight @result_highlight.removeClassName('highlighted') if @result_highlight
@result_highlight = null @result_highlight = null
results_toggle: ->
if @results_showing
this.results_hide()
else
this.results_show()
results_show: -> results_show: ->
if not @is_multiple if not @is_multiple
@selected_item.addClassName('chzn-single-with-drop') @selected_item.addClassName('chzn-single-with-drop')
@ -382,12 +330,6 @@ class Chosen
@form_field.simulate("change") if typeof Event.simulate is 'function' @form_field.simulate("change") if typeof Event.simulate is 'function'
this.search_field_scale() this.search_field_scale()
results_search: (evt) ->
if @results_showing
this.winnow_results()
else
this.results_show()
winnow_results: -> winnow_results: ->
startTime = new Date() startTime = new Date()
this.no_results_clear() this.no_results_clear()
@ -506,27 +448,6 @@ class Chosen
@pending_backstroke.removeClassName("search-choice-focus") if @pending_backstroke @pending_backstroke.removeClassName("search-choice-focus") if @pending_backstroke
@pending_backstroke = null @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) -> keydown_checker: (evt) ->
stroke = evt.which ? evt.keyCode stroke = evt.which ? evt.keyCode
this.search_field_scale() this.search_field_scale()
@ -546,7 +467,6 @@ class Chosen
when 40 when 40
this.keydown_arrow() this.keydown_arrow()
search_field_scale: -> search_field_scale: ->
if @is_multiple if @is_multiple
h = 0 h = 0

View file

@ -0,0 +1,102 @@
###
Chosen source: generate output using 'cake build'
Copyright (c) 2011 by Harvest
###
root = this
class AbstractChosen
constructor: (elmn) ->
this.set_default_values()
@form_field = elmn
@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)
@active_field = false
@mouse_on_container = false
@results_showing = false
@result_highlighted = null
@result_single_selected = null
@choices = 0
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?
'<li id="' + option.dom_id + '" class="' + classes.join(' ') + '">' + 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