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

Unified 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: Fix jsdoc. Move selectedIndex default to proto. 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/browser/resources/shared/js/cr/ui/menu.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/resources/shared/js/cr.js
diff --git a/chrome/browser/resources/shared/js/cr.js b/chrome/browser/resources/shared/js/cr.js
index 90df206f7d9a5c3404558fb188d6edab66dc24b4..93c83b2e0d69be0434c8fbf8875afcad20428ae1 100644
--- a/chrome/browser/resources/shared/js/cr.js
+++ b/chrome/browser/resources/shared/js/cr.js
@@ -130,11 +130,9 @@ const cr = (function() {
* property.
* @param {string} name
* @param {cr.PropertyKind} kind
- * @param {*} defaultValue The default value. This is only used for the ATTR
- * kind.
* @return {function():*} The getter for the property.
*/
- function getGetter(name, kind, defaultValue) {
+ function getGetter(name, kind) {
switch (kind) {
case PropertyKind.JS:
var privateName = name + '_';
@@ -142,21 +140,10 @@ const cr = (function() {
return this[privateName];
};
case PropertyKind.ATTR:
- // For attr with default value we return the default value if the
- // element is missing the attribute.
- if (defaultValue == undefined) {
- return function() {
- return this.getAttribute(name);
- };
- } else {
- return function() {
- // WebKit uses null for non existant attributes.
- var value = this.getAttribute(name);
- return value !== null ? value : defaultValue;
- };
- }
+ return function() {
+ return this.getAttribute(name);
+ };
case PropertyKind.BOOL_ATTR:
- // Boolean attributes don't support default values.
return function() {
return this.hasAttribute(name);
};
@@ -170,9 +157,11 @@ const cr = (function() {
* for.
* @param {cr.PropertyKind} kind The kind of property we are getting the
* setter for.
+ * @param {function(*):void} opt_setHook A function to run after the property
+ * is set, but before the propertyChange event is fired.
* @return {function(*):void} The function to use as a setter.
*/
- function getSetter(name, kind) {
+ function getSetter(name, kind, opt_setHook) {
switch (kind) {
case PropertyKind.JS:
var privateName = name + '_';
@@ -180,6 +169,8 @@ const cr = (function() {
var oldValue = this[privateName];
if (value !== oldValue) {
this[privateName] = value;
+ if (opt_setHook)
+ opt_setHook.call(this, value, oldValue);
dispatchPropertyChange(this, name, value, oldValue);
}
};
@@ -189,6 +180,8 @@ const cr = (function() {
var oldValue = this[name];
if (value !== oldValue) {
this.setAttribute(name, value);
+ if (opt_setHook)
+ opt_setHook.call(this, value, oldValue);
dispatchPropertyChange(this, name, value, oldValue);
}
};
@@ -201,6 +194,8 @@ const cr = (function() {
this.setAttribute(name, name);
else
this.removeAttribute(name);
+ if (opt_setHook)
+ opt_setHook.call(this, value, oldValue);
dispatchPropertyChange(this, name, value, oldValue);
}
};
@@ -213,25 +208,21 @@ const cr = (function() {
* @param {!Object} obj The object to define the property for.
* @param {string} name The name of the property.
* @param {cr.PropertyKind=} opt_kind What kind of underlying storage to use.
- * @param {*} opt_default The default value.
+ * @param {function(*):void} opt_setHook A function to run after the
+ * property is set, but before the propertyChange event is fired.
*/
- function defineProperty(obj, name, opt_kind, opt_default) {
+ function defineProperty(obj, name, opt_kind, opt_setHook) {
if (typeof obj == 'function')
obj = obj.prototype;
var kind = opt_kind || PropertyKind.JS;
if (!obj.__lookupGetter__(name)) {
- // For js properties we set the default value on the prototype.
- if (kind == PropertyKind.JS && arguments.length > 3) {
- var privateName = name + '_';
- obj[privateName] = opt_default;
- }
- obj.__defineGetter__(name, getGetter(name, kind, opt_default));
+ obj.__defineGetter__(name, getGetter(name, kind));
}
if (!obj.__lookupSetter__(name)) {
- obj.__defineSetter__(name, getSetter(name, kind));
+ obj.__defineSetter__(name, getSetter(name, kind, opt_setHook));
}
}
« no previous file with comments | « no previous file | chrome/browser/resources/shared/js/cr/ui/menu.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698