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 // Convert common case aB to a-b, and ABc to a-bc to cover acronyms such as | |
144 // getHTMLElement. | |
145 return jsName.replace(/([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))/g, '$&-') | |
arv1
2011/11/01 17:46:24
This looks overly complex. The following works jus
flackr
2011/11/01 17:55:46
Done. I guess worrying about handling uppercase ac
| |
146 .toLowerCase(); | |
147 } | |
148 | |
149 /** | |
137 * The kind of property to define in {@code defineProperty}. | 150 * The kind of property to define in {@code defineProperty}. |
138 * @enum {number} | 151 * @enum {number} |
139 */ | 152 */ |
140 const PropertyKind = { | 153 const PropertyKind = { |
141 /** | 154 /** |
142 * Plain old JS property where the backing data is stored as a "private" | 155 * Plain old JS property where the backing data is stored as a "private" |
143 * field on the object. | 156 * field on the object. |
144 */ | 157 */ |
145 JS: 'js', | 158 JS: 'js', |
146 | 159 |
(...skipping 17 matching lines...) Expand all Loading... | |
164 * @return {function():*} The getter for the property. | 177 * @return {function():*} The getter for the property. |
165 */ | 178 */ |
166 function getGetter(name, kind) { | 179 function getGetter(name, kind) { |
167 switch (kind) { | 180 switch (kind) { |
168 case PropertyKind.JS: | 181 case PropertyKind.JS: |
169 var privateName = name + '_'; | 182 var privateName = name + '_'; |
170 return function() { | 183 return function() { |
171 return this[privateName]; | 184 return this[privateName]; |
172 }; | 185 }; |
173 case PropertyKind.ATTR: | 186 case PropertyKind.ATTR: |
187 var attributeName = getAttributeName(name); | |
174 return function() { | 188 return function() { |
175 return this.getAttribute(name); | 189 return this.getAttribute(attributeName); |
176 }; | 190 }; |
177 case PropertyKind.BOOL_ATTR: | 191 case PropertyKind.BOOL_ATTR: |
192 var attributeName = getAttributeName(name); | |
178 return function() { | 193 return function() { |
179 return this.hasAttribute(name); | 194 return this.hasAttribute(attributeName); |
180 }; | 195 }; |
181 } | 196 } |
182 } | 197 } |
183 | 198 |
184 /** | 199 /** |
185 * Helper function for defineProperty that returns the setter of the right | 200 * Helper function for defineProperty that returns the setter of the right |
186 * kind. | 201 * kind. |
187 * @param {string} name The name of the property we are defining the setter | 202 * @param {string} name The name of the property we are defining the setter |
188 * for. | 203 * for. |
189 * @param {cr.PropertyKind} kind The kind of property we are getting the | 204 * @param {cr.PropertyKind} kind The kind of property we are getting the |
(...skipping 10 matching lines...) Expand all Loading... | |
200 var oldValue = this[privateName]; | 215 var oldValue = this[privateName]; |
201 if (value !== oldValue) { | 216 if (value !== oldValue) { |
202 this[privateName] = value; | 217 this[privateName] = value; |
203 if (opt_setHook) | 218 if (opt_setHook) |
204 opt_setHook.call(this, value, oldValue); | 219 opt_setHook.call(this, value, oldValue); |
205 dispatchPropertyChange(this, name, value, oldValue); | 220 dispatchPropertyChange(this, name, value, oldValue); |
206 } | 221 } |
207 }; | 222 }; |
208 | 223 |
209 case PropertyKind.ATTR: | 224 case PropertyKind.ATTR: |
225 var attributeName = getAttributeName(name); | |
210 return function(value) { | 226 return function(value) { |
211 var oldValue = this[name]; | 227 var oldValue = this[attributeName]; |
212 if (value !== oldValue) { | 228 if (value !== oldValue) { |
213 if (value == undefined) | 229 if (value == undefined) |
214 this.removeAttribute(name); | 230 this.removeAttribute(attributeName); |
215 else | 231 else |
216 this.setAttribute(name, value); | 232 this.setAttribute(attributeName, value); |
217 if (opt_setHook) | 233 if (opt_setHook) |
218 opt_setHook.call(this, value, oldValue); | 234 opt_setHook.call(this, value, oldValue); |
219 dispatchPropertyChange(this, name, value, oldValue); | 235 dispatchPropertyChange(this, name, value, oldValue); |
220 } | 236 } |
221 }; | 237 }; |
222 | 238 |
223 case PropertyKind.BOOL_ATTR: | 239 case PropertyKind.BOOL_ATTR: |
240 var attributeName = getAttributeName(name); | |
224 return function(value) { | 241 return function(value) { |
225 var oldValue = this[name]; | 242 var oldValue = this[attributeName]; |
226 if (value !== oldValue) { | 243 if (value !== oldValue) { |
227 if (value) | 244 if (value) |
228 this.setAttribute(name, name); | 245 this.setAttribute(attributeName, name); |
229 else | 246 else |
230 this.removeAttribute(name); | 247 this.removeAttribute(attributeName); |
231 if (opt_setHook) | 248 if (opt_setHook) |
232 opt_setHook.call(this, value, oldValue); | 249 opt_setHook.call(this, value, oldValue); |
233 dispatchPropertyChange(this, name, value, oldValue); | 250 dispatchPropertyChange(this, name, value, oldValue); |
234 } | 251 } |
235 }; | 252 }; |
236 } | 253 } |
237 } | 254 } |
238 | 255 |
239 /** | 256 /** |
240 * Defines a property on an object. When the setter changes the value a | 257 * 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. | 389 * The document that we are currently using. |
373 * @type {!Document} | 390 * @type {!Document} |
374 */ | 391 */ |
375 get doc() { | 392 get doc() { |
376 return doc; | 393 return doc; |
377 }, | 394 }, |
378 withDoc: withDoc, | 395 withDoc: withDoc, |
379 Event: CrEvent | 396 Event: CrEvent |
380 }; | 397 }; |
381 })(); | 398 })(); |
OLD | NEW |