corrade-nucleus-nucleons – Blame information for rev 20

Subversion Repositories:
Rev:
Rev Author Line No. Line
20 office 1 /**
2 * @author: Dennis Hernández
3 * @webSite: http://djhvscf.github.io/Blog
4 * @version: v1.1.0
5 */
6  
7 !function ($) {
8  
9 'use strict';
10  
11 var originalRowAttr,
12 dataTTId = 'data-tt-id',
13 dataTTParentId = 'data-tt-parent-id',
14 obj = {},
15 parentId = undefined;
16  
17 var getParentRowId = function (that, id) {
18 var parentRows = that.$body.find('tr').not('[' + 'data-tt-parent-id]');
19  
20 for (var i = 0; i < parentRows.length; i++) {
21 if (i === id) {
22 return $(parentRows[i]).attr('data-tt-id');
23 }
24 }
25  
26 return undefined;
27 };
28  
29 var sumData = function (that, data) {
30 var sumRow = {};
31 $.each(data, function (i, row) {
32 if (!row.IsParent) {
33 for (var prop in row) {
34 if (!isNaN(parseFloat(row[prop]))) {
35 if (that.columns[$.fn.bootstrapTable.utils.getFieldIndex(that.columns, prop)].groupBySumGroup) {
36 if (sumRow[prop] === undefined) {
37 sumRow[prop] = 0;
38 }
39 sumRow[prop] += +row[prop];
40 }
41 }
42 }
43 }
44 });
45 return sumRow;
46 };
47  
48 var rowAttr = function (row, index) {
49 //Call the User Defined Function
50 originalRowAttr.apply([row, index]);
51  
52 obj[dataTTId.toString()] = index;
53  
54 if (!row.IsParent) {
55 obj[dataTTParentId.toString()] = parentId === undefined ? index : parentId;
56 } else {
57 parentId = index;
58 delete obj[dataTTParentId.toString()];
59 }
60  
61 return obj;
62 };
63  
64 var setObjectKeys = function () {
65 // From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
66 Object.keys = function (o) {
67 if (o !== Object(o)) {
68 throw new TypeError('Object.keys called on a non-object');
69 }
70 var k = [],
71 p;
72 for (p in o) {
73 if (Object.prototype.hasOwnProperty.call(o, p)) {
74 k.push(p);
75 }
76 }
77 return k;
78 }
79 };
80  
81 var getDataArrayFromItem = function (that, item) {
82 var itemDataArray = [];
83 for (var i = 0; i < that.options.groupByField.length; i++) {
84 itemDataArray.push(item[that.options.groupByField[i]]);
85 }
86  
87 return itemDataArray;
88 };
89  
90 var getNewRow = function (that, result, index) {
91 var newRow = {};
92 for (var i = 0; i < that.options.groupByField.length; i++) {
93 newRow[that.options.groupByField[i].toString()] = result[index][0][that.options.groupByField[i]];
94 }
95  
96 newRow.IsParent = true;
97  
98 return newRow;
99 };
100  
101 var groupBy = function (array, f) {
102 var groups = {};
103 $.each(array, function (i, o) {
104 var group = JSON.stringify(f(o));
105 groups[group] = groups[group] || [];
106 groups[group].push(o);
107 });
108 return Object.keys(groups).map(function (group) {
109 return groups[group];
110 });
111 };
112  
113 var makeGrouped = function (that, data) {
114 var newData = [],
115 sumRow = {};
116  
117 var result = groupBy(data, function (item) {
118 return getDataArrayFromItem(that, item);
119 });
120  
121 for (var i = 0; i < result.length; i++) {
122 result[i].unshift(getNewRow(that, result, i));
123 if (that.options.groupBySumGroup) {
124 sumRow = sumData(that, result[i]);
125 if (!$.isEmptyObject(sumRow)) {
126 result[i].push(sumRow);
127 }
128 }
129 }
130  
131 newData = newData.concat.apply(newData, result);
132  
133 if (!that.options.loaded && newData.length > 0) {
134 that.options.loaded = true;
135 that.options.originalData = that.options.data;
136 that.options.data = newData;
137 }
138  
139 return newData;
140 };
141  
142 $.extend($.fn.bootstrapTable.defaults, {
143 groupBy: false,
144 groupByField: [],
145 groupBySumGroup: false,
146 groupByInitExpanded: undefined, //node, 'all'
147 //internal variables
148 loaded: false,
149 originalData: undefined
150 });
151  
152 $.fn.bootstrapTable.methods.push('collapseAll', 'expandAll', 'refreshGroupByField');
153  
154 $.extend($.fn.bootstrapTable.COLUMN_DEFAULTS, {
155 groupBySumGroup: false
156 });
157  
158 var BootstrapTable = $.fn.bootstrapTable.Constructor,
159 _init = BootstrapTable.prototype.init,
160 _initData = BootstrapTable.prototype.initData;
161  
162 BootstrapTable.prototype.init = function () {
163 //Temporal validation
164 if (!this.options.sortName) {
165 if ((this.options.groupBy) && (this.options.groupByField.length > 0)) {
166 var that = this;
167  
168 // Compatibility: IE < 9 and old browsers
169 if (!Object.keys) {
170 $.fn.bootstrapTable.utils.objectKeys();
171 }
172  
173 //Make sure that the internal variables are set correctly
174 this.options.loaded = false;
175 this.options.originalData = undefined;
176  
177 originalRowAttr = this.options.rowAttributes;
178 this.options.rowAttributes = rowAttr;
179 this.$el.on('post-body.bs.table', function () {
180 that.$el.treetable({
181 expandable: true,
182 onNodeExpand: function () {
183 if (that.options.height) {
184 that.resetHeader();
185 }
186 },
187 onNodeCollapse: function () {
188 if (that.options.height) {
189 that.resetHeader();
190 }
191 }
192 }, true);
193  
194 if (that.options.groupByInitExpanded !== undefined) {
195 if (typeof that.options.groupByInitExpanded === 'number') {
196 that.expandNode(that.options.groupByInitExpanded);
197 } else if (that.options.groupByInitExpanded.toLowerCase() === 'all') {
198 that.expandAll();
199 }
200 }
201 });
202 }
203 }
204 _init.apply(this, Array.prototype.slice.apply(arguments));
205 };
206  
207 BootstrapTable.prototype.initData = function (data, type) {
208 //Temporal validation
209 if (!this.options.sortName) {
210 if ((this.options.groupBy) && (this.options.groupByField.length > 0)) {
211  
212 this.options.groupByField = typeof this.options.groupByField === 'string' ?
213 this.options.groupByField.replace('[', '').replace(']', '')
214 .replace(/ /g, '').toLowerCase().split(',') : this.options.groupByField;
215  
216 data = makeGrouped(this, data ? data : this.options.data);
217 }
218 }
219 _initData.apply(this, [data, type]);
220 };
221  
222 BootstrapTable.prototype.expandAll = function () {
223 this.$el.treetable('expandAll');
224 };
225  
226 BootstrapTable.prototype.collapseAll = function () {
227 this.$el.treetable('collapseAll');
228 };
229  
230 BootstrapTable.prototype.expandNode = function (id) {
231 id = getParentRowId(this, id);
232 if (id !== undefined) {
233 this.$el.treetable('expandNode', id);
234 }
235 };
236  
237 BootstrapTable.prototype.refreshGroupByField = function (groupByFields) {
238 if (!$.fn.bootstrapTable.utils.compareObjects(this.options.groupByField, groupByFields)) {
239 this.options.groupByField = groupByFields;
240 this.load(this.options.originalData);
241 }
242 };
243 }(jQuery);