-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuper-grid.js
193 lines (162 loc) · 4.9 KB
/
super-grid.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
require([
'dojo/_base/declare',
'dstore/Memory',
'dstore/Trackable',
'dstore/Tree',
'dgrid/Grid',
'dgrid/Tree',
'dgrid/Selection',
'lib/Editor',
'dgrid/OnDemandGrid',
'dgrid/Selector',
], function (declare, Memory, Trackable, TreeStoreMixin, Grid, Tree, Selection, Editor, OnDemandGrid, Selector) {
function createStore(data) {
this.store = new(declare([Memory, Trackable, TreeStoreMixin]))({
data: data
});
}
function createGrid() {
var _this = this;
//Holding array for all the Classes we have to mixin
var mixins = [];
//Decide which mixins to push based on the Host Element's Attributes
_this.getAttribute('tree') ? mixins.push(Tree) : null;
//_this.getAttribute('selection') ? mixins.push(Selection) : null;
//These have to pushed inspite of not getting defined in the Attributes
mixins.push(OnDemandGrid);
mixins.push(Selector);
mixins.push(Editor);
mixins.push(Tree);
//Create a new instance of dgrid. For later purposes we are storing
//the instance as an DOM Fproperty of the hsot element so it can
//retrieved as Element.dgrid
_this.dgrid = new declare(mixins)({
//selectionMode: 'multiple',
columns: _this.columnStructure,
collection: _this.store
});
//Finally append the dGrid instance to the Host DOM.
_this.appendChild(_this.dgrid.domNode);
//Render the dgrid according to the value of the host DOM.
//_this.dgrid.renderArray(_this.value)
}
//This function parses the <super-grid>'s children to find all
//<super-column>s. Based on what it finds, a column object is built.
//This column object is required by dGrid for making columns.
function getColumnStructure() {
var _this = this;
var columns = [];
Array.prototype.slice.call(_this.querySelectorAll('super-column')).forEach(function(superColumn) {
var column = {
field: superColumn.getAttribute('field'),
label: superColumn.getAttribute('label') || superColumn.getAttribute('field'),
className: superColumn.getAttribute('className') || '',
sortable: superColumn.getAttribute('sortable') ? true : false,
};
var template = '';
//We have an editor tag defined within <template> </template>
if (template = superColumn.querySelector('template[data-editor]')) {
//Convert something like -> 'a <paper-input> Hello </paper-input>' to -> 'paper-input'
var patt = /<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>/
var editor = template.innerHTML.match(patt)[0].split('<')[1].split('>')[0];
column.editor = editor;
}
//If the <template> isn't inside an editor then its a case of
//rendering our dGrid cell in a custom way using Handlebars templates
else if (template = superColumn.querySelector('template')) {
column.renderCell = function(object, value, node, options) {
//A holding div to convert the contents of the <template>
//from string to DOM. Then return the DOM accoridng to
//renderCell's specification.
var div = document.createElement('div');
//Handlebars has to be defined on the doc where super-grid is running.
if (Handlebars) {
div.innerHTML = Handlebars.compile(template.innerHTML)(object);
}
//If Handlebars isn't defined, simply dump the output as is.
else {
div.innerHTML = template.innerHTML
}
return div.children[0];
}
}
column.selector = superColumn.getAttribute('selector') == 'checkbox' ? 'checkbox' : null;
if (superColumn.getAttribute('renderExpando') == 'true') {
column.renderExpando = true;
}
columns.push(column)
})
return columns;
}
Polymer({
is: "super-grid",
created: function() {
this.columnStructure = getColumnStructure.call(this);
},
attached: function() {
createStore.call(this, []);
createGrid.call(this);
},
allowSelect: function(row){
this.dgrid.allowSelect(row);
},
selectAll: function(){
this.dgrid.selectAll();
},
select: function(row ,toRow){
this.dgrid.select(row,toRow);
},
deselect: function(row,toRow){
this.dgrid.deselect(row,toRow);
},
clearSelection: function(){
this.dgrid.clearSelection();
},
isSelected: function(row){
this.dgrid.isSelected(row);
},
properties: {
tree: {
type: "boolean",
value: true
},
dgrid: {
type: "Object"
},
selection: {
type: "String",
value: false
},
selectionMode: {
type: "String"
},
allowTextSelection: {
type: "String",
},
deselectOnRefresh: {
type: "boolean",
value: true
},
allowSelectAll: {
type: "boolean",
value: false
},
value: {
type: "Array",
value: [],
observer: '_valueChanged'
}
},
_valueChanged: function(newValue, oldValue) {
if (this.dgrid) {
//this is due to a bug in dgrid refreshing
createStore.call(this, []);
this.dgrid.set('collection', this.store);
this.dgrid.refresh();
createStore.call(this, newValue);
this.dgrid.set('collection', this.store);
this.dgrid.refresh();
}
}
});
});