foo_table-rails 2.0.1.4 → 2.0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,373 @@
1
+ (function($, w, undefined) {
2
+ if (w.footable === undefined || w.foobox === null) throw new Error('Please check and make sure footable.js is included in the page and is loaded prior to this script.');
3
+ var defaults = {
4
+ grid: {
5
+ enabled: true,
6
+ data: null,
7
+ template: null, //row html template, use for make a row.
8
+ cols: null, //column define
9
+ items: null, //data items
10
+ url: null, //get data from url
11
+ ajax: null, //paramater for $.ajax
12
+ activeClass: 'active', //add to row selected
13
+ multiSelect: false, //allow select multiple row
14
+ showIndex: false, //show row index
15
+ showCheckbox: false, //show checkbox for select
16
+ showEmptyInfo: false, //when that is not data in table, show a info to notify user
17
+ emptyInfo: '<p class="text-center text-warning">No Data</p>',
18
+ pagination: {
19
+ "page-size": 20,
20
+ "pagination-class": "pagination pagination-centered"
21
+ },
22
+ indexFormatter: function(val, $td, index) {
23
+ return index + 1;
24
+ },
25
+ checkboxFormatter: function(isTop) {
26
+ return '<input type="checkbox" class="' + (isTop ? 'checkAll' : 'check') + '">';
27
+ },
28
+ events: {
29
+ loaded: 'footable_grid_loaded',
30
+ created: 'footable_grid_created',
31
+ removed: 'footable_grid_removed',
32
+ updated: 'footable_grid_updated'
33
+ }
34
+ }
35
+ };
36
+
37
+ function makeTh(col) {
38
+ var $th = $('<th>' + col.title + '</th>');
39
+ if ($.isPlainObject(col.data)) {
40
+ $th.data(col.data);
41
+ }
42
+ if ($.isPlainObject(col.style)) {
43
+ $th.css(col.style);
44
+ }
45
+ if (col.className) {
46
+ $th.addClass(col.className);
47
+ }
48
+ return $th;
49
+ }
50
+
51
+ function initThead($table, options) {
52
+ var $thead = $table.find('thead');
53
+ if ($thead.size() === 0) {
54
+ $thead = $('<thead>').appendTo($table);
55
+ }
56
+ var $row = $('<tr>').appendTo($thead);
57
+ for (var i = 0, len = options.cols.length; i < len; i++) {
58
+ $row.append(makeTh(options.cols[i]));
59
+ }
60
+ }
61
+
62
+ function initTBody($table) {
63
+ var $tbody = $table.find('tbody');
64
+ if ($tbody.size() === 0) {
65
+ $tbody = $('<tbody>').appendTo($table);
66
+ }
67
+ }
68
+
69
+ function initPagination($table, cols, options) {
70
+ if (options) {
71
+ $table.attr("data-page-size", options['page-size']);
72
+ var $tfoot = $table.find('tfoot');
73
+ if ($tfoot.size() === 0) {
74
+ $tfoot = $('<tfoot class="hide-if-no-paging"></tfoot>').appendTo($table);
75
+ }
76
+ $tfoot.append('<tr><td colspan=' + cols.length + '></td></tr>');
77
+ var $pagination = $("<div>").appendTo($tfoot.find("tr:last-child td"));
78
+ $pagination.addClass(options['pagination-class']);
79
+ }
80
+ }
81
+
82
+ function setToggleColumn(cols) {
83
+ var toggleColumn = cols[0];
84
+ for (var i = 0, len = cols.length; i < len; i++) {
85
+ var col = cols[i];
86
+ if (col.data && (col.data.toggle === true || col.data.toggle === "true")) {
87
+ return;
88
+ }
89
+ }
90
+ toggleColumn.data = $.extend(toggleColumn.data, {
91
+ toggle: true
92
+ });
93
+ }
94
+
95
+ function makeEmptyInfo($table, cols, emptyInfo) {
96
+ if ($table.find("tr.emptyInfo").size() === 0) {
97
+ $table.find('tbody').append('<tr class="emptyInfo"><td colspan="' + cols.length + '">' + emptyInfo + '</td></tr>');
98
+ }
99
+ }
100
+
101
+ function updateRowIndex($tbody, $newRow, detailClass, offset) {
102
+ //update rows index
103
+ $tbody.find('tr:not(.' + detailClass + ')').each(function() {
104
+ var $row = $(this),
105
+ index = $newRow.data('index'),
106
+ oldIndex = parseInt($row.data('index'), 0),
107
+ newIndex = oldIndex + offset;
108
+ if (oldIndex >= index && this !== $newRow.get(0)) {
109
+ $row.attr('data-index', newIndex).data('index', newIndex);
110
+ }
111
+ });
112
+ }
113
+
114
+ function Grid() {
115
+ var grid = this;
116
+ grid.name = 'Footable Grid';
117
+ grid.init = function(ft) {
118
+ var toggleClass = ft.options.classes.toggle;
119
+ var detailClass = ft.options.classes.detail;
120
+ var options = ft.options.grid;
121
+ if (!options.cols) return;
122
+ grid.footable = ft;
123
+ var $table = $(ft.table);
124
+ $table.data('grid', grid);
125
+ if ($.isPlainObject(options.data)) {
126
+ $table.data(options.data);
127
+ }
128
+ grid._items = [];
129
+ setToggleColumn(options.cols);
130
+ if (options.showCheckbox) {
131
+ options.multiSelect = true;
132
+ options.cols.unshift({
133
+ title: options.checkboxFormatter(true),
134
+ name: '',
135
+ data: {
136
+ "sort-ignore": true
137
+ },
138
+ formatter: options.checkboxFormatter
139
+ });
140
+ }
141
+ if (options.showIndex) {
142
+ options.cols.unshift({
143
+ title: '#',
144
+ name: 'index',
145
+ data: {
146
+ "sort-ignore": true
147
+ },
148
+ formatter: options.indexFormatter
149
+ });
150
+ }
151
+ initThead($table, options);
152
+ initTBody($table);
153
+ initPagination($table, options.cols, options.pagination);
154
+ $table.off('.grid').on({
155
+ 'footable_initialized.grid': function(e) {
156
+ if (options.url || options.ajax) {
157
+ $.ajax(options.ajax || {
158
+ url: options.url
159
+ }).then(function(resp) {
160
+ grid.newItem(resp);
161
+ ft.raise(options.events.loaded);
162
+ }, function(jqXHR) {
163
+ throw 'load data from ' + (options.url || options.ajax.url) + ' fail';
164
+ });
165
+ } else {
166
+ grid.newItem(options.items || []);
167
+ ft.raise(options.events.loaded);
168
+ }
169
+ },
170
+ 'footable_sorted.grid footable_grid_created.grid footable_grid_removed.grid': function(event) {
171
+ if (options.showIndex && grid.getItem().length > 0) {
172
+ $table.find('tbody tr:not(.' + detailClass + ')').each(function(index) {
173
+ var $td = $(this).find('td:first');
174
+ $td.html(options.indexFormatter(null, $td, index));
175
+ });
176
+ }
177
+ },
178
+ 'footable_redrawn.grid footable_row_removed.grid': function(event) {
179
+ if (grid.getItem().length === 0 && options.showEmptyInfo) {
180
+ makeEmptyInfo($table, options.cols, options.emptyInfo);
181
+ }
182
+ }
183
+ }).on({
184
+ 'click.grid': function(event) {
185
+ if ($(event.target).closest('td').find('>.' + toggleClass).size() > 0) {
186
+ return true;
187
+ }
188
+ var $tr = $(event.currentTarget);
189
+ if ($tr.hasClass(detailClass)) {
190
+ return true;
191
+ }
192
+ if (!options.multiSelect && !$tr.hasClass(options.activeClass)) {
193
+ $table.find('tbody tr.' + options.activeClass).removeClass(options.activeClass);
194
+ }
195
+ $tr.toggleClass(options.activeClass);
196
+ if (options.showCheckbox) {
197
+ $tr.find('input:checkbox.check').prop('checked', function(index, val) {
198
+ if (event.target === this) {
199
+ return val;
200
+ }
201
+ return !val;
202
+ });
203
+ }
204
+ ft.toggleDetail($tr);
205
+ }
206
+ }, 'tbody tr').on('click.grid', 'thead input:checkbox.checkAll', function(event) {
207
+ var checked = !! event.currentTarget.checked;
208
+ if (checked) {
209
+ $table.find('tbody tr').addClass(options.activeClass);
210
+ } else {
211
+ $table.find('tbody tr').removeClass(options.activeClass);
212
+ }
213
+ $table.find('tbody input:checkbox.check').prop('checked', checked);
214
+ });
215
+ };
216
+ /**
217
+ * get selected rows index;
218
+ */
219
+ grid.getSelected = function() {
220
+ var options = grid.footable.options.grid,
221
+ $selected = $(grid.footable.table).find('tbody>tr.' + options.activeClass);
222
+ return $selected.map(function() {
223
+ return $(this).data('index');
224
+ });
225
+ };
226
+ /**
227
+ * get row's data by index
228
+ */
229
+ grid.getItem = function(index) {
230
+ if (index !== undefined) {
231
+ if ($.isArray(index)) {
232
+ return $.map(index, function(item) {
233
+ return grid._items[item];
234
+ });
235
+ }
236
+ return grid._items[index];
237
+ }
238
+ return grid._items;
239
+ };
240
+
241
+ function makeCell(col, value, index) {
242
+ var $td = $('<td>');
243
+ if (col.formatter) {
244
+ $td.html(col.formatter(value, $td, index));
245
+ } else {
246
+ $td.html(value || '');
247
+ }
248
+ return $td;
249
+ }
250
+ grid._makeRow = function(item, index) {
251
+ var options = grid.footable.options.grid;
252
+ var $row;
253
+ if ($.isFunction(options.template)) {
254
+ $row = $(options.template($.extend({}, {
255
+ __index: index
256
+ }, item)));
257
+ } else {
258
+ $row = $('<tr>');
259
+ for (var i = 0, len = options.cols.length; i < len; i++) {
260
+ var col = options.cols[i];
261
+ $row.append(makeCell(col, item[col.name] || '', index));
262
+ }
263
+ }
264
+ $row.attr('data-index', index);
265
+ return $row;
266
+ };
267
+ /**
268
+ * create rows by js object
269
+ */
270
+ grid.newItem = function(item, index, wait) {
271
+ var $tbody = $(grid.footable.table).find('tbody');
272
+ var detailClass = grid.footable.options.classes.detail;
273
+ $tbody.find('tr.emptyInfo').remove();
274
+ if ($.isArray(item)) {
275
+ for (var atom;
276
+ (atom = item.pop());) {
277
+ grid.newItem(atom, index, true);
278
+ }
279
+ grid.footable.redraw();
280
+ grid.footable.raise(grid.footable.options.grid.events.created, {
281
+ item: item,
282
+ index: index
283
+ });
284
+ return;
285
+ }
286
+ if (!$.isPlainObject(item)) {
287
+ return;
288
+ }
289
+ var $tr, len = grid._items.length;
290
+ if (index === undefined || index < 0 || index > len) {
291
+ $tr = grid._makeRow(item, len++);
292
+ grid._items.push(item);
293
+ $tbody.append($tr);
294
+ } else {
295
+ $tr = grid._makeRow(item, index);
296
+ if (index === 0) {
297
+ grid._items.unshift(item);
298
+ $tbody.prepend($tr);
299
+ } else {
300
+ var $before = $tbody.find('tr[data-index=' + (index - 1) + ']');
301
+ grid._items.splice(index, 0, item);
302
+ if ($before.data('detail_created') === true) {
303
+ $before = $before.next();
304
+ }
305
+ $before.after($tr);
306
+ }
307
+ updateRowIndex($tbody, $tr, detailClass, 1);
308
+ }
309
+ if (!wait) {
310
+ grid.footable.redraw();
311
+ grid.footable.raise(grid.footable.options.grid.events.created, {
312
+ item: item,
313
+ index: index
314
+ });
315
+ }
316
+ };
317
+ /**
318
+ * update row by js object
319
+ */
320
+ grid.setItem = function(item, index) {
321
+ if (!$.isPlainObject(item)) {
322
+ return;
323
+ }
324
+ var $tbody = $(grid.footable.table).find('tbody'),
325
+ $newTr = grid._makeRow(item, index);
326
+ $.extend(grid._items[index], item);
327
+ var $tr = $tbody.find('tr').eq(index);
328
+ $tr.html($newTr.html());
329
+ grid.footable.redraw();
330
+ grid.footable.raise(grid.footable.options.grid.events.updated, {
331
+ item: item,
332
+ index: index
333
+ });
334
+ };
335
+ /**
336
+ * remove rows by index
337
+ */
338
+ grid.removeItem = function(index) {
339
+ var $tbody = $(grid.footable.table).find('tbody');
340
+ var detailClass = grid.footable.options.classes.detail;
341
+ var result = [];
342
+ if ($.isArray(index)) {
343
+ for (var i;
344
+ (i = index.pop());) {
345
+ result.push(grid.removeItem(i));
346
+ }
347
+ grid.footable.raise(grid.footable.options.grid.events.removed, {
348
+ item: result,
349
+ index: index
350
+ });
351
+ return result;
352
+ }
353
+ if (index === undefined) {
354
+ $tbody.find('tr').each(function() {
355
+ result.push(grid._items.shift());
356
+ grid.footable.removeRow(this);
357
+ });
358
+ } else {
359
+ var $tr = $tbody.find('tr[data-index=' + index + ']');
360
+ result = grid._items.splice(index, 1)[0];
361
+ grid.footable.removeRow($tr);
362
+ //update rows index
363
+ updateRowIndex($tbody, $tr, detailClass, -1);
364
+ }
365
+ grid.footable.raise(grid.footable.options.grid.events.removed, {
366
+ item: result,
367
+ index: index
368
+ });
369
+ return result;
370
+ };
371
+ }
372
+ w.footable.plugins.register(Grid, defaults);
373
+ })(jQuery, window);