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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
|
<!--
@element v-grid
-->
<link rel='import' href='vaadin-grid-import.html'>
<link rel='import' href='../../polymer/polymer.html'>
<link rel="stylesheet" href="vaadin-grid.css" shim-shadowdom>
<dom-module id="v-grid">
<template>
</template>
</dom-module>
<script>
var prototype = {
is: "v-grid",
grid: undefined,
properties: {
/**
* The columns array
*
* @property columns
* @type array
*/
columns: {
type: Array,
observer: 'columnsChanged'
},
/**
* The row editor specific fields
*
* @property editor
* @type Object
*/
editor: {
type: Object,
value: function() {
var _this = this;
return {
get enabled() {
return _this.grid.getEditor().isEnabled();
},
set enabled(enabled) {
_this.grid.getEditor().setEnabled(enabled);
if (_this.editable !== enabled) {
_this.editable = enabled;
}
},
get handler() {
return _this.grid.getEditor().getHandler();
},
set handler(handler) {
_this.grid.getEditor().setHandler(handler);
},
get saveButtonText() {
return _this.grid.getEditor().getSaveButtonText();
},
set saveButtonText(saveButtonText) {
_this.grid.getEditor().setSaveButtonText(saveButtonText);
},
get cancelButtonText() {
return _this.grid.getEditor().getSaveButtonText();
},
set cancelButtonText(cancelButtonText) {
_this.grid.getEditor().setCancelButtonText(cancelButtonText);
},
editRow: function(row) {
_this.grid.getEditor().editRow(row);
},
save: function() {
_this.grid.getEditor().save();
},
cancel: function() {
_this.grid.getEditor().cancel();
}
};
},
},
/**
* The data source object for the grid.
*
* @property data
* @type Object
*/
data: {
type: Object,
value: function() {
var _this = this;
return {
get source() {
return this._source;
},
set source(source) {
var sourceFunction = source;
if (Array.isArray(source)) {
sourceFunction = function(req) {
var array = source.slice(req.index, req.index + req.count);
req.success(array, source.length);
};
}
_this.grid.setDataSource(sourceFunction);
this._source = source;
},
/**
* Sets the sort order to use. Setting this causes the Grid to re-sort
* itself. If set to null, the sort order is cleared.
*
* @attribute sortOrder
* @type array
*/
get sortOrder() {
return _this.grid.getSortOrder();
},
set sortOrder(sortOrder) {
_this.grid.setSortOrder(sortOrder);
},
clearCache: function(estimatedNewSize) {
_this.grid.getDataSource().clearCache(estimatedNewSize);
},
};
}
},
/**
* Object for manipulating header rows
*
* @property header
* @type Object
*/
header: {
type: Object,
value: function() {
var _this = this;
return {
getCell: function(rowIndex, columnId) {
return _this.grid.getStaticSection().getHeaderCell(rowIndex, columnId);
},
addRow: function(rowIndex, cellContent) {
_this.grid.getStaticSection().addHeader(rowIndex, cellContent);
},
removeRow: function(rowIndex) {
_this.grid.getStaticSection().removeHeader(rowIndex);
},
setRowClassName: function(rowIndex, className) {
_this.grid.getStaticSection().setHeaderRowClassName(rowIndex, className);
},
/**
* Sets the default row of the header. The default row is a special header
* row providing a user interface for sorting columns.
*
* @attribute rowIndex
* @type number
*/
get defaultRow() {
return _this.grid.getStaticSection().getDefaultHeader();
},
set defaultRow(rowIndex) {
_this.grid.getStaticSection().setDefaultHeader(rowIndex);
},
get hidden() {
return _this.grid.getStaticSection().isHeaderHidden();
},
set hidden(hidden) {
_this.grid.getStaticSection().setHeaderHidden(hidden);
},
get rowCount() {
return _this.grid.getStaticSection().getHeaderRowCount();
}
};
}
},
/**
* Object for manipulating footer rows
*
* @property footer
* @type Object
*/
footer: {
type: Object,
value: function() {
var _this = this;
return {
getCell: function(rowIndex, columnId) {
return _this.grid.getStaticSection().getFooterCell(rowIndex, columnId);
},
addRow: function(rowIndex, cellContent) {
_this.grid.getStaticSection().addFooter(rowIndex, cellContent);
},
removeRow: function(rowIndex) {
_this.grid.getStaticSection().removeFooter(rowIndex);
},
setRowClassName: function(rowIndex, className) {
_this.grid.getStaticSection().setFooterRowClassName(rowIndex, className);
},
get hidden() {
return _this.grid.getStaticSection().isFooterHidden();
},
set hidden(hidden) {
_this.grid.getStaticSection().setFooterHidden(hidden);
},
get rowCount() {
return _this.grid.getStaticSection().getFooterRowCount();
}
};
}
},
selection: {
type: Object,
value: function() {
var _this = this;
return {
select: function(index) {
_this.grid.getSelectionModel().select(index);
return _this;
},
deselect: function(index) {
_this.grid.getSelectionModel().deselect(index);
return _this;
},
clear: function() {
_this.grid.getSelectionModel().clear();
return _this;
},
selectAll: function() {
_this.grid.getSelectionModel().selectAll();
return _this;
},
selected: function(mapper, from, to) {
return _this.grid.getSelectionModel().selected(mapper, from, to);
},
deselected: function(mapper, from, to) {
return _this.grid.getSelectionModel().deselected(mapper, from, to);
},
get size() {
return _this.grid.getSelectionModel().size();
},
get mode() {
return _this.grid.getSelectionMode();
},
set mode(mode) {
_this.grid.setSelectionMode(mode);
_this.serializeValueToAttribute(_this.selection.mode, "selection-mode");
}
};
}
},
},
columnsChanged: function() {
var oldcols = this.grid.getColumns();
var newcols = this.columns;
if (!oldcols._obs || newcols != oldcols) {
if (oldcols._obs) {
Polymer.ArrayObserve.unobserve(oldcols, oldcols._obs);
}
var _this = this;
newcols._obs = function() {
_this.grid.setColumns(newcols);
};
Polymer.ArrayObserve.observe(newcols, newcols._obs);
}
this.grid.setColumns(newcols);
},
attributeChanged: function(name, type, value) {
switch (name) {
case 'disabled':
this.disabled = typeof value == "string";
break;
case 'editable':
this.editable = typeof value == "string";
break;
case 'style':
this.grid.redraw();
break;
case 'selection-mode':
this.selection.mode = value;
break;
default:
this[Polymer.CaseMap.dashToCamelCase(name)] = value;
}
},
listeners: {
/**
* @event sort
*/
/**
* @event select
*/
'select': 'onSelect'
},
onSelect: function() {
this.serializeValueToAttribute(this.selection.mode, "selection-mode");
},
created: function() {
this.grid = new vaadin.GridComponent();
},
ready: function() {
var _this = this;
// Set read-only properties
["data", "editor", "header", "footer"].forEach(function(prop) {
Object.defineProperty(_this, prop, {
writable: false
});
});
setTimeout(function() {
for (var i = 0; i < _this.attributes.length; i++) {
_this.attributeChanged(_this.attributes[i].nodeName, null, _this.attributes[i].nodeValue);
}
}, 1);
this.columns = _this.grid.getColumns();
},
attached: function() {
this.grid.attached(this, Polymer.dom(this).querySelector("table"), Polymer.dom(this.root));
},
/**
* Scrolls to a certain row, using user-specified scroll destination.
* Since its asynchronous nature, this method returns a 'thenable'
* so as you can use the 'then()' method to be notified when the data
* request finished and the scroll is actually moved.
*
* @method scrollToRow
* @param {number} index - zero-based index of the row to scroll to.
* @param {string} scrollDestination - desired destination placement of scrolled-to-row.
*/
scrollToRow: function(index, scrollDestination) {
this.grid.scrollToRow(index, scrollDestination);
return this;
},
/**
* Scrolls to the beginning of the very first row.
* Since its asynchronous nature, this method returns a 'thenable'
* so as you can use the 'then()' method to be notified when the data
* request finished and the scroll is actually moved.
*
* @method scrollToStart
*/
scrollToStart: function() {
this.grid.scrollToStart();
return this;
},
/**
* Scrolls to the end of the very last row.
* Since its asynchronous nature, this method returns a 'thenable'
* so as you can use the 'then()' method to be notified when the data
* request finished and the scroll is actually moved.
*
* @method scrollToEnd
*/
scrollToEnd: function() {
this.grid.scrollToEnd();
return this;
},
/**
* Adds new column
*
* @method addColumn
* @param {Object} column
* @param {string} beforeColumn
*/
addColumn: function(column, beforeColumn) {
this.grid.addColumn(column, beforeColumn);
},
/**
* Removes column with certain id
*
* @method removeColumn
* @param {string} id
*/
removeColumn: function(id) {
this.grid.removeColumn(id);
},
/**
* Sets the style generator that is used for generating styles for rows.
*
* @attribute rowClassGenerator
* @type object
*/
get rowClassGenerator() {
return this.grid.getRowClassGenerator();
},
set rowClassGenerator(rowClassGenerator) {
this.grid.setRowClassGenerator(rowClassGenerator);
},
/**
* Sets the style generator that is used for generating styles for cells.
*
* @attribute cellClassGenerator
* @type object
*/
get cellClassGenerator() {
return this.grid.getCellClassGenerator();
},
set cellClassGenerator(cellClassGenerator) {
this.grid.setCellClassGenerator(cellClassGenerator);
},
/**
* Disables the grid functionality.
*
* @attribute disabled
* @type boolean
*/
get disabled() {
return this.grid.isDisabled();
},
set disabled(disabled) {
this.grid.setDisabled(disabled);
this.reflectPropertyToAttribute("disabled");
},
/**
* Enables the row editor feature (double click/tap or or Enter key in a content row activates the editing mode)
*
* @attribute editable
* @type boolean
*/
get editable() {
return this.editor.enabled;
},
set editable(editable) {
this.editor.enabled = editable;
this.reflectPropertyToAttribute("editable");
},
/**
* Sets the number of frozen columns in this grid. Setting the count to 0
* means that no data columns will be frozen, but the built-in selection
* checkbox column will still be frozen if it's in use. Setting the count to
* -1 will also disable the selection column.
*
* @attribute frozen-columns
* @type Number
*/
get frozenColumns() {
return this.grid.getFrozenColumns();
},
set frozenColumns(frozenColumns) {
this.grid.setFrozenColumns(frozenColumns);
this.reflectPropertyToAttribute("frozenColumns");
},
/**
* Declares the number of visible rows in the grid. Implicitly sets the height
* in the inline style, overriding any previous height. Setting style.height
* in this case should be ignored if possible.
*
* @attribute rows
* @type Number
*/
get rows() {
return this.grid.getRows();
},
set rows(rows) {
this.grid.setRows(rows);
this.reflectPropertyToAttribute("rows");
},
then: function(cb) {
return this.grid.then(cb);
}
};
function loadComponent() {
VGrid = Polymer(prototype);
// Give some time to gwt async processes to run (we need this in FF)
setTimeout(function() {
vaadin._v_grid_ready = true;
document.dispatchEvent(new Event('v-grid-ready'));
}, 5);
}
if (window.vaadin && window.vaadin.GridComponent) {
loadComponent();
} else {
document.addEventListener("v-grid-loaded", function() {
loadComponent();
});
}
</script>
|