-
Notifications
You must be signed in to change notification settings - Fork 0
/
backgrid-expandable-cell.js
92 lines (70 loc) · 3.2 KB
/
backgrid-expandable-cell.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
backgrid-expandable-cell
https://github.com/cime/backgrid-expandable-cell
Copyright (c) 2014 Andrej Cimperšek
Licensed under the MIT @license.
*/
(function () {
var ExpandableCell = Backgrid.ExpandableCell = Backgrid.Cell.extend({
accordion: true,
toggle: '<i style="cursor: pointer;" class="glyphicon toggle pull-left"></i>',
toggleClass: 'toggle',
toggleExpandedClass: 'glyphicon-minus-sign',
toggleCollapsedClass: 'glyphicon-plus-sign',
trClass: 'expandable',
tdClass: 'expandable-content',
events: {
'click': 'setToggle'
},
initialize: function (options) {
if (options.accordion) {
this.accordion = options.accordion;
}
this.column = options.column;
if (!(this.column instanceof Backgrid.Column)) {
this.column = new Backgrid.Column(this.column);
}
var column = this.column, model = this.model, $el = this.$el;
if (Backgrid.callByNeed(column.renderable(), column, model)) $el.addClass("renderable");
},
render: function () {
/* follow along with the original render really... */
this.$el.empty();
this.$toggleEl = $(this.toggle).addClass(this.toggleClass).addClass(this.toggleCollapsedClass);
this.$el.append(this.$toggleEl);
this.delegateEvents();
return this;
},
setToggle: function () {
var detailsRow = this.$el.data('details');
var toggle = this.$toggleEl;
/* if there's details data already stored, then we'll remove it */
if (detailsRow) {
$(detailsRow).remove();
this.$el.data('details', null);
toggle.removeClass(this.toggleExpandedClass).addClass(this.toggleCollapsedClass);
} else {
if (this.accordion) {
var table = this.$el.closest('table');
$('.' + this.toggleClass, table).filter('.' + this.toggleExpandedClass).click();
}
var renderableColumns = this.$el.closest('table').find('th.renderable').length;
var isRenderable = false;
var cellClass = this.tdClass;
if (Backgrid.callByNeed(this.column.renderable(), this.column, this.model)) {
isRenderable = true;
cellClass += ' renderable';
}
/* build a jquery object for the new row... */
detailsRow = $('<tr class="' + this.trClass + '"><td class="' + (isRenderable ? 'renderable' : '') + '"></td><td class="' + cellClass + '" colspan="' + (renderableColumns - 1) + '"></td></tr>');
/* Inject new row */
this.$el.closest('tr').after(detailsRow);
/* Call expand function */
this.column.get('expand')(detailsRow.find('td.' + this.tdClass), this.model);
this.$el.data('details', detailsRow);
toggle.removeClass(this.toggleCollapsedClass).addClass(this.toggleExpandedClass);
}
return this;
}
});
})();