var global = this;
/** Platform, package, object property, and Event support. **/
-this.cr = (function() {
+var cr = function() {
'use strict';
/**
/**
* The kind of property to define in {@code defineProperty}.
- * @enum {number}
+ * @enum {string}
* @const
*/
var PropertyKind = {
/**
* Plain old JS property where the backing data is stored as a "private"
* field on the object.
+ * Use for properties of any type. Type will not be checked.
*/
JS: 'js',
/**
* The property backing data is stored as an attribute on an element.
+ * Use only for properties of type {string}.
*/
ATTR: 'attr',
/**
* The property backing data is stored as an attribute on an element. If the
* element has the attribute then the value is true.
+ * Use only for properties of type {boolean}.
*/
BOOL_ATTR: 'boolAttr'
};
* Helper function for defineProperty that returns the getter to use for the
* property.
* @param {string} name The name of the property.
- * @param {cr.PropertyKind} kind The kind of the property.
+ * @param {PropertyKind} kind The kind of the property.
* @return {function():*} The getter for the property.
*/
function getGetter(name, kind) {
return this.hasAttribute(attributeName);
};
}
+
+ // TODO(dbeam): replace with assertNotReached() in assert.js when I can coax
+ // the browser/unit tests to preprocess this file through grit.
+ throw 'not reached';
}
/**
* kind.
* @param {string} name The name of the property we are defining the setter
* for.
- * @param {cr.PropertyKind} kind The kind of property we are getting the
+ * @param {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.
+ * @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, opt_setHook) {
}
};
}
+
+ // TODO(dbeam): replace with assertNotReached() in assert.js when I can coax
+ // the browser/unit tests to preprocess this file through grit.
+ throw 'not reached';
}
/**
* property change event with the type {@code name + 'Change'} is fired.
* @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 {function(*):void} opt_setHook A function to run after the
+ * @param {PropertyKind=} opt_kind What kind of underlying storage to use.
+ * @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_setHook) {
if (typeof obj == 'function')
obj = obj.prototype;
- var kind = opt_kind || PropertyKind.JS;
+ var kind = /** @type {PropertyKind} */ (opt_kind || PropertyKind.JS);
if (!obj.__lookupGetter__(name))
obj.__defineGetter__(name, getGetter(name, kind));
};
}
- /**
- * Initialization which must be deferred until run-time.
- */
- function initialize() {
- // If 'document' isn't defined, then we must be being pre-compiled,
- // so set a trap so that we're initialized on first access at run-time.
- if (!global.document) {
- var originalCr = cr;
-
- Object.defineProperty(global, 'cr', {
- get: function() {
- Object.defineProperty(global, 'cr', {value: originalCr});
- originalCr.initialize();
- return originalCr;
- },
- configurable: true
- });
-
- return;
- }
-
- cr.doc = document;
-
- /**
- * Whether we are using a Mac or not.
- */
- cr.isMac = /Mac/.test(navigator.platform);
-
- /**
- * Whether this is on the Windows platform or not.
- */
- cr.isWindows = /Win/.test(navigator.platform);
-
- /**
- * Whether this is on chromeOS or not.
- */
- cr.isChromeOS = /CrOS/.test(navigator.userAgent);
-
- /**
- * Whether this is on vanilla Linux (not chromeOS).
- */
- cr.isLinux = /Linux/.test(navigator.userAgent);
-
- /**
- * Whether this uses GTK or not.
- */
- cr.isGTK = typeof chrome.getVariableValue == 'function' &&
- /GTK/.test(chrome.getVariableValue('toolkit'));
-
- /**
- * Whether this uses the views toolkit or not.
- */
- cr.isViews = typeof chrome.getVariableValue == 'function' &&
- /views/.test(chrome.getVariableValue('toolkit'));
- }
-
return {
addSingletonGetter: addSingletonGetter,
createUid: createUid,
dispatchPropertyChange: dispatchPropertyChange,
dispatchSimpleEvent: dispatchSimpleEvent,
getUid: getUid,
- initialize: initialize,
- PropertyKind: PropertyKind
+ PropertyKind: PropertyKind,
+
+ get doc() {
+ return document;
+ },
+
+ /** Whether we are using a Mac or not. */
+ get isMac() {
+ return /Mac/.test(navigator.platform);
+ },
+
+ /** Whether this is on the Windows platform or not. */
+ get isWindows() {
+ return /Win/.test(navigator.platform);
+ },
+
+ /** Whether this is on chromeOS or not. */
+ get isChromeOS() {
+ return /CrOS/.test(navigator.userAgent);
+ },
+
+ /** Whether this is on vanilla Linux (not chromeOS). */
+ get isLinux() {
+ return /Linux/.test(navigator.userAgent);
+ },
+
+ /** Whether this uses the views toolkit or not. */
+ get isViews() {
+ return typeof chrome.getVariableValue == 'function' &&
+ /views/.test(chrome.getVariableValue('toolkit'));
+ },
};
-})();
-
-
-/**
- * TODO(kgr): Move this to another file which is to be loaded last.
- * This will be done as part of future work to make this code pre-compilable.
- */
-cr.initialize();
+}();