|
1 | 1 | /** |
2 | 2 | * Copyright (c) Bartłomiej Semańczyk - bartekss2@gmail.com http://www.blue-world.pl |
3 | | - * @version 1.1 |
4 | | - * Last Update: Sunday, 18 January 2015 |
| 3 | + * @version 1.2 |
| 4 | + * Last Update: Monday, 19 January 2015 |
5 | 5 | */ |
6 | 6 |
|
7 | 7 | (function($) { |
|
55 | 55 |
|
56 | 56 | var Formex_Class = function(instance, opts) { |
57 | 57 | this.$instance; |
58 | | - this.elems; |
| 58 | + this.elems = $(); |
59 | 59 | this.datas = {}; |
60 | 60 | this.defaults = { |
61 | 61 | wrap: true, |
|
80 | 80 | this.$instance = $(instance); |
81 | 81 | } |
82 | 82 |
|
| 83 | + var data = this.$instance.data('veryform'); |
| 84 | + if (data) { |
| 85 | + return data; |
| 86 | + } |
| 87 | + |
83 | 88 | if (this.defaults.init) { |
84 | 89 | this.init(); |
85 | 90 | } |
|
88 | 93 |
|
89 | 94 | Formex_Class.prototype.updateOptions = function(opts) { |
90 | 95 | opts ? $.extend(this.defaults, opts) : ''; |
| 96 | + this.updateData(this.elems); |
91 | 97 | }; |
92 | 98 |
|
93 | 99 | Formex_Class.prototype.init = function() { |
94 | 100 | var that = this; |
95 | 101 |
|
96 | | - that.elems = that.$instance.find('input[type!=submit][type!=hidden][type!=reset], textarea, select'); |
97 | | - |
98 | | - that.elems.each(function(){ |
| 102 | + that.$instance.find('input[type!=submit][type!=hidden][type!=reset], textarea, select').each(function(){ |
99 | 103 | that.add($(this)); |
100 | 104 | }); |
101 | 105 |
|
102 | 106 | that.setHandlers(); |
103 | 107 | }; |
104 | 108 |
|
105 | | - Formex_Class.prototype.add = function(elem) { |
106 | | - var type = elem.attr('type'); |
107 | | - var name = elem.attr('name'); |
108 | | - switch (type) { |
109 | | - case 'radio' : |
110 | | - case 'checkbox' : |
111 | | - if (!name) { |
112 | | - this.elems = this.elems.not(elem); |
113 | | - elem.remove(); |
114 | | - return; |
115 | | - } |
116 | | - break; |
117 | | - default: |
118 | | - } |
| 109 | + Formex_Class.prototype.add = function(elems) { |
| 110 | + var that = this; |
| 111 | + elems.each(function(){ |
| 112 | + var elem = $(this); |
| 113 | + var type = elem.attr('type'); |
| 114 | + var name = elem.attr('name'); |
| 115 | + switch (type) { |
| 116 | + case 'radio' : |
| 117 | + case 'checkbox' : |
| 118 | + if (!name) { |
| 119 | + elem.remove(); |
| 120 | + return; |
| 121 | + } |
| 122 | + break; |
| 123 | + default: |
| 124 | + } |
119 | 125 |
|
120 | | - this.setData(elem); |
121 | | - this.wrap(elem); |
122 | | - this.appendWarning(elem); |
123 | | - this.setShouldbe(elem); |
124 | | - this.valid(elem); |
| 126 | + that.updateData(elem); |
| 127 | + that.wrap(elem); |
| 128 | + that.appendWarning(elem); |
| 129 | + that.setShouldbe(elem); |
| 130 | + that.valid(elem); |
125 | 131 |
|
126 | | - if (this.defaults.autoinit) { |
127 | | - this.valid(elem); |
128 | | - this.performAction(elem); |
129 | | - this.checkGlobalStatus(); |
130 | | - } |
| 132 | + if (that.defaults.autoinit) { |
| 133 | + that.valid(elem); |
| 134 | + that.performAction(elem); |
| 135 | + that.checkGlobalStatus(); |
| 136 | + } |
131 | 137 |
|
132 | | - if (this.$instance.find(this.defaults.exclude).is(elem)) { |
133 | | - this.exclude(elem); |
134 | | - } |
| 138 | + if (that.$instance.find(that.defaults.exclude).is(elem)) { |
| 139 | + that.exclude(elem); |
| 140 | + } else { |
| 141 | + that.elems = that.elems.add(elem); |
| 142 | + } |
| 143 | + }); |
135 | 144 | }; |
136 | 145 |
|
137 | | - Formex_Class.prototype.setData = function(elem) { |
| 146 | + Formex_Class.prototype.updateData = function(elems) { |
138 | 147 | var that = this; |
| 148 | + elems.each(function(){ |
| 149 | + var elem = $(this); |
| 150 | + var data = elem.data(window.veryform.classnames.main); |
139 | 151 |
|
140 | | - that.repairType(elem); |
| 152 | + if (!data) { |
| 153 | + that.repairType(elem); |
141 | 154 |
|
142 | | - var name = elem.attr('name'); |
143 | | - var type = elem.attr('type'); |
| 155 | + var name = elem.attr('name'); |
| 156 | + var type = elem.attr('type'); |
144 | 157 |
|
145 | | - var elemdata = {}; |
146 | | - var data = {}; |
147 | | - elemdata['id'] = this.elemsCounter; |
148 | | - elemdata['data'] = data; |
149 | | - elemdata['validOnStart'] = false; |
| 158 | + var elemdata = {}; |
| 159 | + var data = {}; |
| 160 | + elemdata['id'] = that.elemsCounter; |
| 161 | + elemdata['data'] = data; |
| 162 | + elemdata['validOnStart'] = false; |
150 | 163 |
|
151 | | - for (var i in this.datas.elems) { |
152 | | - if (this.datas.elems[i]['data']['name'] == name) { |
| 164 | + for (var i in that.datas.elems) { |
| 165 | + if (that.datas.elems[i]['data']['name'] == name) { |
153 | 166 |
|
154 | | - this.datas.elems[i]['data']['sum']++; |
| 167 | + that.datas.elems[i]['data']['sum']++; |
155 | 168 |
|
156 | | - elemdata['data'] = this.datas.elems[i]['data']; |
157 | | - this.datas.elems.push(elemdata); |
| 169 | + elemdata['data'] = that.datas.elems[i]['data']; |
| 170 | + that.datas.elems.push(elemdata); |
| 171 | + elem.data(window.veryform.classnames.main, elemdata); |
| 172 | + that.applyHandler(elem); |
| 173 | + that.elemsCounter++; |
| 174 | + return; |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + data['elem'] = elem; |
| 179 | + data['type'] = type ? type : elem[0]['localName']; |
| 180 | + data['name'] = name; |
| 181 | + data['valid'] = false; |
| 182 | + data['sum'] = 1; //elems with the same name |
| 183 | + data['is'] = 0; |
| 184 | + data['wrapped'] = false; |
| 185 | + data['shouldbe'] = 0; |
| 186 | + elemdata['eventHandler'] = undefined; |
| 187 | + elemdata['eventName'] = undefined; |
| 188 | + |
| 189 | + !that.datas.elems ? that.datas.elems = [] : ''; |
| 190 | + that.datas.elems.push(elemdata); |
158 | 191 | elem.data(window.veryform.classnames.main, elemdata); |
| 192 | + that.elemsCounter++; |
159 | 193 | that.applyHandler(elem); |
160 | | - this.elemsCounter++; |
161 | | - return; |
| 194 | + elem.addClass(window.veryform.classnames.elem); |
162 | 195 | } |
163 | | - } |
164 | 196 |
|
165 | | - data['elem'] = elem; |
166 | | - data['type'] = type ? type : elem[0]['localName']; |
167 | | - data['name'] = name; |
168 | | - data['valid'] = false; |
169 | | - data['sum'] = 1; //elems with the same name |
170 | | - data['is'] = 0; |
171 | | - data['wrapped'] = false; |
172 | | - data['shouldbe'] = 0; |
173 | | - data['eventHandler'] = undefined; |
174 | | - data['eventName'] = undefined; |
175 | | - |
176 | | - !that.datas.elems ? that.datas.elems = [] : ''; |
177 | | - that.datas.elems.push(elemdata); |
178 | | - elem.data(window.veryform.classnames.main, elemdata); |
179 | | - that.elemsCounter++; |
180 | | - |
181 | | - that.applyRegex(elem); |
182 | | - that.applyWarnings(elem); |
183 | | - that.repairRegex(elem); |
184 | | - that.applyHandler(elem); |
185 | | - |
186 | | - elem.addClass(window.veryform.classnames.elem); |
187 | | - elem.removeAttr('required'); |
188 | | - !that.defaults.autocomplete ? elem.attr('autocomplete', 'off') : ''; |
| 197 | + that.applyRegex(elem); |
| 198 | + that.applyWarnings(elem); |
| 199 | + elem.removeAttr('required'); |
| 200 | + !that.defaults.autocomplete ? elem.attr('autocomplete', 'off') : ''; |
| 201 | + }); |
189 | 202 | }; |
190 | 203 |
|
191 | 204 | Formex_Class.prototype.applyRegex = function(elem) { |
|
216 | 229 |
|
217 | 230 | Formex_Class.prototype.exclude = function(elems) { |
218 | 231 | var that = this; |
| 232 | + |
219 | 233 | elems.each(function(){ |
220 | | - var data = $(this).data(window.veryform.classnames.main); |
221 | | - that.off($(this), data['data']['eventName'], data['data']['eventHandler']); |
| 234 | + var elem = $(this); |
| 235 | + |
| 236 | + var data = elem.data(window.veryform.classnames.main); |
| 237 | + |
| 238 | + that.off(elem, data['eventName'], data['eventHandler']); |
222 | 239 | data['data']['sum']--; |
223 | 240 | for (var i in that.datas.elems) { |
224 | 241 | var index = parseInt(i); |
|
227 | 244 | break; |
228 | 245 | } |
229 | 246 | } |
230 | | - $(this).removeData(window.veryform.classnames.main); |
| 247 | + that.unwrap(elem); |
| 248 | + elem.removeData(window.veryform.classnames.main); |
| 249 | + that.elems = that.elems.not(elem); |
231 | 250 | }); |
232 | 251 | }; |
233 | 252 |
|
|
360 | 379 | } |
361 | 380 | }; |
362 | 381 |
|
| 382 | + Formex_Class.prototype.unwrap = function(elems) { |
| 383 | + elems.each(function(){ |
| 384 | + var elem = $(this); |
| 385 | + var data = elem.data(window.veryform.classnames.main); |
| 386 | + |
| 387 | + if (data['data']['wrapped']) { |
| 388 | + var name = data['data']['name']; |
| 389 | + var wrap = elem.parents(_(window.veryform.classnames.wrap)); |
| 390 | + var elemsToUnwrap = $(); |
| 391 | + |
| 392 | + $('[name=' + name + ']').each(function(){ |
| 393 | + elemsToUnwrap = elemsToUnwrap.add($(this)); |
| 394 | + elemsToUnwrap = elemsToUnwrap.add($(this).parents('form').find('label[for=' + $(this).attr('id') + ']')); |
| 395 | + }); |
| 396 | + |
| 397 | + wrap.find(_(window.veryform.classnames.warning)).remove(); |
| 398 | + elemsToUnwrap.unwrap(); |
| 399 | + |
| 400 | + data['data']['wrapped'] = false; |
| 401 | + } |
| 402 | + }) |
| 403 | + }; |
| 404 | + |
363 | 405 | Formex_Class.prototype.appendWarning = function(elem) { |
364 | 406 | var that = this; |
365 | 407 | var data = elem.data(window.veryform.classnames.main); |
|
423 | 465 | } |
424 | 466 | break; |
425 | 467 | } |
426 | | - data['data']['eventName'] = eventName |
427 | | - data['data']['eventHandler'] = eventHandler; |
| 468 | + data['eventName'] = eventName |
| 469 | + data['eventHandler'] = eventHandler; |
428 | 470 | that.on(elem, eventName, eventHandler); |
429 | 471 | that.on(elem, 'ontrue', function(){ |
430 | 472 | if (that.defaults.displayWarnings) { |
|
442 | 484 |
|
443 | 485 | Formex_Class.prototype.setHandlers = function(event) { |
444 | 486 | var that = this; |
445 | | - if (that.defaults.validateOnSubmit) { |
446 | | - that.$instance.submit(function(e) { |
| 487 | + that.$instance.submit(function(e) { |
| 488 | + if (that.defaults.validateOnSubmit) { |
447 | 489 | var focused = false; |
448 | 490 | var error = false; |
449 | 491 |
|
|
458 | 500 | } |
459 | 501 | } |
460 | 502 | } |
| 503 | + |
461 | 504 | if (error) { |
462 | 505 | e.preventDefault(); |
463 | 506 | e.stopPropagation(); |
464 | 507 | e.stopImmediatePropagation(); |
465 | 508 | } |
466 | | - }); |
467 | | - } |
| 509 | + } |
| 510 | + }); |
468 | 511 | }; |
469 | 512 |
|
470 | 513 | Formex_Class.prototype.checkGlobalStatus = function() { |
|
0 commit comments