| 776 |
lars |
1 |
+function ($) {
|
|
|
2 |
'use strict';
|
|
|
3 |
|
|
|
4 |
// Minified with jscompress.com
|
|
|
5 |
|
|
|
6 |
// CLASS DEFINITION
|
|
|
7 |
// ===============================
|
|
|
8 |
|
|
|
9 |
// NB: only user input triggers event change on SELECT.
|
|
|
10 |
|
|
|
11 |
var SelectSplitter = function(element, options) {
|
|
|
12 |
this.init('selectsplitter', element, options);
|
|
|
13 |
};
|
|
|
14 |
|
|
|
15 |
SelectSplitter.DEFAULTS = {
|
|
|
16 |
template:
|
|
|
17 |
'<div class="row" data-selectsplitter-wrapper-selector>' +
|
|
|
18 |
|
|
|
19 |
'<div class="col-xs-12 col-sm-6">' +
|
|
|
20 |
'<select class="form-control" data-selectsplitter-firstselect-selector></select>' +
|
|
|
21 |
'</div>' +
|
|
|
22 |
' <!-- Add the extra clearfix for only the required viewport -->' +
|
|
|
23 |
'<div class="clearfix visible-xs-block"></div>' +
|
|
|
24 |
'<div class="col-xs-12 col-sm-6">' +
|
|
|
25 |
'<select class="form-control" data-selectsplitter-secondselect-selector></select>' +
|
|
|
26 |
'</div>' +
|
|
|
27 |
|
|
|
28 |
'</div>'
|
|
|
29 |
};
|
|
|
30 |
|
|
|
31 |
/* Note: Est appelé par la fonction définie en var */
|
|
|
32 |
SelectSplitter.prototype.init = function (type, element, options) {
|
|
|
33 |
|
|
|
34 |
// Initial variables.
|
|
|
35 |
var self = this;
|
|
|
36 |
|
|
|
37 |
self.type = type;
|
|
|
38 |
|
|
|
39 |
self.$element = $(element);
|
|
|
40 |
self.$element.hide();
|
|
|
41 |
|
|
|
42 |
self.options = $.extend( {}, SelectSplitter.DEFAULTS, options);
|
|
|
43 |
|
|
|
44 |
// Get categoryParent data from $element's OPTGROUP.
|
|
|
45 |
self.fullCategoryList = {};
|
|
|
46 |
|
|
|
47 |
var optgroupCount = 0;
|
|
|
48 |
var longestOptionCount = 0;
|
|
|
49 |
|
|
|
50 |
self.$element.find('optgroup').each(function() {
|
|
|
51 |
|
|
|
52 |
self.fullCategoryList[$(this).attr('label')] = {};
|
|
|
53 |
|
|
|
54 |
var $that = $(this);
|
|
|
55 |
|
|
|
56 |
var currentOptionCount = 0;
|
|
|
57 |
var temporaryFullCategoryList = {};
|
|
|
58 |
$(this).find('option').each(function() {
|
|
|
59 |
var x = $(this).attr('value');
|
|
|
60 |
var y = $(this).text();
|
|
|
61 |
temporaryFullCategoryList[$(this).index()] = {
|
|
|
62 |
'x': x,
|
|
|
63 |
'y': y
|
|
|
64 |
};
|
|
|
65 |
currentOptionCount++;
|
|
|
66 |
});
|
|
|
67 |
|
|
|
68 |
if (currentOptionCount > longestOptionCount) { longestOptionCount = currentOptionCount ;}
|
|
|
69 |
|
|
|
70 |
self.fullCategoryList[$that.attr('label')] = temporaryFullCategoryList;
|
|
|
71 |
|
|
|
72 |
optgroupCount++;
|
|
|
73 |
});
|
|
|
74 |
|
|
|
75 |
// Get OPTIONS for $firstSelect.
|
|
|
76 |
var optionsHtml = '';
|
|
|
77 |
|
|
|
78 |
for (var key in self.fullCategoryList) {
|
|
|
79 |
if (self.fullCategoryList.hasOwnProperty(key)) {
|
|
|
80 |
optionsHtml = optionsHtml + '<option>' + key + '</option>';
|
|
|
81 |
}
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
// Add template.
|
|
|
85 |
self.$element.after(self.options.template);
|
|
|
86 |
|
|
|
87 |
|
|
|
88 |
// Define selected elements.
|
|
|
89 |
self.$wrapper = self.$element.next('div[data-selectsplitter-wrapper-selector]'); // improved by keenthemes
|
|
|
90 |
self.$firstSelect = $('select[data-selectsplitter-firstselect-selector]', self.$wrapper); // improved by keenthemes
|
|
|
91 |
self.$secondSelect = $('select[data-selectsplitter-secondselect-selector]', self.$wrapper); // improved by keenthemes
|
|
|
92 |
|
|
|
93 |
// Define $firstSelect and $secondSelect size attribute
|
|
|
94 |
var selectSize = Math.max(optgroupCount, longestOptionCount, 2);
|
|
|
95 |
selectSize = Math.min(selectSize, 10);
|
|
|
96 |
self.$firstSelect.attr('size', selectSize);
|
|
|
97 |
self.$secondSelect.attr('size', selectSize);
|
|
|
98 |
|
|
|
99 |
// Fill $firstSelect with OPTIONS
|
|
|
100 |
self.$firstSelect.append(optionsHtml);
|
|
|
101 |
|
|
|
102 |
// Define events.
|
|
|
103 |
self.$firstSelect.on('change', $.proxy(self.updateParentCategory, self));
|
|
|
104 |
self.$secondSelect.on('change', $.proxy(self.updateChildCategory, self));
|
|
|
105 |
|
|
|
106 |
// Define main variables.
|
|
|
107 |
self.$selectedOption = '';
|
|
|
108 |
self.currentParentCategory = '';
|
|
|
109 |
self.currentChildCategory = '';
|
|
|
110 |
|
|
|
111 |
// Takes in consideration whether an option is already selected before initialization.
|
|
|
112 |
// Note: .val() always returns the last value if SELECT is new. Hence cannot use .val() at init.
|
|
|
113 |
// Note2: find(option:selected) retourne toujours une OPTION même lors du premier affichage.
|
|
|
114 |
if ( self.$element.find('option[selected=selected]').length) {
|
|
|
115 |
|
|
|
116 |
self.$selectedOption = self.$element.find('option[selected=selected]');
|
|
|
117 |
|
|
|
118 |
self.currentParentCategory = self.$selectedOption.closest('optgroup').attr('label');
|
|
|
119 |
self.currentChildCategory = self.$selectedOption.attr('value');
|
|
|
120 |
|
|
|
121 |
self.$firstSelect.find('option:contains('+ self.currentParentCategory +')').attr('selected', 'selected');
|
|
|
122 |
self.$firstSelect.trigger('change');
|
|
|
123 |
}
|
|
|
124 |
};
|
|
|
125 |
|
|
|
126 |
SelectSplitter.prototype.updateParentCategory = function () {
|
|
|
127 |
|
|
|
128 |
var self = this;
|
|
|
129 |
|
|
|
130 |
// Update main variables.
|
|
|
131 |
self.currentParentCategory = self.$firstSelect.val();
|
|
|
132 |
|
|
|
133 |
self.$secondSelect.empty();
|
|
|
134 |
|
|
|
135 |
// Définit la liste de I pour les icônes à afficher en fonction de la page.
|
|
|
136 |
var optionsHtml = '';
|
|
|
137 |
|
|
|
138 |
for (var key in self.fullCategoryList[self.currentParentCategory]) {
|
|
|
139 |
if (self.fullCategoryList[self.currentParentCategory].hasOwnProperty(key)) {
|
|
|
140 |
optionsHtml = optionsHtml + '<option value="' + self.fullCategoryList[self.currentParentCategory][key]['x'] + '">' +
|
|
|
141 |
self.fullCategoryList[self.currentParentCategory][key]['y'] +
|
|
|
142 |
'</option>';
|
|
|
143 |
}
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
self.$secondSelect.append(optionsHtml);
|
|
|
147 |
|
|
|
148 |
if ( self.$selectedOption ) {
|
|
|
149 |
self.$secondSelect.find( 'option[value="' + self.$selectedOption.attr('value') + '"]' ).attr('selected', 'selected');
|
|
|
150 |
}
|
|
|
151 |
};
|
|
|
152 |
|
|
|
153 |
SelectSplitter.prototype.updateChildCategory = function (event) {
|
|
|
154 |
|
|
|
155 |
var self = this;
|
|
|
156 |
|
|
|
157 |
// Update main variables.
|
|
|
158 |
self.currentChildCategory = $(event.target).val(); // Note: event.target returns the SELECT, hence we must use val().
|
|
|
159 |
|
|
|
160 |
// Remove selected items in $element SELECT, if any.
|
|
|
161 |
self.$element.find('option[selected=selected]').removeAttr('selected');
|
|
|
162 |
|
|
|
163 |
// Add selected attribute to the new selected OPTION.
|
|
|
164 |
self.$element.find('option[value="' + self.currentChildCategory + '"]').attr('selected', 'selected'); // Note: Adding attr also updates val().
|
|
|
165 |
self.$element.trigger('change'); // Required for external plugins.
|
|
|
166 |
|
|
|
167 |
self.$selectedOption = self.$element.find('option[selected=selected]');
|
|
|
168 |
};
|
|
|
169 |
|
|
|
170 |
SelectSplitter.prototype.destroy = function () {
|
|
|
171 |
|
|
|
172 |
var self = this;
|
|
|
173 |
|
|
|
174 |
self.$wrapper.remove();
|
|
|
175 |
|
|
|
176 |
self.$element.removeData(self.type);
|
|
|
177 |
self.$element.show();
|
|
|
178 |
};
|
|
|
179 |
|
|
|
180 |
// PLUGIN DEFINITION
|
|
|
181 |
// =========================
|
|
|
182 |
|
|
|
183 |
function Plugin(option) {
|
|
|
184 |
return this.each(function() {
|
|
|
185 |
var $this = $(this);
|
|
|
186 |
var data = $this.data('selectsplitter');
|
|
|
187 |
|
|
|
188 |
var options = typeof option === 'object' && option;
|
|
|
189 |
|
|
|
190 |
if (!data && option == 'destroy') { return; }
|
|
|
191 |
if (!data) { $this.data('selectsplitter', ( data = new SelectSplitter(this, options) ) ); }
|
|
|
192 |
if (typeof option == 'string') { data[option](); }
|
|
|
193 |
});
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
$.fn.selectsplitter = Plugin;
|
|
|
197 |
/* http://stackoverflow.com/questions/10525600/what-is-the-purpose-of-fn-foo-constructor-foo-in-twitter-bootstrap-js */
|
|
|
198 |
$.fn.selectsplitter.Constructor = SelectSplitter;
|
|
|
199 |
|
|
|
200 |
|
|
|
201 |
}(jQuery);
|
|
|
202 |
|