-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgridEditor.js
167 lines (158 loc) · 5.76 KB
/
gridEditor.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
var gridEditor = {
state: null,
init: function(event) {
this.el = event.target;
},
onclick: function(event) {
if (event.target.name != "deleteItem") {return;}
var select = this.el.querySelector('[name="track"]');
if (select.selectedIndex == -1) {return;}
var option = select.options[select.selectedIndex];
var arr = option.value.split("-");
var itemType = arr[0]; // tack or cell
var itemIndex = arr[1]*1;
if (itemType == 'cell') {
config.cells.splice(itemIndex, 1);
} else if (itemType == 'col') {
config.cols.splice(itemIndex, 1);
} else if (itemType == 'row') {
config.rows.splice(itemIndex, 1);
}
// Reflect config changes in the state
app.updateState({q: window.btoa(JSON.stringify(config || {})), row: null, col: null, cell: null});
},
onconnected: function() {
window.addEventListener('statechange', this);
},
onstatechange: function(event) {
var newState = event.detail.newState;
var changes = event.detail.changed;
// Smart render
if (!this.state) {
this.state = newState;
this.render();
} else if (changes.row !== undefined || changes.col !== undefined || changes.cell !== undefined || changes.q !== undefined) {
this.state = event.detail.newState
this.render();
} else {
this.state = event.detail.newState
}
},
onchange: function(event) {
if (event.target.name == "track") { // Switch track
// detect if track or grid item was selected
var arr = event.target.value.split("-");
var type = arr[0];
var value = arr[1];
var changes = {
cell: null,
col: null,
row: null
}
changes[type] = value;
app.updateState(changes);
return;
} else if (event.target.name == "align") {
config.align = event.target.value;
} else if (event.target.name == "justify") {
config.justify = event.target.value;
} else if (event.target.name == "value") { // Update config for tracks
if (this.state.col) {
config.cols[1*this.state.col] = event.target.value;
} else if (this.state.row) {
config.rows[1*this.state.row] = event.target.value;
}
} else if (event.target.name.indexOf("cell-") != -1) { // Update config for cell
var arr = event.target.name.split("-");
var cellProperty = arr[1];
var cellIndex = 1*this.state.cell;
if (arr[0] != 'cell') {return;}
if (!config.cells[cellIndex]) {config.cells[cellIndex] = {};}
config.cells[cellIndex][cellProperty] = event.target.value;
} else if (event.target.name == "rows") {
config.rows.length = 1*event.target.value || 0;
} else if (event.target.name == "cols") {
config.cols.length = 1*event.target.value || 0;
} else if (event.target.name == "cells") {
config.cells.length = 1*event.target.value || 0;
} else {
return;
}
// Reflect config changes in the state
app.updateState({q: window.btoa(JSON.stringify(config || {}))});
},
updateField: function(name, value) {
if (!this.el) {return;}
var fieldEl = this.el.querySelector('[name="'+name+'"]')
if (!fieldEl) {return;}
fieldEl.value = value;
},
updateSelect: function(name, optionIndex, value, label) {
if (!this.el) {return;}
var fieldEl = this.el.querySelector('[name="'+name+'"]');
if (!fieldEl) {return;}
if (!fieldEl.options[optionIndex]) {fieldEl.appendChild(document.createElement('option'))}
fieldEl.options[optionIndex].setAttribute('value', value);
fieldEl.options[optionIndex].innerHTML = label;
return fieldEl;
},
updateCell: function(cellProperties) {
if (!cellProperties || typeof cellProperties != "object") {return;}
this.updateField("cell-col", cellProperties.col || "");
this.updateField("cell-text", cellProperties.text || "");
this.updateField("cell-colSpan", cellProperties.colSpan || "");
this.updateField("cell-rowSpan", cellProperties.rowSpan || "");
this.updateField("cell-row", cellProperties.row || "");
this.updateField("cell-align", cellProperties.align || "");
this.updateField("cell-justify", cellProperties.justify || "");
},
render: function() {
this.updateField('rows', config.rows.length);
this.updateField('cols', config.cols.length);
this.updateField('cells', config.cells.length);
this.updateField("align", config.align || "");
this.updateField("justify", config.justify || "");
var selectField, value, cellIndex;
// Update rows
this.el.querySelector('[name="track"]').innerHTML =""; // clear options
for(var i=0;i<config.rows.length; i++) {
selectField = this.updateSelect('track', i, "row-" + i, 'row ' + (i+1));
}
if (selectField && this.state && this.state.row >=0) {
selectField.selectedIndex = 1*this.state.row;
value = config.rows[1*this.state.row];
}
// Update cols
for(var j=0; j<config.cols.length; j++) {
selectField = this.updateSelect('track', i+j, "col-" + j, 'col ' + (j+1));
}
if (selectField && this.state && this.state.col>=0) {
selectField.selectedIndex = i+1*this.state.col;
value = config.cols[1*this.state.col];
}
// Update cells
for(var k=0; k<config.cells.length; k++) {
selectField = this.updateSelect('track', i+j+k, "cell-" + k, 'cell ' + (k+1));
}
if (selectField && this.state && this.state.cell>=0) {
cellIndex = 1*this.state.cell;
value = config.cells[cellIndex];
selectField.selectedIndex = i+j+cellIndex;
}
// Default selection
if (value === undefined ) {
value = config.rows[0] || config.cols[0] || "";
}
if (selectField) {
if (cellIndex !== undefined) { // Cell
this.el.querySelector('#cellProperties').classList.remove('d-none');
this.el.querySelector('[name="value"]').parentNode.classList.add('d-none');
this.updateCell(config.cells[cellIndex]);
} else { // Track
this.el.querySelector('#cellProperties').classList.add('d-none');
this.el.querySelector('[name="value"]').parentNode.classList.remove('d-none');
this.updateField('value', value);
}
}
}
};