OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 const cr = (function() { | 5 const cr = (function() { |
6 | 6 |
7 /** | 7 /** |
8 * Whether we are using a Mac or not. | 8 * Whether we are using a Mac or not. |
9 * @type {boolean} | 9 * @type {boolean} |
10 */ | 10 */ |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 * element has the attribute then the value is true. | 123 * element has the attribute then the value is true. |
124 */ | 124 */ |
125 BOOL_ATTR: 'boolAttr' | 125 BOOL_ATTR: 'boolAttr' |
126 }; | 126 }; |
127 | 127 |
128 /** | 128 /** |
129 * Helper function for defineProperty that returns the getter to use for the | 129 * Helper function for defineProperty that returns the getter to use for the |
130 * property. | 130 * property. |
131 * @param {string} name | 131 * @param {string} name |
132 * @param {cr.PropertyKind} kind | 132 * @param {cr.PropertyKind} kind |
133 * @param {*} defaultValue The default value. This is only used for the ATTR | |
134 * kind. | |
135 * @return {function():*} The getter for the property. | 133 * @return {function():*} The getter for the property. |
136 */ | 134 */ |
137 function getGetter(name, kind, defaultValue) { | 135 function getGetter(name, kind) { |
138 switch (kind) { | 136 switch (kind) { |
139 case PropertyKind.JS: | 137 case PropertyKind.JS: |
140 var privateName = name + '_'; | 138 var privateName = name + '_'; |
141 return function() { | 139 return function() { |
142 return this[privateName]; | 140 return this[privateName]; |
143 }; | 141 }; |
144 case PropertyKind.ATTR: | 142 case PropertyKind.ATTR: |
145 // For attr with default value we return the default value if the | 143 return function() { |
146 // element is missing the attribute. | 144 return this.getAttribute(name); |
147 if (defaultValue == undefined) { | 145 }; |
148 return function() { | |
149 return this.getAttribute(name); | |
150 }; | |
151 } else { | |
152 return function() { | |
153 // WebKit uses null for non existant attributes. | |
154 var value = this.getAttribute(name); | |
155 return value !== null ? value : defaultValue; | |
156 }; | |
157 } | |
158 case PropertyKind.BOOL_ATTR: | 146 case PropertyKind.BOOL_ATTR: |
159 // Boolean attributes don't support default values. | |
160 return function() { | 147 return function() { |
161 return this.hasAttribute(name); | 148 return this.hasAttribute(name); |
162 }; | 149 }; |
163 } | 150 } |
164 } | 151 } |
165 | 152 |
166 /** | 153 /** |
167 * Helper function for defineProperty that returns the setter of the right | 154 * Helper function for defineProperty that returns the setter of the right |
168 * kind. | 155 * kind. |
169 * @param {string} name The name of the property we are defining the setter | 156 * @param {string} name The name of the property we are defining the setter |
170 * for. | 157 * for. |
171 * @param {cr.PropertyKind} kind The kind of property we are getting the | 158 * @param {cr.PropertyKind} kind The kind of property we are getting the |
172 * setter for. | 159 * setter for. |
| 160 * @param {function(*):void} opt_setHook A function to run after the property |
| 161 * is set, but before the propertyChange event is fired. |
173 * @return {function(*):void} The function to use as a setter. | 162 * @return {function(*):void} The function to use as a setter. |
174 */ | 163 */ |
175 function getSetter(name, kind) { | 164 function getSetter(name, kind, opt_setHook) { |
176 switch (kind) { | 165 switch (kind) { |
177 case PropertyKind.JS: | 166 case PropertyKind.JS: |
178 var privateName = name + '_'; | 167 var privateName = name + '_'; |
179 return function(value) { | 168 return function(value) { |
180 var oldValue = this[privateName]; | 169 var oldValue = this[privateName]; |
181 if (value !== oldValue) { | 170 if (value !== oldValue) { |
182 this[privateName] = value; | 171 this[privateName] = value; |
| 172 if (opt_setHook) |
| 173 opt_setHook.call(this, value, oldValue); |
183 dispatchPropertyChange(this, name, value, oldValue); | 174 dispatchPropertyChange(this, name, value, oldValue); |
184 } | 175 } |
185 }; | 176 }; |
186 | 177 |
187 case PropertyKind.ATTR: | 178 case PropertyKind.ATTR: |
188 return function(value) { | 179 return function(value) { |
189 var oldValue = this[name]; | 180 var oldValue = this[name]; |
190 if (value !== oldValue) { | 181 if (value !== oldValue) { |
191 this.setAttribute(name, value); | 182 this.setAttribute(name, value); |
| 183 if (opt_setHook) |
| 184 opt_setHook.call(this, value, oldValue); |
192 dispatchPropertyChange(this, name, value, oldValue); | 185 dispatchPropertyChange(this, name, value, oldValue); |
193 } | 186 } |
194 }; | 187 }; |
195 | 188 |
196 case PropertyKind.BOOL_ATTR: | 189 case PropertyKind.BOOL_ATTR: |
197 return function(value) { | 190 return function(value) { |
198 var oldValue = this[name]; | 191 var oldValue = this[name]; |
199 if (value !== oldValue) { | 192 if (value !== oldValue) { |
200 if (value) | 193 if (value) |
201 this.setAttribute(name, name); | 194 this.setAttribute(name, name); |
202 else | 195 else |
203 this.removeAttribute(name); | 196 this.removeAttribute(name); |
| 197 if (opt_setHook) |
| 198 opt_setHook.call(this, value, oldValue); |
204 dispatchPropertyChange(this, name, value, oldValue); | 199 dispatchPropertyChange(this, name, value, oldValue); |
205 } | 200 } |
206 }; | 201 }; |
207 } | 202 } |
208 } | 203 } |
209 | 204 |
210 /** | 205 /** |
211 * Defines a property on an object. When the setter changes the value a | 206 * Defines a property on an object. When the setter changes the value a |
212 * property change event with the type {@code name + 'Change'} is fired. | 207 * property change event with the type {@code name + 'Change'} is fired. |
213 * @param {!Object} obj The object to define the property for. | 208 * @param {!Object} obj The object to define the property for. |
214 * @param {string} name The name of the property. | 209 * @param {string} name The name of the property. |
215 * @param {cr.PropertyKind=} opt_kind What kind of underlying storage to use. | 210 * @param {cr.PropertyKind=} opt_kind What kind of underlying storage to use. |
216 * @param {*} opt_default The default value. | 211 * @param {function(*):void} opt_setHook A function to run after the |
| 212 * property is set, but before the propertyChange event is fired. |
217 */ | 213 */ |
218 function defineProperty(obj, name, opt_kind, opt_default) { | 214 function defineProperty(obj, name, opt_kind, opt_setHook) { |
219 if (typeof obj == 'function') | 215 if (typeof obj == 'function') |
220 obj = obj.prototype; | 216 obj = obj.prototype; |
221 | 217 |
222 var kind = opt_kind || PropertyKind.JS; | 218 var kind = opt_kind || PropertyKind.JS; |
223 | 219 |
224 if (!obj.__lookupGetter__(name)) { | 220 if (!obj.__lookupGetter__(name)) { |
225 // For js properties we set the default value on the prototype. | 221 obj.__defineGetter__(name, getGetter(name, kind)); |
226 if (kind == PropertyKind.JS && arguments.length > 3) { | |
227 var privateName = name + '_'; | |
228 obj[privateName] = opt_default; | |
229 } | |
230 obj.__defineGetter__(name, getGetter(name, kind, opt_default)); | |
231 } | 222 } |
232 | 223 |
233 if (!obj.__lookupSetter__(name)) { | 224 if (!obj.__lookupSetter__(name)) { |
234 obj.__defineSetter__(name, getSetter(name, kind)); | 225 obj.__defineSetter__(name, getSetter(name, kind, opt_setHook)); |
235 } | 226 } |
236 } | 227 } |
237 | 228 |
238 /** | 229 /** |
239 * Counter for use with createUid | 230 * Counter for use with createUid |
240 */ | 231 */ |
241 var uidCounter = 1; | 232 var uidCounter = 1; |
242 | 233 |
243 /** | 234 /** |
244 * @return {number} A new unique ID. | 235 * @return {number} A new unique ID. |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 * The document that we are currently using. | 336 * The document that we are currently using. |
346 * @type {!Document} | 337 * @type {!Document} |
347 */ | 338 */ |
348 get doc() { | 339 get doc() { |
349 return doc; | 340 return doc; |
350 }, | 341 }, |
351 withDoc: withDoc, | 342 withDoc: withDoc, |
352 Event: CrEvent | 343 Event: CrEvent |
353 }; | 344 }; |
354 })(); | 345 })(); |
OLD | NEW |