forked from mleibman/SlickGrid
-
Notifications
You must be signed in to change notification settings - Fork 2
/
slick.model.js
292 lines (239 loc) · 9.26 KB
/
slick.model.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/***
* A simple observer pattern implementation.
*/
function EventHelper() {
this.handlers = [];
this.subscribe = function(fn) {
this.handlers.push(fn);
};
this.notify = function(args) {
for (var i = 0; i < this.handlers.length; i++) {
this.handlers[i].call(this, args);
}
};
return this;
}
(function($) {
/***
* A sample Model implementation.
* Provides a filtered view of the underlying data.
*
* Relies on the data item having an "id" property uniquely identifying it.
*/
function DataView() {
var self = this;
// private
var idProperty = "id"; // property holding a unique row id
var items = []; // data by index
var rows = []; // data by row
var idxById = {}; // indexes by id
var rowsById = null; // rows by id; lazy-calculated
var filter = null; // filter function
var updated = null; // updated item ids
var suspend = false; // suspends the recalculation
var sortAsc = true;
var sortComparer = null;
var fastSortField = null;
var pagesize = 0;
var pagenum = 0;
var totalRows = 0;
// events
var onRowCountChanged = new EventHelper();
var onRowsChanged = new EventHelper();
var onPagingInfoChanged = new EventHelper();
function beginUpdate() {
suspend = true;
}
function endUpdate() {
suspend = false;
refresh();
}
function refreshIdxById() {
idxById = {};
for (var i = 0,l = items.length; i < l; i++) {
var id = items[i][idProperty];
if (id == undefined || idxById[id] != undefined)
throw "Each data element must implement a unique 'id' property";
idxById[id] = i;
}
}
function getItems() {
return items;
}
function setItems(data, objectIdProperty) {
if (objectIdProperty !== undefined) idProperty = objectIdProperty;
items = data;
refreshIdxById();
refresh();
}
function setPagingOptions(args) {
if (args.pageSize != undefined)
pagesize = args.pageSize;
if (args.pageNum != undefined)
pagenum = Math.min(args.pageNum, Math.ceil(totalRows / pagesize));
onPagingInfoChanged.notify(getPagingInfo());
refresh();
}
function getPagingInfo() {
return {pageSize:pagesize, pageNum:pagenum, totalRows:totalRows};
}
function sort(comparer, ascending) {
sortAsc = ascending;
sortComparer = comparer;
fastSortField = null;
if (ascending === false) items.reverse();
items.sort(comparer);
if (ascending === false) items.reverse();
refreshIdxById();
refresh();
}
/***
* Provides a workaround for the extremely slow sorting in IE.
* Does a [lexicographic] sort on a give column by temporarily overriding Object.prototype.toString
* to return the value of that field and then doing a native Array.sort().
*/
function fastSort(field, ascending) {
sortAsc = ascending;
fastSortField = field;
sortComparer = null;
var oldToString = Object.prototype.toString;
Object.prototype.toString = (typeof field == "function")?field:function() { return this[field] };
// an extra reversal for descending sort keeps the sort stable
// (assuming a stable native sort implementation, which isn't true in some cases)
if (ascending === false) items.reverse();
items.sort();
Object.prototype.toString = oldToString;
if (ascending === false) items.reverse();
refreshIdxById();
refresh();
}
function reSort() {
if (sortComparer)
sort(sortComparer,sortAsc);
else if (fastSortField)
fastSort(fastSortField,sortAsc);
}
function setFilter(filterFn) {
filter = filterFn;
refresh();
}
function getItemByIdx(i) {
return items[i];
}
function getIdxById(id) {
return idxById[id];
}
// calculate the lookup table on first call
function getRowById(id) {
if (!rowsById) {
rowsById = {};
for (var i = 0, l = rows.length; i < l; ++i) {
rowsById[rows[i][idProperty]] = i;
}
}
return rowsById[id];
}
function getItemById(id) {
return items[idxById[id]];
}
function updateItem(id, item) {
if (idxById[id] === undefined || id !== item[idProperty])
throw "Invalid or non-matching id";
items[idxById[id]] = item;
if (!updated) updated = {};
updated[id] = true;
refresh();
}
function insertItem(insertBefore, item) {
items.splice(insertBefore, 0, item);
refreshIdxById(); // TODO: optimize
refresh();
}
function addItem(item) {
items.push(item);
refreshIdxById(); // TODO: optimize
refresh();
}
function deleteItem(id) {
if (idxById[id] === undefined)
throw "Invalid id";
items.splice(idxById[id], 1);
refreshIdxById(); // TODO: optimize
refresh();
}
function recalc(_items, _rows, _filter, _updated) {
var diff = [];
var items = _items, rows = _rows, filter = _filter, updated = _updated; // cache as local vars
rowsById = null;
// go over all items remapping them to rows on the fly
// while keeping track of the differences and updating indexes
var rl = rows.length;
var currentRowIndex = 0;
var currentPageIndex = 0;
var item,id;
for (var i = 0, il = items.length; i < il; ++i) {
item = items[i];
id = item[idProperty];
if (!filter || filter(item)) {
if (!pagesize || (currentRowIndex >= pagesize * pagenum && currentRowIndex < pagesize * (pagenum + 1))) {
if (currentPageIndex >= rl || id != rows[currentPageIndex][idProperty] || (updated && updated[id]))
diff[diff.length] = currentPageIndex;
rows[currentPageIndex] = item;
currentPageIndex++;
}
currentRowIndex++;
}
}
if (rl > currentPageIndex)
rows.splice(currentPageIndex, rl - currentPageIndex);
totalRows = currentRowIndex;
return diff;
}
function refresh() {
if (suspend) return;
var countBefore = rows.length;
var totalRowsBefore = totalRows;
var diff = recalc(items, rows, filter, updated); // pass as direct refs to avoid closure perf hit
// if the current page is no longer valid, go to last page and recalc
// we suffer a performance penalty here, but the main loop (recalc) remains highly optimized
if (pagesize && totalRows < pagenum * pagesize) {
pagenum = Math.floor(totalRows / pagesize);
diff = recalc(items, rows, filter, updated);
}
updated = null;
if (totalRowsBefore != totalRows) onPagingInfoChanged.notify(getPagingInfo());
if (countBefore != rows.length) onRowCountChanged.notify({previous:countBefore, current:rows.length});
if (diff.length > 0) onRowsChanged.notify(diff);
}
return {
// properties
"rows": rows, // note: neither the array or the data in it should be modified directly
// methods
"beginUpdate": beginUpdate,
"endUpdate": endUpdate,
"setPagingOptions": setPagingOptions,
"getPagingInfo": getPagingInfo,
"getItems": getItems,
"setItems": setItems,
"setFilter": setFilter,
"sort": sort,
"fastSort": fastSort,
"reSort": reSort,
"getIdxById": getIdxById,
"getRowById": getRowById,
"getItemById": getItemById,
"getItemByIdx": getItemByIdx,
"refresh": refresh,
"updateItem": updateItem,
"insertItem": insertItem,
"addItem": addItem,
"deleteItem": deleteItem,
// events
"onRowCountChanged": onRowCountChanged,
"onRowsChanged": onRowsChanged,
"onPagingInfoChanged": onPagingInfoChanged
};
}
// Slick.Data.DataView
$.extend(true, window, { Slick: { Data: { DataView: DataView }}});
})(jQuery);