-
Notifications
You must be signed in to change notification settings - Fork 863
/
base.js
427 lines (390 loc) · 11.8 KB
/
base.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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/**
* base class for transition anim and timer anim
* @author yiminghe@gmail.com
* @ignore
*/
var Dom = require('dom'),
querystring = require('querystring'),
Utils = require('./base/utils'),
Q = require('./base/queue'),
Promise = require('promise'),
util = require('util'),
NodeType = Dom.NodeType,
camelCase = util.camelCase,
noop = util.noop,
specialVals = {
toggle: 1,
hide: 1,
show: 1
};
var undef;
var defaultConfig = {
duration: 1,
easing: 'linear'
};
// stop(true) will run complete function synchronously
function syncComplete(self) {
var _backupProps,
complete = self.config.complete;
// only recover after complete anim
if (!util.isEmptyObject(_backupProps = self._backupProps)) {
Dom.css(self.node, _backupProps);
}
if (complete) {
complete.call(self);
}
}
/**
* @class KISSY.Anim
* A class for constructing animation instances.
*
* @example
* KISSY.use('dom,anim',function(S,Dom,Anim){
* var d=Dom.create('<div style="width:50px;height:50px;border:1px solid red;">running</div>');
* document.body.appendChild(d);
* new Anim({
* node: d,
* to: {width:100,height:100}
* }).run().then(function(){
* d.innerHTML='completed';
* });
* });
*
* @extend KISSY.Promise
* @cfg {HTMLElement|Window} node html dom node or window
* (window can only animate scrollTop/scrollLeft)
* @cfg {Object} to end css style value.
* @cfg {Number} [duration=1] duration(second) or anim config
* @cfg {String|Function} [easing='easeNone'] easing fn or string
* @cfg {Function} [complete] callback function when this animation is complete
* @cfg {String|Boolean} [queue] current animation's queue, if false then no queue
*/
function AnimBase(node, to, duration, easing, complete) {
var self = this;
var config;
if (node.node) {
config = node;
} else {
// the transition properties
if (typeof to === 'string') {
to = querystring.parse(String(to), ';', ':');
util.each(to, function (value, prop) {
var trimProp = util.trim(prop);
if (trimProp) {
to[trimProp] = util.trim(value);
}
if (!trimProp || trimProp !== prop) {
delete to[prop];
}
});
}
// animation config
if (util.isPlainObject(duration)) {
config = util.clone(duration);
} else {
config = {
complete: complete
};
if (duration) {
config.duration = duration;
}
if (easing) {
config.easing = easing;
}
}
config.node = node;
config.to = to;
}
config = util.merge(defaultConfig, config);
// Promise.call(self);
AnimBase.superclass.constructor.call(self);
Promise.Defer(self);
/**
* config object of current anim instance
* @type {Object}
*/
self.config = config;
node = config.node;
if (!util.isPlainObject(node)) {
node = Dom.get(config.node);
}
self.node = self.el = node;
self._backupProps = {};
self._propsData = {};
// camel case uniformity
var newTo = {};
to = config.to;
for (var prop in to) {
newTo[camelCase(prop)] = to[prop];
}
config.to = newTo;
}
util.extend(AnimBase, Promise, {
/**
* prepare fx hook
* @protected
* @method
*/
prepareFx: noop,
runInternal: function () {
var self = this,
config = self.config,
node = self.node,
val,
_backupProps = self._backupProps,
_propsData = self._propsData,
to = config.to,
defaultDelay = (config.delay || 0),
defaultDuration = config.duration;
// 进入该函数即代表执行(q[0] 已经是 ...)
Utils.saveRunningAnim(self);
// 分离 easing
util.each(to, function (val, prop) {
if (!util.isPlainObject(val)) {
val = {
value: val
};
}
_propsData[prop] = util.mix({
// simulate css3
delay: defaultDelay,
//// timing-function
easing: config.easing,
frame: config.frame,
duration: defaultDuration
}, val);
});
if (node.nodeType === NodeType.ELEMENT_NODE) {
// 放在前面,设置 overflow hidden,否则后面 ie6 取 width/height 初值导致错误
// <div style='width:0'><div style='width:100px'></div></div>
if (to.width || to.height) {
// Make sure that nothing sneaks out
// Record all 3 overflow attributes because IE does not
// change the overflow attribute when overflowX and
// overflowY are set to the same value
var elStyle = node.style;
util.mix(_backupProps, {
overflow: elStyle.overflow,
'overflow-x': elStyle.overflowX,
'overflow-y': elStyle.overflowY
});
elStyle.overflow = 'hidden';
// inline element should has layout/inline-block
// performance! user should set himself
// https://github.com/kissyteam/kissy/issues/651
// if (Dom.css(node, 'display') === 'inline' &&
// Dom.css(node, 'float') === 'none') {
// elStyle.zoom = 1;
// elStyle.display = 'inline-block';
// }
}
var exit, hidden;
util.each(_propsData, function (_propData, prop) {
val = _propData.value;
// 直接结束
if (specialVals[val]) {
if (hidden === undef) {
hidden = (Dom.css(node, 'display') === 'none');
}
if (val === 'hide' && hidden || val === 'show' && !hidden) {
// need to invoke complete
self.stop(true);
exit = false;
return exit;
}
// backup original inline css value
_backupProps[prop] = Dom.style(node, prop);
if (val === 'toggle') {
val = hidden ? 'show' : 'hide';
}
if (val === 'hide') {
_propData.value = 0;
// 执行完后隐藏
_backupProps.display = 'none';
} else {
_propData.value = Dom.css(node, prop);
// prevent flash of content
Dom.css(node, prop, 0);
Dom.show(node);
}
}
return undefined;
});
if (exit === false) {
return;
}
}
self.startTime = util.now();
if (util.isEmptyObject(_propsData)) {
self.__totalTime = defaultDuration * 1000;
self.__waitTimeout = setTimeout(function () {
self.stop(true);
}, self.__totalTime);
} else {
self.prepareFx();
self.doStart();
}
},
/**
* whether this animation is running
* @return {Boolean}
*/
isRunning: function () {
return Utils.isAnimRunning(this);
},
/**
* whether this animation is paused
* @return {Boolean}
*/
isPaused: function () {
return Utils.isAnimPaused(this);
},
/**
* pause current anim
* @chainable
*/
pause: function () {
var self = this;
if (self.isRunning()) {
// already run time
self._runTime = util.now() - self.startTime;
self.__totalTime -= self._runTime;
Utils.removeRunningAnim(self);
Utils.savePausedAnim(self);
if (self.__waitTimeout) {
clearTimeout(self.__waitTimeout);
} else {
self.doStop();
}
}
return self;
},
/**
* stop by dom operation
* @protected
* @method
*/
doStop: noop,
/**
* start by dom operation
* @protected
* @method
*/
doStart: noop,
/**
* resume current anim
* @chainable
*/
resume: function () {
var self = this;
if (self.isPaused()) {
// adjust time by run time caused by pause
self.startTime = util.now() - self._runTime;
Utils.removePausedAnim(self);
Utils.saveRunningAnim(self);
if (self.__waitTimeout) {
self.__waitTimeout = setTimeout(function () {
self.stop(true);
}, self.__totalTime);
} else {
self.beforeResume();
self.doStart();
}
}
return self;
},
/**
* before resume hook
* @protected
* @method
*/
beforeResume: noop,
/**
* start this animation
* @chainable
*/
run: function () {
var self = this,
q,
queue = self.config.queue;
if (queue === false) {
self.runInternal();
} else {
// 当前动画对象加入队列
q = Q.queue(self.node, queue, self);
if (q.length === 1) {
self.runInternal();
}
}
return self;
},
/**
* stop this animation
* @param {Boolean} [finish] whether jump to the last position of this animation
* @chainable
*/
stop: function (finish) {
var self = this,
node = self.node,
q,
queue = self.config.queue;
if (self.isResolved() || self.isRejected()) {
return self;
}
if (self.__waitTimeout) {
clearTimeout(self.__waitTimeout);
self.__waitTimeout = 0;
}
if (!self.isRunning() && !self.isPaused()) {
if (queue !== false) {
// queued but not start to run
Q.remove(node, queue, self);
}
return self;
}
self.doStop(finish);
Utils.removeRunningAnim(self);
Utils.removePausedAnim(self);
var defer = self.defer;
if (finish) {
syncComplete(self);
defer.resolve([self]);
} else {
defer.reject([self]);
}
if (queue !== false) {
// notify next anim to run in the same queue
q = Q.dequeue(node, queue);
if (q && q[0]) {
q[0].runInternal();
}
}
return self;
}
});
var Statics = AnimBase.Statics = {
isRunning: Utils.isElRunning,
isPaused: Utils.isElPaused,
stop: Utils.stopEl,
Q: Q
};
util.each(['pause', 'resume'], function (action) {
Statics[action] = function (node, queue) {
if (
// default queue
queue === null ||
// name of specified queue
typeof queue === 'string' ||
// anims not belong to any queue
queue === false
) {
return Utils.pauseOrResumeQueue(node, queue, action);
}
return Utils.pauseOrResumeQueue(node, undefined, action);
};
});
module.exports = AnimBase;
/*
yiminghe@gmail.com 2014-03-13
- anim alias to transition in css3 anim enabled browser
*/