corrade-nucleus-nucleons – Blame information for rev 20

Subversion Repositories:
Rev:
Rev Author Line No. Line
20 office 1 /**
2 * @author: Jewway
3 * @version: v1.0.0
4 */
5  
6 !function ($) {
7 'use strict';
8  
9 function getCurrentHeader(that) {
10 var header = that.$header;
11 if (that.options.height) {
12 header = that.$tableHeader;
13 }
14  
15 return header;
16 }
17  
18 function getFilterFields(that) {
19 return getCurrentHeader(that).find('[data-filter-field]');
20 }
21  
22 function setFilterValues(that) {
23 var $filterElms = getFilterFields(that);
24 if (!$.isEmptyObject(that.filterColumnsPartial)) {
25 $filterElms.each(function (index, ele) {
26 var $ele = $(ele),
27 field = $ele.attr('data-filter-field'),
28 value = that.filterColumnsPartial[field];
29  
30 if ($ele.is("select")) {
31 $ele.val(value).trigger('change');
32 }
33 else {
34 $ele.val(value);
35 }
36 });
37 }
38 }
39  
40 function createFilter(that, header) {
41 var enableFilter = false,
42 isVisible,
43 html,
44 timeoutId = 0;
45  
46 $.each(that.columns, function (i, column) {
47 isVisible = 'hidden';
48 html = [];
49  
50 if (!column.visible) {
51 return;
52 }
53  
54 if (!column.filter) {
55 html.push('<div class="no-filter"></div>');
56 } else {
57 var filterClass = column.filter.class ? ' ' + column.filter.class : '';
58 html.push('<div style="margin: 0px 2px 2px 2px;" class="filter' + filterClass + '">');
59  
60 if (column.searchable) {
61 enableFilter = true;
62 isVisible = 'visible'
63 }
64  
65 switch (column.filter.type.toLowerCase()) {
66 case 'input' :
67 html.push('<input type="text" data-filter-field="' + column.field + '" style="width: 100%; visibility:' + isVisible + '">');
68 break;
69 case 'select':
70 html.push('<select data-filter-field="' + column.field + '" style="width: 100%; visibility:' + isVisible + '"></select>');
71 break;
72 }
73 }
74  
75 $.each(header.children().children(), function (i, tr) {
76 tr = $(tr);
77 if (tr.data('field') === column.field) {
78 tr.find('.fht-cell').append(html.join(''));
79 return false;
80 }
81 });
82 });
83  
84 if (enableFilter) {
85 var $inputs = header.find('input'),
86 $selects = header.find('select');
87  
88  
89 if ($inputs.length > 0) {
90 $inputs.off('keyup').on('keyup', function (event) {
91 clearTimeout(timeoutId);
92 timeoutId = setTimeout(function () {
93 that.onColumnSearch(event);
94 }, that.options.searchTimeOut);
95 });
96  
97  
98 $inputs.off('mouseup').on('mouseup', function (event) {
99 var $input = $(this),
100 oldValue = $input.val();
101  
102 if (oldValue === "") {
103 return;
104 }
105  
106 setTimeout(function () {
107 var newValue = $input.val();
108  
109 if (newValue === "") {
110 clearTimeout(timeoutId);
111 timeoutId = setTimeout(function () {
112 that.onColumnSearch(event);
113 }, that.options.searchTimeOut);
114 }
115 }, 1);
116 });
117 }
118  
119 if ($selects.length > 0) {
120 $selects.on('select2:select', function (event) {
121 that.onColumnSearch(event);
122 });
123 }
124 } else {
125 header.find('.filter').hide();
126 }
127 }
128  
129 function initSelect2(that) {
130 var $header = getCurrentHeader(that);
131  
132 $.each(that.columns, function (idx, column) {
133 if (column.filter && column.filter.type === 'select') {
134 var $selectEle = $header.find('select[data-filter-field=' + column.field + ']');
135  
136 if ($selectEle.length > 0 && !$selectEle.data().select2) {
137 column.filter.data.unshift("");
138  
139 var select2Opts = {
140 placeholder: "",
141 allowClear: true,
142 data: column.filter.data,
143 dropdownParent: that.$el.closest(".bootstrap-table")
144 };
145  
146 $selectEle.select2(select2Opts);
147 $selectEle.on("select2:unselecting", function (event) {
148 event.preventDefault();
149 $selectEle.val(null).trigger('change');
150 that.searchText = undefined;
151 that.onColumnSearch(event);
152 });
153 }
154 }
155 });
156 }
157  
158 $.extend($.fn.bootstrapTable.defaults, {
159 filter: false,
160 filterValues: {}
161 });
162  
163 $.extend($.fn.bootstrapTable.COLUMN_DEFAULTS, {
164 filter: undefined
165 });
166  
167 var BootstrapTable = $.fn.bootstrapTable.Constructor,
168 _init = BootstrapTable.prototype.init,
169 _initHeader = BootstrapTable.prototype.initHeader,
170 _initSearch = BootstrapTable.prototype.initSearch;
171  
172 BootstrapTable.prototype.init = function () {
173 //Make sure that the filtercontrol option is set
174 if (this.options.filter) {
175 var that = this;
176  
177 if (!$.isEmptyObject(that.options.filterValues)) {
178 that.filterColumnsPartial = that.options.filterValues;
179 that.options.filterValues = {};
180 }
181  
182 this.$el.on('reset-view.bs.table', function () {
183 //Create controls on $tableHeader if the height is set
184 if (!that.options.height) {
185 return;
186 }
187  
188 //Avoid recreate the controls
189 if (that.$tableHeader.find('select').length > 0 || that.$tableHeader.find('input').length > 0) {
190 return;
191 }
192  
193 createFilter(that, that.$tableHeader);
194 }).on('post-header.bs.table', function () {
195 var timeoutId = 0;
196  
197 initSelect2(that);
198 clearTimeout(timeoutId);
199 timeoutId = setTimeout(function () {
200 setFilterValues(that);
201 }, that.options.searchTimeOut - 1000);
202 }).on('column-switch.bs.table', function (field, checked) {
203 setFilterValues(that);
204 });
205 }
206  
207 _init.apply(this, Array.prototype.slice.apply(arguments));
208 };
209  
210 BootstrapTable.prototype.initHeader = function () {
211 _initHeader.apply(this, Array.prototype.slice.apply(arguments));
212 if (this.options.filter) {
213 createFilter(this, this.$header);
214 }
215 };
216  
217 BootstrapTable.prototype.initSearch = function () {
218 _initSearch.apply(this, Array.prototype.slice.apply(arguments));
219  
220 var that = this,
221 filterValues = that.filterColumnsPartial;
222  
223 // Filter for client
224 if (that.options.sidePagination === 'client') {
225 this.data = $.grep(this.data, function (row, idx) {
226 for (var field in filterValues) {
227 var column = that.columns[$.fn.bootstrapTable.utils.getFieldIndex(that.columns, field)],
228 filterValue = filterValues[field].toLowerCase(),
229 rowValue = row[field];
230  
231 rowValue = $.fn.bootstrapTable.utils.calculateObjectValue(
232 that.header,
233 that.header.formatters[$.inArray(field, that.header.fields)],
234 [rowValue, row, idx], rowValue);
235  
236 if (column.filterStrictSearch) {
237 if (!($.inArray(field, that.header.fields) !== -1 &&
238 (typeof rowValue === 'string' || typeof rowValue === 'number') &&
239 rowValue.toString().toLowerCase() === filterValue.toString().toLowerCase())) {
240 return false;
241 }
242 } else {
243 if (!($.inArray(field, that.header.fields) !== -1 &&
244 (typeof rowValue === 'string' || typeof rowValue === 'number') &&
245 (rowValue + '').toLowerCase().indexOf(filterValue) !== -1)) {
246 return false;
247 }
248 }
249 }
250  
251 return true;
252 });
253 }
254 };
255  
256 BootstrapTable.prototype.onColumnSearch = function (event) {
257 var field = $(event.currentTarget).attr('data-filter-field'),
258 value = $.trim($(event.currentTarget).val());
259  
260 if ($.isEmptyObject(this.filterColumnsPartial)) {
261 this.filterColumnsPartial = {};
262 }
263  
264 if (value) {
265 this.filterColumnsPartial[field] = value;
266 } else {
267 delete this.filterColumnsPartial[field];
268 }
269  
270 this.options.pageNumber = 1;
271 this.onSearch(event);
272 };
273  
274 BootstrapTable.prototype.setFilterData = function (field, data) {
275 var that = this,
276 $header = getCurrentHeader(that),
277 $selectEle = $header.find('select[data-filter-field=\"' + field + '\"]');
278  
279 data.unshift("");
280 $selectEle.empty();
281 $selectEle.select2({
282 data: data,
283 placeholder: "",
284 allowClear: true,
285 dropdownParent: that.$el.closest(".bootstrap-table")
286 });
287  
288 $.each(this.columns, function (idx, column) {
289 if (column.field === field) {
290 column.filter.data = data;
291 return false;
292 }
293 });
294 };
295  
296 BootstrapTable.prototype.setFilterValues = function (values) {
297 this.filterColumnsPartial = values;
298 };
299  
300 $.fn.bootstrapTable.methods.push('setFilterData');
301 $.fn.bootstrapTable.methods.push('setFilterValues');
302  
303 }(jQuery);