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
|
// Copyright (c) 2013 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.
'use strict';
base.require('base.iteration_helpers');
base.require('base.rect');
base.exportTo('base', function() {
/**
* 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());
};
}
function instantiateTemplate(selector) {
return document.querySelector(selector).content.cloneNode(true);
}
function tracedFunction(fn, name, opt_this) {
function F() {
console.time(name);
try {
fn.apply(opt_this, arguments);
} finally {
console.timeEnd(name);
}
}
return F;
}
function normalizeException(e) {
if (typeof(e) == 'string') {
return {
message: e,
stack: ['<unknown>']
};
}
return {
message: e.message,
stack: e.stack ? e.stack : ['<unknown>']
};
}
function stackTrace() {
var stack = new Error().stack + '';
stack = stack.split('\n');
return stack.slice(2);
}
function windowRectForElement(element) {
var position = [element.offsetLeft, element.offsetTop];
var size = [element.offsetWidth, element.offsetHeight];
var node = element.offsetParent;
while (node) {
position[0] += node.offsetLeft;
position[1] += node.offsetTop;
node = node.offsetParent;
}
return base.Rect.fromXYWH(position[0], position[1], size[0], size[1]);
}
function clamp(x, lo, hi) {
return Math.min(Math.max(x, lo), hi);
}
function lerp(percentage, lo, hi) {
var range = hi - lo;
return lo + percentage * range;
}
function deg2rad(deg) {
return (Math.PI * deg) / 180.0;
}
function scrollIntoViewIfNeeded(el) {
var pr = el.parentElement.getBoundingClientRect();
var cr = el.getBoundingClientRect();
if (cr.top < pr.top) {
el.scrollIntoView(true);
} else if (cr.bottom > pr.bottom) {
el.scrollIntoView(false);
}
}
return {
addSingletonGetter: addSingletonGetter,
tracedFunction: tracedFunction,
normalizeException: normalizeException,
instantiateTemplate: instantiateTemplate,
stackTrace: stackTrace,
windowRectForElement: windowRectForElement,
scrollIntoViewIfNeeded: scrollIntoViewIfNeeded,
clamp: clamp,
lerp: lerp,
deg2rad: deg2rad
};
});
|