ajax-chosen.js
4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Generated by CoffeeScript 1.4.0
/* USING JOOMLA.JTEXT TO TRANSLATE LANGUAGE STRINGS
* ================================================= */
(function($) {
return $.fn.ajaxChosen = function(settings, callback, chosenOptions) {
var chosenXhr, defaultOptions, options, select;
if (settings == null) {
settings = {};
}
if (callback == null) {
callback = {};
}
if (chosenOptions == null) {
chosenOptions = function() {};
}
defaultOptions = {
minTermLength: 3,
afterTypeDelay: 500,
jsonTermKey: "term",
keepTypingMsg: Joomla.JText._('JGLOBAL_KEEP_TYPING'),
lookingForMsg: Joomla.JText._('JGLOBAL_LOOKING_FOR')
};
select = this;
chosenXhr = null;
options = $.extend({}, defaultOptions, $(select).data(), settings);
this.chosen(chosenOptions ? chosenOptions : {});
return this.each(function() {
return $(this).next('.chzn-container').find(".search-field > input, .chzn-search > input").bind('keyup', function() {
var field, msg, success, untrimmed_val, val;
untrimmed_val = $(this).val();
val = $.trim($(this).val());
msg = val.length < options.minTermLength ? options.keepTypingMsg : options.lookingForMsg + (" '" + val + "'");
select.next('.chzn-container').find('.no-results').text(msg);
if (val === $(this).data('prevVal')) {
return false;
}
$(this).data('prevVal', val);
if (this.timer) {
clearTimeout(this.timer);
}
if (val.length < options.minTermLength) {
return false;
}
field = $(this);
if (!(options.data != null)) {
options.data = {};
}
options.data[options.jsonTermKey] = val;
if (options.dataCallback != null) {
options.data = options.dataCallback(options.data);
}
success = options.success;
options.success = function(data) {
var items, nbItems, selected_values;
if (!(data != null)) {
return;
}
selected_values = [];
select.find('option').each(function() {
if (!$(this).is(":selected")) {
return $(this).remove();
} else {
return selected_values.push($(this).val() + "-" + $(this).text());
}
});
select.find('optgroup:empty').each(function() {
return $(this).remove();
});
items = callback(data);
nbItems = 0;
$.each(items, function(i, element) {
var group, text, value;
nbItems++;
if (element.group) {
group = select.find("optgroup[label='" + element.text + "']");
if (!group.size()) {
group = $("<optgroup />");
}
group.attr('label', element.text).appendTo(select);
return $.each(element.items, function(i, element) {
var text, value;
if (typeof element === "string") {
value = i;
text = element;
} else {
value = element.value;
text = element.text;
}
if ($.inArray(value + "-" + text, selected_values) === -1) {
return $("<option />").attr('value', value).html(text).appendTo(group);
}
});
} else {
if (typeof element === "string") {
value = i;
text = element;
} else {
value = element.value;
text = element.text;
}
if ($.inArray(value + "-" + text, selected_values) === -1) {
return $("<option />").attr('value', value).html(text).appendTo(select);
}
}
});
if (nbItems) {
select.trigger("liszt:updated");
} else {
select.data().chosen.no_results_clear();
select.data().chosen.no_results(field.val());
}
if (success != null) {
success(data);
}
return field.val(untrimmed_val);
};
return this.timer = setTimeout(function() {
if (chosenXhr) {
chosenXhr.abort();
}
return chosenXhr = $.ajax(options);
}, options.afterTypeDelay);
});
});
};
})(jQuery);