corrade-nucleus-nucleons – Blame information for rev 20

Subversion Repositories:
Rev:
Rev Author Line No. Line
20 office 1 /**
2 * @author: Yura Knoxville
3 * @version: v1.0.0
4 */
5  
6 !function ($) {
7  
8 'use strict';
9  
10 var initBodyCaller,
11 tableGroups;
12  
13 // it only does '%s', and return '' when arguments are undefined
14 var sprintf = function (str) {
15 var args = arguments,
16 flag = true,
17 i = 1;
18  
19 str = str.replace(/%s/g, function () {
20 var arg = args[i++];
21  
22 if (typeof arg === 'undefined') {
23 flag = false;
24 return '';
25 }
26 return arg;
27 });
28 return flag ? str : '';
29 };
30  
31 var groupBy = function (array , f) {
32 var groups = {};
33 array.forEach(function(o) {
34 var group = f(o);
35 groups[group] = groups[group] || [];
36 groups[group].push(o);
37 });
38  
39 return groups;
40 };
41  
42 $.extend($.fn.bootstrapTable.defaults, {
43 groupBy: false,
44 groupByField: ''
45 });
46  
47 var BootstrapTable = $.fn.bootstrapTable.Constructor,
48 _initSort = BootstrapTable.prototype.initSort,
49 _initBody = BootstrapTable.prototype.initBody,
50 _updateSelected = BootstrapTable.prototype.updateSelected;
51  
52 BootstrapTable.prototype.initSort = function () {
53 _initSort.apply(this, Array.prototype.slice.apply(arguments));
54  
55 var that = this;
56 tableGroups = [];
57  
58 if ((this.options.groupBy) && (this.options.groupByField !== '')) {
59  
60 if ((this.options.sortName != this.options.groupByField)) {
61 this.data.sort(function(a, b) {
62 return a[that.options.groupByField].localeCompare(b[that.options.groupByField]);
63 });
64 }
65  
66 var that = this;
67 var groups = groupBy(that.data, function (item) {
68 return [item[that.options.groupByField]];
69 });
70  
71 var index = 0;
72 $.each(groups, function(key, value) {
73 tableGroups.push({
74 id: index,
75 name: key
76 });
77  
78 value.forEach(function(item) {
79 if (!item._data) {
80 item._data = {};
81 }
82  
83 item._data['parent-index'] = index;
84 });
85  
86 index++;
87 });
88 }
89 }
90  
91 BootstrapTable.prototype.initBody = function () {
92 initBodyCaller = true;
93  
94 _initBody.apply(this, Array.prototype.slice.apply(arguments));
95  
96 if ((this.options.groupBy) && (this.options.groupByField !== '')) {
97 var that = this,
98 checkBox = false,
99 visibleColumns = 0;
100  
101 this.columns.forEach(function(column) {
102 if (column.checkbox) {
103 checkBox = true;
104 } else {
105 if (column.visible) {
106 visibleColumns += 1;
107 }
108 }
109 });
110  
111 if (this.options.detailView && !this.options.cardView) {
112 visibleColumns += 1;
113 }
114  
115 tableGroups.forEach(function(item){
116 var html = [];
117  
118 html.push(sprintf('<tr class="info groupBy expanded" data-group-index="%s">', item.id));
119  
120 if (that.options.detailView && !that.options.cardView) {
121 html.push('<td class="detail"></td>');
122 }
123  
124 if (checkBox) {
125 html.push('<td class="bs-checkbox">',
126 '<input name="btSelectGroup" type="checkbox" />',
127 '</td>'
128 );
129 }
130  
131 html.push('<td',
132 sprintf(' colspan="%s"', visibleColumns),
133 '>', item.name, '</td>'
134 );
135  
136 html.push('</tr>');
137  
138 that.$body.find('tr[data-parent-index='+item.id+']:first').before($(html.join('')));
139 });
140  
141 this.$selectGroup = [];
142 this.$body.find('[name="btSelectGroup"]').each(function() {
143 var self = $(this);
144  
145 that.$selectGroup.push({
146 group: self,
147 item: that.$selectItem.filter(function () {
148 return ($(this).closest('tr').data('parent-index') ===
149 self.closest('tr').data('group-index'));
150 })
151 });
152 });
153  
154 this.$container.off('click', '.groupBy')
155 .on('click', '.groupBy', function() {
156 $(this).toggleClass('expanded');
157 that.$body.find('tr[data-parent-index='+$(this).closest('tr').data('group-index')+']').toggleClass('hidden');
158 });
159  
160 this.$container.off('click', '[name="btSelectGroup"]')
161 .on('click', '[name="btSelectGroup"]', function (event) {
162 event.stopImmediatePropagation();
163  
164 var self = $(this);
165 var checked = self.prop('checked');
166 that[checked ? 'checkGroup' : 'uncheckGroup']($(this).closest('tr').data('group-index'));
167 });
168 }
169  
170 initBodyCaller = false;
171 this.updateSelected();
172 };
173  
174 BootstrapTable.prototype.updateSelected = function () {
175 if (!initBodyCaller) {
176 _updateSelected.apply(this, Array.prototype.slice.apply(arguments));
177  
178 if ((this.options.groupBy) && (this.options.groupByField !== '')) {
179 this.$selectGroup.forEach(function (item) {
180 var checkGroup = item.item.filter(':enabled').length ===
181 item.item.filter(':enabled').filter(':checked').length;
182  
183 item.group.prop('checked', checkGroup);
184 });
185 }
186 }
187 };
188  
189 BootstrapTable.prototype.getGroupSelections = function (index) {
190 var that = this;
191  
192 return $.grep(this.data, function (row) {
193 return (row[that.header.stateField] && (row._data['parent-index'] === index));
194 });
195 };
196  
197 BootstrapTable.prototype.checkGroup = function (index) {
198 this.checkGroup_(index, true);
199 };
200  
201 BootstrapTable.prototype.uncheckGroup = function (index) {
202 this.checkGroup_(index, false);
203 };
204  
205 BootstrapTable.prototype.checkGroup_ = function (index, checked) {
206 var rows;
207 var filter = function() {
208 return ($(this).closest('tr').data('parent-index') === index);
209 };
210  
211 if (!checked) {
212 rows = this.getGroupSelections(index);
213 }
214  
215 this.$selectItem.filter(filter).prop('checked', checked);
216  
217  
218 this.updateRows();
219 this.updateSelected();
220 if (checked) {
221 rows = this.getGroupSelections(index);
222 }
223 this.trigger(checked ? 'check-all' : 'uncheck-all', rows);
224 };
225  
226 }(jQuery);