corrade-nucleus-nucleons – Blame information for rev 24
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
20 | office | 1 | /** |
2 | * @author Nadim Basalamah <dimbslmh@gmail.com> |
||
3 | * @version: v1.1.0 |
||
4 | * https://github.com/dimbslmh/bootstrap-table/tree/master/src/extensions/multiple-sort/bootstrap-table-multiple-sort.js |
||
5 | * Modification: ErwannNevou <https://github.com/ErwannNevou> |
||
6 | */ |
||
7 | |||
8 | (function($) { |
||
9 | 'use strict'; |
||
10 | |||
11 | var isSingleSort = false; |
||
12 | |||
13 | var showSortModal = function(that) { |
||
14 | var _selector = that.$sortModal.selector, |
||
15 | _id = _selector.substr(1); |
||
16 | |||
17 | if (!$(_id).hasClass("modal")) { |
||
18 | var sModal = ' <div class="modal fade" id="' + _id + '" tabindex="-1" role="dialog" aria-labelledby="' + _id + 'Label" aria-hidden="true">'; |
||
19 | sModal += ' <div class="modal-dialog">'; |
||
20 | sModal += ' <div class="modal-content">'; |
||
21 | sModal += ' <div class="modal-header">'; |
||
22 | sModal += ' <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>'; |
||
23 | sModal += ' <h4 class="modal-title" id="' + _id + 'Label">' + that.options.formatMultipleSort() + '</h4>'; |
||
24 | sModal += ' </div>'; |
||
25 | sModal += ' <div class="modal-body">'; |
||
26 | sModal += ' <div class="bootstrap-table">'; |
||
27 | sModal += ' <div class="fixed-table-toolbar">'; |
||
28 | sModal += ' <div class="bars">'; |
||
29 | sModal += ' <div id="toolbar">'; |
||
30 | sModal += ' <button id="add" type="button" class="btn btn-default"><i class="' + that.options.iconsPrefix + ' ' + that.options.icons.plus + '"></i> ' + that.options.formatAddLevel() + '</button>'; |
||
31 | sModal += ' <button id="delete" type="button" class="btn btn-default" disabled><i class="' + that.options.iconsPrefix + ' ' + that.options.icons.minus + '"></i> ' + that.options.formatDeleteLevel() + '</button>'; |
||
32 | sModal += ' </div>'; |
||
33 | sModal += ' </div>'; |
||
34 | sModal += ' </div>'; |
||
35 | sModal += ' <div class="fixed-table-container">'; |
||
36 | sModal += ' <table id="multi-sort" class="table">'; |
||
37 | sModal += ' <thead>'; |
||
38 | sModal += ' <tr>'; |
||
39 | sModal += ' <th></th>'; |
||
40 | sModal += ' <th><div class="th-inner">' + that.options.formatColumn() + '</div></th>'; |
||
41 | sModal += ' <th><div class="th-inner">' + that.options.formatOrder() + '</div></th>'; |
||
42 | sModal += ' </tr>'; |
||
43 | sModal += ' </thead>'; |
||
44 | sModal += ' <tbody></tbody>'; |
||
45 | sModal += ' </table>'; |
||
46 | sModal += ' </div>'; |
||
47 | sModal += ' </div>'; |
||
48 | sModal += ' </div>'; |
||
49 | sModal += ' <div class="modal-footer">'; |
||
50 | sModal += ' <button type="button" class="btn btn-default" data-dismiss="modal">' + that.options.formatCancel() + '</button>'; |
||
51 | sModal += ' <button type="button" class="btn btn-primary">' + that.options.formatSort() + '</button>'; |
||
52 | sModal += ' </div>'; |
||
53 | sModal += ' </div>'; |
||
54 | sModal += ' </div>'; |
||
55 | sModal += ' </div>'; |
||
56 | |||
57 | $('body').append($(sModal)); |
||
58 | |||
59 | that.$sortModal = $(_selector); |
||
60 | var $rows = that.$sortModal.find('tbody > tr'); |
||
61 | |||
62 | that.$sortModal.off('click', '#add').on('click', '#add', function() { |
||
63 | var total = that.$sortModal.find('.multi-sort-name:first option').length, |
||
64 | current = that.$sortModal.find('tbody tr').length; |
||
65 | |||
66 | if (current < total) { |
||
67 | current++; |
||
68 | that.addLevel(); |
||
69 | that.setButtonStates(); |
||
70 | } |
||
71 | }); |
||
72 | |||
73 | that.$sortModal.off('click', '#delete').on('click', '#delete', function() { |
||
74 | var total = that.$sortModal.find('.multi-sort-name:first option').length, |
||
75 | current = that.$sortModal.find('tbody tr').length; |
||
76 | |||
77 | if (current > 1 && current <= total) { |
||
78 | current--; |
||
79 | that.$sortModal.find('tbody tr:last').remove(); |
||
80 | that.setButtonStates(); |
||
81 | } |
||
82 | }); |
||
83 | |||
84 | that.$sortModal.off('click', '.btn-primary').on('click', '.btn-primary', function() { |
||
85 | var $rows = that.$sortModal.find('tbody > tr'), |
||
86 | $alert = that.$sortModal.find('div.alert'), |
||
87 | fields = [], |
||
88 | results = []; |
||
89 | |||
90 | |||
91 | that.options.sortPriority = $.map($rows, function(row) { |
||
92 | var $row = $(row), |
||
93 | name = $row.find('.multi-sort-name').val(), |
||
94 | order = $row.find('.multi-sort-order').val(); |
||
95 | |||
96 | fields.push(name); |
||
97 | |||
98 | return { |
||
99 | sortName: name, |
||
100 | sortOrder: order |
||
101 | }; |
||
102 | }); |
||
103 | |||
104 | var sorted_fields = fields.sort(); |
||
105 | |||
106 | for (var i = 0; i < fields.length - 1; i++) { |
||
107 | if (sorted_fields[i + 1] == sorted_fields[i]) { |
||
108 | results.push(sorted_fields[i]); |
||
109 | } |
||
110 | } |
||
111 | |||
112 | if (results.length > 0) { |
||
113 | if ($alert.length === 0) { |
||
114 | $alert = '<div class="alert alert-danger" role="alert"><strong>' + that.options.formatDuplicateAlertTitle() + '</strong> ' + that.options.formatDuplicateAlertDescription() + '</div>'; |
||
115 | $($alert).insertBefore(that.$sortModal.find('.bars')); |
||
116 | } |
||
117 | } else { |
||
118 | if ($alert.length === 1) { |
||
119 | $($alert).remove(); |
||
120 | } |
||
121 | |||
122 | that.$sortModal.modal('hide'); |
||
123 | that.options.sortName = ''; |
||
124 | |||
125 | if (that.options.sidePagination === 'server') { |
||
126 | |||
127 | that.options.queryParams = function(params) { |
||
128 | params.multiSort = that.options.sortPriority; |
||
129 | return params; |
||
130 | }; |
||
131 | |||
132 | that.initServer(that.options.silentSort); |
||
133 | return; |
||
134 | } |
||
135 | |||
136 | that.onMultipleSort(); |
||
137 | |||
138 | } |
||
139 | }); |
||
140 | |||
141 | if (that.options.sortPriority === null || that.options.sortPriority.length === 0) { |
||
142 | if (that.options.sortName) { |
||
143 | that.options.sortPriority = [{ |
||
144 | sortName: that.options.sortName, |
||
145 | sortOrder: that.options.sortOrder |
||
146 | }]; |
||
147 | } |
||
148 | } |
||
149 | |||
150 | if (that.options.sortPriority !== null && that.options.sortPriority.length > 0) { |
||
151 | if ($rows.length < that.options.sortPriority.length && typeof that.options.sortPriority === 'object') { |
||
152 | for (var i = 0; i < that.options.sortPriority.length; i++) { |
||
153 | that.addLevel(i, that.options.sortPriority[i]); |
||
154 | } |
||
155 | } |
||
156 | } else { |
||
157 | that.addLevel(0); |
||
158 | } |
||
159 | |||
160 | that.setButtonStates(); |
||
161 | } |
||
162 | }; |
||
163 | |||
164 | $.extend($.fn.bootstrapTable.defaults, { |
||
165 | showMultiSort: false, |
||
166 | sortPriority: null, |
||
167 | onMultipleSort: function() { |
||
168 | return false; |
||
169 | } |
||
170 | }); |
||
171 | |||
172 | $.extend($.fn.bootstrapTable.defaults.icons, { |
||
173 | sort: 'glyphicon-sort', |
||
174 | plus: 'glyphicon-plus', |
||
175 | minus: 'glyphicon-minus' |
||
176 | }); |
||
177 | |||
178 | $.extend($.fn.bootstrapTable.Constructor.EVENTS, { |
||
179 | 'multiple-sort.bs.table': 'onMultipleSort' |
||
180 | }); |
||
181 | |||
182 | $.extend($.fn.bootstrapTable.locales, { |
||
183 | formatMultipleSort: function() { |
||
184 | return 'Multiple Sort'; |
||
185 | }, |
||
186 | formatAddLevel: function() { |
||
187 | return 'Add Level'; |
||
188 | }, |
||
189 | formatDeleteLevel: function() { |
||
190 | return 'Delete Level'; |
||
191 | }, |
||
192 | formatColumn: function() { |
||
193 | return 'Column'; |
||
194 | }, |
||
195 | formatOrder: function() { |
||
196 | return 'Order'; |
||
197 | }, |
||
198 | formatSortBy: function() { |
||
199 | return 'Sort by'; |
||
200 | }, |
||
201 | formatThenBy: function() { |
||
202 | return 'Then by'; |
||
203 | }, |
||
204 | formatSort: function() { |
||
205 | return 'Sort'; |
||
206 | }, |
||
207 | formatCancel: function() { |
||
208 | return 'Cancel'; |
||
209 | }, |
||
210 | formatDuplicateAlertTitle: function() { |
||
211 | return 'Duplicate(s) detected!'; |
||
212 | }, |
||
213 | formatDuplicateAlertDescription: function() { |
||
214 | return 'Please remove or change any duplicate column.'; |
||
215 | }, |
||
216 | formatSortOrders: function() { |
||
217 | return { |
||
218 | asc: 'Ascending', |
||
219 | desc: 'Descending' |
||
220 | }; |
||
221 | } |
||
222 | }); |
||
223 | |||
224 | $.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales); |
||
225 | |||
226 | var BootstrapTable = $.fn.bootstrapTable.Constructor, |
||
227 | _initToolbar = BootstrapTable.prototype.initToolbar; |
||
228 | |||
229 | BootstrapTable.prototype.initToolbar = function() { |
||
230 | this.showToolbar = true; |
||
231 | var that = this, |
||
232 | sortModalId = '#sortModal_' + this.$el.attr('id'); |
||
233 | this.$sortModal = $(sortModalId); |
||
234 | |||
235 | _initToolbar.apply(this, Array.prototype.slice.apply(arguments)); |
||
236 | |||
237 | if (this.options.showMultiSort) { |
||
238 | var $btnGroup = this.$toolbar.find('>.btn-group').first(), |
||
239 | $multiSortBtn = this.$toolbar.find('div.multi-sort'); |
||
240 | |||
241 | if (!$multiSortBtn.length) { |
||
242 | $multiSortBtn = ' <button class="multi-sort btn btn-default' + (this.options.iconSize === undefined ? '' : ' btn-' + this.options.iconSize) + '" type="button" data-toggle="modal" data-target="' + sortModalId + '" title="' + this.options.formatMultipleSort() + '">'; |
||
243 | $multiSortBtn += ' <i class="' + this.options.iconsPrefix + ' ' + this.options.icons.sort + '"></i>'; |
||
244 | $multiSortBtn += '</button>'; |
||
245 | |||
246 | $btnGroup.append($multiSortBtn); |
||
247 | |||
248 | showSortModal(that); |
||
249 | } |
||
250 | |||
251 | this.$el.on('sort.bs.table', function() { |
||
252 | isSingleSort = true; |
||
253 | }); |
||
254 | |||
255 | this.$el.on('multiple-sort.bs.table', function() { |
||
256 | isSingleSort = false; |
||
257 | }); |
||
258 | |||
259 | this.$el.on('load-success.bs.table', function() { |
||
260 | if (!isSingleSort && that.options.sortPriority !== null && typeof that.options.sortPriority === 'object' && that.options.sidePagination !== 'server') { |
||
261 | that.onMultipleSort(); |
||
262 | } |
||
263 | }); |
||
264 | |||
265 | this.$el.on('column-switch.bs.table', function(field, checked) { |
||
266 | for (var i = 0; i < that.options.sortPriority.length; i++) { |
||
267 | if (that.options.sortPriority[i].sortName === checked) { |
||
268 | that.options.sortPriority.splice(i, 1); |
||
269 | } |
||
270 | } |
||
271 | |||
272 | that.assignSortableArrows(); |
||
273 | that.$sortModal.remove(); |
||
274 | showSortModal(that); |
||
275 | }); |
||
276 | |||
277 | this.$el.on('reset-view.bs.table', function() { |
||
278 | if (!isSingleSort && that.options.sortPriority !== null && typeof that.options.sortPriority === 'object') { |
||
279 | that.assignSortableArrows(); |
||
280 | } |
||
281 | }); |
||
282 | } |
||
283 | }; |
||
284 | |||
285 | BootstrapTable.prototype.onMultipleSort = function() { |
||
286 | var that = this; |
||
287 | |||
288 | var cmp = function(x, y) { |
||
289 | return x > y ? 1 : x < y ? -1 : 0; |
||
290 | }; |
||
291 | |||
292 | var arrayCmp = function(a, b) { |
||
293 | var arr1 = [], |
||
294 | arr2 = []; |
||
295 | |||
296 | for (var i = 0; i < that.options.sortPriority.length; i++) { |
||
297 | var order = that.options.sortPriority[i].sortOrder === 'desc' ? -1 : 1, |
||
298 | aa = a[that.options.sortPriority[i].sortName], |
||
299 | bb = b[that.options.sortPriority[i].sortName]; |
||
300 | |||
301 | if (aa === undefined || aa === null) { |
||
302 | aa = ''; |
||
303 | } |
||
304 | if (bb === undefined || bb === null) { |
||
305 | bb = ''; |
||
306 | } |
||
307 | if ($.isNumeric(aa) && $.isNumeric(bb)) { |
||
308 | aa = parseFloat(aa); |
||
309 | bb = parseFloat(bb); |
||
310 | } |
||
311 | if (typeof aa !== 'string') { |
||
312 | aa = aa.toString(); |
||
313 | } |
||
314 | |||
315 | arr1.push( |
||
316 | order * cmp(aa, bb)); |
||
317 | arr2.push( |
||
318 | order * cmp(bb, aa)); |
||
319 | } |
||
320 | |||
321 | return cmp(arr1, arr2); |
||
322 | }; |
||
323 | |||
324 | this.data.sort(function(a, b) { |
||
325 | return arrayCmp(a, b); |
||
326 | }); |
||
327 | |||
328 | this.initBody(); |
||
329 | this.assignSortableArrows(); |
||
330 | this.trigger('multiple-sort'); |
||
331 | }; |
||
332 | |||
333 | BootstrapTable.prototype.addLevel = function(index, sortPriority) { |
||
334 | var text = index === 0 ? this.options.formatSortBy() : this.options.formatThenBy(); |
||
335 | |||
336 | this.$sortModal.find('tbody') |
||
337 | .append($('<tr>') |
||
338 | .append($('<td>').text(text)) |
||
339 | .append($('<td>').append($('<select class="form-control multi-sort-name">'))) |
||
340 | .append($('<td>').append($('<select class="form-control multi-sort-order">'))) |
||
341 | ); |
||
342 | |||
343 | var $multiSortName = this.$sortModal.find('.multi-sort-name').last(), |
||
344 | $multiSortOrder = this.$sortModal.find('.multi-sort-order').last(); |
||
345 | |||
346 | $.each(this.columns, function(i, column) { |
||
347 | if (column.sortable === false || column.visible === false) { |
||
348 | return true; |
||
349 | } |
||
350 | $multiSortName.append('<option value="' + column.field + '">' + column.title + '</option>'); |
||
351 | }); |
||
352 | |||
353 | $.each(this.options.formatSortOrders(), function(value, order) { |
||
354 | $multiSortOrder.append('<option value="' + value + '">' + order + '</option>'); |
||
355 | }); |
||
356 | |||
357 | if (sortPriority !== undefined) { |
||
358 | $multiSortName.find('option[value="' + sortPriority.sortName + '"]').attr("selected", true); |
||
359 | $multiSortOrder.find('option[value="' + sortPriority.sortOrder + '"]').attr("selected", true); |
||
360 | } |
||
361 | }; |
||
362 | |||
363 | BootstrapTable.prototype.assignSortableArrows = function() { |
||
364 | var that = this, |
||
365 | headers = that.$header.find('th'); |
||
366 | |||
367 | for (var i = 0; i < headers.length; i++) { |
||
368 | for (var c = 0; c < that.options.sortPriority.length; c++) { |
||
369 | if ($(headers[i]).data('field') === that.options.sortPriority[c].sortName) { |
||
370 | $(headers[i]).find('.sortable').removeClass('desc asc').addClass(that.options.sortPriority[c].sortOrder); |
||
371 | } |
||
372 | } |
||
373 | } |
||
374 | }; |
||
375 | |||
376 | BootstrapTable.prototype.setButtonStates = function() { |
||
377 | var total = this.$sortModal.find('.multi-sort-name:first option').length, |
||
378 | current = this.$sortModal.find('tbody tr').length; |
||
379 | |||
380 | if (current == total) { |
||
381 | this.$sortModal.find('#add').attr('disabled', 'disabled'); |
||
382 | } |
||
383 | if (current > 1) { |
||
384 | this.$sortModal.find('#delete').removeAttr('disabled'); |
||
385 | } |
||
386 | if (current < total) { |
||
387 | this.$sortModal.find('#add').removeAttr('disabled'); |
||
388 | } |
||
389 | if (current == 1) { |
||
390 | this.$sortModal.find('#delete').attr('disabled', 'disabled'); |
||
391 | } |
||
392 | }; |
||
393 | })(jQuery); |