|
18 | 18 | ], |
19 | 19 |
|
20 | 20 | properties: { |
| 21 | + view: { |
| 22 | + type: Object |
| 23 | + }, |
21 | 24 | unsaved: { |
22 | 25 | type: Boolean, |
23 | 26 | value: true, |
|
82 | 85 | type: Boolean, |
83 | 86 | computed: '_displayMessage(_showFooterMessage, showFooterMessages)' |
84 | 87 | }, |
| 88 | + |
85 | 89 | // config/initial data & settings: |
86 | 90 | config: { |
87 | 91 | type: Object, |
|
90 | 94 | }, |
91 | 95 | data: { |
92 | 96 | type: Object, |
93 | | - observer: '_dataChanged' |
| 97 | + observer: '_dataChanged', |
| 98 | + value: function() { return {}; } |
94 | 99 | } |
95 | 100 | }, |
96 | 101 |
|
|
104 | 109 |
|
105 | 110 | _dataChanged: function(newVal, oldVal) { |
106 | 111 | console.log('_dataChanged:', newVal); |
107 | | - this.debounce('init', this._initForm); |
| 112 | + // this.debounce('init', this._initForm); |
| 113 | + this._initData(); |
108 | 114 | }, |
109 | 115 |
|
110 | 116 | _configChanged: function(newVal, oldVal) { |
111 | 117 | console.log('_configChanged:', newVal); |
112 | | - this.debounce('init', this._initForm); |
| 118 | + // this.debounce('init', this._initForm); |
| 119 | + this._initConfig(); |
113 | 120 | }, |
114 | 121 |
|
115 | | - _selectEle: function(ele, parent) { |
| 122 | + _select: function(ele, parent) { |
116 | 123 | var scope = this || parent; |
117 | 124 | return Polymer.dom(scope).querySelector(ele); |
118 | 125 | }, |
119 | 126 |
|
120 | | - _initForm: function() { |
121 | | - if (!this.data) return; |
122 | | - |
123 | | - for (var key in this.data) { |
124 | | - var cfgKey = this.config[key] ? this.config[key] : null, |
125 | | - field = this._selectEle('[name='+key+']'), |
126 | | - validation = null, |
127 | | - label = null, |
128 | | - errorMsg = null, |
129 | | - errorMsgEle = null, |
130 | | - parentEle = null, |
131 | | - value = this.data[key] || null; |
| 127 | + _initConfig: function() { |
| 128 | + for (var key in this.config) { |
| 129 | + var cfg = this.config[key] ? this.config[key] : null, |
| 130 | + field = this._select('[name='+key+']'), |
| 131 | + validation = null, |
| 132 | + validateIf = null, |
| 133 | + label = null, |
| 134 | + errorMsg = null, |
| 135 | + errorMsgEle = null, |
| 136 | + parentEle = null; |
132 | 137 |
|
133 | 138 | if (!field) { |
134 | | - throw 'There must be a corresponding DOM element for data[\''+key+'\']'; |
| 139 | + throw 'There must be a corresponding DOM element for config[\''+key+'\']'; |
135 | 140 | } |
136 | 141 |
|
137 | 142 | // a config was or was not supplied - if one was supplied, |
138 | 143 | // use it, if not create one |
139 | | - if (cfgKey) { |
140 | | - validation = cfgKey.validation ? cfgKey.validation : null, |
141 | | - label = cfgKey.label ? cfgKey.label : null, |
142 | | - errorMsg = cfgKey.errorMsg ? cfgKey.errorMsg : null, |
143 | | - errorMsgEle = cfgKey.errorMsgEle ? this._selectEle('#'+cfgKey.errorMsgEle) : null, |
144 | | - parentEle = cfgKey.parentEle ? this._selectEle('#'+cfgKey.parentEle) : Polymer.dom(field).parentNode; |
| 144 | + if (cfg) { |
| 145 | + validation = cfg.validation ? cfg.validation : null; |
| 146 | + validateIf = cfg.validateIf ? cfg.validateIf : null; |
| 147 | + label = cfg.label ? cfg.label : null; |
| 148 | + errorMsg = cfg.errorMsg ? cfg.errorMsg : null; |
| 149 | + errorMsgEle = cfg.errorMsgEle ? this._select('#'+cfg.errorMsgEle) : null; |
| 150 | + parentEle = cfg.parentEle ? this._select('#'+cfg.parentEle) : Polymer.dom(field).parentNode; |
| 151 | + exclude = cfg.exclude ? cfg.exclude : false; |
145 | 152 | } else { |
146 | | - this.config[key] = {}; |
147 | | - } |
148 | | - |
149 | | - // If there was an initial value set in markup, use that |
150 | | - // However, values set in the config will always 'win' |
151 | | - if (field.value && value === null) { |
152 | | - value = field.value; |
| 153 | + this.config[key] = {}; |
153 | 154 | } |
154 | 155 |
|
155 | 156 | // Store everything for reference later, assumes it's possible |
156 | 157 | // to have a config which didn't include ALL of these items |
157 | | - this.config[key].field = field; |
158 | | - this.config[key].validation = validation; |
159 | | - this.config[key].label = label; |
160 | | - this.config[key].errorMsg = errorMsg; |
161 | | - this.config[key].errorMsgEle = errorMsgEle; |
162 | | - this.config[key].parentEle = parentEle; |
163 | | - |
164 | | - // update data and DOM |
165 | | - this._updateData(key, value); |
166 | | - this._initialData[key] = value; |
| 158 | + this.config[key].field = field; |
| 159 | + this.config[key].validation = validation; |
| 160 | + this.config[key].validateIf = validateIf; |
| 161 | + this.config[key].label = label; |
| 162 | + this.config[key].errorMsg = errorMsg; |
| 163 | + this.config[key].errorMsgEle = errorMsgEle; |
| 164 | + this.config[key].parentEle = parentEle; |
| 165 | + this.config[key].exclude = exclude; |
167 | 166 |
|
168 | 167 | if (errorMsg && !errorMsgEle) { |
169 | 168 | this._createErrorMsg(key, errorMsg, errorMsgEle, parentEle); |
|
172 | 171 | } |
173 | 172 |
|
174 | 173 | if (label) this._createLabel(key, label, field, parentEle); |
| 174 | + } |
| 175 | + }, |
| 176 | + |
| 177 | + _initData: function() { |
| 178 | + for (var key in this.config) { |
| 179 | + var field = this._select('[name='+key+']'), |
| 180 | + exclude = this.config[key].exclude, |
| 181 | + value = this.data[key] || null; |
| 182 | + |
| 183 | + if (!field) { |
| 184 | + throw 'There must be a corresponding DOM element for data[\''+key+'\']'; |
| 185 | + } |
| 186 | + |
| 187 | + // If there was an initial value set in markup, use that |
| 188 | + // However, values set in the config will always 'win' |
| 189 | + if (field.value && value === null) { |
| 190 | + value = field.value; |
| 191 | + } |
| 192 | + |
| 193 | + // update data and DOM |
| 194 | + if (!exclude) this._updateData(key, value); |
| 195 | + this._initialData[key] = value; |
175 | 196 |
|
176 | 197 | // Populate the fields if necessary |
177 | 198 | if (value && field.value !== value) { |
|
181 | 202 | }, |
182 | 203 |
|
183 | 204 | _createErrorMsg:function(key, errorMsg, errorMsgEle, parentEle) { |
184 | | - var existingMsgEle = this._selectEle('._'+key+'-error-msg') || null; |
| 205 | + var existingMsgEle = this._select('._'+key+'-error-msg') || null; |
185 | 206 |
|
186 | 207 | if (!existingMsgEle) { |
187 | 208 | // create one: |
|
200 | 221 | }, |
201 | 222 |
|
202 | 223 | _createLabel:function(key, label, field, parentEle) { |
203 | | - var existingLblEle = this._selectEle('._'+key+'-label') || null, |
204 | | - formLabel = null, |
205 | | - labelTxt = null; |
| 224 | + var existingLblEle = this._select('._'+key+'-label') || null, |
| 225 | + formLabel = null, |
| 226 | + labelTxt = null; |
206 | 227 |
|
207 | 228 | if (!existingLblEle) { |
208 | 229 | // create one: |
|
228 | 249 |
|
229 | 250 | // handle changes within the form |
230 | 251 | _handleChanged: function(e) { |
231 | | - var field = e.target, |
232 | | - key = field.getAttribute('name'), |
233 | | - value = null, |
234 | | - validation = null, |
235 | | - isFormElement = this.data.hasOwnProperty(key); |
| 252 | + var field = e.target, |
| 253 | + key = field.getAttribute('name'), |
| 254 | + value = null, |
| 255 | + validation = null, |
| 256 | + exclude = null, |
| 257 | + isFormElement = this.config.hasOwnProperty(key); |
236 | 258 |
|
237 | 259 | if (isFormElement) { |
238 | | - value = e.detail.value; |
239 | | - validation = this.config[key].validation; |
| 260 | + exclude = this.config[key].exclude ? this.config[key].exclude : false; |
| 261 | + value = e.detail.value; |
| 262 | + validation = this.config[key].validation; |
240 | 263 |
|
241 | | - this._updateData(key, value); |
242 | | - this.unsaved = this._diffData(); |
| 264 | + if (!exclude) { |
| 265 | + this._updateData(key, value); |
| 266 | + this.unsaved = this._diffData(); |
| 267 | + } |
243 | 268 |
|
244 | 269 | if (validation) this._validateField(key, value); |
245 | 270 |
|
|
271 | 296 | this._invalidFields = []; |
272 | 297 | this._validFields = []; |
273 | 298 |
|
274 | | - for (var key in this.data) { |
| 299 | + for (var key in this.config) { |
275 | 300 | var value = this.data[key], |
276 | 301 | validation = this.config[key].validation, |
| 302 | + validateIf = this.config[key].validateIf ? this.config[key].validateIf(key, value, this.data, this.view) : null, |
277 | 303 | valid = false; |
278 | 304 |
|
279 | | - if (validation) { |
| 305 | + if (validation && (validateIf === null || validateIf === true)) { |
280 | 306 | valid = this._validateField(key, value); |
281 | 307 |
|
282 | 308 | // Store valid and invalid for this validation pass |
|
285 | 311 | } else { |
286 | 312 | this._invalidFields.push(key); |
287 | 313 | } |
| 314 | + } else if (validation && (validateIf !== null || validateIf === false)) { |
| 315 | + // clean up prior validations if they were there |
| 316 | + this.resetFieldValidation(key); |
288 | 317 | } |
289 | 318 |
|
290 | 319 | // show messaging in the footer |
|
319 | 348 | // }, |
320 | 349 |
|
321 | 350 | _validateField: function(key, value) { |
322 | | - var valid = false, |
| 351 | + var valid = null, |
323 | 352 | field = this.config[key].field, |
324 | 353 | validation = this.config[key].validation, |
| 354 | + errorMsg = this.config[key].errorMsg, |
325 | 355 | errorMsgEle = this.config[key].errorMsgEle; |
326 | 356 |
|
327 | 357 | if (typeof(validation) === 'string') { |
|
336 | 366 |
|
337 | 367 | valid = result.length === testSet.length; |
338 | 368 | } else if (typeof(validation) === 'function') { |
339 | | - valid = validation(key, value, this.data); |
| 369 | + valid = validation(key, value, this.data, this.view); |
340 | 370 | } |
341 | 371 |
|
342 | 372 | // show or hide messaging in the ui |
| 373 | + errorMsgEle.message = errorMsg; |
343 | 374 | field.error = errorMsgEle.visible = !valid; |
344 | | - |
| 375 | + |
345 | 376 | return valid; |
346 | 377 | }, |
347 | 378 |
|
| 379 | + resetFieldValidation: function(key) { |
| 380 | + var field = this.config[key].field, |
| 381 | + errorMsgEle = this.config[key].errorMsgEle; |
| 382 | + |
| 383 | + field.error = errorMsgEle.visible = false; |
| 384 | + }, |
| 385 | + |
348 | 386 | serializeForm: function() { |
349 | 387 | this.validateFields(); |
350 | 388 |
|
|
0 commit comments