OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 */ | 127 */ |
128 function dispatchPropertyChange(target, propertyName, newValue, oldValue) { | 128 function dispatchPropertyChange(target, propertyName, newValue, oldValue) { |
129 var e = new CrEvent(propertyName + 'Change'); | 129 var e = new CrEvent(propertyName + 'Change'); |
130 e.propertyName = propertyName; | 130 e.propertyName = propertyName; |
131 e.newValue = newValue; | 131 e.newValue = newValue; |
132 e.oldValue = oldValue; | 132 e.oldValue = oldValue; |
133 target.dispatchEvent(e); | 133 target.dispatchEvent(e); |
134 } | 134 } |
135 | 135 |
136 /** | 136 /** |
| 137 * Converts a camelCase javascript property name to a hyphenated-lower-case |
| 138 * attribute name. |
| 139 * @param {string} jsName The javascript camelCase property name. |
| 140 * @return {string} The equivalent hyphenated-lower-case attribute name. |
| 141 */ |
| 142 function getAttributeName(jsName) { |
| 143 return jsName.replace(/([A-Z])/g, '-$1').toLowerCase(); |
| 144 } |
| 145 |
| 146 /** |
137 * The kind of property to define in {@code defineProperty}. | 147 * The kind of property to define in {@code defineProperty}. |
138 * @enum {number} | 148 * @enum {number} |
139 */ | 149 */ |
140 const PropertyKind = { | 150 const PropertyKind = { |
141 /** | 151 /** |
142 * Plain old JS property where the backing data is stored as a "private" | 152 * Plain old JS property where the backing data is stored as a "private" |
143 * field on the object. | 153 * field on the object. |
144 */ | 154 */ |
145 JS: 'js', | 155 JS: 'js', |
146 | 156 |
(...skipping 17 matching lines...) Expand all Loading... |
164 * @return {function():*} The getter for the property. | 174 * @return {function():*} The getter for the property. |
165 */ | 175 */ |
166 function getGetter(name, kind) { | 176 function getGetter(name, kind) { |
167 switch (kind) { | 177 switch (kind) { |
168 case PropertyKind.JS: | 178 case PropertyKind.JS: |
169 var privateName = name + '_'; | 179 var privateName = name + '_'; |
170 return function() { | 180 return function() { |
171 return this[privateName]; | 181 return this[privateName]; |
172 }; | 182 }; |
173 case PropertyKind.ATTR: | 183 case PropertyKind.ATTR: |
| 184 var attributeName = getAttributeName(name); |
174 return function() { | 185 return function() { |
175 return this.getAttribute(name); | 186 return this.getAttribute(attributeName); |
176 }; | 187 }; |
177 case PropertyKind.BOOL_ATTR: | 188 case PropertyKind.BOOL_ATTR: |
| 189 var attributeName = getAttributeName(name); |
178 return function() { | 190 return function() { |
179 return this.hasAttribute(name); | 191 return this.hasAttribute(attributeName); |
180 }; | 192 }; |
181 } | 193 } |
182 } | 194 } |
183 | 195 |
184 /** | 196 /** |
185 * Helper function for defineProperty that returns the setter of the right | 197 * Helper function for defineProperty that returns the setter of the right |
186 * kind. | 198 * kind. |
187 * @param {string} name The name of the property we are defining the setter | 199 * @param {string} name The name of the property we are defining the setter |
188 * for. | 200 * for. |
189 * @param {cr.PropertyKind} kind The kind of property we are getting the | 201 * @param {cr.PropertyKind} kind The kind of property we are getting the |
(...skipping 10 matching lines...) Expand all Loading... |
200 var oldValue = this[privateName]; | 212 var oldValue = this[privateName]; |
201 if (value !== oldValue) { | 213 if (value !== oldValue) { |
202 this[privateName] = value; | 214 this[privateName] = value; |
203 if (opt_setHook) | 215 if (opt_setHook) |
204 opt_setHook.call(this, value, oldValue); | 216 opt_setHook.call(this, value, oldValue); |
205 dispatchPropertyChange(this, name, value, oldValue); | 217 dispatchPropertyChange(this, name, value, oldValue); |
206 } | 218 } |
207 }; | 219 }; |
208 | 220 |
209 case PropertyKind.ATTR: | 221 case PropertyKind.ATTR: |
| 222 var attributeName = getAttributeName(name); |
210 return function(value) { | 223 return function(value) { |
211 var oldValue = this[name]; | 224 var oldValue = this[attributeName]; |
212 if (value !== oldValue) { | 225 if (value !== oldValue) { |
213 if (value == undefined) | 226 if (value == undefined) |
214 this.removeAttribute(name); | 227 this.removeAttribute(attributeName); |
215 else | 228 else |
216 this.setAttribute(name, value); | 229 this.setAttribute(attributeName, value); |
217 if (opt_setHook) | 230 if (opt_setHook) |
218 opt_setHook.call(this, value, oldValue); | 231 opt_setHook.call(this, value, oldValue); |
219 dispatchPropertyChange(this, name, value, oldValue); | 232 dispatchPropertyChange(this, name, value, oldValue); |
220 } | 233 } |
221 }; | 234 }; |
222 | 235 |
223 case PropertyKind.BOOL_ATTR: | 236 case PropertyKind.BOOL_ATTR: |
| 237 var attributeName = getAttributeName(name); |
224 return function(value) { | 238 return function(value) { |
225 var oldValue = this[name]; | 239 var oldValue = this[attributeName]; |
226 if (value !== oldValue) { | 240 if (value !== oldValue) { |
227 if (value) | 241 if (value) |
228 this.setAttribute(name, name); | 242 this.setAttribute(attributeName, name); |
229 else | 243 else |
230 this.removeAttribute(name); | 244 this.removeAttribute(attributeName); |
231 if (opt_setHook) | 245 if (opt_setHook) |
232 opt_setHook.call(this, value, oldValue); | 246 opt_setHook.call(this, value, oldValue); |
233 dispatchPropertyChange(this, name, value, oldValue); | 247 dispatchPropertyChange(this, name, value, oldValue); |
234 } | 248 } |
235 }; | 249 }; |
236 } | 250 } |
237 } | 251 } |
238 | 252 |
239 /** | 253 /** |
240 * Defines a property on an object. When the setter changes the value a | 254 * Defines a property on an object. When the setter changes the value a |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 * The document that we are currently using. | 386 * The document that we are currently using. |
373 * @type {!Document} | 387 * @type {!Document} |
374 */ | 388 */ |
375 get doc() { | 389 get doc() { |
376 return doc; | 390 return doc; |
377 }, | 391 }, |
378 withDoc: withDoc, | 392 withDoc: withDoc, |
379 Event: CrEvent | 393 Event: CrEvent |
380 }; | 394 }; |
381 })(); | 395 })(); |
OLD | NEW |