|
20 | 20 | } |
21 | 21 |
|
22 | 22 | var empty = { |
23 | | - classList: { add: function () { }, remove: function () { } } |
| 23 | + classList: { add: doNothing, remove: doNothing }, |
| 24 | + style: {}, |
| 25 | + addEventListener: doNothing, |
| 26 | + dispatchEvent: doNothing |
24 | 27 | }; |
25 | 28 |
|
26 | 29 | function single(aoObj, elem) { |
|
41 | 44 | } else if (typeof elemOrId === 'string') { |
42 | 45 | single(this, ao.get(elemOrId)); |
43 | 46 | } else { |
44 | | - if (isArrayLike(elemOrId)) { |
| 47 | + if (elemOrId === window || elemOrId === document || elemOrId === document.body) { |
| 48 | + single(this, elemOrId); |
| 49 | + } |
| 50 | + else if (isArrayLike(elemOrId)) { |
45 | 51 | if (elemOrId.length === 1) { |
46 | 52 | single(this, elemOrId[0]); |
47 | 53 | } else { |
|
54 | 60 | } |
55 | 61 | }; |
56 | 62 |
|
| 63 | + function registerOrDispatch(eventName, callback) { |
| 64 | + if (typeof callback === 'function') { |
| 65 | + return this.on(eventName, callback); |
| 66 | + } |
| 67 | + this.e.dispatchEvent(new Event(eventName)); |
| 68 | + return this; |
| 69 | + } |
| 70 | + |
57 | 71 | AoObj.prototype = { |
58 | 72 | getByCss: function (selector) { |
59 | 73 | return this.e.querySelector(selector); |
|
71 | 85 | e.classList.remove(name); |
72 | 86 | }); |
73 | 87 | }, |
| 88 | + toggleClass: function (name) { |
| 89 | + return this._do(function (e) { |
| 90 | + e.classList.contains(name) |
| 91 | + ? e.classList.remove(name) |
| 92 | + : e.classList.add(name); |
| 93 | + }); |
| 94 | + }, |
74 | 95 | css: function () { |
| 96 | + if (arguments.length === 1) { |
| 97 | + var name = arguments[0]; |
| 98 | + if (typeof name === 'string') { |
| 99 | + return getComputedStyle(this.e).getPropertyValue(name); |
| 100 | + } |
| 101 | + } |
75 | 102 | var namesAndValues = arguments; |
76 | 103 | var l = namesAndValues.length; |
77 | 104 | return this._do(function (e) { |
|
122 | 149 | e.parentNode.insertBefore(elem, e.nextSibling); |
123 | 150 | }); |
124 | 151 | }, |
125 | | - on: function (eventTypes, callback) { |
| 152 | + on: function (eventTypes, callback, ctx) { |
126 | 153 | var events = eventTypes.split(' '); |
127 | 154 | var l = events.length; |
128 | | - var that = this; |
129 | | - var cxtCallback = function () { |
130 | | - callback.call(that); |
| 155 | + var that = ctx || this; |
| 156 | + var cxtCallback = function (evt) { |
| 157 | + callback.call(that, evt); |
131 | 158 | }; |
132 | 159 | return this._do(function (e) { |
133 | 160 | for (var i = 0; i < l; ++i) { |
134 | 161 | e.addEventListener(events[i], cxtCallback); |
135 | 162 | } |
136 | 163 | }); |
| 164 | + }, |
| 165 | + click: function (callback) { |
| 166 | + return registerOrDispatch.call(this, 'click', callback); |
| 167 | + }, |
| 168 | + blur: function (callback) { |
| 169 | + return registerOrDispatch.call(this, 'blur', callback); |
137 | 170 | } |
138 | 171 | }; |
139 | 172 |
|
140 | | - // AoSubmit |
141 | | - var AoSubmit = function (formAoObj) { |
142 | | - AoObj.call(this, formAoObj.getByCss('input[type="submit"]')); |
| 173 | + ao.derive = function (ctor) { |
| 174 | + ctor.prototype = Object.create(AoObj.prototype); |
| 175 | + ctor.prototype._base = AoObj; |
| 176 | + return ctor; |
143 | 177 | }; |
144 | 178 |
|
145 | | - AoSubmit.prototype = Object.create(AoObj.prototype); |
| 179 | + // AoSubmit |
| 180 | + var AoSubmit = ao.derive(function (formAoObj) { |
| 181 | + AoObj.call(this, formAoObj.getByCss('input[type="submit"]')); |
| 182 | + }); |
146 | 183 |
|
147 | 184 | AoSubmit.prototype.confirm = function () { |
148 | 185 | if (this.e.value === 'Confirm') { return true; } |
|
155 | 192 | }; |
156 | 193 |
|
157 | 194 | // AoPopup |
158 | | - var AoPopup = function () { |
| 195 | + var AoPopup = ao.derive(function () { |
159 | 196 | AoObj.call(this, 'popup'); |
160 | | - }; |
161 | | - |
162 | | - AoPopup.prototype = Object.create(AoObj.prototype); |
| 197 | + }); |
163 | 198 |
|
164 | 199 | AoPopup.prototype.show = function (contentId) { |
165 | 200 | var content = ao(contentId); |
|
187 | 222 | var validatorNames = Object.keys(validators); |
188 | 223 | var validatorsCount = validatorNames.length; |
189 | 224 |
|
190 | | - var AoValidator = function (input) { |
| 225 | + var AoValidator = ao.derive(function (input) { |
191 | 226 | AoObj.call(this, input); |
192 | 227 | this.on('blur keyup', this.validate); |
193 | 228 | this._msg = ao(document.createElement('span')).addClass('error-message'); |
|
200 | 235 | this._validators.push({ test: validators[validatorName], msg: msg }); |
201 | 236 | } |
202 | 237 | } |
203 | | - }; |
204 | | - |
205 | | - AoValidator.prototype = Object.create(AoObj.prototype); |
| 238 | + }); |
206 | 239 |
|
207 | 240 | AoValidator.prototype.validate = function () { |
208 | 241 | for (var i = 0, l = this._validators.length; i < l; ++i) { |
|
218 | 251 | }; |
219 | 252 |
|
220 | 253 | // AoForm |
221 | | - var AoForm = function (form) { |
| 254 | + var AoForm = ao.derive(function (form) { |
222 | 255 | AoObj.call(this, form); |
223 | 256 | this._cover = ao('progress-cover'); |
224 | 257 | this._popup = new AoPopup(); |
|
230 | 263 | if (!Boolean(input.getAttribute('data-val'))) { continue; } |
231 | 264 | this._validators.push(new AoValidator(input)); |
232 | 265 | } |
233 | | - }; |
234 | | - |
235 | | - AoForm.prototype = Object.create(AoObj.prototype); |
| 266 | + }); |
236 | 267 |
|
237 | 268 | AoForm.prototype.validate = function () { |
238 | 269 | var result = true; |
|
0 commit comments