-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
202 lines (175 loc) · 4.73 KB
/
utils.js
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
import $ from 'jquery';
// 类型判定接口
import * as Is from './lib/is.js';
// 公开接口
export * from './lib/is.js';
export * from './lib/template.js';
// jquery 对象
export var win = $(window);
export var doc = $(document);
/**
* 属性拷贝
*
* @export
* @param {Object} target 目标对象
* @param {Object} seed 继承对象
* @param {Array} list 名单
* @param {Boolean} isWhite 是否是白名单
*/
export function mix(target, seed, list, isWhite) {
if (!Array.isArray(list)) {
list = false;
}
var index;
// Copy "all" properties including inherited ones.
for (var prop in seed) {
if (seed.hasOwnProperty(prop)) {
// 检测白名单
if (list) {
index = list.indexOf(prop);
// 区分黑白名单
if (isWhite ? index === -1 : index !== -1) {
continue;
}
}
// 在 iPhone 1 代等设备的 Safari 中,prototype 也会被枚举出来,需排除
if (prop !== 'prototype') {
target[prop] = seed[prop];
}
}
}
return target;
}
// 为了节省内存,使用一个共享的构造器
function TClass() {
// 空白中转类,可以减少内存占用
}
// Object setPrototypeOf
var setPrototypeOf = Object.setPrototypeOf;
// not suport setPrototypeOf
if (!Is.native(setPrototypeOf)) {
setPrototypeOf = false;
}
// Object create
var objectCreate = Object.create;
// not suport create
if (!Is.native(objectCreate)) {
objectCreate = false;
}
/**
* 继承
*
* @export
* @param {Class} subClass
* @param {Class} superClass
* @param {Object} properties
* @returns {subClass}
*/
export function inherits(subClass, superClass, properties) {
var superPrototype = superClass.prototype;
if (setPrototypeOf) {
setPrototypeOf(subClass.prototype, superPrototype);
} else if (objectCreate) {
subClass.prototype = objectCreate(superPrototype);
} else {
// 原型属性继承
TClass.prototype = superPrototype;
// 初始化实例
subClass.prototype = new TClass();
// 不要保持一个 superClass 的杂散引用
TClass.prototype = null;
}
// 混合属性
properties && mix(subClass.prototype, properties);
// 设置构造函数
subClass.prototype.constructor = subClass;
return subClass;
}
/**
* 高性能 apply
*
* @param {Function} func
* @param {any} context
* @param {Array} args
* call is faster than apply, optimize less than 6 args
* https://github.com/micro-js/apply
* http://blog.csdn.net/zhengyinhui100/article/details/7837127
*/
export function apply(func, context, args) {
switch (args.length) {
// faster
case 0:
return func.call(context);
case 1:
return func.call(context, args[0]);
case 2:
return func.call(context, args[0], args[1]);
case 3:
return func.call(context, args[0], args[1], args[2]);
default:
// slower
return func.apply(context, args);
}
}
// original getComputedStyle
var originalGetComputedStyle = window.getComputedStyle;
/**
* getComputedStyle
*
* @export
* @param {HTMLElement} element
* @param {String} prop
* @returns {Object}
* @see https://github.com/the-simian/ie8-getcomputedstyle/blob/master/index.js
* @see https://github.com/twolfson/computedStyle/blob/master/lib/computedStyle.js
* @see http://www.zhangxinxu.com/wordpress/2012/05/getcomputedstyle-js-getpropertyvalue-currentstyle
*/
export function getComputedStyle(element, prop) {
var style =
// If we have getComputedStyle
originalGetComputedStyle ?
// Query it
// From CSS-Query notes, we might need (node, null) for FF
originalGetComputedStyle(element, null) :
// Otherwise, we are in IE and use currentStyle
element.currentStyle;
// 返回 getPropertyValue 方法
return {
/**
* getPropertyValue
*
* @param {String} prop
*/
getPropertyValue: function(prop) {
if (style) {
// Original support
if (style.getPropertyValue) {
return style.getPropertyValue(prop);
}
// Switch to camelCase for CSSOM
// DEV: Grabbed from jQuery
// https://github.com/jquery/jquery/blob/1.9-stable/src/css.js#L191-L194
// https://github.com/jquery/jquery/blob/1.9-stable/src/core.js#L593-L597
prop = prop.replace(/-(\w)/gi, function(word, letter) {
return letter.toUpperCase();
});
// Old IE
if (style.getAttribute) {
return style.getAttribute(prop);
}
// Read property directly
return style[prop];
}
}
};
}
/**
* addCSSUnit
*
* @param {any} value
* @returns {String}
*/
export function addCSSUnit(value) {
var matches = /^([+-]?(?:\d*\.|)\d+(?:[eE][+-]?\d+|))(.*)$/i.exec(value);
return matches ? matches[1] + (matches[2] || 'px') : value;
}