-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathgrid.js
More file actions
233 lines (210 loc) · 6.38 KB
/
grid.js
File metadata and controls
233 lines (210 loc) · 6.38 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
steal('mxui/layout/table_scroll',
'mxui/data',
'jquery/controller/view',
'jquery/view/ejs',
'mxui/data/order',
'mxui/nav/selectable')
.then('./views/th.ejs','./views/init.ejs','./views/list.ejs')
.then(function($){
/**
* @class Mxui.Data.Grid
* @parent Mxui
*
* A simple data grid that is paginate-able and sortable.
*
* ## Use
*
* Add the grid to a div (or other element) like:
*
* $('#grid').mxui_data_grid({
*
* model : Recipe, // a model to use to make requests
*
* params : new Mxui.Data, // a model to use for pagination
* // and sorting values
*
* row : "//path/to/row.ejs" // a template to render a row with
*
* columns : { // column titles
* title : "Title",
* date : "Date"
* }
* })
*
* The grid will automatically 'fill'
* its parent element's height.
*
* @constructor
*
* @param {Object} columns A object of columnName -> columnTitle pairs. Ex:
*
* columns : { title : "Title", date : "Date" }
*
*/
$.Controller.extend("Mxui.Data.Grid",{
defaults: {
columns: {},
params: new Mxui.Data,
initTemplate: '//mxui/data/grid/views/init.ejs',
listTemplate: '//mxui/data/grid/views/list.ejs',
titleTemplate: '//mxui/data/grid/views/th.ejs',
rowTemplate : null,
model : null,
noItems : "No Items",
// if true, can sort by multiple columns at a time
multiSort: true,
// if true, there are three states (asc, desc, no sort)
canUnsort: true,
// set to false for infinite scroll
offsetEmpties: true,
// set to false to turn off the filler
filler: true,
// immediately uses the model to request items for the grid
loadImmediate: true,
selectable : true,
// default crud manipulations
refresh: function(tbody, elt, newElt){
elt.replaceWith(newElt)
tbody.resize()
},
append: function(tbody, newElt){
tbody.prepend(newElt)
tbody.resize()
},
remove: function(tbody, elt){
elt.remove()
tbody.resize()
}
},
listensTo : ["select","deselect"]
},
{
setup : function(el, options){
// check params has attrs
if(options && options.params && typeof options.params.attrs != 'function'){
options.params = new Mxui.Data(options.params)
}
this._super.apply(this, arguments);
},
init : function(){
//create the scrollable table
var count = 0;
for(var name in this.options.columns){
count++;
}
this.element.append( this.view(this.options.initTemplate,{titleTemplate: this.options.titleTemplate, columns: this.options.columns, count: count}) )
this.element.children('table').mxui_layout_table_scroll({
filler: this.options.filler
});
this.$ = this.element.children(":first").controller(Mxui.Layout.TableScroll).elements()
this.$.thead.mxui_data_order({
params: this.options.params,
multiSort: this.options.multiSort,
canUnsort: this.options.canUnsort
})
this.options.selectable && this.$.tbody.mxui_nav_selectable();
//this.scrollable.cache.thead.mxui_layout_resizer({selector: "th"});
this.element.addClass("grid");
if (this.options.filler) {
this.element.mxui_layout_fill();
}
//this.setFixedAndColumns()
// add jQuery UI stuff ...
this.element.find(".header table").attr('cellSpacing', '0').attr('cellPadding', '0');
var ths = this.$.thead.find('th').addClass("ui-helper-reset ui-state-default");
ths.eq(0).addClass('ui-corner-left')
ths.eq(-1).addClass('ui-corner-right')
if(this.options.loadImmediate){
this.options.model.findAll(this.options.params.attrs(), this.callback('list', true))
}
},
/**
*
* @param {Object} clear if this is true, clear the grid and create a new one, else insert
* @param {Object} items
*/
list : function(clear, items){
this.curentParams = this.options.params.attrs();
this.options.params.attr('updating', false);
var trs = $(this.view(this.options.listTemplate,{
rowTemplate : this.options.rowTemplate,
items: items
}));
if(clear){
this.empty();
}
this.append(trs);
// update the items
this.options.params.attr('count',items.count)
},
"{params} updated.attr" : function(params, ev, attr, val){
if(attr !== 'count' && attr !== 'updating'){
//want to throttle for rapid updates
params.attr('updating', true)
clearTimeout(this.newRequestTimer)
this.newRequestTimer = setTimeout(this.callback('newRequest', attr, val), 100)
}
},
newRequest : function(attr, val){
var clear = true;
if(!this.options.offsetEmpties && attr == "offset"){ // if offset changes and we have offsetEmpties false
clear = false;
}
this.makeRequest(clear)
},
makeRequest: function(clear){
this.options.model.findAll(this.options.params.attrs(), this.callback('list', clear))
},
_getRows: function(viewTemplateOption, items){
items = ( $.isArray(items) || items instanceof $.Model.List ) ? items : [items]
return $(this.view(this.options.listTemplate,{
rowTemplate : this.options[viewTemplateOption],
items: items
}, { columns: this.options.columns, renderer: this.options.renderer } ));
},
/**
* Listen for updates and replace the text of the list
* @param {Object} called
* @param {Object} item
*/
"{model} updated" : function(model, ev, item){
var el = item.elements(this.element)
if (el.length > 0){
var newElt= this._getRows('rowTemplate', item)
this.options.refresh(this.$.tbody, el, newElt)
}
},
"{model} created" : function(model, ev, item){
var newEl = this._getRows('rowTemplate', item)
this.options.append(this.$.tbody, newEl)
},
"{model} destroyed" : function(model, ev, item){
var el = item.elements(this.element)
this.options.remove(this.$.tbody, el)
},
/**
* Insert rows into the table
* @param {Object} row insert after this row
* @param {Object} newEls new elements to insert (they should be trs)
*/
append: function( row, newEls ) {
if( !newEls ) {
newEls = row;
row = undefined;
}
this.element.children(":first").mxui_layout_table_scroll("append", newEls, row)
},
/**
* Remove all children from the table
*/
empty: function(){
this.element.children(".mxui_layout_table_scroll").mxui_layout_table_scroll("empty")
},
"select" : function(el, ev){
ev.preventDefault();
},
"deselect" : function(el, ev){
ev.preventDefault();
}
});
})