UNPKG

13.6 kBJavaScriptView Raw
1(function (global, factory) {
2 if (typeof define === "function" && define.amd) {
3 define([], factory);
4 } else if (typeof exports !== "undefined") {
5 factory();
6 } else {
7 var mod = {
8 exports: {}
9 };
10 factory();
11 global.bootstrapTableToolbar = mod.exports;
12 }
13})(this, function () {
14 'use strict';
15
16 var _slicedToArray = function () {
17 function sliceIterator(arr, i) {
18 var _arr = [];
19 var _n = true;
20 var _d = false;
21 var _e = undefined;
22
23 try {
24 for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
25 _arr.push(_s.value);
26
27 if (i && _arr.length === i) break;
28 }
29 } catch (err) {
30 _d = true;
31 _e = err;
32 } finally {
33 try {
34 if (!_n && _i["return"]) _i["return"]();
35 } finally {
36 if (_d) throw _e;
37 }
38 }
39
40 return _arr;
41 }
42
43 return function (arr, i) {
44 if (Array.isArray(arr)) {
45 return arr;
46 } else if (Symbol.iterator in Object(arr)) {
47 return sliceIterator(arr, i);
48 } else {
49 throw new TypeError("Invalid attempt to destructure non-iterable instance");
50 }
51 };
52 }();
53
54 function _classCallCheck(instance, Constructor) {
55 if (!(instance instanceof Constructor)) {
56 throw new TypeError("Cannot call a class as a function");
57 }
58 }
59
60 var _createClass = function () {
61 function defineProperties(target, props) {
62 for (var i = 0; i < props.length; i++) {
63 var descriptor = props[i];
64 descriptor.enumerable = descriptor.enumerable || false;
65 descriptor.configurable = true;
66 if ("value" in descriptor) descriptor.writable = true;
67 Object.defineProperty(target, descriptor.key, descriptor);
68 }
69 }
70
71 return function (Constructor, protoProps, staticProps) {
72 if (protoProps) defineProperties(Constructor.prototype, protoProps);
73 if (staticProps) defineProperties(Constructor, staticProps);
74 return Constructor;
75 };
76 }();
77
78 function _possibleConstructorReturn(self, call) {
79 if (!self) {
80 throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
81 }
82
83 return call && (typeof call === "object" || typeof call === "function") ? call : self;
84 }
85
86 var _get = function get(object, property, receiver) {
87 if (object === null) object = Function.prototype;
88 var desc = Object.getOwnPropertyDescriptor(object, property);
89
90 if (desc === undefined) {
91 var parent = Object.getPrototypeOf(object);
92
93 if (parent === null) {
94 return undefined;
95 } else {
96 return get(parent, property, receiver);
97 }
98 } else if ("value" in desc) {
99 return desc.value;
100 } else {
101 var getter = desc.get;
102
103 if (getter === undefined) {
104 return undefined;
105 }
106
107 return getter.call(receiver);
108 }
109 };
110
111 function _inherits(subClass, superClass) {
112 if (typeof superClass !== "function" && superClass !== null) {
113 throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
114 }
115
116 subClass.prototype = Object.create(superClass && superClass.prototype, {
117 constructor: {
118 value: subClass,
119 enumerable: false,
120 writable: true,
121 configurable: true
122 }
123 });
124 if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
125 }
126
127 /**
128 * @author: aperez <aperez@datadec.es>
129 * @version: v2.0.0
130 *
131 * @update Dennis Hernández <http://djhvscf.github.io/Blog>
132 * @update zhixin wen <wenzhixin2010@gmail.com>
133 */
134
135 (function ($) {
136 var Utils = $.fn.bootstrapTable.utils;
137
138 var bootstrap = {
139 3: {
140 icons: {
141 advancedSearchIcon: 'glyphicon-chevron-down'
142 },
143 html: {
144 modalHeader: '\n <div class="modal-header">\n <button type="button" class="close" data-dismiss="modal" aria-label="Close">\n <span aria-hidden="true">&times;</span>\n </button>\n <h4 class="modal-title">%s</h4>\n </div>\n '
145 }
146 },
147 4: {
148 icons: {
149 advancedSearchIcon: 'fa-chevron-down'
150 },
151 html: {
152 modalHeader: '\n <div class="modal-header">\n <h4 class="modal-title">%s</h4>\n <button type="button" class="close" data-dismiss="modal" aria-label="Close">\n <span aria-hidden="true">&times;</span>\n </button>\n </div>\n '
153 }
154 }
155 }[Utils.bootstrapVersion];
156
157 $.extend($.fn.bootstrapTable.defaults, {
158 advancedSearch: false,
159 idForm: 'advancedSearch',
160 actionForm: '',
161 idTable: undefined,
162 onColumnAdvancedSearch: function onColumnAdvancedSearch(field, text) {
163 return false;
164 }
165 });
166
167 $.extend($.fn.bootstrapTable.defaults.icons, {
168 advancedSearchIcon: bootstrap.icons.advancedSearchIcon
169 });
170
171 $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
172 'column-advanced-search.bs.table': 'onColumnAdvancedSearch'
173 });
174
175 $.extend($.fn.bootstrapTable.locales, {
176 formatAdvancedSearch: function formatAdvancedSearch() {
177 return 'Advanced search';
178 },
179 formatAdvancedCloseButton: function formatAdvancedCloseButton() {
180 return 'Close';
181 }
182 });
183
184 $.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales);
185
186 $.BootstrapTable = function (_$$BootstrapTable) {
187 _inherits(_class, _$$BootstrapTable);
188
189 function _class() {
190 _classCallCheck(this, _class);
191
192 return _possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).apply(this, arguments));
193 }
194
195 _createClass(_class, [{
196 key: 'initToolbar',
197 value: function initToolbar() {
198 var _this2 = this;
199
200 var o = this.options;
201
202 this.showToolbar = this.showToolbar || o.search && o.advancedSearch && o.idTable;
203
204 _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'initToolbar', this).call(this);
205
206 if (!o.search || !o.advancedSearch || !o.idTable) {
207 return;
208 }
209
210 this.$toolbar.find('>.btn-group').append('\n <button class="btn btn-default' + Utils.sprintf(' btn-%s', o.buttonsClass) + Utils.sprintf(' btn-%s', o.iconSize) + '"\n type="button"\n name="advancedSearch"\n aria-label="advanced search"\n title="' + o.formatAdvancedSearch() + '">\n <i class="' + o.iconsPrefix + ' ' + o.icons.advancedSearchIcon + '"></i>\n </button>\n ');
211
212 this.$toolbar.find('button[name="advancedSearch"]').off('click').on('click', function () {
213 return _this2.showAvdSearch();
214 });
215 }
216 }, {
217 key: 'showAvdSearch',
218 value: function showAvdSearch() {
219 var _this3 = this;
220
221 var o = this.options;
222
223 if (!$('#avdSearchModal_' + o.idTable).hasClass('modal')) {
224 $('body').append('\n <div id="avdSearchModal_' + o.idTable + '" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">\n <div class="modal-dialog modal-xs">\n <div class="modal-content">\n ' + Utils.sprintf(bootstrap.html.modalHeader, o.formatAdvancedSearch()) + '\n <div class="modal-body modal-body-custom">\n <div class="container-fluid" id="avdSearchModalContent_' + o.idTable + '"\n style="padding-right: 0px; padding-left: 0px;" >\n </div>\n </div>\n <div class="modal-footer">\n <button type="button" id="btnCloseAvd_' + o.idTable + '" class="btn btn-' + o.buttonsClass + '">\n ' + o.formatAdvancedCloseButton() + '\n </button>\n </div>\n </div>\n </div>\n </div>\n ');
225
226 var timeoutId = 0;
227
228 $('#avdSearchModalContent_' + o.idTable).append(this.createFormAvd().join(''));
229
230 $('#' + o.idForm).off('keyup blur', 'input').on('keyup blur', 'input', function (e) {
231 if (o.sidePagination === 'server') {
232 _this3.onColumnAdvancedSearch(e);
233 } else {
234 clearTimeout(timeoutId);
235 timeoutId = setTimeout(function () {
236 _this3.onColumnAdvancedSearch(e);
237 }, o.searchTimeOut);
238 }
239 });
240
241 $('#btnCloseAvd_' + o.idTable).click(function () {
242 $('#avdSearchModal_' + o.idTable).modal('hide');
243 if (o.sidePagination === 'server') {
244 _this3.options.pageNumber = 1;
245 _this3.updatePagination();
246 _this3.trigger('column-advanced-search', _this3.filterColumnsPartial);
247 }
248 });
249
250 $('#avdSearchModal_' + o.idTable).modal();
251 } else {
252 $('#avdSearchModal_' + o.idTable).modal();
253 }
254 }
255 }, {
256 key: 'createFormAvd',
257 value: function createFormAvd() {
258 var o = this.options;
259 var html = ['<form class="form-horizontal" id="' + o.idForm + '" action="' + o.actionForm + '">'];
260
261 var _iteratorNormalCompletion = true;
262 var _didIteratorError = false;
263 var _iteratorError = undefined;
264
265 try {
266 for (var _iterator = this.columns[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
267 var column = _step.value;
268
269 if (!column.checkbox && column.visible && column.searchable) {
270 html.push('\n <div class="form-group row">\n <label class="col-sm-4 control-label">' + column.title + '</label>\n <div class="col-sm-6">\n <input type="text" class="form-control input-md" name="' + column.field + '" placeholder="' + column.title + '" id="' + column.field + '">\n </div>\n </div>\n ');
271 }
272 }
273 } catch (err) {
274 _didIteratorError = true;
275 _iteratorError = err;
276 } finally {
277 try {
278 if (!_iteratorNormalCompletion && _iterator.return) {
279 _iterator.return();
280 }
281 } finally {
282 if (_didIteratorError) {
283 throw _iteratorError;
284 }
285 }
286 }
287
288 html.push('</form>');
289
290 return html;
291 }
292 }, {
293 key: 'initSearch',
294 value: function initSearch() {
295 var _this4 = this;
296
297 _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'initSearch', this).call(this);
298
299 if (!this.options.advancedSearch || this.options.sidePagination === 'server') {
300 return;
301 }
302
303 var fp = $.isEmptyObject(this.filterColumnsPartial) ? null : this.filterColumnsPartial;
304
305 this.data = fp ? $.grep(this.data, function (item, i) {
306 var _iteratorNormalCompletion2 = true;
307 var _didIteratorError2 = false;
308 var _iteratorError2 = undefined;
309
310 try {
311 for (var _iterator2 = Object.entries(fp)[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
312 var _ref = _step2.value;
313
314 var _ref2 = _slicedToArray(_ref, 2);
315
316 var key = _ref2[0];
317 var v = _ref2[1];
318
319 var fval = v.toLowerCase();
320 var value = item[key];
321 var index = _this4.header.fields.indexOf(key);
322 value = Utils.calculateObjectValue(_this4.header, _this4.header.formatters[index], [value, item, i], value);
323
324 if (!(index !== -1 && (typeof value === 'string' || typeof value === 'number') && ('' + value).toLowerCase().includes(fval))) {
325 return false;
326 }
327 }
328 } catch (err) {
329 _didIteratorError2 = true;
330 _iteratorError2 = err;
331 } finally {
332 try {
333 if (!_iteratorNormalCompletion2 && _iterator2.return) {
334 _iterator2.return();
335 }
336 } finally {
337 if (_didIteratorError2) {
338 throw _iteratorError2;
339 }
340 }
341 }
342
343 return true;
344 }) : this.data;
345 }
346 }, {
347 key: 'onColumnAdvancedSearch',
348 value: function onColumnAdvancedSearch(e) {
349 var text = $.trim($(e.currentTarget).val());
350 var $field = $(e.currentTarget)[0].id;
351
352 if ($.isEmptyObject(this.filterColumnsPartial)) {
353 this.filterColumnsPartial = {};
354 }
355 if (text) {
356 this.filterColumnsPartial[$field] = text;
357 } else {
358 delete this.filterColumnsPartial[$field];
359 }
360
361 if (this.options.sidePagination !== 'server') {
362 this.options.pageNumber = 1;
363 this.onSearch(e);
364 this.updatePagination();
365 this.trigger('column-advanced-search', $field, text);
366 }
367 }
368 }]);
369
370 return _class;
371 }($.BootstrapTable);
372 })(jQuery);
373});
\No newline at end of file