This repository was archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patheditable-table.html
More file actions
363 lines (355 loc) · 10.7 KB
/
editable-table.html
File metadata and controls
363 lines (355 loc) · 10.7 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
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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-ajax/iron-ajax.html">
<link rel="import" href="../paper-input/paper-input.html">
<link rel="import" href="../iron-autogrow-textarea/iron-autogrow-textarea.html">
<link rel="import" href="editable-table-settings-menu.html">
<link rel="import" href="editable-table-rowcol-menu.html">
<link rel="import" href="editable-table-cell.html">
<link rel="import" href="tablesaw-js.html">
<!--
`editable-table`
A LRN element
@demo demo/index.html
@microcopy - the mental model for this element
-
-
-
-->
<dom-module id="editable-table">
<template>
<style>
:host {
display: block;
}
:host table caption {
text-align: left;
}
:host table caption label {
font-weight: normal;
}
:host .flex {
display: flex;
width: 100%;
margin: 0 0 10px;
padding: 0;
}
:host .flex label {
line-height: 30px;
}
:host .flex > * {
justify-content: space-between;
align-items: baseline;
}
:host .flex > .flex-grow {
flex-grow: 1;
width: auto;
}
:host .flex > .flex-padding {
padding-right: 0.5em;
}
:host .flex paper-toggle-button {
display: inline-block;
}
:host table {
width: 100%;
}
:host table,
:host table th,
:host table td {
border-collapse: collapse;
}
:host table,
:host table th,
:host table td {
border: 1px solid #ddd;
}
:host table[bordered],
:host table[bordered] th,
:host table[bordered] td {
border: 1px solid #888;
}
:host table th,
:host table td {
padding: 0;
}
:host table th {
background-color: #f0f0f0;
}
:host table[striped] tbody tr:nth-child(2n) td {
background-color: #f8f8f8;
}
</style>
<iron-ajax
id="ajax"
auto
url="[[dataSrc]]"
handle-as="json"
last-response="{{data}}"></iron-ajax>
<table id="table" class="tablesaw tablesaw-stack" data-tablesaw-mode="stack" summary="editable table" bordered$="{{bordered}}" condensed$="{{condensed}}" striped$="{{striped}}">
<caption>
<p class="flex">
<label for="caption" class="flex-padding">Table Caption: </label>
<iron-autogrow-textarea id="caption" class="flex-grow" value$="{{caption}}"></iron-autogrow-textarea>
</p>
<p class="flex">
<label for="summary" class="flex-padding">Table Summary (for accessibility): </label>
<iron-autogrow-textarea id="summary" class="flex-grow" value$="{{summary}}"></iron-autogrow-textarea>
</p>
<template is="dom-if" if="[[enableResponsive]]"></template>
</caption>
<thead>
<tr>
<th scope="col">
<template is="dom-if" if="[[enableStyles]]">
<editable-table-settings-menu
bordered$="[[bordered]]"
condensed$="[[condensed]]"
striped$="[[striped]]">
</editable-table-settings-menu>
</template>
</th>
<template id="headers" is="dom-repeat" items="{{__data.columns}}" as="header">
<th scope="col"><editable-table-rowcol-menu condensed$="{{condensed}}" index="{{header.index}}" label="{{header.letter}}" type="Column"></editable-table-rowcol-menu></th>
</template>
</tr>
</thead>
<tbody id="tbody">
<template id="rows" is="dom-repeat" items="{{__data.rows}}" as="row">
<tr>
<th scope="row"><editable-table-rowcol-menu condensed$="{{condensed}}" index="{{row.index}}" label="{{row.index}}" type="Row"></editable-table-rowcol-menu></th>
<template id="columns" is="dom-repeat" items="{{row.celldata}}" as="cell">
<td><editable-table-cell value$="{{cell}}"></editable-table-cell></td>
</template>
</tr>
</template>
</tbody>
</table>
</template>
<script>
Polymer({
is: 'editable-table',
listeners: {
'toggle-changed': '_onToggleChanged',
'cell-move': '_onCellMove'
},
properties: {
/**
* bordered?
*/
bordered: {
type: Boolean,
value: false
},
/**
* a table caption
*/
caption: {
type: String,
value: null
},
/**
* condensed height?
*/
condensed: {
type: Boolean,
value: false
},
/**
* raw data
*/
data: {
type: Array,
value: null,
notify: true
},
/**
* Source to pull the gallery items from
*/
dataSrc: {
type: String,
notify: true,
reflectToAttribute: true
},
/**
* allow striped, condensed, and bordered
*/
enableStyles: {
type: Boolean,
value: false,
reflectToAttribute: true
},
/**
* allow responsive settings
*/
enableResponsive: {
type: Boolean,
value: false,
reflectToAttribute: true
},
/**
* striped (altenrating rows)?
*/
striped: {
type: Boolean,
value: false
},
/**
* a table summary
*/
summary: {
type: String,
value: null
},
},
ready: function(){
let root = this;
root.__data = {};
root.$.ajax.addEventListener('last-response-changed', (e) => {
this.updateData(root.data);
});
root.$.table.addEventListener('modify-table', (e) => {
let row = e.detail.item.getAttribute('type') === 'Row', index = parseInt(e.detail.item.getAttribute('index')), action = e.detail.item.getAttribute('action');
if(action === 'delete') {
if (row) {
root.deleteRow(index);
} else {
root.deleteColumn(index);
}
} else if (action === 'insert-before') {
if (row) {
root.insertRow(index-1);
} else {
root.insertColumn(index);
}
} else if (action === 'insert-after') {
if (row) {
root.insertRow(index);
} else {
root.insertColumn(index+1);
}
}
});
},
attached: function(){
let root = this;
},
insertRow: function(index){
let temp = new Array();
for(let i = 0; i < this.data[0].length; i++){
temp.push('');
}
this.splice('data',index,0,temp);
this.updateData(this.data);
},
deleteRow: function(index){
if (confirm('Delete entire row?')) {
this.splice('data',index-1,1);
this.updateData(this.data);
}
},
insertColumn: function(index){
let temp = new Array();
for(let i = 0; i < this.data.length; i++){
temp.push(this.data[i]);
temp[i].splice(index,0,'');
}
this.set('data',temp);
this.updateData(this.data);
},
deleteColumn: function(index){
let temp = [];
if (confirm('Delete entire column?')) {
for(let i=0; i<this.data.length; i++){
let temp2 = this.data[i], temp3 = temp2.splice(index,1);
temp.push(temp2);
}
this.set('data',temp);
this.updateData(this.data);
}
},
getData: function(){
return JSON.stringify({
"bordered": this.enableStyles ? this.bordered : null,
"caption": this.caption,
"condensed": this.enableStyles ? this.condensed: null,
"data": this.data,
"striped": this.enableStyles ? this.striped : null,
"summary": this.summary,
});
},
updateData: function(data){
let temp = {}, cols = new Array(), convertAlpha = function(num){
let alpha = '',
numerals = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''),
j= Math.log(num)/Math.log(10),
k= Math.log(26)/Math.log(10),
i = Math.floor(j/k);
while(i >= 0) {
let multiplier = Math.pow(26,i), letter = Math.floor(num/multiplier);
num = num - multiplier * letter;
alpha += numerals[letter-1];
i--;
}
return alpha;
};
temp.rows = new Array();
temp.columns = new Array();
temp.cells = new Array();
if (data !== null && data !== undefined) {
for (var i = 0; i < data.length; i++){
temp.rows[i] = {'index': i+1, 'celldata': []};
temp.cells[i] = new Array();
for (var j = 0; j < data[i].length; j++){
temp.rows[i].celldata[j] = data[i][j];
temp.cells[i][j] = data[i][j];
}
}
}
if (data !== null && data !== undefined) {
for (var i = 0; i < data[0].length; i++){
temp.columns.push({'index': i,'letter': convertAlpha(i+1)});
}
}
this.set('__data',{});
this.set('__data',temp);
},
_onToggleChanged: function(e){
if (e.detail.id === 'bordered'){
this.bordered = e.detail.checked;
} else if (e.detail.id === 'condensed'){
this.condensed = e.detail.checked;
} if (e.detail.id === 'striped'){
this.striped = e.detail.checked;
}
},
_onCellMove: function(e){
console.log('move');
let dir = e.detail.direction, cell = e.detail.cell;
let row = cell.parentNode, body = this.$.tbody;
let x = Array.prototype.indexOf.call(row.children, cell);
let y = Array.prototype.indexOf.call(body.children, row);
if(dir ==='down'){
if(y+1 < body.children.length-1) {
body.children[y+1].children[x].children[0].focus();
}
} else if(dir ==='up'){
if(y > 0) {
body.children[y-1].children[x].children[0].focus();
}
} else if(dir ==='right'){
if(x+1 < row.children.length-1) {
row.children[x+1].children[0].focus();
} else if (y+1 < body.children.length-1) {
body.children[y+1].children[1].children[0].focus();
}
} else if(dir ==='left'){
if(x > 1) {
row.children[x-1].children[0].focus();
} else if (y > 0) {
body.children[y-2].children[body.children[y-2].children.length-2].children[0].focus();
}
}
},
});
</script>
</dom-module>