1318 |
alexandre_ |
1 |
/*
|
|
|
2 |
Copyright (c) 2004-2006, The Dojo Foundation
|
|
|
3 |
All Rights Reserved.
|
|
|
4 |
|
|
|
5 |
Licensed under the Academic Free License version 2.1 or above OR the
|
|
|
6 |
modified BSD license. For more information on Dojo licensing, see:
|
|
|
7 |
|
|
|
8 |
http://dojotoolkit.org/community/licensing.shtml
|
|
|
9 |
*/
|
|
|
10 |
|
1422 |
alexandre_ |
11 |
|
|
|
12 |
|
1318 |
alexandre_ |
13 |
dojo.provide("dojo.widget.FilteringTable");
|
|
|
14 |
dojo.require("dojo.date.format");
|
|
|
15 |
dojo.require("dojo.math");
|
|
|
16 |
dojo.require("dojo.collections.Store");
|
|
|
17 |
dojo.require("dojo.html.*");
|
|
|
18 |
dojo.require("dojo.html.util");
|
|
|
19 |
dojo.require("dojo.html.style");
|
|
|
20 |
dojo.require("dojo.html.selection");
|
|
|
21 |
dojo.require("dojo.event.*");
|
|
|
22 |
dojo.require("dojo.widget.*");
|
|
|
23 |
dojo.require("dojo.widget.HtmlWidget");
|
|
|
24 |
dojo.widget.defineWidget("dojo.widget.FilteringTable", dojo.widget.HtmlWidget, function () {
|
|
|
25 |
this.store = new dojo.collections.Store();
|
|
|
26 |
this.valueField = "Id";
|
|
|
27 |
this.multiple = false;
|
|
|
28 |
this.maxSelect = 0;
|
|
|
29 |
this.maxSortable = 1;
|
|
|
30 |
this.minRows = 0;
|
|
|
31 |
this.defaultDateFormat = "%D";
|
|
|
32 |
this.isInitialized = false;
|
|
|
33 |
this.alternateRows = false;
|
|
|
34 |
this.columns = [];
|
|
|
35 |
this.sortInformation = [{index:0, direction:0}];
|
|
|
36 |
this.headClass = "";
|
|
|
37 |
this.tbodyClass = "";
|
|
|
38 |
this.headerClass = "";
|
|
|
39 |
this.headerUpClass = "selectedUp";
|
|
|
40 |
this.headerDownClass = "selectedDown";
|
|
|
41 |
this.rowClass = "";
|
|
|
42 |
this.rowAlternateClass = "alt";
|
|
|
43 |
this.rowSelectedClass = "selected";
|
|
|
44 |
this.columnSelected = "sorted-column";
|
|
|
45 |
}, {isContainer:false, templatePath:null, templateCssPath:null, getTypeFromString:function (s) {
|
|
|
46 |
var parts = s.split("."), i = 0, obj = dj_global;
|
|
|
47 |
do {
|
|
|
48 |
obj = obj[parts[i++]];
|
|
|
49 |
} while (i < parts.length && obj);
|
|
|
50 |
return (obj != dj_global) ? obj : null;
|
|
|
51 |
}, getByRow:function (row) {
|
|
|
52 |
return this.store.getByKey(dojo.html.getAttribute(row, "value"));
|
|
|
53 |
}, getDataByRow:function (row) {
|
|
|
54 |
return this.store.getDataByKey(dojo.html.getAttribute(row, "value"));
|
|
|
55 |
}, getRow:function (obj) {
|
|
|
56 |
var rows = this.domNode.tBodies[0].rows;
|
|
|
57 |
for (var i = 0; i < rows.length; i++) {
|
|
|
58 |
if (this.store.getDataByKey(dojo.html.getAttribute(rows[i], "value")) == obj) {
|
|
|
59 |
return rows[i];
|
|
|
60 |
}
|
|
|
61 |
}
|
|
|
62 |
return null;
|
|
|
63 |
}, getColumnIndex:function (fieldPath) {
|
|
|
64 |
for (var i = 0; i < this.columns.length; i++) {
|
|
|
65 |
if (this.columns[i].getField() == fieldPath) {
|
|
|
66 |
return i;
|
|
|
67 |
}
|
|
|
68 |
}
|
|
|
69 |
return -1;
|
|
|
70 |
}, getSelectedData:function () {
|
|
|
71 |
var data = this.store.get();
|
|
|
72 |
var a = [];
|
|
|
73 |
for (var i = 0; i < data.length; i++) {
|
|
|
74 |
if (data[i].isSelected) {
|
|
|
75 |
a.push(data[i].src);
|
|
|
76 |
}
|
|
|
77 |
}
|
|
|
78 |
if (this.multiple) {
|
|
|
79 |
return a;
|
|
|
80 |
} else {
|
|
|
81 |
return a[0];
|
|
|
82 |
}
|
|
|
83 |
}, isSelected:function (obj) {
|
|
|
84 |
var data = this.store.get();
|
|
|
85 |
for (var i = 0; i < data.length; i++) {
|
|
|
86 |
if (data[i].src == obj) {
|
|
|
87 |
return true;
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
return false;
|
|
|
91 |
}, isValueSelected:function (val) {
|
|
|
92 |
var v = this.store.getByKey(val);
|
|
|
93 |
if (v) {
|
|
|
94 |
return v.isSelected;
|
|
|
95 |
}
|
|
|
96 |
return false;
|
|
|
97 |
}, isIndexSelected:function (idx) {
|
|
|
98 |
var v = this.store.getByIndex(idx);
|
|
|
99 |
if (v) {
|
|
|
100 |
return v.isSelected;
|
|
|
101 |
}
|
|
|
102 |
return false;
|
|
|
103 |
}, isRowSelected:function (row) {
|
|
|
104 |
var v = this.getByRow(row);
|
|
|
105 |
if (v) {
|
|
|
106 |
return v.isSelected;
|
|
|
107 |
}
|
|
|
108 |
return false;
|
|
|
109 |
}, reset:function () {
|
|
|
110 |
this.store.clearData();
|
|
|
111 |
this.columns = [];
|
|
|
112 |
this.sortInformation = [{index:0, direction:0}];
|
|
|
113 |
this.resetSelections();
|
|
|
114 |
this.isInitialized = false;
|
|
|
115 |
this.onReset();
|
|
|
116 |
}, resetSelections:function () {
|
|
|
117 |
this.store.forEach(function (element) {
|
|
|
118 |
element.isSelected = false;
|
|
|
119 |
});
|
|
|
120 |
}, onReset:function () {
|
|
|
121 |
}, select:function (obj) {
|
|
|
122 |
var data = this.store.get();
|
|
|
123 |
for (var i = 0; i < data.length; i++) {
|
|
|
124 |
if (data[i].src == obj) {
|
|
|
125 |
data[i].isSelected = true;
|
|
|
126 |
break;
|
|
|
127 |
}
|
|
|
128 |
}
|
|
|
129 |
this.onDataSelect(obj);
|
|
|
130 |
}, selectByValue:function (val) {
|
|
|
131 |
this.select(this.store.getDataByKey(val));
|
|
|
132 |
}, selectByIndex:function (idx) {
|
|
|
133 |
this.select(this.store.getDataByIndex(idx));
|
|
|
134 |
}, selectByRow:function (row) {
|
|
|
135 |
this.select(this.getDataByRow(row));
|
|
|
136 |
}, selectAll:function () {
|
|
|
137 |
this.store.forEach(function (element) {
|
|
|
138 |
element.isSelected = true;
|
|
|
139 |
});
|
|
|
140 |
}, onDataSelect:function (obj) {
|
|
|
141 |
}, toggleSelection:function (obj) {
|
|
|
142 |
var data = this.store.get();
|
|
|
143 |
for (var i = 0; i < data.length; i++) {
|
|
|
144 |
if (data[i].src == obj) {
|
|
|
145 |
data[i].isSelected = !data[i].isSelected;
|
|
|
146 |
break;
|
|
|
147 |
}
|
|
|
148 |
}
|
|
|
149 |
this.onDataToggle(obj);
|
|
|
150 |
}, toggleSelectionByValue:function (val) {
|
|
|
151 |
this.toggleSelection(this.store.getDataByKey(val));
|
|
|
152 |
}, toggleSelectionByIndex:function (idx) {
|
|
|
153 |
this.toggleSelection(this.store.getDataByIndex(idx));
|
|
|
154 |
}, toggleSelectionByRow:function (row) {
|
|
|
155 |
this.toggleSelection(this.getDataByRow(row));
|
|
|
156 |
}, toggleAll:function () {
|
|
|
157 |
this.store.forEach(function (element) {
|
|
|
158 |
element.isSelected = !element.isSelected;
|
|
|
159 |
});
|
|
|
160 |
}, onDataToggle:function (obj) {
|
|
|
161 |
}, _meta:{field:null, format:null, filterer:null, noSort:false, sortType:"String", dataType:String, sortFunction:null, filterFunction:null, label:null, align:"left", valign:"middle", getField:function () {
|
|
|
162 |
return this.field || this.label;
|
|
|
163 |
}, getType:function () {
|
|
|
164 |
return this.dataType;
|
|
|
165 |
}}, createMetaData:function (obj) {
|
|
|
166 |
for (var p in this._meta) {
|
|
|
167 |
if (!obj[p]) {
|
|
|
168 |
obj[p] = this._meta[p];
|
|
|
169 |
}
|
|
|
170 |
}
|
|
|
171 |
if (!obj.label) {
|
|
|
172 |
obj.label = obj.field;
|
|
|
173 |
}
|
|
|
174 |
if (!obj.filterFunction) {
|
|
|
175 |
obj.filterFunction = this._defaultFilter;
|
|
|
176 |
}
|
|
|
177 |
return obj;
|
|
|
178 |
}, parseMetadata:function (head) {
|
|
|
179 |
this.columns = [];
|
|
|
180 |
this.sortInformation = [];
|
|
|
181 |
var row = head.getElementsByTagName("tr")[0];
|
|
|
182 |
var cells = row.getElementsByTagName("td");
|
|
|
183 |
if (cells.length == 0) {
|
|
|
184 |
cells = row.getElementsByTagName("th");
|
|
|
185 |
}
|
|
|
186 |
for (var i = 0; i < cells.length; i++) {
|
|
|
187 |
var o = this.createMetaData({});
|
|
|
188 |
if (dojo.html.hasAttribute(cells[i], "align")) {
|
|
|
189 |
o.align = dojo.html.getAttribute(cells[i], "align");
|
|
|
190 |
}
|
|
|
191 |
if (dojo.html.hasAttribute(cells[i], "valign")) {
|
|
|
192 |
o.valign = dojo.html.getAttribute(cells[i], "valign");
|
|
|
193 |
}
|
|
|
194 |
if (dojo.html.hasAttribute(cells[i], "nosort")) {
|
|
|
195 |
o.noSort = (dojo.html.getAttribute(cells[i], "nosort") == "true");
|
|
|
196 |
}
|
|
|
197 |
if (dojo.html.hasAttribute(cells[i], "sortusing")) {
|
|
|
198 |
var trans = dojo.html.getAttribute(cells[i], "sortusing");
|
|
|
199 |
var f = this.getTypeFromString(trans);
|
|
|
200 |
if (f != null && f != window && typeof (f) == "function") {
|
|
|
201 |
o.sortFunction = f;
|
|
|
202 |
}
|
|
|
203 |
}
|
|
|
204 |
o.label = dojo.html.renderedTextContent(cells[i]);
|
|
|
205 |
if (dojo.html.hasAttribute(cells[i], "field")) {
|
|
|
206 |
o.field = dojo.html.getAttribute(cells[i], "field");
|
|
|
207 |
} else {
|
|
|
208 |
if (o.label.length > 0) {
|
|
|
209 |
o.field = o.label;
|
|
|
210 |
} else {
|
|
|
211 |
o.field = "field" + i;
|
|
|
212 |
}
|
|
|
213 |
}
|
|
|
214 |
if (dojo.html.hasAttribute(cells[i], "format")) {
|
|
|
215 |
o.format = dojo.html.getAttribute(cells[i], "format");
|
|
|
216 |
}
|
|
|
217 |
if (dojo.html.hasAttribute(cells[i], "dataType")) {
|
|
|
218 |
var sortType = dojo.html.getAttribute(cells[i], "dataType");
|
|
|
219 |
if (sortType.toLowerCase() == "html" || sortType.toLowerCase() == "markup") {
|
|
|
220 |
o.sortType = "__markup__";
|
|
|
221 |
} else {
|
|
|
222 |
var type = this.getTypeFromString(sortType);
|
|
|
223 |
if (type) {
|
|
|
224 |
o.sortType = sortType;
|
|
|
225 |
o.dataType = type;
|
|
|
226 |
}
|
|
|
227 |
}
|
|
|
228 |
}
|
|
|
229 |
if (dojo.html.hasAttribute(cells[i], "filterusing")) {
|
|
|
230 |
var trans = dojo.html.getAttribute(cells[i], "filterusing");
|
|
|
231 |
var f = this.getTypeFromString(trans);
|
|
|
232 |
if (f != null && f != window && typeof (f) == "function") {
|
|
|
233 |
o.filterFunction = f;
|
|
|
234 |
}
|
|
|
235 |
}
|
|
|
236 |
this.columns.push(o);
|
|
|
237 |
if (dojo.html.hasAttribute(cells[i], "sort")) {
|
|
|
238 |
var info = {index:i, direction:0};
|
|
|
239 |
var dir = dojo.html.getAttribute(cells[i], "sort");
|
|
|
240 |
if (!isNaN(parseInt(dir))) {
|
|
|
241 |
dir = parseInt(dir);
|
|
|
242 |
info.direction = (dir != 0) ? 1 : 0;
|
|
|
243 |
} else {
|
|
|
244 |
info.direction = (dir.toLowerCase() == "desc") ? 1 : 0;
|
|
|
245 |
}
|
|
|
246 |
this.sortInformation.push(info);
|
|
|
247 |
}
|
|
|
248 |
}
|
|
|
249 |
if (this.sortInformation.length == 0) {
|
|
|
250 |
this.sortInformation.push({index:0, direction:0});
|
|
|
251 |
} else {
|
|
|
252 |
if (this.sortInformation.length > this.maxSortable) {
|
|
|
253 |
this.sortInformation.length = this.maxSortable;
|
|
|
254 |
}
|
|
|
255 |
}
|
|
|
256 |
}, parseData:function (body) {
|
|
|
257 |
if (body.rows.length == 0 && this.columns.length == 0) {
|
|
|
258 |
return;
|
|
|
259 |
}
|
|
|
260 |
var self = this;
|
|
|
261 |
this["__selected__"] = [];
|
|
|
262 |
var arr = this.store.getFromHtml(this.columns, body, function (obj, row) {
|
|
|
263 |
if (typeof (obj[self.valueField]) == "undefined" || obj[self.valueField] == null) {
|
|
|
264 |
obj[self.valueField] = dojo.html.getAttribute(row, "value");
|
|
|
265 |
}
|
|
|
266 |
if (dojo.html.getAttribute(row, "selected") == "true") {
|
|
|
267 |
self["__selected__"].push(obj);
|
|
|
268 |
}
|
|
|
269 |
});
|
|
|
270 |
this.store.setData(arr, true);
|
|
|
271 |
this.render();
|
|
|
272 |
for (var i = 0; i < this["__selected__"].length; i++) {
|
|
|
273 |
this.select(this["__selected__"][i]);
|
|
|
274 |
}
|
|
|
275 |
this.renderSelections();
|
|
|
276 |
delete this["__selected__"];
|
|
|
277 |
this.isInitialized = true;
|
|
|
278 |
}, onSelect:function (e) {
|
|
|
279 |
var row = dojo.html.getParentByType(e.target, "tr");
|
|
|
280 |
if (dojo.html.hasAttribute(row, "emptyRow")) {
|
|
|
281 |
return;
|
|
|
282 |
}
|
|
|
283 |
var body = dojo.html.getParentByType(row, "tbody");
|
|
|
284 |
if (this.multiple) {
|
|
|
285 |
if (e.shiftKey) {
|
|
|
286 |
var startRow;
|
|
|
287 |
var rows = body.rows;
|
|
|
288 |
for (var i = 0; i < rows.length; i++) {
|
|
|
289 |
if (rows[i] == row) {
|
|
|
290 |
break;
|
|
|
291 |
}
|
|
|
292 |
if (this.isRowSelected(rows[i])) {
|
|
|
293 |
startRow = rows[i];
|
|
|
294 |
}
|
|
|
295 |
}
|
|
|
296 |
if (!startRow) {
|
|
|
297 |
startRow = row;
|
|
|
298 |
for (; i < rows.length; i++) {
|
|
|
299 |
if (this.isRowSelected(rows[i])) {
|
|
|
300 |
row = rows[i];
|
|
|
301 |
break;
|
|
|
302 |
}
|
|
|
303 |
}
|
|
|
304 |
}
|
|
|
305 |
this.resetSelections();
|
|
|
306 |
if (startRow == row) {
|
|
|
307 |
this.toggleSelectionByRow(row);
|
|
|
308 |
} else {
|
|
|
309 |
var doSelect = false;
|
|
|
310 |
for (var i = 0; i < rows.length; i++) {
|
|
|
311 |
if (rows[i] == startRow) {
|
|
|
312 |
doSelect = true;
|
|
|
313 |
}
|
|
|
314 |
if (doSelect) {
|
|
|
315 |
this.selectByRow(rows[i]);
|
|
|
316 |
}
|
|
|
317 |
if (rows[i] == row) {
|
|
|
318 |
doSelect = false;
|
|
|
319 |
}
|
|
|
320 |
}
|
|
|
321 |
}
|
|
|
322 |
} else {
|
|
|
323 |
this.toggleSelectionByRow(row);
|
|
|
324 |
}
|
|
|
325 |
} else {
|
|
|
326 |
this.resetSelections();
|
|
|
327 |
this.toggleSelectionByRow(row);
|
|
|
328 |
}
|
|
|
329 |
this.renderSelections();
|
|
|
330 |
}, onSort:function (e) {
|
|
|
331 |
var oldIndex = this.sortIndex;
|
|
|
332 |
var oldDirection = this.sortDirection;
|
|
|
333 |
var source = e.target;
|
|
|
334 |
var row = dojo.html.getParentByType(source, "tr");
|
|
|
335 |
var cellTag = "td";
|
|
|
336 |
if (row.getElementsByTagName(cellTag).length == 0) {
|
|
|
337 |
cellTag = "th";
|
|
|
338 |
}
|
|
|
339 |
var headers = row.getElementsByTagName(cellTag);
|
|
|
340 |
var header = dojo.html.getParentByType(source, cellTag);
|
|
|
341 |
for (var i = 0; i < headers.length; i++) {
|
|
|
342 |
dojo.html.setClass(headers[i], this.headerClass);
|
|
|
343 |
if (headers[i] == header) {
|
|
|
344 |
if (this.sortInformation[0].index != i) {
|
|
|
345 |
this.sortInformation.unshift({index:i, direction:0});
|
|
|
346 |
} else {
|
|
|
347 |
this.sortInformation[0] = {index:i, direction:(~this.sortInformation[0].direction) & 1};
|
|
|
348 |
}
|
|
|
349 |
}
|
|
|
350 |
}
|
|
|
351 |
this.sortInformation.length = Math.min(this.sortInformation.length, this.maxSortable);
|
|
|
352 |
for (var i = 0; i < this.sortInformation.length; i++) {
|
|
|
353 |
var idx = this.sortInformation[i].index;
|
|
|
354 |
var dir = (~this.sortInformation[i].direction) & 1;
|
|
|
355 |
dojo.html.setClass(headers[idx], dir == 0 ? this.headerDownClass : this.headerUpClass);
|
|
|
356 |
}
|
|
|
357 |
this.render();
|
|
|
358 |
}, onFilter:function () {
|
|
|
359 |
}, _defaultFilter:function (obj) {
|
|
|
360 |
return true;
|
|
|
361 |
}, setFilter:function (field, fn) {
|
|
|
362 |
for (var i = 0; i < this.columns.length; i++) {
|
|
|
363 |
if (this.columns[i].getField() == field) {
|
|
|
364 |
this.columns[i].filterFunction = fn;
|
|
|
365 |
break;
|
|
|
366 |
}
|
|
|
367 |
}
|
|
|
368 |
this.applyFilters();
|
|
|
369 |
}, setFilterByIndex:function (idx, fn) {
|
|
|
370 |
this.columns[idx].filterFunction = fn;
|
|
|
371 |
this.applyFilters();
|
|
|
372 |
}, clearFilter:function (field) {
|
|
|
373 |
for (var i = 0; i < this.columns.length; i++) {
|
|
|
374 |
if (this.columns[i].getField() == field) {
|
|
|
375 |
this.columns[i].filterFunction = this._defaultFilter;
|
|
|
376 |
break;
|
|
|
377 |
}
|
|
|
378 |
}
|
|
|
379 |
this.applyFilters();
|
|
|
380 |
}, clearFilterByIndex:function (idx) {
|
|
|
381 |
this.columns[idx].filterFunction = this._defaultFilter;
|
|
|
382 |
this.applyFilters();
|
|
|
383 |
}, clearFilters:function () {
|
|
|
384 |
for (var i = 0; i < this.columns.length; i++) {
|
|
|
385 |
this.columns[i].filterFunction = this._defaultFilter;
|
|
|
386 |
}
|
|
|
387 |
var rows = this.domNode.tBodies[0].rows;
|
|
|
388 |
for (var i = 0; i < rows.length; i++) {
|
|
|
389 |
rows[i].style.display = "";
|
|
|
390 |
if (this.alternateRows) {
|
|
|
391 |
dojo.html[((i % 2 == 1) ? "addClass" : "removeClass")](rows[i], this.rowAlternateClass);
|
|
|
392 |
}
|
|
|
393 |
}
|
|
|
394 |
this.onFilter();
|
|
|
395 |
}, applyFilters:function () {
|
|
|
396 |
var alt = 0;
|
|
|
397 |
var rows = this.domNode.tBodies[0].rows;
|
|
|
398 |
for (var i = 0; i < rows.length; i++) {
|
|
|
399 |
var b = true;
|
|
|
400 |
var row = rows[i];
|
|
|
401 |
for (var j = 0; j < this.columns.length; j++) {
|
|
|
402 |
var value = this.store.getField(this.getDataByRow(row), this.columns[j].getField());
|
|
|
403 |
if (this.columns[j].getType() == Date && value != null && !value.getYear) {
|
|
|
404 |
value = new Date(value);
|
|
|
405 |
}
|
|
|
406 |
if (!this.columns[j].filterFunction(value)) {
|
|
|
407 |
b = false;
|
|
|
408 |
break;
|
|
|
409 |
}
|
|
|
410 |
}
|
|
|
411 |
row.style.display = (b ? "" : "none");
|
|
|
412 |
if (b && this.alternateRows) {
|
|
|
413 |
dojo.html[((alt++ % 2 == 1) ? "addClass" : "removeClass")](row, this.rowAlternateClass);
|
|
|
414 |
}
|
|
|
415 |
}
|
|
|
416 |
this.onFilter();
|
|
|
417 |
}, createSorter:function (info) {
|
|
|
418 |
var self = this;
|
|
|
419 |
var sortFunctions = [];
|
|
|
420 |
function createSortFunction(fieldIndex, dir) {
|
|
|
421 |
var meta = self.columns[fieldIndex];
|
|
|
422 |
var field = meta.getField();
|
|
|
423 |
return function (rowA, rowB) {
|
|
|
424 |
if (dojo.html.hasAttribute(rowA, "emptyRow")) {
|
|
|
425 |
return 1;
|
|
|
426 |
}
|
|
|
427 |
if (dojo.html.hasAttribute(rowB, "emptyRow")) {
|
|
|
428 |
return -1;
|
|
|
429 |
}
|
|
|
430 |
var a = self.store.getField(self.getDataByRow(rowA), field);
|
|
|
431 |
var b = self.store.getField(self.getDataByRow(rowB), field);
|
|
|
432 |
var ret = 0;
|
|
|
433 |
if (a > b) {
|
|
|
434 |
ret = 1;
|
|
|
435 |
}
|
|
|
436 |
if (a < b) {
|
|
|
437 |
ret = -1;
|
|
|
438 |
}
|
|
|
439 |
return dir * ret;
|
|
|
440 |
};
|
|
|
441 |
}
|
|
|
442 |
var current = 0;
|
|
|
443 |
var max = Math.min(info.length, this.maxSortable, this.columns.length);
|
|
|
444 |
while (current < max) {
|
|
|
445 |
var direction = (info[current].direction == 0) ? 1 : -1;
|
|
|
446 |
sortFunctions.push(createSortFunction(info[current].index, direction));
|
|
|
447 |
current++;
|
|
|
448 |
}
|
|
|
449 |
return function (rowA, rowB) {
|
|
|
450 |
var idx = 0;
|
|
|
451 |
while (idx < sortFunctions.length) {
|
|
|
452 |
var ret = sortFunctions[idx++](rowA, rowB);
|
|
|
453 |
if (ret != 0) {
|
|
|
454 |
return ret;
|
|
|
455 |
}
|
|
|
456 |
}
|
|
|
457 |
return 0;
|
|
|
458 |
};
|
|
|
459 |
}, createRow:function (obj) {
|
|
|
460 |
var row = document.createElement("tr");
|
|
|
461 |
dojo.html.disableSelection(row);
|
|
|
462 |
if (obj.key != null) {
|
|
|
463 |
row.setAttribute("value", obj.key);
|
|
|
464 |
}
|
|
|
465 |
for (var j = 0; j < this.columns.length; j++) {
|
|
|
466 |
var cell = document.createElement("td");
|
|
|
467 |
cell.setAttribute("align", this.columns[j].align);
|
|
|
468 |
cell.setAttribute("valign", this.columns[j].valign);
|
|
|
469 |
dojo.html.disableSelection(cell);
|
|
|
470 |
var val = this.store.getField(obj.src, this.columns[j].getField());
|
|
|
471 |
if (typeof (val) == "undefined") {
|
|
|
472 |
val = "";
|
|
|
473 |
}
|
|
|
474 |
this.fillCell(cell, this.columns[j], val);
|
|
|
475 |
row.appendChild(cell);
|
|
|
476 |
}
|
|
|
477 |
return row;
|
|
|
478 |
}, fillCell:function (cell, meta, val) {
|
|
|
479 |
if (meta.sortType == "__markup__") {
|
|
|
480 |
cell.innerHTML = val;
|
|
|
481 |
} else {
|
|
|
482 |
if (meta.getType() == Date) {
|
|
|
483 |
val = new Date(val);
|
|
|
484 |
if (!isNaN(val)) {
|
|
|
485 |
var format = this.defaultDateFormat;
|
|
|
486 |
if (meta.format) {
|
|
|
487 |
format = meta.format;
|
|
|
488 |
}
|
|
|
489 |
cell.innerHTML = dojo.date.strftime(val, format);
|
|
|
490 |
} else {
|
|
|
491 |
cell.innerHTML = val;
|
|
|
492 |
}
|
|
|
493 |
} else {
|
|
|
494 |
if ("Number number int Integer float Float".indexOf(meta.getType()) > -1) {
|
|
|
495 |
if (val.length == 0) {
|
|
|
496 |
val = "0";
|
|
|
497 |
}
|
|
|
498 |
var n = parseFloat(val, 10) + "";
|
|
|
499 |
if (n.indexOf(".") > -1) {
|
|
|
500 |
n = dojo.math.round(parseFloat(val, 10), 2);
|
|
|
501 |
}
|
|
|
502 |
cell.innerHTML = n;
|
|
|
503 |
} else {
|
|
|
504 |
cell.innerHTML = val;
|
|
|
505 |
}
|
|
|
506 |
}
|
|
|
507 |
}
|
|
|
508 |
}, prefill:function () {
|
|
|
509 |
this.isInitialized = false;
|
|
|
510 |
var body = this.domNode.tBodies[0];
|
|
|
511 |
while (body.childNodes.length > 0) {
|
|
|
512 |
body.removeChild(body.childNodes[0]);
|
|
|
513 |
}
|
|
|
514 |
if (this.minRows > 0) {
|
|
|
515 |
for (var i = 0; i < this.minRows; i++) {
|
|
|
516 |
var row = document.createElement("tr");
|
|
|
517 |
if (this.alternateRows) {
|
|
|
518 |
dojo.html[((i % 2 == 1) ? "addClass" : "removeClass")](row, this.rowAlternateClass);
|
|
|
519 |
}
|
|
|
520 |
row.setAttribute("emptyRow", "true");
|
|
|
521 |
for (var j = 0; j < this.columns.length; j++) {
|
|
|
522 |
var cell = document.createElement("td");
|
|
|
523 |
cell.innerHTML = " ";
|
|
|
524 |
row.appendChild(cell);
|
|
|
525 |
}
|
|
|
526 |
body.appendChild(row);
|
|
|
527 |
}
|
|
|
528 |
}
|
|
|
529 |
}, init:function () {
|
|
|
530 |
this.isInitialized = false;
|
|
|
531 |
var head = this.domNode.getElementsByTagName("thead")[0];
|
|
|
532 |
if (head.getElementsByTagName("tr").length == 0) {
|
|
|
533 |
var row = document.createElement("tr");
|
|
|
534 |
for (var i = 0; i < this.columns.length; i++) {
|
|
|
535 |
var cell = document.createElement("td");
|
|
|
536 |
cell.setAttribute("align", this.columns[i].align);
|
|
|
537 |
cell.setAttribute("valign", this.columns[i].valign);
|
|
|
538 |
dojo.html.disableSelection(cell);
|
|
|
539 |
cell.innerHTML = this.columns[i].label;
|
|
|
540 |
row.appendChild(cell);
|
|
|
541 |
if (!this.columns[i].noSort) {
|
|
|
542 |
dojo.event.connect(cell, "onclick", this, "onSort");
|
|
|
543 |
}
|
|
|
544 |
}
|
|
|
545 |
dojo.html.prependChild(row, head);
|
|
|
546 |
}
|
|
|
547 |
if (this.store.get().length == 0) {
|
|
|
548 |
return false;
|
|
|
549 |
}
|
|
|
550 |
var idx = this.domNode.tBodies[0].rows.length;
|
|
|
551 |
if (!idx || idx == 0 || this.domNode.tBodies[0].rows[0].getAttribute("emptyRow") == "true") {
|
|
|
552 |
idx = 0;
|
|
|
553 |
var body = this.domNode.tBodies[0];
|
|
|
554 |
while (body.childNodes.length > 0) {
|
|
|
555 |
body.removeChild(body.childNodes[0]);
|
|
|
556 |
}
|
|
|
557 |
var data = this.store.get();
|
|
|
558 |
for (var i = 0; i < data.length; i++) {
|
|
|
559 |
var row = this.createRow(data[i]);
|
|
|
560 |
body.appendChild(row);
|
|
|
561 |
idx++;
|
|
|
562 |
}
|
|
|
563 |
}
|
|
|
564 |
if (this.minRows > 0 && idx < this.minRows) {
|
|
|
565 |
idx = this.minRows - idx;
|
|
|
566 |
for (var i = 0; i < idx; i++) {
|
|
|
567 |
row = document.createElement("tr");
|
|
|
568 |
row.setAttribute("emptyRow", "true");
|
|
|
569 |
for (var j = 0; j < this.columns.length; j++) {
|
|
|
570 |
cell = document.createElement("td");
|
|
|
571 |
cell.innerHTML = " ";
|
|
|
572 |
row.appendChild(cell);
|
|
|
573 |
}
|
|
|
574 |
body.appendChild(row);
|
|
|
575 |
}
|
|
|
576 |
}
|
|
|
577 |
var row = this.domNode.getElementsByTagName("thead")[0].rows[0];
|
|
|
578 |
var cellTag = "td";
|
|
|
579 |
if (row.getElementsByTagName(cellTag).length == 0) {
|
|
|
580 |
cellTag = "th";
|
|
|
581 |
}
|
|
|
582 |
var headers = row.getElementsByTagName(cellTag);
|
|
|
583 |
for (var i = 0; i < headers.length; i++) {
|
|
|
584 |
dojo.html.setClass(headers[i], this.headerClass);
|
|
|
585 |
}
|
|
|
586 |
for (var i = 0; i < this.sortInformation.length; i++) {
|
|
|
587 |
var idx = this.sortInformation[i].index;
|
|
|
588 |
var dir = (~this.sortInformation[i].direction) & 1;
|
|
|
589 |
dojo.html.setClass(headers[idx], dir == 0 ? this.headerDownClass : this.headerUpClass);
|
|
|
590 |
}
|
|
|
591 |
this.isInitialized = true;
|
|
|
592 |
return this.isInitialized;
|
|
|
593 |
}, render:function () {
|
|
|
594 |
if (!this.isInitialized) {
|
|
|
595 |
var b = this.init();
|
|
|
596 |
if (!b) {
|
|
|
597 |
this.prefill();
|
|
|
598 |
return;
|
|
|
599 |
}
|
|
|
600 |
}
|
|
|
601 |
var rows = [];
|
|
|
602 |
var body = this.domNode.tBodies[0];
|
|
|
603 |
var emptyRowIdx = -1;
|
|
|
604 |
for (var i = 0; i < body.rows.length; i++) {
|
|
|
605 |
rows.push(body.rows[i]);
|
|
|
606 |
}
|
|
|
607 |
var sortFunction = this.createSorter(this.sortInformation);
|
|
|
608 |
if (sortFunction) {
|
|
|
609 |
rows.sort(sortFunction);
|
|
|
610 |
}
|
|
|
611 |
for (var i = 0; i < rows.length; i++) {
|
|
|
612 |
if (this.alternateRows) {
|
|
|
613 |
dojo.html[((i % 2 == 1) ? "addClass" : "removeClass")](rows[i], this.rowAlternateClass);
|
|
|
614 |
}
|
|
|
615 |
dojo.html[(this.isRowSelected(body.rows[i]) ? "addClass" : "removeClass")](body.rows[i], this.rowSelectedClass);
|
|
|
616 |
body.appendChild(rows[i]);
|
|
|
617 |
}
|
|
|
618 |
}, renderSelections:function () {
|
|
|
619 |
var body = this.domNode.tBodies[0];
|
|
|
620 |
for (var i = 0; i < body.rows.length; i++) {
|
|
|
621 |
dojo.html[(this.isRowSelected(body.rows[i]) ? "addClass" : "removeClass")](body.rows[i], this.rowSelectedClass);
|
|
|
622 |
}
|
|
|
623 |
}, initialize:function () {
|
|
|
624 |
var self = this;
|
|
|
625 |
dojo.event.connect(this.store, "onSetData", function () {
|
|
|
626 |
self.store.forEach(function (element) {
|
|
|
627 |
element.isSelected = false;
|
|
|
628 |
});
|
|
|
629 |
self.isInitialized = false;
|
|
|
630 |
var body = self.domNode.tBodies[0];
|
|
|
631 |
if (body) {
|
|
|
632 |
while (body.childNodes.length > 0) {
|
|
|
633 |
body.removeChild(body.childNodes[0]);
|
|
|
634 |
}
|
|
|
635 |
}
|
|
|
636 |
self.render();
|
|
|
637 |
});
|
|
|
638 |
dojo.event.connect(this.store, "onClearData", function () {
|
|
|
639 |
self.isInitialized = false;
|
|
|
640 |
self.render();
|
|
|
641 |
});
|
|
|
642 |
dojo.event.connect(this.store, "onAddData", function (addedObject) {
|
|
|
643 |
var row = self.createRow(addedObject);
|
|
|
644 |
self.domNode.tBodies[0].appendChild(row);
|
|
|
645 |
self.render();
|
|
|
646 |
});
|
|
|
647 |
dojo.event.connect(this.store, "onAddDataRange", function (arr) {
|
|
|
648 |
for (var i = 0; i < arr.length; i++) {
|
|
|
649 |
arr[i].isSelected = false;
|
|
|
650 |
var row = self.createRow(arr[i]);
|
|
|
651 |
self.domNode.tBodies[0].appendChild(row);
|
|
|
652 |
}
|
|
|
653 |
self.render();
|
|
|
654 |
});
|
|
|
655 |
dojo.event.connect(this.store, "onRemoveData", function (removedObject) {
|
|
|
656 |
var rows = self.domNode.tBodies[0].rows;
|
|
|
657 |
for (var i = 0; i < rows.length; i++) {
|
|
|
658 |
if (self.getDataByRow(rows[i]) == removedObject.src) {
|
|
|
659 |
rows[i].parentNode.removeChild(rows[i]);
|
|
|
660 |
break;
|
|
|
661 |
}
|
|
|
662 |
}
|
|
|
663 |
self.render();
|
|
|
664 |
});
|
|
|
665 |
dojo.event.connect(this.store, "onUpdateField", function (obj, fieldPath, val) {
|
|
|
666 |
var row = self.getRow(obj);
|
|
|
667 |
var idx = self.getColumnIndex(fieldPath);
|
|
|
668 |
if (row && row.cells[idx] && self.columns[idx]) {
|
|
|
669 |
self.fillCell(row.cells[idx], self.columns[idx], val);
|
|
|
670 |
}
|
|
|
671 |
});
|
|
|
672 |
}, postCreate:function () {
|
|
|
673 |
this.store.keyField = this.valueField;
|
|
|
674 |
if (this.domNode) {
|
|
|
675 |
if (this.domNode.nodeName.toLowerCase() != "table") {
|
|
|
676 |
}
|
|
|
677 |
if (this.domNode.getElementsByTagName("thead")[0]) {
|
|
|
678 |
var head = this.domNode.getElementsByTagName("thead")[0];
|
|
|
679 |
if (this.headClass.length > 0) {
|
|
|
680 |
head.className = this.headClass;
|
|
|
681 |
}
|
|
|
682 |
dojo.html.disableSelection(this.domNode);
|
|
|
683 |
this.parseMetadata(head);
|
|
|
684 |
var header = "td";
|
|
|
685 |
if (head.getElementsByTagName(header).length == 0) {
|
|
|
686 |
header = "th";
|
|
|
687 |
}
|
|
|
688 |
var headers = head.getElementsByTagName(header);
|
|
|
689 |
for (var i = 0; i < headers.length; i++) {
|
|
|
690 |
if (!this.columns[i].noSort) {
|
|
|
691 |
dojo.event.connect(headers[i], "onclick", this, "onSort");
|
|
|
692 |
}
|
|
|
693 |
}
|
|
|
694 |
} else {
|
|
|
695 |
this.domNode.appendChild(document.createElement("thead"));
|
|
|
696 |
}
|
|
|
697 |
if (this.domNode.tBodies.length < 1) {
|
|
|
698 |
var body = document.createElement("tbody");
|
|
|
699 |
this.domNode.appendChild(body);
|
|
|
700 |
} else {
|
|
|
701 |
var body = this.domNode.tBodies[0];
|
|
|
702 |
}
|
|
|
703 |
if (this.tbodyClass.length > 0) {
|
|
|
704 |
body.className = this.tbodyClass;
|
|
|
705 |
}
|
|
|
706 |
dojo.event.connect(body, "onclick", this, "onSelect");
|
|
|
707 |
this.parseData(body);
|
|
|
708 |
}
|
|
|
709 |
}});
|
|
|
710 |
|