-
Notifications
You must be signed in to change notification settings - Fork 4
/
component.html
400 lines (364 loc) · 14.4 KB
/
component.html
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
<polymer-element name="ceci-dataview" showdelete="true" showedit="true" attributes="showaddtoitem listschema showkey dataname datascope showedit showdelete" dataname="Items" extends="ceci-element">
<template>
<link rel="stylesheet" href="component.css">
<div id="datascope"></div>
<div id="keys-wrapper" >
<h2 on-click="{{toggleKeyConfig}}" id="configureView">Configure View</h2>
<div id="keys" show="no"></div>
</div>
<div id="items"></div>
<shadow></shadow>
<script type="text/json" id="ceci-definition">
</script>
</template>
<ceci-definition>
{
"thumbnail": "./thumbnail.png",
"description": "Display any data you have created using forms made with addform component.",
"broadcasts": {
"editItem": {
"label": "Send Item",
"description": "Broadcasts a reference to the record when the 'Edit' button is tapped."
},
"addToItem": {
"description": "Broadcasts a reference to the record when the 'Add to' button is tapped.",
"label": "Add to Item"
},
"sendItem": {
"description": "Broadcasts a reference to the record when it is tapped.",
"label": "View Item"
}
},
"listeners": {
"showItem": {
"description": "Shows the data for this record.",
"label": "Show item"
}
},
"attributes": {
"dataname": {
"description": "Will try to show this kind of item in the saved data, i.e. 'Cats'",
"label": "I show...",
"editable": "text"
},
"showdelete": {
"description": "Hides or shows the delete icon for each record.",
"label": "Show Delete Link",
"editable": "boolean"
},
"showedit": {
"description": "Show 'Edit' button",
"label": "Hides or shows the Edit button for each record.",
"editable": "boolean"
},
"showaddtoitem": {
"description": "Show 'Add To' Button",
"label": "Hides or shows the 'Add to This' button",
"editable": "boolean"
}
}
}
</ceci-definition>
<tags>data</tags>
<script>
Polymer("ceci-dataview", {
keyConfig: {},
storedData: {},
dataChangedEvent: new CustomEvent("storage"),
ready: function(){
this.super();
this.datascope = [];
this.$.configureView.textContent = this.gettext(this.localName + "/configureView") || this.$.configureView.textContent;
},
toggleKeyConfig: function(){
var keysEl = this.shadowRoot.querySelector("#keys");
if(keysEl.getAttribute("show") == "no") {
keysEl.setAttribute("show","yes");
} else {
keysEl.setAttribute("show","no");
}
},
showdeleteChanged: function(oldValue,newValue){
this.showData();
},
showaddtoitemChanged: function(oldValue,newValue){
this.showData();
},
showeditChanged: function(oldValue,newValue){
this.showData();
},
datanameChanged: function (oldValue, newValue) {
this.updateKeyUI();
this.showData();
},
showItem: function(e){
this.datascope = e;
},
datascopeChanged: function(){
// If we want to indicate the scope of the data that is shown, there is where we'd do it.
// var scopeEl = this.shadowRoot.querySelector("#datascope");
this.updateKeyUI();
this.showData();
},
deleteItem: function(id){
var self = this;
var ca = document.querySelector("ceci-app");
if(!ca){
window.addEventListener("CeciDataConnectionLoaded", function(){
self._deleteItemFirebase(id);
});
}
else {
if(ca.firebase){
this._deleteItemFirebase(id);
}
}
},
_deleteItemFirebase: function(id){
var self = this;
var ca = document.querySelector("ceci-app")
ca.getFirebaseObject('form-data', "", function(data){
self.storedData = data || {};
for (var i = 0; i < self.datascope.length; i++){
var scopeItem = self.datascope[i];
self.storedData = self.storedData[scopeItem["dataset"]][scopeItem["index"]];
}
self.storedData = self.storedData[self.dataname] || [];
self.storedData.splice(id, 1);
ca.setFirebaseObject('form-data', data);
window.dispatchEvent(self.dataChangedEvent);
});
},
updateKeyUI: function(){
var self = this;
var ca = document.querySelector("ceci-app");
if(!ca){
window.addEventListener("CeciDataConnectionLoaded", function(){
self._updateKeyUIFirebase();
})
}
else {
if(ca.firebase){
this._updateKeyUIFirebase();
}
else {
window.addEventListener("CeciDataConnectionLoaded", function(){
self._updateKeyUIFirebase();
});
}
}
},
_updateKeyUIFirebase : function(){
//This builds the UI that lets you choose how each key in the data set is displayed!
var self = this;
document.querySelector("ceci-app").getFirebaseObject('form-data',"", function(data){
// Clears out the old UI
self.shadowRoot.querySelector("#keys").textContent = "";
// Tries to grab the stored Data
var db = data || {};
self.storedData = db[self.dataname] || [];
// Tries to grab it's own keyConfig (which tells it how to render each Key field)
self.keyConfig = JSON.parse(self.listschema) || {};
// Grabs the schema from the form
var formSchema = db[self.dataname + "_formschema"] || {};
// Builds a list of all the keys from the form and data set
// To know what to display options for.
var keylist = [];
for (var i = 0; i < self.storedData.length; i++){
var item = self.storedData[i];
for (var key in item) {
if(keylist.indexOf(key) < 0) {
keylist.push(key);
}
}
}
for (var j = 0; j < formSchema.length; j++){
var item = formSchema[j];
if(keylist.indexOf(item["name"]) < 0 ){
keylist.push(item["name"]);
}
}
for (var k = 0; k < keylist.length; k++){
var key = keylist[k];
if(!self.keyConfig[key]) {
self.keyConfig[key] = "text";
}
}
// CLean up the keyConfig file
// Removes everything in the keyConfig that isn't in the keylist anymore.
for (var key in self.keyConfig){
if(keylist.indexOf(key) < 0) {
delete self.keyConfig[key];
}
}
// We should also clean up the key config.
// we should also get rid of keys that are no longer needed from keyConfig
// Like if something is NOT in the keylist, then dump it
// Have a way to choose how each key is displayed
for (var j = 0; j < keylist.length; j ++){
var keyConfig = document.createElement("div");
keyConfig.classList.add("keyConfig");
keyConfig.textContent = "Show <strong>" + keylist[j] + "</strong> as ";
var select = document.createElement("select");
keyConfig.appendChild(select);
select.setAttribute("key",keylist[j]);
select.innerHTML = "<option value='text'>text</option><option value='image'>image</option><option value='hide'>hide</option>";
select.value = self.keyConfig[keylist[j]];
select.addEventListener("change",function(e){
self.keyConfig[e.target.getAttribute("key")] = e.target.value;
self.showData();
self.listschema = JSON.stringify(self.keyConfig);
});
self.shadowRoot.querySelector("#keys").appendChild(keyConfig);
}
//Put the list schema into the db
db[self.dataname + "_listSchema"] = self.keyConfig;
document.querySelector("ceci-app").setFirebaseObject('form-data', db);
self.showData();
});
},
enteredView : function(){
var self = this;
//Listens to updates to the data set
window.addEventListener('storage', this.showData.bind(this), false);
window.addEventListener('storage', this.updateKeyUI.bind(this), false);
window.addEventListener('formChange', this.updateKeyUI.bind(this), false);
this.updateKeyUI();
//Show exising data
this.showData();
this.super();
},
getOrWaitForCeciApp: function(callback){
var ceciApp = document.querySelector("ceci-app");
if(!ceciApp){
window.addEventListener("CeciDataConnectionLoaded", function(){
callback(document.querySelector("ceci-app"));
});
} else {
if(ceciApp && ceciApp.firebase){
callback(ceciApp);
} else {
window.addEventListener("CeciDataConnectionLoaded", function(){
callback(document.querySelector("ceci-app"));
});
}
}
},
showData: function(){
var self = this;
this.getOrWaitForCeciApp(function(ceciApp){
var that = self;
ceciApp.getFirebaseObject('form-data', "", function(data){
var those = that;
those.storedData = data || {};
//handle case where dataview is just loading and listSchema hasn't been parsed and
//stored in keyConfig
if(those.keyConfig && Object.getOwnPropertyNames(those.keyConfig).length < 1){
those.keyConfig = those.storedData[those.dataname+"_listSchema"];
}
var lastData;
for (var i = 0; i < those.datascope.length; i++){
var scopeItem = those.datascope[i];
self.storedData = those.storedData[scopeItem["dataset"]][scopeItem["index"]];
lastData = scopeItem["dataset"];
}
if (lastData == those.dataname){
var blam = [];
blam.push(those.storedData);
those.storedData = blam;
}
else {
those.storedData = those.storedData[those.dataname] || [];
}
var itemList = those.shadowRoot.querySelector("#items");
itemList.innerHTML = "";
for(var i = 0; i < those.storedData.length; i++){
var item = those.storedData[i];
var itemEl = document.createElement("div");
itemEl.classList.add("dataGroup");
for (var key in item) {
var displayType = those.keyConfig[key];
var dataItem;
if (displayType == "text") {
dataItem = document.createElement("div");
if (typeof item[key] == "object"){
dataItem.textContent = "List of " + item[key].length + " " + key;
}
else {
dataItem.textContent = item[key];
}
}
else if (displayType == "image") {
dataItem = document.createElement("img");
dataItem.setAttribute("src", item[key]);
}
else {
dataItem = document.createElement("div");
}
dataItem.classList.add("dataItem");
var these = those;
dataItem.addEventListener("click",function(e){
var nodeList = Array.prototype.slice.call(e.currentTarget.parentNode.parentNode.children);
var index = nodeList.indexOf(e.currentTarget.parentNode);
var sendScope = JSON.parse(JSON.stringify(that.datascope));
var scopeItem = {};
scopeItem["dataset"] = these.dataname;
scopeItem["index"] = index;
sendScope.push(scopeItem);
these.broadcast('sendItem', sendScope);
});
if(displayType != "hide") {
itemEl.appendChild(dataItem);
}
}
if(self.showedit == "true"){
var editItem = document.createElement("div");
editItem.textContent = "Edit";
editItem.classList.add("editItem");
itemEl.appendChild(editItem);
editItem.addEventListener("click",function(e){
var nodeList = Array.prototype.slice.call(e.currentTarget.parentNode.parentNode.children);
var index = nodeList.indexOf( e.currentTarget.parentNode );
var sendScope = JSON.parse(JSON.stringify(that.datascope));
var scopeItem = {};
scopeItem["dataset"] = these.dataname;
scopeItem["index"] = index;
sendScope.push(scopeItem);
these.broadcast('editItem', sendScope);
});
}
if(these.showaddtoitem == "true"){
var el = document.createElement("div");
el.textContent = "Add to self";
el.classList.add("addToItem");
itemEl.appendChild(el);
el.addEventListener("click",function(e){
var nodeList = Array.prototype.slice.call(e.currentTarget.parentNode.parentNode.children);
var index = nodeList.indexOf( e.currentTarget.parentNode );
var sendScope = JSON.parse(JSON.stringify(that.datascope));
var scopeItem = {};
scopeItem["dataset"] = these.dataname;
scopeItem["index"] = index;
sendScope.push(scopeItem);
these.broadcast('addToItem', sendScope);
});
}
if(self.showdelete == "true"){
var deleteItem = document.createElement("div");
deleteItem.textContent = "X";
deleteItem.classList.add("deleteItem");
itemEl.appendChild(deleteItem);
deleteItem.addEventListener("click",function(e){
var nodeList = Array.prototype.slice.call(e.currentTarget.parentNode.parentNode.children);
var index = nodeList.indexOf( e.currentTarget.parentNode );
these.deleteItem(index);
});
}
itemList.appendChild(itemEl);
}
});
});
}
});
</script>
</polymer-element>