OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 const cr = (function() { |
| 6 |
| 7 /** |
| 8 * Whether we are using a Mac or not. |
| 9 * @type {boolean} |
| 10 */ |
| 11 const isMac = /Mac/.test(navigator.platform); |
| 12 |
| 13 /** |
| 14 * Builds an object structure for the provided namespace path, |
| 15 * ensuring that names that already exist are not overwritten. For |
| 16 * example: |
| 17 * "a.b.c" -> a = {};a.b={};a.b.c={}; |
| 18 * @param {string} name Name of the object that this file defines. |
| 19 * @param {*=} opt_object The object to expose at the end of the path. |
| 20 * @param {Object=} opt_objectToExportTo The object to add the path to; |
| 21 * default is {@code window}. |
| 22 * @private |
| 23 */ |
| 24 function exportPath(name, opt_object, opt_objectToExportTo) { |
| 25 var parts = name.split('.'); |
| 26 var cur = opt_objectToExportTo || window /* global */; |
| 27 |
| 28 for (var part; parts.length && (part = parts.shift());) { |
| 29 if (!parts.length && opt_object !== undefined) { |
| 30 // last part and we have an object; use it |
| 31 cur[part] = opt_object; |
| 32 } else if (part in cur) { |
| 33 cur = cur[part]; |
| 34 } else { |
| 35 cur = cur[part] = {}; |
| 36 } |
| 37 } |
| 38 return cur; |
| 39 }; |
| 40 |
| 41 /** |
| 42 * Fires a property change event on the target. |
| 43 * @param {EventTarget} target The target to dispatch the event on. |
| 44 * @param {string} propertyName The name of the property that changed. |
| 45 * @param {*} newValue The new value for the property. |
| 46 * @param {*} oldValue The old value for the property. |
| 47 */ |
| 48 function dispatchPropertyChange(target, propertyName, newValue, oldValue) { |
| 49 // TODO(arv): Depending on cr.Event here is a bit ugly. |
| 50 var e = new cr.Event(propertyName + 'Change'); |
| 51 e.propertyName = propertyName; |
| 52 e.newValue = newValue; |
| 53 e.oldValue = oldValue; |
| 54 target.dispatchEvent(e); |
| 55 } |
| 56 |
| 57 /** |
| 58 * The kind of property to define in {@code defineProperty}. |
| 59 * @enum {number} |
| 60 */ |
| 61 const PropertyKind = { |
| 62 /** |
| 63 * Plain old JS property where the backing data is stored as a "private" |
| 64 * field on the object. |
| 65 */ |
| 66 JS: 'js', |
| 67 |
| 68 /** |
| 69 * The property backing data is stored as an attribute on an element. |
| 70 */ |
| 71 ATTR: 'attr', |
| 72 |
| 73 /** |
| 74 * The property backing data is stored as an attribute on an element. If the |
| 75 * element has the attribute then the value is true. |
| 76 */ |
| 77 BOOL_ATTR: 'boolAttr' |
| 78 }; |
| 79 |
| 80 /** |
| 81 * Helper function for defineProperty that returns the getter to use for the |
| 82 * property. |
| 83 * @param {string} name |
| 84 * @param {cr.PropertyKind} kind |
| 85 * @param {*} defaultValue The default value. This is only used for the ATTR |
| 86 * kind. |
| 87 * @return {function():*} The getter for the property. |
| 88 */ |
| 89 function getGetter(name, kind, defaultValue) { |
| 90 switch (kind) { |
| 91 case PropertyKind.JS: |
| 92 var privateName = name + '_'; |
| 93 return function() { |
| 94 return this[privateName]; |
| 95 }; |
| 96 case PropertyKind.ATTR: |
| 97 // For attr with default value we return the default value if the |
| 98 // element is missing the attribute. |
| 99 if (defaultValue == undefined) { |
| 100 return function() { |
| 101 return this.getAttribute(name); |
| 102 }; |
| 103 } else { |
| 104 return function() { |
| 105 // WebKit uses null for non existant attributes. |
| 106 var value = this.getAttribute(name); |
| 107 return value !== null ? value : defaultValue; |
| 108 }; |
| 109 } |
| 110 case PropertyKind.BOOL_ATTR: |
| 111 // Boolean attributes don't support default values. |
| 112 return function() { |
| 113 return this.hasAttribute(name); |
| 114 }; |
| 115 } |
| 116 } |
| 117 |
| 118 /** |
| 119 * Helper function for defineProperty that returns the setter of the right |
| 120 * kind. |
| 121 * @param {string} name The name of the property we are defining the setter |
| 122 * for. |
| 123 * @param {cr.PropertyKind} kind The kind of property we are getting the |
| 124 * setter for. |
| 125 * @return {function(*):void} The function to use as a setter. |
| 126 */ |
| 127 function getSetter(name, kind) { |
| 128 switch (kind) { |
| 129 case PropertyKind.JS: |
| 130 var privateName = name + '_'; |
| 131 return function(value) { |
| 132 var oldValue = this[privateName]; |
| 133 if (value !== oldValue) { |
| 134 this[privateName] = value; |
| 135 dispatchPropertyChange(this, name, value, oldValue); |
| 136 } |
| 137 }; |
| 138 |
| 139 case PropertyKind.ATTR: |
| 140 return function(value) { |
| 141 var oldValue = this[name]; |
| 142 if (value !== oldValue) { |
| 143 this.setAttribute(name, value); |
| 144 dispatchPropertyChange(this, name, value, oldValue); |
| 145 } |
| 146 }; |
| 147 |
| 148 case PropertyKind.BOOL_ATTR: |
| 149 return function(value) { |
| 150 var oldValue = this[name]; |
| 151 if (value !== oldValue) { |
| 152 if (value) |
| 153 this.setAttribute(name, name); |
| 154 else |
| 155 this.removeAttribute(name); |
| 156 dispatchPropertyChange(this, name, value, oldValue); |
| 157 } |
| 158 }; |
| 159 } |
| 160 } |
| 161 |
| 162 /** |
| 163 * Defines a property on an object. When the setter changes the value a |
| 164 * property change event with the type {@code name + 'Change'} is fired. |
| 165 * @param {!Object} The object to define the property for. |
| 166 * @param {string} The name of the property. |
| 167 * @param {cr.PropertyKind=} opt_kind What kind of underlying storage to use. |
| 168 * @param {*} opt_defaultValue The default value. |
| 169 */ |
| 170 function defineProperty(obj, name, opt_kind, opt_default) { |
| 171 if (typeof obj == 'function') |
| 172 obj = obj.prototype; |
| 173 |
| 174 var kind = opt_kind || PropertyKind.JS; |
| 175 |
| 176 if (!obj.__lookupGetter__(name)) { |
| 177 // For js properties we set the default value on the prototype. |
| 178 if (kind == PropertyKind.JS && arguments.length > 3) { |
| 179 var privateName = name + '_'; |
| 180 obj[privateName] = opt_default; |
| 181 } |
| 182 obj.__defineGetter__(name, getGetter(name, kind, opt_default)); |
| 183 } |
| 184 |
| 185 if (!obj.__lookupSetter__(name)) { |
| 186 obj.__defineSetter__(name, getSetter(name, kind)); |
| 187 } |
| 188 } |
| 189 |
| 190 /** |
| 191 * Counter for use with createUid |
| 192 */ |
| 193 var uidCounter = 1; |
| 194 |
| 195 /** |
| 196 * @return {number} A new unique ID. |
| 197 */ |
| 198 function createUid() { |
| 199 return uidCounter++; |
| 200 } |
| 201 |
| 202 /** |
| 203 * Returns a unique ID for the item. This mutates the item so it needs to be |
| 204 * an object |
| 205 * @param {!Object} item The item to get the unique ID for. |
| 206 * @return {number} The unique ID for the item. |
| 207 */ |
| 208 function getUid(item) { |
| 209 if (item.hasOwnProperty('uid')) |
| 210 return item.uid; |
| 211 return item.uid = createUid(); |
| 212 } |
| 213 |
| 214 /** |
| 215 * Partially applies this function to a particular 'this object' and zero or |
| 216 * more arguments. The result is a new function with some arguments of the |
| 217 * first function pre-filled and the value of |this| 'pre-specified'. |
| 218 * |
| 219 * Remaining arguments specified at call-time are appended to the pre- |
| 220 * specified ones. |
| 221 * |
| 222 * Usage: |
| 223 * <pre>var barMethBound = bind(myFunction, myObj, 'arg1', 'arg2'); |
| 224 * barMethBound('arg3', 'arg4');</pre> |
| 225 * |
| 226 * @param {Function} fn A function to partially apply. |
| 227 * @param {Object|undefined} selfObj Specifies the object which |this| should |
| 228 * point to when the function is run. If the value is null or undefined, |
| 229 * it will default to the global object. |
| 230 * @param {...*} var_args Additional arguments that are partially |
| 231 * applied to the function. |
| 232 * |
| 233 * @return {!Function} A partially-applied form of the function bind() was |
| 234 * invoked as a method of. |
| 235 */ |
| 236 function bind(fn, selfObj, var_args) { |
| 237 var boundArgs = Array.prototype.slice.call(arguments, 2); |
| 238 return function() { |
| 239 var args = Array.prototype.slice.call(arguments); |
| 240 args.unshift.apply(args, boundArgs); |
| 241 return fn.apply(selfObj, args); |
| 242 } |
| 243 } |
| 244 |
| 245 /** |
| 246 * Dispatches a simple event on an event target. |
| 247 * @param {!EventTarget} target The event target to dispatch the event on. |
| 248 * @param {string} type The type of the event. |
| 249 * @param {boolean=} opt_bubbles Whether the event bubbles or not. |
| 250 * @param {boolean=} opt_cancelable Whether the default action of the event |
| 251 * can be prevented. |
| 252 * @return {boolean} If any of the listeners called {@code preventDefault} |
| 253 * during the dispatch this will return false. |
| 254 */ |
| 255 function dispatchSimpleEvent(target, type, opt_bubbles, opt_cancelable) { |
| 256 var e = new cr.Event(type, opt_bubbles, opt_cancelable); |
| 257 return target.dispatchEvent(e); |
| 258 } |
| 259 |
| 260 /** |
| 261 * @param {string} name |
| 262 * @param {!Function} fun |
| 263 */ |
| 264 function define(name, fun) { |
| 265 var obj = exportPath(name); |
| 266 var exports = fun(); |
| 267 for (var key in exports) { |
| 268 obj[key] = exports[key]; |
| 269 } |
| 270 } |
| 271 |
| 272 /** |
| 273 * Document used for various document related operations. |
| 274 * @type {!Document} |
| 275 */ |
| 276 var doc = document; |
| 277 |
| 278 |
| 279 /** |
| 280 * Allows you to run func in the context of a different document. |
| 281 * @param {!Document} document The document to use. |
| 282 * @param {function():*} func The function to call. |
| 283 */ |
| 284 function withDoc(document, func) { |
| 285 var oldDoc = doc; |
| 286 doc = document; |
| 287 try { |
| 288 func(); |
| 289 } finally { |
| 290 doc = oldDoc; |
| 291 } |
| 292 } |
| 293 |
| 294 return { |
| 295 isMac: isMac, |
| 296 define: define, |
| 297 defineProperty: defineProperty, |
| 298 PropertyKind: PropertyKind, |
| 299 createUid: createUid, |
| 300 getUid: getUid, |
| 301 bind: bind, |
| 302 dispatchSimpleEvent: dispatchSimpleEvent, |
| 303 dispatchPropertyChange: dispatchPropertyChange, |
| 304 |
| 305 /** |
| 306 * The document that we are currently using. |
| 307 * @type {!Document} |
| 308 */ |
| 309 get doc() { |
| 310 return doc; |
| 311 }, |
| 312 withDoc: withDoc |
| 313 }; |
| 314 })(); |
OLD | NEW |