2009-04-08 05:44:44 -04:00
|
|
|
/*
|
|
|
|
Animation Task Manager
|
|
|
|
|
|
|
|
Usage:
|
2009-04-08 06:11:37 -04:00
|
|
|
window['piro.sakura.ne.jp'].animationManager.addTask(
|
2009-04-09 21:32:03 -04:00
|
|
|
function(aTime, aBeginningValue, aTotalChange, aDuration) {
|
2009-04-08 06:11:37 -04:00
|
|
|
// some animation task runned by interval
|
2009-04-09 21:32:03 -04:00
|
|
|
var current = someEasingFunction(aTime, aBeginningValue, aTotalChange, aDuration);
|
2009-04-08 06:11:37 -04:00
|
|
|
target.style.left = current+'px';
|
2009-04-09 21:32:03 -04:00
|
|
|
return aTime > aDuration; // return true if the animation finished.
|
2009-04-08 06:11:37 -04:00
|
|
|
},
|
|
|
|
100, // beginning
|
2009-04-09 21:32:03 -04:00
|
|
|
200, // total change (so, the final value will be 100+200=300)
|
2010-09-01 04:34:30 -04:00
|
|
|
250, // msec, duration
|
|
|
|
window // the window (used by Firefox 4 animation frame API)
|
2009-04-08 06:11:37 -04:00
|
|
|
);
|
2009-04-08 05:44:44 -04:00
|
|
|
// stop all
|
|
|
|
window['piro.sakura.ne.jp'].animationManager.stop();
|
2009-04-09 21:35:01 -04:00
|
|
|
// restart after doing something
|
|
|
|
window['piro.sakura.ne.jp'].animationManager.start();
|
2009-04-08 05:44:44 -04:00
|
|
|
|
2012-10-13 14:31:06 -04:00
|
|
|
license: The MIT License, Copyright (c) 2009-2012 YUKI "Piro" Hiroshi
|
2011-06-10 14:32:34 -04:00
|
|
|
http://github.com/piroor/fxaddonlibs/blob/master/license.txt
|
2009-04-08 05:44:44 -04:00
|
|
|
original:
|
2011-06-10 14:32:34 -04:00
|
|
|
http://github.com/piroor/fxaddonlibs/blob/master/animationManager.js
|
2009-04-08 05:44:44 -04:00
|
|
|
*/
|
2010-06-22 19:46:51 -04:00
|
|
|
|
2010-06-26 11:13:11 -04:00
|
|
|
/* To work as a JS Code Module (*require jstimer.jsm)
|
2011-06-10 14:32:34 -04:00
|
|
|
http://github.com/piroor/fxaddonlibs/blob/master/jstimer.jsm */
|
2010-08-27 05:58:57 -04:00
|
|
|
if (typeof window == 'undefined' ||
|
|
|
|
(window && typeof window.constructor == 'function')) {
|
2010-06-22 21:32:31 -04:00
|
|
|
this.EXPORTED_SYMBOLS = ['animationManager'];
|
2010-06-22 19:46:51 -04:00
|
|
|
|
2010-06-26 11:13:11 -04:00
|
|
|
// If namespace.jsm is available, export symbols to the shared namespace.
|
2011-06-10 14:32:34 -04:00
|
|
|
// See: http://github.com/piroor/fxaddonlibs/blob/master/namespace.jsm
|
2010-06-22 19:46:51 -04:00
|
|
|
let ns = {};
|
2010-06-26 11:13:11 -04:00
|
|
|
try {
|
2010-12-06 07:04:52 -05:00
|
|
|
Components.utils.import('resource://treestyletab-modules/lib/namespace.jsm', ns);
|
2010-06-26 11:13:11 -04:00
|
|
|
/* var */ window = ns.getNamespaceFor('piro.sakura.ne.jp');
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
window = {};
|
|
|
|
}
|
2010-06-22 19:46:51 -04:00
|
|
|
if (!('setInterval' in window))
|
2010-12-06 07:04:52 -05:00
|
|
|
Components.utils.import('resource://treestyletab-modules/lib/jstimer.jsm', window);
|
2010-06-22 19:46:51 -04:00
|
|
|
}
|
|
|
|
|
2009-04-08 05:44:44 -04:00
|
|
|
(function() {
|
2012-11-11 08:30:45 -05:00
|
|
|
const currentRevision = 16;
|
2009-04-08 05:44:44 -04:00
|
|
|
|
|
|
|
if (!('piro.sakura.ne.jp' in window)) window['piro.sakura.ne.jp'] = {};
|
|
|
|
|
|
|
|
var loadedRevision = 'animationManager' in window['piro.sakura.ne.jp'] ?
|
|
|
|
window['piro.sakura.ne.jp'].animationManager.revision :
|
|
|
|
0 ;
|
|
|
|
var tasks = !loadedRevision ? [] : window['piro.sakura.ne.jp'].animationManager.tasks ;
|
2010-09-01 04:34:30 -04:00
|
|
|
var windows = !loadedRevision ? [] : window['piro.sakura.ne.jp'].animationManager._windows || [] ;
|
2009-04-08 05:44:44 -04:00
|
|
|
if (loadedRevision && loadedRevision > currentRevision) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var Cc = Components.classes;
|
|
|
|
var Ci = Components.interfaces;
|
|
|
|
|
2009-04-08 06:35:29 -04:00
|
|
|
if (tasks.length)
|
|
|
|
window['piro.sakura.ne.jp'].animationManager.stop();
|
|
|
|
|
2009-04-08 05:44:44 -04:00
|
|
|
window['piro.sakura.ne.jp'].animationManager = {
|
|
|
|
revision : currentRevision,
|
|
|
|
|
2012-11-08 11:33:42 -05:00
|
|
|
running : false,
|
|
|
|
|
2010-09-01 04:34:30 -04:00
|
|
|
addTask : function(aTask, aBeginningValue, aTotalChange, aDuration, aRelatedWindow)
|
2009-04-08 05:44:44 -04:00
|
|
|
{
|
|
|
|
if (!aTask) return;
|
2010-09-01 04:34:30 -04:00
|
|
|
|
2012-11-08 11:33:42 -05:00
|
|
|
if (this._windows.indexOf(aRelatedWindow) < 0)
|
|
|
|
this._windows.push(aRelatedWindow);
|
2010-09-01 04:34:30 -04:00
|
|
|
|
2009-04-08 05:44:44 -04:00
|
|
|
this.tasks.push({
|
|
|
|
task : aTask,
|
2012-09-02 21:25:08 -04:00
|
|
|
start : aRelatedWindow ? aRelatedWindow.mozAnimationStartTime : Date.now(),
|
2009-04-08 05:44:44 -04:00
|
|
|
beginning : aBeginningValue,
|
2009-04-09 21:32:03 -04:00
|
|
|
change : aTotalChange,
|
2010-09-01 04:34:30 -04:00
|
|
|
duration : aDuration,
|
|
|
|
window : aRelatedWindow
|
2009-04-08 05:44:44 -04:00
|
|
|
});
|
2010-09-01 04:34:30 -04:00
|
|
|
|
2012-11-11 08:30:45 -05:00
|
|
|
this.start();
|
2009-04-08 05:44:44 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
removeTask : function(aTask)
|
|
|
|
{
|
|
|
|
if (!aTask) return;
|
2012-08-09 14:49:07 -04:00
|
|
|
for (let i = this.tasks.length - 1; i > -1; i--)
|
|
|
|
{
|
|
|
|
let registeredTask = this.tasks[i];
|
|
|
|
if (registeredTask) {
|
|
|
|
if (registeredTask.task != aTask)
|
|
|
|
continue;
|
|
|
|
delete registeredTask.task;
|
|
|
|
delete registeredTask.start;
|
|
|
|
delete registeredTask.beginning;
|
|
|
|
delete registeredTask.change;
|
|
|
|
delete registeredTask.duration;
|
|
|
|
delete registeredTask.window;
|
|
|
|
}
|
|
|
|
this.tasks.splice(i, 1);
|
|
|
|
}
|
|
|
|
this._cleanUpWindows();
|
2009-04-08 05:44:44 -04:00
|
|
|
},
|
|
|
|
|
2009-04-09 21:35:01 -04:00
|
|
|
start : function()
|
|
|
|
{
|
2012-11-11 08:30:45 -05:00
|
|
|
if (!this._windows.length)
|
|
|
|
return;
|
|
|
|
this._windows.forEach(function(aWindow) {
|
|
|
|
if (this._animatingWindows.indexOf(aWindow) > -1)
|
|
|
|
return;
|
|
|
|
this._animatingWindows.push(aWindow);
|
|
|
|
let self = this;
|
|
|
|
aWindow.mozRequestAnimationFrame(function() {
|
|
|
|
self.processAnimationFrame(aWindow);
|
|
|
|
});
|
|
|
|
}, this);
|
2009-04-09 21:35:01 -04:00
|
|
|
},
|
|
|
|
|
2009-04-08 05:44:44 -04:00
|
|
|
stop : function()
|
|
|
|
{
|
2012-11-11 08:30:45 -05:00
|
|
|
this._animatingWindows = [];
|
2009-04-08 05:44:44 -04:00
|
|
|
},
|
|
|
|
|
2009-04-09 21:32:03 -04:00
|
|
|
removeAllTasks : function()
|
|
|
|
{
|
|
|
|
this.stop();
|
|
|
|
this.tasks = [];
|
2010-09-01 04:34:30 -04:00
|
|
|
this._windows = [];
|
2009-04-09 21:32:03 -04:00
|
|
|
},
|
|
|
|
|
2009-04-08 05:44:44 -04:00
|
|
|
tasks : tasks,
|
2010-09-01 04:34:30 -04:00
|
|
|
_windows : windows,
|
2011-11-30 12:22:15 -05:00
|
|
|
_animatingWindows : [],
|
2010-09-01 04:34:30 -04:00
|
|
|
|
|
|
|
_cleanUpWindows : function()
|
|
|
|
{
|
2012-11-08 11:33:42 -05:00
|
|
|
for (let i = this._windows.length - 1; i > -1; i--)
|
|
|
|
{
|
|
|
|
let w = this._windows[i];
|
2010-09-01 04:34:30 -04:00
|
|
|
if (this.tasks.some(function(aTask) {
|
2012-08-08 02:35:59 -04:00
|
|
|
return aTask && aTask.window && this._windows.indexOf(aTask.window) > -1;
|
2010-09-01 04:34:30 -04:00
|
|
|
}, this))
|
2012-11-08 11:33:42 -05:00
|
|
|
continue;
|
|
|
|
|
|
|
|
let index = this._animatingWindows.indexOf(w);
|
|
|
|
if (index > -1)
|
2011-11-30 12:22:15 -05:00
|
|
|
this._animatingWindows.splice(index, 1);
|
2012-11-08 11:33:42 -05:00
|
|
|
|
2012-11-11 08:30:45 -05:00
|
|
|
this._windows.splice(i, 1);
|
2012-11-08 11:33:42 -05:00
|
|
|
}
|
2010-09-01 04:34:30 -04:00
|
|
|
},
|
|
|
|
|
2011-11-30 12:22:15 -05:00
|
|
|
processAnimationFrame : function(aWindow)
|
2010-09-01 04:34:30 -04:00
|
|
|
{
|
2012-11-11 08:30:45 -05:00
|
|
|
if (this._animatingWindows.indexOf(aWindow) > -1) {
|
|
|
|
this.onAnimation(aWindow);
|
2011-06-10 14:32:34 -04:00
|
|
|
}
|
2010-09-01 04:34:30 -04:00
|
|
|
this._cleanUpWindows();
|
2012-11-11 08:30:45 -05:00
|
|
|
if (this._animatingWindows.indexOf(aWindow) > -1) {
|
2012-11-08 11:33:42 -05:00
|
|
|
let self = this;
|
|
|
|
aWindow.mozRequestAnimationFrame(function() {
|
|
|
|
self.processAnimationFrame(aWindow);
|
|
|
|
});
|
|
|
|
}
|
2010-09-01 04:34:30 -04:00
|
|
|
},
|
|
|
|
|
2012-11-11 08:30:45 -05:00
|
|
|
onAnimation : function(aWindow)
|
2009-04-08 05:44:44 -04:00
|
|
|
{
|
|
|
|
// task should return true if it finishes.
|
2012-09-02 21:25:08 -04:00
|
|
|
var now = Date.now();
|
2012-11-11 08:30:45 -05:00
|
|
|
for (let i = this.tasks.length - 1; i > -1; i--)
|
2012-08-09 14:49:07 -04:00
|
|
|
{
|
2012-11-11 08:30:45 -05:00
|
|
|
let task = this.tasks[i];
|
2009-04-08 06:03:17 -04:00
|
|
|
try {
|
2012-08-09 14:49:07 -04:00
|
|
|
if (task) {
|
|
|
|
if (aWindow && task.window != aWindow)
|
|
|
|
continue;
|
|
|
|
let time = Math.min(task.duration, now - task.start);
|
|
|
|
let finished = task.task(
|
|
|
|
time,
|
|
|
|
task.beginning,
|
|
|
|
task.change,
|
|
|
|
task.duration
|
|
|
|
);
|
|
|
|
if (!finished && (time < task.duration))
|
|
|
|
continue;
|
|
|
|
}
|
2009-04-08 06:03:17 -04:00
|
|
|
}
|
|
|
|
catch(e) {
|
2012-02-04 18:24:26 -05:00
|
|
|
dump(e+'\n'+e.stack+'\n');
|
2009-04-08 06:03:17 -04:00
|
|
|
}
|
2012-11-11 08:30:45 -05:00
|
|
|
this.tasks.splice(i, 1);
|
2012-08-09 14:49:07 -04:00
|
|
|
}
|
2009-04-08 05:44:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2009-04-09 21:37:29 -04:00
|
|
|
|
|
|
|
if (tasks.length)
|
|
|
|
window['piro.sakura.ne.jp'].animationManager.start();
|
2009-04-08 05:44:44 -04:00
|
|
|
})();
|
2010-06-22 19:46:51 -04:00
|
|
|
|
|
|
|
if (window != this) { // work as a JS Code Module
|
2010-06-22 21:32:31 -04:00
|
|
|
this.animationManager = window['piro.sakura.ne.jp'].animationManager;
|
2010-06-22 19:46:51 -04:00
|
|
|
}
|