Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 /** | 5 /** |
| 6 * The global object. | 6 * The global object. |
| 7 * @type {!Object} | 7 * @type {!Object} |
| 8 * @const | 8 * @const |
| 9 */ | 9 */ |
| 10 var global = this; | 10 var global = this; |
| 11 | 11 |
| 12 /** Platform, package, object property, and Event support. **/ | 12 /** Platform, package, object property, and Event support. **/ |
| 13 this.cr = (function() { | 13 var cr = global.cr = (function() { |
| 14 'use strict'; | 14 'use strict'; |
| 15 | 15 |
| 16 /** | 16 /** |
| 17 * Builds an object structure for the provided namespace path, | 17 * Builds an object structure for the provided namespace path, |
| 18 * ensuring that names that already exist are not overwritten. For | 18 * ensuring that names that already exist are not overwritten. For |
| 19 * example: | 19 * example: |
| 20 * "a.b.c" -> a = {};a.b={};a.b.c={}; | 20 * "a.b.c" -> a = {};a.b={};a.b.c={}; |
| 21 * @param {string} name Name of the object that this file defines. | 21 * @param {string} name Name of the object that this file defines. |
| 22 * @param {*=} opt_object The object to expose at the end of the path. | 22 * @param {*=} opt_object The object to expose at the end of the path. |
| 23 * @param {Object=} opt_objectToExportTo The object to add the path to; | 23 * @param {Object=} opt_objectToExportTo The object to add the path to; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 61 * attribute name. | 61 * attribute name. |
| 62 * @param {string} jsName The javascript camelCase property name. | 62 * @param {string} jsName The javascript camelCase property name. |
| 63 * @return {string} The equivalent hyphenated-lower-case attribute name. | 63 * @return {string} The equivalent hyphenated-lower-case attribute name. |
| 64 */ | 64 */ |
| 65 function getAttributeName(jsName) { | 65 function getAttributeName(jsName) { |
| 66 return jsName.replace(/([A-Z])/g, '-$1').toLowerCase(); | 66 return jsName.replace(/([A-Z])/g, '-$1').toLowerCase(); |
| 67 } | 67 } |
| 68 | 68 |
| 69 /** | 69 /** |
| 70 * The kind of property to define in {@code defineProperty}. | 70 * The kind of property to define in {@code defineProperty}. |
| 71 * @enum {number} | 71 * @enum {string} |
| 72 * @const | 72 * @const |
| 73 */ | 73 */ |
| 74 var PropertyKind = { | 74 var PropertyKind = { |
| 75 /** | 75 /** |
| 76 * Plain old JS property where the backing data is stored as a "private" | 76 * Plain old JS property where the backing data is stored as a "private" |
| 77 * field on the object. | 77 * field on the object. |
| 78 */ | 78 */ |
| 79 JS: 'js', | 79 JS: 'js', |
| 80 | 80 |
| 81 /** | 81 /** |
| 82 * The property backing data is stored as an attribute on an element. | 82 * The property backing data is stored as an attribute on an element. |
| 83 */ | 83 */ |
| 84 ATTR: 'attr', | 84 ATTR: 'attr', |
| 85 | 85 |
| 86 /** | 86 /** |
| 87 * The property backing data is stored as an attribute on an element. If the | 87 * The property backing data is stored as an attribute on an element. If the |
| 88 * element has the attribute then the value is true. | 88 * element has the attribute then the value is true. |
| 89 */ | 89 */ |
| 90 BOOL_ATTR: 'boolAttr' | 90 BOOL_ATTR: 'boolAttr' |
| 91 }; | 91 }; |
| 92 | 92 |
| 93 /** | 93 /** |
| 94 * Helper function for defineProperty that returns the getter to use for the | 94 * Helper function for defineProperty that returns the getter to use for the |
| 95 * property. | 95 * property. |
| 96 * @param {string} name The name of the property. | 96 * @param {string} name The name of the property. |
| 97 * @param {cr.PropertyKind} kind The kind of the property. | 97 * @param {PropertyKind} kind The kind of the property. |
| 98 * @return {function():*} The getter for the property. | 98 * @return {Function|undefined} The getter for the property. |
|
arv (Not doing code reviews)
2014/07/16 18:33:02
This never returns undefined. Maybe add a `throw '
Dan Beam
2014/07/19 02:28:40
Done.
| |
| 99 */ | 99 */ |
| 100 function getGetter(name, kind) { | 100 function getGetter(name, kind) { |
| 101 switch (kind) { | 101 switch (kind) { |
| 102 case PropertyKind.JS: | 102 case PropertyKind.JS: |
| 103 var privateName = name + '_'; | 103 var privateName = name + '_'; |
| 104 return function() { | 104 return function() { |
| 105 return this[privateName]; | 105 return this[privateName]; |
| 106 }; | 106 }; |
| 107 case PropertyKind.ATTR: | 107 case PropertyKind.ATTR: |
| 108 var attributeName = getAttributeName(name); | 108 var attributeName = getAttributeName(name); |
| 109 return function() { | 109 return function() { |
| 110 return this.getAttribute(attributeName); | 110 return this.getAttribute(attributeName); |
| 111 }; | 111 }; |
| 112 case PropertyKind.BOOL_ATTR: | 112 case PropertyKind.BOOL_ATTR: |
| 113 var attributeName = getAttributeName(name); | 113 var attributeName = getAttributeName(name); |
| 114 return function() { | 114 return function() { |
| 115 return this.hasAttribute(attributeName); | 115 return this.hasAttribute(attributeName); |
| 116 }; | 116 }; |
| 117 } | 117 } |
| 118 } | 118 } |
| 119 | 119 |
| 120 /** | 120 /** |
| 121 * Helper function for defineProperty that returns the setter of the right | 121 * Helper function for defineProperty that returns the setter of the right |
| 122 * kind. | 122 * kind. |
| 123 * @param {string} name The name of the property we are defining the setter | 123 * @param {string} name The name of the property we are defining the setter |
| 124 * for. | 124 * for. |
| 125 * @param {cr.PropertyKind} kind The kind of property we are getting the | 125 * @param {PropertyKind} kind The kind of property we are getting the |
| 126 * setter for. | 126 * setter for. |
| 127 * @param {function(*):void} opt_setHook A function to run after the property | 127 * @param {Function=} opt_setHook A function to run after the property is set, |
| 128 * is set, but before the propertyChange event is fired. | 128 * but before the propertyChange event is fired. |
| 129 * @return {function(*):void} The function to use as a setter. | 129 * @return {Function|undefined} The function to use as a setter. |
|
arv (Not doing code reviews)
2014/07/16 18:33:02
same
Dan Beam
2014/07/19 02:28:40
Done.
| |
| 130 */ | 130 */ |
| 131 function getSetter(name, kind, opt_setHook) { | 131 function getSetter(name, kind, opt_setHook) { |
| 132 switch (kind) { | 132 switch (kind) { |
| 133 case PropertyKind.JS: | 133 case PropertyKind.JS: |
| 134 var privateName = name + '_'; | 134 var privateName = name + '_'; |
| 135 return function(value) { | 135 return function(value) { |
| 136 var oldValue = this[name]; | 136 var oldValue = this[name]; |
| 137 if (value !== oldValue) { | 137 if (value !== oldValue) { |
| 138 this[privateName] = value; | 138 this[privateName] = value; |
| 139 if (opt_setHook) | 139 if (opt_setHook) |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 172 } | 172 } |
| 173 }; | 173 }; |
| 174 } | 174 } |
| 175 } | 175 } |
| 176 | 176 |
| 177 /** | 177 /** |
| 178 * Defines a property on an object. When the setter changes the value a | 178 * Defines a property on an object. When the setter changes the value a |
| 179 * property change event with the type {@code name + 'Change'} is fired. | 179 * property change event with the type {@code name + 'Change'} is fired. |
| 180 * @param {!Object} obj The object to define the property for. | 180 * @param {!Object} obj The object to define the property for. |
| 181 * @param {string} name The name of the property. | 181 * @param {string} name The name of the property. |
| 182 * @param {cr.PropertyKind=} opt_kind What kind of underlying storage to use. | 182 * @param {PropertyKind=} opt_kind What kind of underlying storage to use. |
| 183 * @param {function(*):void} opt_setHook A function to run after the | 183 * @param {function(*):void=} opt_setHook A function to run after the |
| 184 * property is set, but before the propertyChange event is fired. | 184 * property is set, but before the propertyChange event is fired. |
| 185 * @suppress {checkTypes} | |
| 185 */ | 186 */ |
| 186 function defineProperty(obj, name, opt_kind, opt_setHook) { | 187 function defineProperty(obj, name, opt_kind, opt_setHook) { |
| 187 if (typeof obj == 'function') | 188 if (typeof obj == 'function') |
| 188 obj = obj.prototype; | 189 obj = obj.prototype; |
| 189 | 190 |
| 190 var kind = opt_kind || PropertyKind.JS; | 191 var kind = /** @type {PropertyKind} */ (opt_kind || PropertyKind.JS); |
|
arv (Not doing code reviews)
2014/07/16 18:33:02
so the @suppress didn't work?
Dan Beam
2014/07/19 02:28:40
right, removed @suppress
| |
| 191 | 192 |
| 192 if (!obj.__lookupGetter__(name)) | 193 if (!obj.__lookupGetter__(name)) |
| 193 obj.__defineGetter__(name, getGetter(name, kind)); | 194 obj.__defineGetter__(name, getGetter(name, kind)); |
| 194 | 195 |
| 195 if (!obj.__lookupSetter__(name)) | 196 if (!obj.__lookupSetter__(name)) |
| 196 obj.__defineSetter__(name, getSetter(name, kind, opt_setHook)); | 197 obj.__defineSetter__(name, getSetter(name, kind, opt_setHook)); |
| 197 } | 198 } |
| 198 | 199 |
| 199 /** | 200 /** |
| 200 * Counter for use with createUid | 201 * Counter for use with createUid |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 351 PropertyKind: PropertyKind | 352 PropertyKind: PropertyKind |
| 352 }; | 353 }; |
| 353 })(); | 354 })(); |
| 354 | 355 |
| 355 | 356 |
| 356 /** | 357 /** |
| 357 * TODO(kgr): Move this to another file which is to be loaded last. | 358 * TODO(kgr): Move this to another file which is to be loaded last. |
| 358 * This will be done as part of future work to make this code pre-compilable. | 359 * This will be done as part of future work to make this code pre-compilable. |
| 359 */ | 360 */ |
| 360 cr.initialize(); | 361 cr.initialize(); |
| OLD | NEW |