-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcomponent.html
121 lines (101 loc) · 3.3 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
<polymer-element name="ceci-editform" extends="ceci-element">
<template>
<link rel="stylesheet" href="component.css">
<div class="form">
</div>
<shadow></shadow>
</template>
<ceci-definition>
{
"thumbnail": "./thumbnail.png",
"name": "Edit Form",
"description": "Edits an item!",
"listeners": {
"editItem": {
"description": "Set the item to edit with the form.",
"label": "Set Edit Item"
},
"submitForm": {
"description": "Submit this form.",
"label": "Submit Form"
}
}
}
</ceci-definition>
<script>
Polymer("ceci-editform", {
storedData : {},
db: {},
datascope: {},
storedData: {},
ready: function(){
this.super();
},
editItem : function(scope){
var self = this;
var ca = document.querySelector("ceci-app");
if(!ca){
window.addEventListener("CeciDataConnectionLoaded", function(){
self.editItemFirebase(scope);
});
} else {
if(ca.firebase){
this.editItemFirebase(scope);
}
}
},
editItemFirebase : function(scope){
var self = this;
document.querySelector("ceci-app").getFirebaseObject('form-data', "", function(data){
self.db = data;
self.datascope = scope;
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"]];
}
var form = self.shadowRoot.querySelector(".form");
form.innerHTML = "";
for(key in self.storedData){
if(typeof self.storedData[key] != "object"){
var label = document.createElement("label");
label.textContent = key;
form.appendChild(label);
var input = document.createElement("input");
input.value = self.storedData[key];
input.setAttribute("storeName",key);
form.appendChild(input);
}
}
});
},
submitForm : function(){
// This resubmits the entire DB with the updated record
var self = this;
var newData = self.storedData;
var fields = self.shadowRoot.querySelectorAll(".form input");
self.storedData = self.db;
for (var field in fields) {
if (fields.hasOwnProperty(field)) {
var f = fields[field];
var name = f.getAttribute("storeName");
var value = f.value;
newData[name] = value;
}
}
// Gets a reference to the correct item
for (var i = 0; i < self.datascope.length; i++){
var scopeItem = self.datascope[i];
self.storedData = self.storedData[scopeItem["dataset"]][scopeItem["index"]];
}
// Replaces it with the new item
self.storedData = newData;
// Saves over the entire DB with the new item included
var ca = document.querySelector("ceci-app");
ca.setFirebaseObject('form-data', self.db);
var event = new CustomEvent("storage");
window.dispatchEvent(event);
}
});
</script>
</polymer-element>