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
|
|
|
|
2010-08-26 11:40:13 -04:00
|
|
|
license: The MIT License, Copyright (c) 2009-2010 SHIMODA "Piro" Hiroshi
|
2009-04-08 05:44:44 -04:00
|
|
|
http://www.cozmixng.org/repos/piro/fx3-compatibility-lib/trunk/license.txt
|
|
|
|
original:
|
|
|
|
http://www.cozmixng.org/repos/piro/fx3-compatibility-lib/trunk/animationManager.js
|
|
|
|
*/
|
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)
|
2010-06-22 21:32:31 -04:00
|
|
|
http://www.cozmixng.org/repos/piro/fx3-compatibility-lib/trunk/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.
|
|
|
|
// See: http://www.cozmixng.org/repos/piro/fx3-compatibility-lib/trunk/namespace.jsm
|
2010-06-22 19:46:51 -04:00
|
|
|
let ns = {};
|
2010-06-26 11:13:11 -04:00
|
|
|
try {
|
|
|
|
Components.utils.import('resource://treestyletab-modules/namespace.jsm', ns);
|
|
|
|
/* var */ window = ns.getNamespaceFor('piro.sakura.ne.jp');
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
window = {};
|
|
|
|
}
|
2010-06-22 19:46:51 -04:00
|
|
|
if (!('setInterval' in window))
|
|
|
|
Components.utils.import('resource://treestyletab-modules/jstimer.jsm', window);
|
|
|
|
}
|
|
|
|
|
2009-04-08 05:44:44 -04:00
|
|
|
(function() {
|
2010-09-01 05:27:32 -04:00
|
|
|
const currentRevision = 7;
|
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,
|
|
|
|
|
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
|
|
|
|
|
|
|
if (this._isAnimationFrameAvailable(aRelatedWindow)) {
|
|
|
|
if (this._windows.indexOf(aRelatedWindow) < 0)
|
|
|
|
this._windows.push(aRelatedWindow);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
aRelatedWindow = null;
|
|
|
|
}
|
|
|
|
|
2009-04-08 05:44:44 -04:00
|
|
|
this.tasks.push({
|
|
|
|
task : aTask,
|
2010-09-01 04:34:30 -04:00
|
|
|
start : aRelatedWindow ? aRelatedWindow.mozAnimationStartTime : (new Date()).getTime(),
|
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
|
|
|
|
2009-04-09 21:35:01 -04:00
|
|
|
if (this.tasks.length == 1)
|
|
|
|
this.start();
|
2009-04-08 05:44:44 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
removeTask : function(aTask)
|
|
|
|
{
|
|
|
|
if (!aTask) return;
|
|
|
|
var task;
|
|
|
|
for (var i in this.tasks)
|
|
|
|
{
|
|
|
|
task = this.tasks[i];
|
|
|
|
if (task.task != aTask) continue;
|
|
|
|
delete task.task;
|
|
|
|
delete task.start;
|
|
|
|
delete task.beginning;
|
2009-04-09 21:32:03 -04:00
|
|
|
delete task.change;
|
|
|
|
delete task.duration;
|
2010-09-01 04:34:30 -04:00
|
|
|
delete task.window;
|
2009-04-08 05:44:44 -04:00
|
|
|
this.tasks.splice(i, 1);
|
2010-09-01 04:34:30 -04:00
|
|
|
this._cleanUpWindows();
|
2009-04-08 05:44:44 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!this.tasks.length)
|
|
|
|
this.stop();
|
|
|
|
},
|
|
|
|
|
2009-04-09 21:35:01 -04:00
|
|
|
start : function()
|
|
|
|
{
|
|
|
|
this.stop();
|
2010-09-01 04:34:30 -04:00
|
|
|
if (this.tasks.some(function(aTask) {
|
|
|
|
return !aTask.window;
|
|
|
|
})) {
|
|
|
|
this.timer = window.setInterval(
|
|
|
|
this.onAnimation,
|
|
|
|
this.interval,
|
|
|
|
this
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (this._windows.length) { // Firefox 4-
|
|
|
|
this._windows.forEach(function(aWindow) {
|
|
|
|
if (this._listeningWindows.indexOf(aWindow) < 0) {
|
|
|
|
aWindow.addEventListener('MozBeforePaint', this, false);
|
|
|
|
this._listeningWindows.push(aWindow);
|
|
|
|
}
|
|
|
|
aWindow.mozRequestAnimationFrame();
|
|
|
|
}, this);
|
|
|
|
}
|
2009-04-09 21:35:01 -04:00
|
|
|
},
|
|
|
|
|
2009-04-08 05:44:44 -04:00
|
|
|
stop : function()
|
|
|
|
{
|
2010-09-01 04:34:30 -04:00
|
|
|
if (this.timer) {
|
|
|
|
window.clearInterval(this.timer);
|
|
|
|
this.timer = null;
|
|
|
|
}
|
|
|
|
if (this._listeningWindows.length) { // Firefox 4-
|
|
|
|
this._listeningWindows.forEach(function(aWindow) {
|
|
|
|
aWindow.removeEventListener('MozBeforePaint', this, false);
|
|
|
|
}, this);
|
|
|
|
this._listeningWindows = [];
|
|
|
|
}
|
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,
|
|
|
|
interval : 10,
|
|
|
|
timer : null,
|
|
|
|
|
2010-09-01 04:34:30 -04:00
|
|
|
// Firefox 4 animation frame API
|
|
|
|
_windows : windows,
|
|
|
|
_listeningWindows : [],
|
|
|
|
|
|
|
|
_isAnimationFrameAvailable : function(aWindow)
|
|
|
|
{
|
|
|
|
return aWindow && 'mozRequestAnimationFrame' in aWindow;
|
|
|
|
},
|
|
|
|
|
|
|
|
_cleanUpWindows : function()
|
|
|
|
{
|
|
|
|
this._windows = this._windows.filter(function(aWindow) {
|
|
|
|
if (this.tasks.some(function(aTask) {
|
|
|
|
return aTask.window && this._windows.indexOf(aTask.window) > -1;
|
|
|
|
}, this))
|
|
|
|
return true;
|
|
|
|
let index = this._listeningWindows.indexOf(aWindow);
|
|
|
|
if (index > -1) {
|
|
|
|
this._listeningWindows.splice(index, 1);
|
|
|
|
aWindow.removeEventListener('MozBeforePaint', this, false);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}, this);
|
|
|
|
},
|
|
|
|
|
|
|
|
handleEvent : function(aEvent)
|
|
|
|
{
|
|
|
|
this.onAnimation(this, aEvent);
|
|
|
|
this._cleanUpWindows();
|
|
|
|
if (this._listeningWindows.indexOf(aEvent.target.defaultView) > -1)
|
|
|
|
aEvent.target.defaultView.mozRequestAnimationFrame();
|
|
|
|
},
|
|
|
|
|
|
|
|
onAnimation : function(aSelf, aEvent)
|
2009-04-08 05:44:44 -04:00
|
|
|
{
|
|
|
|
// task should return true if it finishes.
|
2010-09-01 04:34:30 -04:00
|
|
|
var now = aEvent ? aEvent.timeStamp : (new Date()).getTime() ;
|
2009-11-25 05:35:52 -05:00
|
|
|
var tasks = aSelf.tasks;
|
|
|
|
aSelf.tasks = [null];
|
|
|
|
tasks = tasks.filter(function(aTask) {
|
2010-09-01 04:34:30 -04:00
|
|
|
if (!aTask)
|
|
|
|
return false;
|
|
|
|
if (aEvent && aTask.window != aEvent.target.defaultView)
|
|
|
|
return true;
|
2009-04-08 06:03:17 -04:00
|
|
|
try {
|
2010-09-01 05:27:32 -04:00
|
|
|
var time = Math.min(aTask.duration, now - aTask.start);
|
|
|
|
var finished = aTask.task(
|
|
|
|
time,
|
|
|
|
aTask.beginning,
|
|
|
|
aTask.change,
|
|
|
|
aTask.duration
|
|
|
|
);
|
|
|
|
return !finished && (time < aTask.duration);
|
2009-04-08 06:03:17 -04:00
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
}
|
2009-04-08 07:07:24 -04:00
|
|
|
return false;
|
2009-04-08 05:44:44 -04:00
|
|
|
});
|
2009-11-25 05:35:52 -05:00
|
|
|
aSelf.tasks = aSelf.tasks.slice(1).concat(tasks);
|
2009-04-08 05:44:44 -04:00
|
|
|
if (!aSelf.tasks.length)
|
|
|
|
aSelf.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
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
|
|
|
}
|