1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
const cr = (function() {
/**
* Whether we are using a Mac or not.
* @type {boolean}
*/
const isMac = /Mac/.test(navigator.platform);
/**
* Whether this is on the Windows platform or not.
* @type {boolean}
*/
const isWindows = /Win/.test(navigator.platform);
/**
* Whether this is on chromeOS or not.
* @type {boolean}
*/
const isChromeOS = /CrOS/.test(navigator.userAgent);
/**
* Whether this is on touchui build or not.
* @type {boolean}
*/
const isTouch = /Touch/.test(navigator.userAgent);
/**
* Whether this is on vanilla Linux (not chromeOS).
* @type {boolean}
*/
const isLinux = /Linux/.test(navigator.userAgent);
/**
* Whether this uses GTK or not.
* @type {boolean}
*/
const isGTK = /GTK/.test(chrome.toolkit);
/**
* Whether this uses the views toolkit or not.
* @type {boolean}
*/
const isViews = /views/.test(chrome.toolkit);
/**
* Sets the os and toolkit attributes in the <html> element so that platform
* specific css rules can be applied.
*/
function enablePlatformSpecificCSSRules() {
if (isMac)
doc.documentElement.setAttribute('os', 'mac');
if (isWindows)
doc.documentElement.setAttribute('os', 'windows');
if (isChromeOS)
doc.documentElement.setAttribute('os', 'chromeos');
if (isLinux)
doc.documentElement.setAttribute('os', 'linux');
if (isGTK)
doc.documentElement.setAttribute('toolkit', 'gtk');
if (isViews)
doc.documentElement.setAttribute('toolkit', 'views');
}
/**
* Builds an object structure for the provided namespace path,
* ensuring that names that already exist are not overwritten. For
* example:
* "a.b.c" -> a = {};a.b={};a.b.c={};
* @param {string} name Name of the object that this file defines.
* @param {*=} opt_object The object to expose at the end of the path.
* @param {Object=} opt_objectToExportTo The object to add the path to;
* default is {@code window}.
* @private
*/
function exportPath(name, opt_object, opt_objectToExportTo) {
var parts = name.split('.');
var cur = opt_objectToExportTo || window /* global */;
for (var part; parts.length && (part = parts.shift());) {
if (!parts.length && opt_object !== undefined) {
// last part and we have an object; use it
cur[part] = opt_object;
} else if (part in cur) {
cur = cur[part];
} else {
cur = cur[part] = {};
}
}
return cur;
};
// cr.Event is called CrEvent in here to prevent naming conflicts. We also
// store the original Event in case someone does a global alias of cr.Event.
const DomEvent = Event;
/**
* Creates a new event to be used with cr.EventTarget or DOM EventTarget
* objects.
* @param {string} type The name of the event.
* @param {boolean=} opt_bubbles Whether the event bubbles. Default is false.
* @param {boolean=} opt_preventable Whether the default action of the event
* can be prevented.
* @constructor
* @extends {DomEvent}
*/
function CrEvent(type, opt_bubbles, opt_preventable) {
var e = cr.doc.createEvent('Event');
e.initEvent(type, !!opt_bubbles, !!opt_preventable);
e.__proto__ = CrEvent.prototype;
return e;
}
CrEvent.prototype = {
__proto__: DomEvent.prototype
};
/**
* Fires a property change event on the target.
* @param {EventTarget} target The target to dispatch the event on.
* @param {string} propertyName The name of the property that changed.
* @param {*} newValue The new value for the property.
* @param {*} oldValue The old value for the property.
*/
function dispatchPropertyChange(target, propertyName, newValue, oldValue) {
var e = new CrEvent(propertyName + 'Change');
e.propertyName = propertyName;
e.newValue = newValue;
e.oldValue = oldValue;
target.dispatchEvent(e);
}
/**
* The kind of property to define in {@code defineProperty}.
* @enum {number}
*/
const PropertyKind = {
/**
* Plain old JS property where the backing data is stored as a "private"
* field on the object.
*/
JS: 'js',
/**
* The property backing data is stored as an attribute on an element.
*/
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.
*/
BOOL_ATTR: 'boolAttr'
};
/**
* Helper function for defineProperty that returns the getter to use for the
* property.
* @param {string} name
* @param {cr.PropertyKind} kind
* @return {function():*} The getter for the property.
*/
function getGetter(name, kind) {
switch (kind) {
case PropertyKind.JS:
var privateName = name + '_';
return function() {
return this[privateName];
};
case PropertyKind.ATTR:
return function() {
return this.getAttribute(name);
};
case PropertyKind.BOOL_ATTR:
return function() {
return this.hasAttribute(name);
};
}
}
/**
* Helper function for defineProperty that returns the setter of the right
* 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
* 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, opt_setHook) {
switch (kind) {
case PropertyKind.JS:
var privateName = name + '_';
return function(value) {
var oldValue = this[privateName];
if (value !== oldValue) {
this[privateName] = value;
if (opt_setHook)
opt_setHook.call(this, value, oldValue);
dispatchPropertyChange(this, name, value, oldValue);
}
};
case PropertyKind.ATTR:
return function(value) {
var oldValue = this[name];
if (value !== oldValue) {
if (value == undefined)
this.removeAttribute(name);
else
this.setAttribute(name, value);
if (opt_setHook)
opt_setHook.call(this, value, oldValue);
dispatchPropertyChange(this, name, value, oldValue);
}
};
case PropertyKind.BOOL_ATTR:
return function(value) {
var oldValue = this[name];
if (value !== oldValue) {
if (value)
this.setAttribute(name, name);
else
this.removeAttribute(name);
if (opt_setHook)
opt_setHook.call(this, value, oldValue);
dispatchPropertyChange(this, name, value, oldValue);
}
};
}
}
/**
* Defines a property on an object. When the setter changes the value a
* 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
* 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;
if (!obj.__lookupGetter__(name)) {
obj.__defineGetter__(name, getGetter(name, kind));
}
if (!obj.__lookupSetter__(name)) {
obj.__defineSetter__(name, getSetter(name, kind, opt_setHook));
}
}
/**
* Counter for use with createUid
*/
var uidCounter = 1;
/**
* @return {number} A new unique ID.
*/
function createUid() {
return uidCounter++;
}
/**
* Returns a unique ID for the item. This mutates the item so it needs to be
* an object
* @param {!Object} item The item to get the unique ID for.
* @return {number} The unique ID for the item.
*/
function getUid(item) {
if (item.hasOwnProperty('uid'))
return item.uid;
return item.uid = createUid();
}
/**
* Dispatches a simple event on an event target.
* @param {!EventTarget} target The event target to dispatch the event on.
* @param {string} type The type of the event.
* @param {boolean=} opt_bubbles Whether the event bubbles or not.
* @param {boolean=} opt_cancelable Whether the default action of the event
* can be prevented.
* @return {boolean} If any of the listeners called {@code preventDefault}
* during the dispatch this will return false.
*/
function dispatchSimpleEvent(target, type, opt_bubbles, opt_cancelable) {
var e = new cr.Event(type, opt_bubbles, opt_cancelable);
return target.dispatchEvent(e);
}
/**
* @param {string} name
* @param {!Function} fun
*/
function define(name, fun) {
var obj = exportPath(name);
var exports = fun();
for (var propertyName in exports) {
// Maybe we should check the prototype chain here? The current usage
// pattern is always using an object literal so we only care about own
// properties.
var propertyDescriptor = Object.getOwnPropertyDescriptor(exports,
propertyName);
if (propertyDescriptor)
Object.defineProperty(obj, propertyName, propertyDescriptor);
}
}
/**
* Document used for various document related operations.
* @type {!Document}
*/
var doc = document;
/**
* Allows you to run func in the context of a different document.
* @param {!Document} document The document to use.
* @param {function():*} func The function to call.
*/
function withDoc(document, func) {
var oldDoc = doc;
doc = document;
try {
func();
} finally {
doc = oldDoc;
}
}
/**
* Adds a {@code getInstance} static method that always return the same
* instance object.
* @param {!Function} ctor The constructor for the class to add the static
* method to.
*/
function addSingletonGetter(ctor) {
ctor.getInstance = function() {
return ctor.instance_ || (ctor.instance_ = new ctor());
};
}
return {
addSingletonGetter: addSingletonGetter,
isChromeOS: isChromeOS,
isMac: isMac,
isWindows: isWindows,
isLinux: isLinux,
isViews: isViews,
isTouch: isTouch,
enablePlatformSpecificCSSRules: enablePlatformSpecificCSSRules,
define: define,
defineProperty: defineProperty,
PropertyKind: PropertyKind,
createUid: createUid,
getUid: getUid,
dispatchSimpleEvent: dispatchSimpleEvent,
dispatchPropertyChange: dispatchPropertyChange,
/**
* The document that we are currently using.
* @type {!Document}
*/
get doc() {
return doc;
},
withDoc: withDoc,
Event: CrEvent
};
})();
|