3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js">(function(){
\r
9 var EXTLIB = Ext.lib,
\r
10 noNegatives = /width|height|opacity|padding/i,
\r
11 offsetAttribute = /^((width|height)|(top|left))$/,
\r
12 defaultUnit = /width|height|top$|bottom$|left$|right$/i,
\r
13 offsetUnit = /\d+(em|%|en|ex|pt|in|cm|mm|pc)$/i,
\r
14 isset = function(v){
\r
15 return typeof v !== 'undefined';
\r
22 motion : function(el, args, duration, easing, cb, scope) {
\r
23 return this.run(el, args, duration, easing, cb, scope, Ext.lib.Motion);
\r
26 run : function(el, args, duration, easing, cb, scope, type) {
\r
27 type = type || Ext.lib.AnimBase;
\r
28 if (typeof easing == "string") {
\r
29 easing = Ext.lib.Easing[easing];
\r
31 var anim = new type(el, args, duration, easing);
\r
32 anim.animateX(function() {
\r
33 if(Ext.isFunction(cb)){
\r
41 EXTLIB.AnimBase = function(el, attributes, duration, method) {
\r
43 this.init(el, attributes, duration, method);
\r
47 EXTLIB.AnimBase.prototype = {
\r
48 doMethod: function(attr, start, end) {
\r
50 return me.method(me.curFrame, start, end - start, me.totalFrames);
\r
54 setAttr: function(attr, val, unit) {
\r
55 if (noNegatives.test(attr) && val < 0) {
\r
58 Ext.fly(this.el, '_anim').setStyle(attr, val + unit);
\r
62 getAttr: function(attr) {
\r
63 var el = Ext.fly(this.el),
\r
64 val = el.getStyle(attr),
\r
65 a = offsetAttribute.exec(attr) || []
\r
67 if (val !== 'auto' && !offsetUnit.test(val)) {
\r
68 return parseFloat(val);
\r
71 return (!!(a[2]) || (el.getStyle('position') == 'absolute' && !!(a[3]))) ? el.dom['offset' + a[0].charAt(0).toUpperCase() + a[0].substr(1)] : 0;
\r
75 getDefaultUnit: function(attr) {
\r
76 return defaultUnit.test(attr) ? 'px' : '';
\r
79 animateX : function(callback, scope) {
\r
82 me.onComplete.removeListener(f);
\r
83 if (Ext.isFunction(callback)) {
\r
84 callback.call(scope || me, me);
\r
87 me.onComplete.addListener(f, me);
\r
92 setRunAttr: function(attr) {
\r
94 a = this.attributes[attr],
\r
99 ra = (this.runAttrs[attr] = {}),
\r
102 if (!isset(to) && !isset(by)){
\r
106 var start = isset(from) ? from : me.getAttr(attr);
\r
109 }else if(isset(by)) {
\r
110 if (Ext.isArray(start)){
\r
112 Ext.each(start, function(v, i){
\r
113 end[i] = v + by[i];
\r
123 unit: isset(unit) ? unit : me.getDefaultUnit(attr)
\r
128 init: function(el, attributes, duration, method) {
\r
131 mgr = EXTLIB.AnimMgr;
\r
136 el: Ext.getDom(el),
\r
137 attributes: attributes || {},
\r
138 duration: duration || 1,
\r
139 method: method || EXTLIB.Easing.easeNone,
\r
142 totalFrames: mgr.fps,
\r
144 animate: function(){
\r
153 me.totalFrames = me.useSec ? Math.ceil(mgr.fps * d) : d;
\r
154 mgr.registerElement(me);
\r
157 stop: function(finish){
\r
161 me.curFrame = me.totalFrames;
\r
162 me._onTween.fire();
\r
168 var onStart = function(){
\r
174 for(attr in this.attributes){
\r
175 this.setRunAttr(attr);
\r
178 me.isAnimated = true;
\r
179 me.startTime = now();
\r
184 var onTween = function(){
\r
188 duration: now() - me.startTime,
\r
189 curFrame: me.curFrame
\r
192 var ra = me.runAttrs;
\r
193 for (var attr in ra) {
\r
194 this.setAttr(attr, me.doMethod(attr, ra[attr].start, ra[attr].end), ra[attr].unit);
\r
200 var onComplete = function() {
\r
202 actual = (now() - me.startTime) / 1000,
\r
205 frames: actualFrames,
\r
206 fps: actualFrames / actual
\r
209 me.isAnimated = false;
\r
211 me.onComplete.fire(data);
\r
214 me.onStart = new Ext.util.Event(me);
\r
215 me.onTween = new Ext.util.Event(me);
\r
216 me.onComplete = new Ext.util.Event(me);
\r
217 (me._onStart = new Ext.util.Event(me)).addListener(onStart);
\r
218 (me._onTween = new Ext.util.Event(me)).addListener(onTween);
\r
219 (me._onComplete = new Ext.util.Event(me)).addListener(onComplete);
\r
224 Ext.lib.AnimMgr = new function() {
\r
234 registerElement: function(tween){
\r
237 tween._onStart.fire();
\r
241 unRegister: function(tween, index){
\r
242 tween._onComplete.fire();
\r
243 index = index || getIndex(tween);
\r
245 queue.splice(index, 1);
\r
248 if (--tweenCount <= 0) {
\r
254 if(thread === null){
\r
255 thread = setInterval(me.run, me.delay);
\r
259 stop: function(tween){
\r
261 clearInterval(thread);
\r
262 for(var i = 0, len = queue.length; i < len; ++i){
\r
263 if(queue[0].isAnimated){
\r
264 me.unRegister(queue[0], 0);
\r
272 me.unRegister(tween);
\r
278 Ext.each(queue, function(tween){
\r
279 if(tween && tween.isAnimated){
\r
280 tf = tween.totalFrames;
\r
281 if(tween.curFrame < tf || tf === null){
\r
284 correctFrame(tween);
\r
286 tween._onTween.fire();
\r
295 var getIndex = function(anim) {
\r
297 Ext.each(queue, function(item, idx){
\r
307 var correctFrame = function(tween) {
\r
308 var frames = tween.totalFrames,
\r
309 frame = tween.curFrame,
\r
310 duration = tween.duration,
\r
311 expected = (frame * duration * 1000 / frames),
\r
312 elapsed = (now() - tween.startTime),
\r
315 if(elapsed < duration * 1000){
\r
316 tweak = Math.round((elapsed / expected - 1) * frame);
\r
318 tweak = frames - (frame + 1);
\r
320 if(tweak > 0 && isFinite(tweak)){
\r
321 if(tween.curFrame + tweak >= frames){
\r
322 tweak = frames - (frame + 1);
\r
324 tween.curFrame += tweak;
\r
329 EXTLIB.Bezier = new function() {
\r
331 this.getPosition = function(points, t) {
\r
332 var n = points.length,
\r
338 for (i = 0; i < n; ++i) {
\r
339 tmp[i] = [points[i][0], points[i][1]];
\r
342 for (j = 1; j < n; ++j) {
\r
343 for (i = 0; i < n - j; ++i) {
\r
344 tmp[i][0] = c * tmp[i][0] + t * tmp[parseInt(i + 1, 10)][0];
\r
345 tmp[i][1] = c * tmp[i][1] + t * tmp[parseInt(i + 1, 10)][1];
\r
349 return [ tmp[0][0], tmp[0][1] ];
\r
356 easeNone: function (t, b, c, d) {
\r
357 return c * t / d + b;
\r
361 easeIn: function (t, b, c, d) {
\r
362 return c * (t /= d) * t + b;
\r
366 easeOut: function (t, b, c, d) {
\r
367 return -c * (t /= d) * (t - 2) + b;
\r
372 EXTLIB.Motion = function(el, attributes, duration, method) {
\r
374 EXTLIB.Motion.superclass.constructor.call(this, el, attributes, duration, method);
\r
378 Ext.extend(EXTLIB.Motion, Ext.lib.AnimBase);
\r
380 var superclass = EXTLIB.Motion.superclass,
\r
381 proto = EXTLIB.Motion.prototype,
\r
382 pointsRe = /^points$/i;
\r
384 Ext.apply(EXTLIB.Motion.prototype, {
\r
385 setAttr: function(attr, val, unit){
\r
387 setAttr = superclass.setAttr;
\r
389 if (pointsRe.test(attr)) {
\r
390 unit = unit || 'px';
\r
391 setAttr.call(me, 'left', val[0], unit);
\r
392 setAttr.call(me, 'top', val[1], unit);
\r
394 setAttr.call(me, attr, val, unit);
\r
398 getAttr: function(attr){
\r
400 getAttr = superclass.getAttr;
\r
402 return pointsRe.test(attr) ? [getAttr.call(me, 'left'), getAttr.call(me, 'top')] : getAttr.call(me, attr);
\r
405 doMethod: function(attr, start, end){
\r
408 return pointsRe.test(attr)
\r
409 ? EXTLIB.Bezier.getPosition(me.runAttrs[attr], me.method(me.curFrame, 0, 100, me.totalFrames) / 100)
\r
410 : superclass.doMethod.call(me, attr, start, end);
\r
413 setRunAttr: function(attr){
\r
414 if(pointsRe.test(attr)){
\r
418 points = this.attributes.points,
\r
419 control = points.control || [],
\r
420 from = points.from,
\r
431 if(control.length > 0 && !Ext.isArray(control[0])){
\r
432 control = [control];
\r
436 for (i = 0,len = control.length; i < len; ++i) {
\r
437 tmp[i] = control[i];
\r
443 Ext.fly(el, '_anim').position();
\r
444 DOM.setXY(el, isset(from) ? from : DOM.getXY(el));
\r
445 start = me.getAttr('points');
\r
449 end = translateValues.call(me, to, start);
\r
450 for (i = 0,len = control.length; i < len; ++i) {
\r
451 control[i] = translateValues.call(me, control[i], start);
\r
453 } else if (isset(by)) {
\r
454 end = [start[0] + by[0], start[1] + by[1]];
\r
456 for (i = 0,len = control.length; i < len; ++i) {
\r
457 control[i] = [ start[0] + control[i][0], start[1] + control[i][1] ];
\r
461 ra = this.runAttrs[attr] = [start];
\r
462 if (control.length > 0) {
\r
463 ra = ra.concat(control);
\r
466 ra[ra.length] = end;
\r
468 superclass.setRunAttr.call(this, attr);
\r
473 var translateValues = function(val, start) {
\r
474 var pageXY = EXTLIB.Dom.getXY(this.el);
\r
475 return [val[0] - pageXY[0] + start[0], val[1] - pageXY[1] + start[1]];
\r