Skip to content

Commit

Permalink
Merge pull request #1168 from harvesthq/fix-for-1165
Browse files Browse the repository at this point in the history
Fix issue when using both jQuery & Prototype
  • Loading branch information
pfiller committed Apr 26, 2013
2 parents 69030a5 + af8a1bf commit b374da6
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 105 deletions.
71 changes: 20 additions & 51 deletions chosen/chosen.jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
var SelectParser;

SelectParser = (function() {

function SelectParser() {
this.options_index = 0;
this.parsed = [];
Expand All @@ -26,7 +27,6 @@

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,
Expand Down Expand Up @@ -79,7 +79,6 @@

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++) {
Expand All @@ -92,6 +91,7 @@
this.SelectParser = SelectParser;

}).call(this);

/*
Chosen source: generate output using 'cake build'
Copyright (c) 2011 by Harvest
Expand All @@ -104,6 +104,7 @@ Copyright (c) 2011 by Harvest
root = this;

AbstractChosen = (function() {

function AbstractChosen(form_field, options) {
this.form_field = form_field;
this.options = options != null ? options : {};
Expand All @@ -121,7 +122,6 @@ Copyright (c) 2011 by Harvest

AbstractChosen.prototype.set_default_values = function() {
var _this = this;

this.click_test_action = function(evt) {
return _this.test_active_click(evt);
};
Expand Down Expand Up @@ -165,7 +165,6 @@ Copyright (c) 2011 by Harvest

AbstractChosen.prototype.input_focus = function(evt) {
var _this = this;

if (this.is_multiple) {
if (!this.active_field) {
return setTimeout((function() {
Expand All @@ -181,7 +180,6 @@ Copyright (c) 2011 by Harvest

AbstractChosen.prototype.input_blur = function(evt) {
var _this = this;

if (!this.mouse_on_container) {
this.active_field = false;
return setTimeout((function() {
Expand All @@ -192,7 +190,6 @@ Copyright (c) 2011 by Harvest

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"];
Expand Down Expand Up @@ -240,7 +237,6 @@ Copyright (c) 2011 by Harvest

AbstractChosen.prototype.keyup_checker = function(evt) {
var stroke, _ref;

stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
this.search_field_scale();
switch (stroke) {
Expand Down Expand Up @@ -277,33 +273,29 @@ Copyright (c) 2011 by Harvest

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 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
rand = Math.floor(Math.random() * chars.length);
return newchar = chars.substring(rand, rand + 1);
};

AbstractChosen.prototype.container_width = function() {
var width;

if (this.options.width != null) {
return this.options.width;
}
width = window.getComputedStyle != null ? parseFloat(window.getComputedStyle(this.form_field).getPropertyValue('width')) : typeof jQuery !== "undefined" && jQuery !== null ? this.form_field_jq.outerWidth() : this.form_field.getWidth();
width = window.getComputedStyle != null ? parseFloat(window.getComputedStyle(this.form_field).getPropertyValue('width')) : (typeof jQuery !== "undefined" && jQuery !== null) && (this.form_field_jq != null) ? this.form_field_jq.outerWidth() : this.form_field.getWidth();
return width + "px";
};

AbstractChosen.browser_is_supported = function() {
var _ref;

if (window.navigator.appName === "Microsoft Internet Explorer") {
return (null !== (_ref = document.documentMode) && _ref >= 8);
}
Expand All @@ -323,14 +315,15 @@ Copyright (c) 2011 by Harvest
root.AbstractChosen = AbstractChosen;

}).call(this);

/*
Chosen source: generate output using 'cake build'
Copyright (c) 2011 by Harvest
*/


(function() {
var $, Chosen, get_side_border_padding, root, _ref,
var $, Chosen, get_side_border_padding, root,
__hasProp = {}.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; };

Expand All @@ -345,7 +338,6 @@ Copyright (c) 2011 by Harvest
}
return this.each(function(input_field) {
var $this;

$this = $(this);
if (!$this.hasClass("chzn-done")) {
return $this.data('chosen', new Chosen(this, options));
Expand All @@ -355,11 +347,11 @@ Copyright (c) 2011 by Harvest
});

Chosen = (function(_super) {

__extends(Chosen, _super);

function Chosen() {
_ref = Chosen.__super__.constructor.apply(this, arguments);
return _ref;
return Chosen.__super__.constructor.apply(this, arguments);
}

Chosen.prototype.setup = function() {
Expand All @@ -374,7 +366,6 @@ Copyright (c) 2011 by Harvest

Chosen.prototype.set_up_html = function() {
var container_classes, container_props;

this.container_id = this.form_field.id.length ? this.form_field.id.replace(/[^\w]/g, '_') : this.generate_field_id();
this.container_id += "_chzn";
container_classes = ["chzn-container"];
Expand Down Expand Up @@ -420,7 +411,6 @@ Copyright (c) 2011 by Harvest

Chosen.prototype.register_observers = function() {
var _this = this;

this.container.mousedown(function(evt) {
_this.container_mousedown(evt);
});
Expand Down Expand Up @@ -531,10 +521,9 @@ Copyright (c) 2011 by Harvest
};

Chosen.prototype.search_results_mousewheel_ff = function(evt) {
var bottom_overflow, delta, target, top_overflow, _ref1;

var bottom_overflow, delta, target, top_overflow, _ref;
target = evt.currentTarget;
delta = ((_ref1 = evt.originalEvent) != null ? _ref1.wheelDelta : void 0) || -evt.detail;
delta = ((_ref = evt.originalEvent) != null ? _ref.wheelDelta : void 0) || -evt.detail;
bottom_overflow = target.scrollTop + $(target).outerHeight() - target.scrollHeight >= 0;
top_overflow = target.scrollTop <= 0;
if (target.scrollHeight > $(target).outerHeight() && ((delta < 0 && bottom_overflow) || (delta > 0 && top_overflow))) {
Expand Down Expand Up @@ -575,8 +564,7 @@ Copyright (c) 2011 by Harvest
};

Chosen.prototype.results_build = function() {
var content, data, _i, _len, _ref1;

var content, data, _i, _len, _ref;
this.parsing = true;
this.results_data = root.SelectParser.select_to_array(this.form_field);
if (this.is_multiple && this.choices > 0) {
Expand All @@ -591,9 +579,9 @@ Copyright (c) 2011 by Harvest
}
}
content = '';
_ref1 = this.results_data;
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
data = _ref1[_i];
_ref = this.results_data;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
data = _ref[_i];
if (data.group) {
content += this.result_add_group(data);
} else if (!data.empty) {
Expand Down Expand Up @@ -626,7 +614,6 @@ Copyright (c) 2011 by Harvest

Chosen.prototype.result_do_highlight = function(el) {
var high_bottom, high_top, maxHeight, visible_bottom, visible_top;

if (el.length) {
this.result_clear_highlight();
this.result_highlight = el;
Expand Down Expand Up @@ -681,7 +668,6 @@ Copyright (c) 2011 by Harvest

Chosen.prototype.set_tab_index = function(el) {
var ti;

if (this.form_field_jq.attr("tabindex")) {
ti = this.form_field_jq.attr("tabindex");
this.form_field_jq.attr("tabindex", -1);
Expand All @@ -691,7 +677,6 @@ Copyright (c) 2011 by Harvest

Chosen.prototype.set_label_behavior = function() {
var _this = this;

this.form_field_label = this.form_field_jq.parents("label");
if (!this.form_field_label.length && this.form_field.id.length) {
this.form_field_label = $("label[for=" + this.form_field.id + "]");
Expand Down Expand Up @@ -719,7 +704,6 @@ Copyright (c) 2011 by Harvest

Chosen.prototype.search_results_mouseup = function(evt) {
var target;

target = $(evt.target).hasClass("active-result") ? $(evt.target) : $(evt.target).parents(".active-result").first();
if (target.length) {
this.result_highlight = target;
Expand All @@ -730,7 +714,6 @@ Copyright (c) 2011 by Harvest

Chosen.prototype.search_results_mouseover = function(evt) {
var target;

target = $(evt.target).hasClass("active-result") ? $(evt.target) : $(evt.target).parents(".active-result").first();
if (target) {
return this.result_do_highlight(target);
Expand All @@ -753,7 +736,6 @@ Copyright (c) 2011 by Harvest
Chosen.prototype.choice_build = function(item) {
var choice_id, html, link,
_this = this;

if (this.is_multiple && this.max_selected_options <= this.choices) {
this.form_field_jq.trigger("liszt:maxselected", {
chosen: this
Expand Down Expand Up @@ -815,7 +797,6 @@ Copyright (c) 2011 by Harvest

Chosen.prototype.result_select = function(evt) {
var high, high_id, item, position;

if (this.result_highlight) {
high = this.result_highlight;
high_id = high.attr("id");
Expand Down Expand Up @@ -864,7 +845,6 @@ Copyright (c) 2011 by Harvest

Chosen.prototype.result_deselect = function(pos) {
var result, result_data;

result_data = this.results_data[pos];
if (!this.form_field.options[result_data.options_index].disabled) {
result_data.selected = false;
Expand All @@ -890,17 +870,16 @@ Copyright (c) 2011 by Harvest
};

Chosen.prototype.winnow_results = function() {
var found, option, part, parts, regex, regexAnchor, result, result_id, results, searchText, startpos, text, zregex, _i, _j, _len, _len1, _ref1;

var found, option, part, parts, regex, regexAnchor, result, result_id, results, searchText, startpos, text, zregex, _i, _j, _len, _len1, _ref;
this.no_results_clear();
results = 0;
searchText = this.search_field.val() === this.default_text ? "" : $('<div/>').text($.trim(this.search_field.val())).html();
regexAnchor = this.search_contains ? "" : "^";
regex = new RegExp(regexAnchor + searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i');
zregex = new RegExp(searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i');
_ref1 = this.results_data;
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
option = _ref1[_i];
_ref = this.results_data;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
option = _ref[_i];
if (!option.disabled && !option.empty) {
if (option.group) {
$('#' + option.dom_id).css('display', 'none');
Expand Down Expand Up @@ -954,7 +933,6 @@ Copyright (c) 2011 by Harvest

Chosen.prototype.winnow_results_clear = function() {
var li, lis, _i, _len, _results;

this.search_field.val("");
lis = this.search_results.find("li");
_results = [];
Expand All @@ -974,7 +952,6 @@ Copyright (c) 2011 by Harvest

Chosen.prototype.winnow_results_set_highlight = function() {
var do_high, selected_results;

if (!this.result_highlight) {
selected_results = !this.is_multiple ? this.search_results.find(".result-selected.active-result") : [];
do_high = selected_results.length ? selected_results.first() : this.search_results.find(".active-result").first();
Expand All @@ -986,7 +963,6 @@ Copyright (c) 2011 by Harvest

Chosen.prototype.no_results = function(terms) {
var no_results_html;

no_results_html = $('<li class="no-results">' + this.results_none_found + ' "<span></span>"</li>');
no_results_html.find("span").first().html(terms);
return this.search_results.append(no_results_html);
Expand All @@ -998,7 +974,6 @@ Copyright (c) 2011 by Harvest

Chosen.prototype.keydown_arrow = function() {
var first_active, next_sib;

if (!this.result_highlight) {
first_active = this.search_results.find("li.active-result").first();
if (first_active) {
Expand All @@ -1017,7 +992,6 @@ Copyright (c) 2011 by Harvest

Chosen.prototype.keyup_arrow = function() {
var prev_sibs;

if (!this.results_showing && !this.is_multiple) {
return this.results_show();
} else if (this.result_highlight) {
Expand All @@ -1035,7 +1009,6 @@ Copyright (c) 2011 by Harvest

Chosen.prototype.keydown_backstroke = function() {
var next_available_destroy;

if (this.pending_backstroke) {
this.choice_destroy(this.pending_backstroke.find("a").first());
return this.clear_backstroke();
Expand All @@ -1060,9 +1033,8 @@ Copyright (c) 2011 by Harvest
};

Chosen.prototype.keydown_checker = function(evt) {
var stroke, _ref1;

stroke = (_ref1 = evt.which) != null ? _ref1 : evt.keyCode;
var stroke, _ref;
stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
this.search_field_scale();
if (stroke !== 8 && this.pending_backstroke) {
this.clear_backstroke();
Expand Down Expand Up @@ -1092,7 +1064,6 @@ Copyright (c) 2011 by Harvest

Chosen.prototype.search_field_scale = function() {
var div, h, style, style_block, styles, w, _i, _len;

if (this.is_multiple) {
h = 0;
w = 0;
Expand Down Expand Up @@ -1123,7 +1094,6 @@ Copyright (c) 2011 by Harvest

Chosen.prototype.generate_random_id = function() {
var string;

string = "sel" + this.generate_random_char() + this.generate_random_char() + this.generate_random_char();
while ($("#" + string).length > 0) {
string += this.generate_random_char();
Expand All @@ -1139,7 +1109,6 @@ Copyright (c) 2011 by Harvest

get_side_border_padding = function(elmt) {
var side_border_padding;

return side_border_padding = elmt.outerWidth() - elmt.width();
};

Expand Down
2 changes: 1 addition & 1 deletion chosen/chosen.jquery.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit b374da6

Please sign in to comment.