-
Notifications
You must be signed in to change notification settings - Fork 0
/
carbo-form.html
289 lines (229 loc) · 8.46 KB
/
carbo-form.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
<link rel="import" href="../polymer/polymer.html">
<dom-module id="carbo-form">
<style type="text/css">
#invalid,
#error,
#success,
#loading {
visibility: hidden;
}
#wrapper[state="invalid"] #invalid,
#wrapper[state="error"] #error,
#wrapper[state="loading"] #loading,
#wrapper[state="success"] #success {
visibility: visible;
}
#form {
padding-left: 0.29em;
padding-right: 0.29em;
}
</style>
<template>
<div id="wrapper">
<div id="form">
<content id="content-form" select="form"></content>
</div>
<div id="form-control">
<content select="carbo-form-control"></content>
</div>
<!--
Use single quotes here for the `select` attribute
so that we may use the double quotes for the css selector.
It seems to be ok to use single quotes for attribute definition.
http://stackoverflow.com/questions/2373074/single-vs-double-quotes-vs
-->
<div id="invalid">
<content id="content-invalid" select="[state='invalid']"></content>
</div>
<div id="loading">
<content id="content-loading" select="[state='loading']"></content>
</div>
<div id="error">
<content id="content-error" select="[state='error']"></content>
</div>
<div id="success">
<content id="content-success" select="[state='success']"></content>
</div>
</div>
</template>
<script type="text/javascript">
/**
* Mapping of iron-form events to their
* respective event handlers
* @type {Object}
*/
var IRON_FORM_EVENT_HANDLERS = {
'iron-form-invalid': '_handleIronFormInvalid',
'iron-form-submit': '_handleIronFormSubmit',
'iron-form-error': '_handleIronFormError',
'iron-form-response': '_handleIronFormResponse',
};
/**
* Helper function to transform arguments object into array
* @param {Arguments} listObject The listObject object within any js function
* @return {Array} Array equivalent to the listObject object
*/
function toArray(listObject) {
return Array.prototype.slice.call(listObject, 0);
}
/**
* Regular expression for matching action events and their
* correspondant actions.
*
* 'event:action'.match(actionRegExp)
* returns ['event:action', 'event', 'action']
*
* 'action'.match(actionRegExp)
* returns ['action', undefined, 'action']
*
* ':action'.match(actionRegExp)
* returns ['event:action', undefined, 'action']
*
* @type {RegExp}
*/
var actionRegExp = /(?:(.+):)?(.+)/;
Polymer({
is: 'carbo-form',
/**
* Called whenever the component is ready and the
* dom nodes are available for inspection.
*/
ready: function () {
var invalid = Polymer.dom(this.$['content-invalid']).getDistributedNodes()[0].children;
var error = Polymer.dom(this.$['content-error']).getDistributedNodes()[0].children;
var success = Polymer.dom(this.$['content-success']).getDistributedNodes()[0].children;
// Property to hold content nodes
this._contents = {
form: Polymer.dom(this.$['content-form']).getDistributedNodes()[0],
invalid: toArray(invalid),
error: toArray(error),
success: toArray(success),
};
console.log(this._contents.success)
this._listenActionableElements();
this._listenFormEvents();
},
/**
* Set up event listeners onto all elements
* that have 'action' attribute set onto them.
*
* Pipe those event listeners to the corresponding
* methods on the carbo-form element.
*
* Some of the methods are simple proxies to the `iron-form`
* itself.
*/
_listenActionableElements: function () {
// Select all nodes that are potential actionable nodes
var actionable = Polymer.dom(this).querySelectorAll('[action]');
actionable.forEach(function (el) {
var actionStr = el.getAttribute('action');
if (!actionStr) {
console.warn('No action string for ');
console.log(el);
// If no action string is defined, simply ignore the element
return;
}
var actionMatch = actionStr.match(actionRegExp);
var eventName = actionMatch[1] || 'click';
var methodName = actionMatch[2];
el.addEventListener(eventName, this._executeMethod.bind(this, methodName));
}.bind(this));
},
/**
* Sets up listeners for the iron-form event
*/
_listenFormEvents: function () {
var form = this._contents.form;
for (var evt in IRON_FORM_EVENT_HANDLERS) {
form.addEventListener(
evt,
this[IRON_FORM_EVENT_HANDLERS[evt]].bind(this)
);
}
},
_handleIronFormInvalid: function (event) {
// Set state to invalid and flush dom modifications
Polymer.dom(this).setAttribute('state', 'invalid');
Polymer.dom(this.$.wrapper).setAttribute('state', 'invalid');
// Attempt to show
var invalidElements = this._contents.invalid;
invalidElements.forEach(function (el) {
var showMethod = el.getAttribute('show');
if (showMethod) {
el[showMethod](event);
}
}.bind(this));
},
_handleIronFormSubmit: function (event) {
console.log('form loading')
console.log(event)
Polymer.dom(this).setAttribute('state', 'loading');
Polymer.dom(this.$.wrapper).setAttribute('state', 'loading');
},
_handleIronFormResponse: function (event) {
console.log('form response')
console.log(event)
Polymer.dom(this).setAttribute('state', 'success');
Polymer.dom(this.$.wrapper).setAttribute('state', 'success');
this._contents.success.forEach(function (el) {
var showMethod = el.getAttribute('show');
if (showMethod) {
el[showMethod](event);
}
}.bind(this));
},
_handleIronFormError: function (event) {
Polymer.dom(this).setAttribute('state', 'error');
Polymer.dom(this.$.wrapper).setAttribute('state', 'error');
this._contents.error.forEach(function (el) {
var showMethod = el.getAttribute('show');
if (showMethod) {
el[showMethod](event);
}
}.bind(this));
},
/**
* Executes a given method.
*
* TODO: implement receiving arguments.
*
* @param {String} methodName The name of the action to be executed
* @param {Event} event Event
* @return {*} Returns whatever the original method returns.
*/
_executeMethod: function (methodName, event) {
var operation = this[methodName];
if (!operation) {
console.warn('No ' + methodName + ' is defined.');
return;
}
return operation.call(this, event);
},
/**
* Proxy to the iron-form element
*/
submit: function () {
var form = this._contents.form;
// submit
return form.submit.apply(form, toArray(arguments));
},
/**
* Proxy to the iron-form element
*/
validate: function () {
var form = this._contents.form;
return form.validate.apply(form, toArray(arguments));
},
/**
* Resets the state of the form
*
* TODO: reset values from inputs
*/
reset: function () {
Polymer.dom(this).setAttribute('state', '');
Polymer.dom(this.$.wrapper).setAttribute('state', '');
}
});
</script>
</dom-module>