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 <include src="assert.js"> |
| 6 |
5 /** | 7 /** |
6 * The global object. | 8 * The global object. |
7 * @type {!Object} | 9 * @type {!Object} |
8 * @const | 10 * @const |
9 */ | 11 */ |
10 var global = this; | 12 var global = this; |
11 | 13 |
12 /** Platform, package, object property, and Event support. **/ | 14 /** Platform, package, object property, and Event support. **/ |
13 this.cr = (function() { | 15 var cr = (function() { |
14 'use strict'; | 16 'use strict'; |
15 | 17 |
16 /** | 18 /** |
17 * Builds an object structure for the provided namespace path, | 19 * Builds an object structure for the provided namespace path, |
18 * ensuring that names that already exist are not overwritten. For | 20 * ensuring that names that already exist are not overwritten. For |
19 * example: | 21 * example: |
20 * "a.b.c" -> a = {};a.b={};a.b.c={}; | 22 * "a.b.c" -> a = {};a.b={};a.b.c={}; |
21 * @param {string} name Name of the object that this file defines. | 23 * @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. | 24 * @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; | 25 * @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. | 63 * attribute name. |
62 * @param {string} jsName The javascript camelCase property name. | 64 * @param {string} jsName The javascript camelCase property name. |
63 * @return {string} The equivalent hyphenated-lower-case attribute name. | 65 * @return {string} The equivalent hyphenated-lower-case attribute name. |
64 */ | 66 */ |
65 function getAttributeName(jsName) { | 67 function getAttributeName(jsName) { |
66 return jsName.replace(/([A-Z])/g, '-$1').toLowerCase(); | 68 return jsName.replace(/([A-Z])/g, '-$1').toLowerCase(); |
67 } | 69 } |
68 | 70 |
69 /** | 71 /** |
70 * The kind of property to define in {@code defineProperty}. | 72 * The kind of property to define in {@code defineProperty}. |
71 * @enum {number} | 73 * @enum {string} |
72 * @const | 74 * @const |
73 */ | 75 */ |
74 var PropertyKind = { | 76 var PropertyKind = { |
75 /** | 77 /** |
76 * Plain old JS property where the backing data is stored as a "private" | 78 * Plain old JS property where the backing data is stored as a "private" |
77 * field on the object. | 79 * field on the object. |
78 */ | 80 */ |
79 JS: 'js', | 81 JS: 'js', |
80 | 82 |
81 /** | 83 /** |
82 * The property backing data is stored as an attribute on an element. | 84 * The property backing data is stored as an attribute on an element. |
83 */ | 85 */ |
84 ATTR: 'attr', | 86 ATTR: 'attr', |
85 | 87 |
86 /** | 88 /** |
87 * The property backing data is stored as an attribute on an element. If the | 89 * The property backing data is stored as an attribute on an element. If the |
88 * element has the attribute then the value is true. | 90 * element has the attribute then the value is true. |
89 */ | 91 */ |
90 BOOL_ATTR: 'boolAttr' | 92 BOOL_ATTR: 'boolAttr' |
91 }; | 93 }; |
92 | 94 |
93 /** | 95 /** |
94 * Helper function for defineProperty that returns the getter to use for the | 96 * Helper function for defineProperty that returns the getter to use for the |
95 * property. | 97 * property. |
96 * @param {string} name The name of the property. | 98 * @param {string} name The name of the property. |
97 * @param {cr.PropertyKind} kind The kind of the property. | 99 * @param {PropertyKind} kind The kind of the property. |
98 * @return {function():*} The getter for the property. | 100 * @return {function():*} The getter for the property. |
99 */ | 101 */ |
100 function getGetter(name, kind) { | 102 function getGetter(name, kind) { |
101 switch (kind) { | 103 switch (kind) { |
102 case PropertyKind.JS: | 104 case PropertyKind.JS: |
103 var privateName = name + '_'; | 105 var privateName = name + '_'; |
104 return function() { | 106 return function() { |
105 return this[privateName]; | 107 return this[privateName]; |
106 }; | 108 }; |
107 case PropertyKind.ATTR: | 109 case PropertyKind.ATTR: |
108 var attributeName = getAttributeName(name); | 110 var attributeName = getAttributeName(name); |
109 return function() { | 111 return function() { |
110 return this.getAttribute(attributeName); | 112 return this.getAttribute(attributeName); |
111 }; | 113 }; |
112 case PropertyKind.BOOL_ATTR: | 114 case PropertyKind.BOOL_ATTR: |
113 var attributeName = getAttributeName(name); | 115 var attributeName = getAttributeName(name); |
114 return function() { | 116 return function() { |
115 return this.hasAttribute(attributeName); | 117 return this.hasAttribute(attributeName); |
116 }; | 118 }; |
117 } | 119 } |
| 120 |
| 121 assertNotReached(); |
| 122 return function() {}; |
118 } | 123 } |
119 | 124 |
120 /** | 125 /** |
121 * Helper function for defineProperty that returns the setter of the right | 126 * Helper function for defineProperty that returns the setter of the right |
122 * kind. | 127 * kind. |
123 * @param {string} name The name of the property we are defining the setter | 128 * @param {string} name The name of the property we are defining the setter |
124 * for. | 129 * for. |
125 * @param {cr.PropertyKind} kind The kind of property we are getting the | 130 * @param {PropertyKind} kind The kind of property we are getting the |
126 * setter for. | 131 * setter for. |
127 * @param {function(*):void} opt_setHook A function to run after the property | 132 * @param {function():void=} opt_setHook A function to run after the property |
128 * is set, but before the propertyChange event is fired. | 133 * is set, but before the propertyChange event is fired. |
129 * @return {function(*):void} The function to use as a setter. | 134 * @return {function(*):void} The function to use as a setter. |
130 */ | 135 */ |
131 function getSetter(name, kind, opt_setHook) { | 136 function getSetter(name, kind, opt_setHook) { |
132 switch (kind) { | 137 switch (kind) { |
133 case PropertyKind.JS: | 138 case PropertyKind.JS: |
134 var privateName = name + '_'; | 139 var privateName = name + '_'; |
135 return function(value) { | 140 return function(value) { |
136 var oldValue = this[name]; | 141 var oldValue = this[name]; |
137 if (value !== oldValue) { | 142 if (value !== oldValue) { |
(...skipping 27 matching lines...) Expand all Loading... |
165 if (value) | 170 if (value) |
166 this.setAttribute(attributeName, name); | 171 this.setAttribute(attributeName, name); |
167 else | 172 else |
168 this.removeAttribute(attributeName); | 173 this.removeAttribute(attributeName); |
169 if (opt_setHook) | 174 if (opt_setHook) |
170 opt_setHook.call(this, value, oldValue); | 175 opt_setHook.call(this, value, oldValue); |
171 dispatchPropertyChange(this, name, value, oldValue); | 176 dispatchPropertyChange(this, name, value, oldValue); |
172 } | 177 } |
173 }; | 178 }; |
174 } | 179 } |
| 180 |
| 181 assertNotReached(); |
| 182 return function() {}; |
175 } | 183 } |
176 | 184 |
177 /** | 185 /** |
178 * Defines a property on an object. When the setter changes the value a | 186 * 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. | 187 * property change event with the type {@code name + 'Change'} is fired. |
180 * @param {!Object} obj The object to define the property for. | 188 * @param {!Object} obj The object to define the property for. |
181 * @param {string} name The name of the property. | 189 * @param {string} name The name of the property. |
182 * @param {cr.PropertyKind=} opt_kind What kind of underlying storage to use. | 190 * @param {PropertyKind=} opt_kind What kind of underlying storage to use. |
183 * @param {function(*):void} opt_setHook A function to run after the | 191 * @param {function(*):void=} opt_setHook A function to run after the |
184 * property is set, but before the propertyChange event is fired. | 192 * property is set, but before the propertyChange event is fired. |
185 */ | 193 */ |
186 function defineProperty(obj, name, opt_kind, opt_setHook) { | 194 function defineProperty(obj, name, opt_kind, opt_setHook) { |
187 if (typeof obj == 'function') | 195 if (typeof obj == 'function') |
188 obj = obj.prototype; | 196 obj = obj.prototype; |
189 | 197 |
190 var kind = opt_kind || PropertyKind.JS; | 198 var kind = /** @type {PropertyKind} */ (opt_kind || PropertyKind.JS); |
191 | 199 |
192 if (!obj.__lookupGetter__(name)) | 200 if (!obj.__lookupGetter__(name)) |
193 obj.__defineGetter__(name, getGetter(name, kind)); | 201 obj.__defineGetter__(name, getGetter(name, kind)); |
194 | 202 |
195 if (!obj.__lookupSetter__(name)) | 203 if (!obj.__lookupSetter__(name)) |
196 obj.__defineSetter__(name, getSetter(name, kind, opt_setHook)); | 204 obj.__defineSetter__(name, getSetter(name, kind, opt_setHook)); |
197 } | 205 } |
198 | 206 |
199 /** | 207 /** |
200 * Counter for use with createUid | 208 * Counter for use with createUid |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 PropertyKind: PropertyKind | 353 PropertyKind: PropertyKind |
346 }; | 354 }; |
347 })(); | 355 })(); |
348 | 356 |
349 | 357 |
350 /** | 358 /** |
351 * TODO(kgr): Move this to another file which is to be loaded last. | 359 * TODO(kgr): Move this to another file which is to be loaded last. |
352 * This will be done as part of future work to make this code pre-compilable. | 360 * This will be done as part of future work to make this code pre-compilable. |
353 */ | 361 */ |
354 cr.initialize(); | 362 cr.initialize(); |
OLD | NEW |