Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(453)

Side by Side Diff: chrome/browser/resources/shared/js/cr.js

Issue 6246078: Add an optional set_hook argument to defineProperty. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/chrome/browser/resources
Patch Set: Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 } 163 }
164 } 164 }
165 165
166 /** 166 /**
167 * Helper function for defineProperty that returns the setter of the right 167 * Helper function for defineProperty that returns the setter of the right
168 * kind. 168 * kind.
169 * @param {string} name The name of the property we are defining the setter 169 * @param {string} name The name of the property we are defining the setter
170 * for. 170 * for.
171 * @param {cr.PropertyKind} kind The kind of property we are getting the 171 * @param {cr.PropertyKind} kind The kind of property we are getting the
172 * setter for. 172 * setter for.
173 * @param {function():void} opt_set_hook A function to run after the property
174 * is set, but before the propertyChange event is fired.
173 * @return {function(*):void} The function to use as a setter. 175 * @return {function(*):void} The function to use as a setter.
174 */ 176 */
175 function getSetter(name, kind) { 177 function getSetter(name, kind, opt_set_hook) {
arv (Not doing code reviews) 2011/02/03 21:52:08 opt_setHook
nduca 2011/02/04 01:31:32 Done.
176 switch (kind) { 178 switch (kind) {
177 case PropertyKind.JS: 179 case PropertyKind.JS:
178 var privateName = name + '_'; 180 var privateName = name + '_';
179 return function(value) { 181 return function(value) {
180 var oldValue = this[privateName]; 182 var oldValue = this[privateName];
181 if (value !== oldValue) { 183 if (value !== oldValue) {
182 this[privateName] = value; 184 this[privateName] = value;
185 if (opt_set_hook)
186 opt_set_hook.apply(this);
arv (Not doing code reviews) 2011/02/03 21:52:08 I was thinking that it should pass the new value f
nduca 2011/02/04 01:31:32 Done.
183 dispatchPropertyChange(this, name, value, oldValue); 187 dispatchPropertyChange(this, name, value, oldValue);
184 } 188 }
185 }; 189 };
186 190
187 case PropertyKind.ATTR: 191 case PropertyKind.ATTR:
188 return function(value) { 192 return function(value) {
189 var oldValue = this[name]; 193 var oldValue = this[name];
190 if (value !== oldValue) { 194 if (value !== oldValue) {
191 this.setAttribute(name, value); 195 this.setAttribute(name, value);
196 if (opt_set_hook)
197 opt_set_hook.apply(this);
192 dispatchPropertyChange(this, name, value, oldValue); 198 dispatchPropertyChange(this, name, value, oldValue);
193 } 199 }
194 }; 200 };
195 201
196 case PropertyKind.BOOL_ATTR: 202 case PropertyKind.BOOL_ATTR:
197 return function(value) { 203 return function(value) {
198 var oldValue = this[name]; 204 var oldValue = this[name];
199 if (value !== oldValue) { 205 if (value !== oldValue) {
200 if (value) 206 if (value)
201 this.setAttribute(name, name); 207 this.setAttribute(name, name);
202 else 208 else
203 this.removeAttribute(name); 209 this.removeAttribute(name);
210 if (opt_set_hook)
211 opt_set_hook.apply(this);
204 dispatchPropertyChange(this, name, value, oldValue); 212 dispatchPropertyChange(this, name, value, oldValue);
205 } 213 }
206 }; 214 };
207 } 215 }
208 } 216 }
209 217
210 /** 218 /**
211 * Defines a property on an object. When the setter changes the value a 219 * 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. 220 * property change event with the type {@code name + 'Change'} is fired.
213 * @param {!Object} The object to define the property for. 221 * @param {!Object} The object to define the property for.
214 * @param {string} The name of the property. 222 * @param {string} The name of the property.
215 * @param {cr.PropertyKind=} opt_kind What kind of underlying storage to use. 223 * @param {cr.PropertyKind=} opt_kind What kind of underlying storage to use.
216 * @param {*} opt_defaultValue The default value. 224 * @param {*} opt_default The default value.
225 * @param {function():void} opt_set_hook A function to run after the property
226 * is set, but before the propertyChange event is fired.
217 */ 227 */
218 function defineProperty(obj, name, opt_kind, opt_default) { 228 function defineProperty(obj, name, opt_kind, opt_default, opt_set_hook) {
219 if (typeof obj == 'function') 229 if (typeof obj == 'function')
220 obj = obj.prototype; 230 obj = obj.prototype;
221 231
222 var kind = opt_kind || PropertyKind.JS; 232 var kind = opt_kind || PropertyKind.JS;
223 233
224 if (!obj.__lookupGetter__(name)) { 234 if (!obj.__lookupGetter__(name)) {
225 // For js properties we set the default value on the prototype. 235 // For js properties we set the default value on the prototype.
226 if (kind == PropertyKind.JS && arguments.length > 3) { 236 if (kind == PropertyKind.JS && arguments.length > 3) {
227 var privateName = name + '_'; 237 var privateName = name + '_';
228 obj[privateName] = opt_default; 238 obj[privateName] = opt_default;
229 } 239 }
230 obj.__defineGetter__(name, getGetter(name, kind, opt_default)); 240 obj.__defineGetter__(name, getGetter(name, kind, opt_default));
231 } 241 }
232 242
233 if (!obj.__lookupSetter__(name)) { 243 if (!obj.__lookupSetter__(name)) {
234 obj.__defineSetter__(name, getSetter(name, kind)); 244 obj.__defineSetter__(name, getSetter(name, kind, opt_set_hook));
235 } 245 }
236 } 246 }
237 247
238 /** 248 /**
239 * Counter for use with createUid 249 * Counter for use with createUid
240 */ 250 */
241 var uidCounter = 1; 251 var uidCounter = 1;
242 252
243 /** 253 /**
244 * @return {number} A new unique ID. 254 * @return {number} A new unique ID.
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 * The document that we are currently using. 355 * The document that we are currently using.
346 * @type {!Document} 356 * @type {!Document}
347 */ 357 */
348 get doc() { 358 get doc() {
349 return doc; 359 return doc;
350 }, 360 },
351 withDoc: withDoc, 361 withDoc: withDoc,
352 Event: CrEvent 362 Event: CrEvent
353 }; 363 };
354 })(); 364 })();
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698