|
26 | 26 | aoObj._do = doOne;
|
27 | 27 | }
|
28 | 28 |
|
| 29 | + var ao = function (elem) { |
| 30 | + return new AoObj(elem); |
| 31 | + }; |
| 32 | + |
29 | 33 | // Instance members
|
| 34 | + // AoObj |
30 | 35 | var AoObj = function (elemOrId) {
|
31 | 36 | this._data = {};
|
32 | 37 | if (typeof elemOrId === 'undefined') {
|
33 | 38 | single(this, empty);
|
34 | 39 | }
|
35 | 40 | else if (typeof elemOrId === 'string') {
|
36 |
| - single(this, Ao.get(elemOrId)); |
| 41 | + single(this, ao.get(elemOrId)); |
37 | 42 | }
|
38 | 43 | else {
|
39 | 44 | if (isArrayLike(elemOrId)) {
|
|
51 | 56 | }
|
52 | 57 | };
|
53 | 58 |
|
54 |
| - var AoPopup = function () { |
55 |
| - AoObj.call(this, 'popup'); |
56 |
| - }; |
57 |
| - |
58 |
| - var AoSubmit = function (formAoObj) { |
59 |
| - AoObj.call(this, formAoObj.getByCss('input[type="submit"]')); |
60 |
| - }; |
61 |
| - |
62 |
| - var AoForm = function (form) { |
63 |
| - AoObj.call(this, form); |
64 |
| - this._cover = new AoObj('progress-cover'); |
65 |
| - this._popup = new AoPopup(); |
66 |
| - this.submit = new AoSubmit(this); |
67 |
| - }; |
68 |
| - |
69 | 59 | AoObj.prototype = {
|
70 | 60 | getByCss: function (selector) {
|
71 | 61 | return this.e.querySelector(selector);
|
|
129 | 119 | }
|
130 | 120 | };
|
131 | 121 |
|
| 122 | + // AoSubmit |
| 123 | + var AoSubmit = function (formAoObj) { |
| 124 | + AoObj.call(this, formAoObj.getByCss('input[type="submit"]')); |
| 125 | + }; |
| 126 | + |
132 | 127 | AoSubmit.prototype = Object.create(AoObj.prototype);
|
133 | 128 |
|
134 | 129 | AoSubmit.prototype.confirm = function () {
|
|
141 | 136 | return false;
|
142 | 137 | };
|
143 | 138 |
|
| 139 | + // AoPopup |
| 140 | + var AoPopup = function () { |
| 141 | + AoObj.call(this, 'popup'); |
| 142 | + }; |
| 143 | + |
144 | 144 | AoPopup.prototype = Object.create(AoObj.prototype);
|
145 | 145 |
|
146 | 146 | AoPopup.prototype.show = function (contentId) {
|
147 |
| - var content = new AoObj(contentId); |
| 147 | + var content = ao(contentId); |
148 | 148 | if (content.getByCss('.message')) {
|
149 | 149 | this.addClass('with-message');
|
150 | 150 | }
|
|
156 | 156 | return this.removeClass('with-message');
|
157 | 157 | };
|
158 | 158 |
|
159 |
| - AoForm.prototype = Object.create(AoObj.prototype); |
| 159 | + // AoValidator |
| 160 | + function required() { |
| 161 | + return Boolean(this.e.value); |
| 162 | + } |
160 | 163 |
|
161 |
| - AoForm.prototype.validate = function () { |
162 |
| - for (var i = 0, l = this.e.length; i < l; ++i) { |
163 |
| - var input = this.e[i]; |
| 164 | + var AoValidator = function (input) { |
| 165 | + AoObj.call(this, input); |
| 166 | + this._msg = ao(input.nextElementSibling); |
| 167 | + this._validators = []; |
| 168 | + var requiredMsg = input.getAttribute('data-val-required'); |
| 169 | + if (Boolean(requiredMsg)) { |
| 170 | + this._validators.push({ test: required, msg: requiredMsg }); |
| 171 | + } |
| 172 | + }; |
| 173 | + |
| 174 | + AoValidator.prototype = Object.create(AoObj.prototype); |
| 175 | + |
| 176 | + AoValidator.prototype.validate = function () { |
| 177 | + for (var i = 0, l = this._validators.length; i < l; ++i) { |
| 178 | + var validator = this._validators[i]; |
| 179 | + if (validator.test.call(this) === false) { |
| 180 | + this._msg.e.innerHTML = validator.msg; |
| 181 | + this._msg.removeClass('field-validation-valid').addClass('field-validation-error'); |
| 182 | + return false; |
| 183 | + } |
| 184 | + } |
| 185 | + this._msg.removeClass('field-validation-error').addClass('field-validation-valid'); |
| 186 | + return true; |
| 187 | + }; |
| 188 | + |
| 189 | + // AoForm |
| 190 | + var AoForm = function (form) { |
| 191 | + AoObj.call(this, form); |
| 192 | + this._cover = ao('progress-cover'); |
| 193 | + this._popup = new AoPopup(); |
| 194 | + this.submit = new AoSubmit(this); |
| 195 | + |
| 196 | + this._validators = []; |
| 197 | + for (var i = 0, l = form.length; i < l; ++i) { |
| 198 | + var input = form[i]; |
164 | 199 | if (!Boolean(input.getAttribute('data-val'))) { continue; }
|
| 200 | + this._validators.push(new AoValidator(input)); |
| 201 | + } |
| 202 | + }; |
165 | 203 |
|
| 204 | + AoForm.prototype = Object.create(AoObj.prototype); |
| 205 | + |
| 206 | + AoForm.prototype.validate = function () { |
| 207 | + var result = true; |
| 208 | + for (var i = 0, l = this._validators.length; i < l; ++i) { |
| 209 | + if (this._validators[i].validate() === false) { |
| 210 | + result = false; |
| 211 | + } |
166 | 212 | }
|
167 |
| - return false; |
| 213 | + return result; |
168 | 214 | };
|
169 | 215 |
|
170 | 216 | AoForm.prototype.submitting = function () {
|
|
192 | 238 | };
|
193 | 239 |
|
194 | 240 | // Static members
|
195 |
| - var ao = function (elem) { |
196 |
| - return new AoObj(elem); |
197 |
| - }; |
198 |
| - |
199 | 241 | ao.ready = function (callback) {
|
200 | 242 | if (document.readyState === 'complete' ||
|
201 | 243 | (document.readyState !== 'loading' && !document.documentElement.doScroll)) {
|
|
0 commit comments