Subversion Repositories Applications.papyrus

Rev

Rev 1371 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1265 jp_milcent 1
/** $Id: alphaAPI.js,v 1.1 2007-03-16 12:39:35 jp_milcent Exp $ */
2
// {{{ license
3
 
4
/*
5
 * Copyright 2002-2005 Dan Allen, Mojavelinux.com (dan.allen@mojavelinux.com)
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *      http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
 
20
// }}}
21
// {{{ intro
22
 
23
/**
24
 * Title: alphaAPI
25
 * Original Author: chrisken
26
 * Original Url: http://www.cs.utexas.edu/users/chrisken/alphaapi.html
27
 *
28
 * Modified by Dan Allen <dan.allen@mojavelinux.com>
29
 * Note: When the stopAlpha is reached and it is equal to 0, the element's
30
 * style is set to display: none to fix a bug in domTT
31
 */
32
 
33
// }}}
34
function alphaAPI(element, fadeInDelay, fadeOutDelay, startAlpha, stopAlpha, offsetTime, deltaAlpha)
35
{
36
	// {{{ properties
37
 
38
	this.element = typeof(element) == 'object' ? element : document.getElementById(element);
39
	this.fadeInDelay = fadeInDelay || 40;
40
	this.fadeOutDelay = fadeOutDelay || this.fadeInDelay;
41
	this.startAlpha = startAlpha;
42
	this.stopAlpha = stopAlpha;
43
	// make sure a filter exists so an error is not thrown
44
	if (typeof(this.element.filters) == 'object')
45
	{
46
		if (typeof(this.element.filters.alpha) == 'undefined')
47
		{
48
			this.element.style.filter += 'alpha(opacity=100)';
49
		}
50
	}
51
 
52
	this.offsetTime = (offsetTime || 0) * 1000;
53
	this.deltaAlpha = deltaAlpha || 10;
54
	this.timer = null;
55
	this.paused = false;
56
	this.started = false;
57
	this.cycle = false;
58
	this.command = function() {};
59
    return this;
60
 
61
	// }}}
62
}
63
 
64
// use prototype methods to save memory
65
// {{{ repeat()
66
 
67
alphaAPI.prototype.repeat = function(repeat)
68
{
69
    this.cycle = repeat ? true : false;
70
}
71
 
72
// }}}
73
// {{{ setAlphaBy()
74
 
75
alphaAPI.prototype.setAlphaBy = function(deltaAlpha)
76
{
77
    this.setAlpha(this.getAlpha() + deltaAlpha);
78
}
79
 
80
// }}}
81
// {{{ toggle()
82
 
83
alphaAPI.prototype.toggle = function()
84
{
85
    if (!this.started)
86
    {
87
        this.start();
88
    }
89
    else if (this.paused)
90
    {
91
        this.unpause();
92
    }
93
    else
94
    {
95
        this.pause();
96
    }
97
}
98
 
99
// }}}
100
// {{{ timeout()
101
 
102
alphaAPI.prototype.timeout = function(command, delay)
103
{
104
    this.command = command;
105
    this.timer = setTimeout(command, delay);
106
}
107
 
108
// }}}
109
// {{{ setAlpha()
110
 
111
alphaAPI.prototype.setAlpha = function(opacity)
112
{
113
    if (typeof(this.element.filters) == 'object')
114
    {
115
        this.element.filters.alpha.opacity = opacity;
116
    }
117
    else if (this.element.style.setProperty)
118
    {
119
        this.element.style.setProperty('opacity', opacity / 100, '');
120
		// handle the case of mozilla < 1.7
121
        this.element.style.setProperty('-moz-opacity', opacity / 100, '');
122
		// handle the case of old kthml
123
        this.element.style.setProperty('-khtml-opacity', opacity / 100, '');
124
    }
125
}
126
 
127
// }}}
128
// {{{ getAlpha()
129
 
130
alphaAPI.prototype.getAlpha = function()
131
{
132
    if (typeof(this.element.filters) == 'object')
133
    {
134
        return this.element.filters.alpha.opacity;
135
    }
136
    else if (this.element.style.getPropertyValue)
137
    {
138
		var opacityValue = this.element.style.getPropertyValue('opacity');
139
		// handle the case of mozilla < 1.7
140
		if (opacityValue == '')
141
		{
142
			opacityValue = this.element.style.getPropertyValue('-moz-opacity');
143
		}
144
 
145
		// handle the case of old khtml
146
		if (opacityValue == '')
147
		{
148
			opacityValue = this.element.style.getPropertyValue('-khtml-opacity');
149
		}
150
 
151
        return opacityValue * 100;
152
    }
153
 
154
    return 100;
155
}
156
 
157
// }}}
158
// {{{ start()
159
 
160
alphaAPI.prototype.start = function()
161
{
162
    this.started = true;
163
    this.setAlpha(this.startAlpha);
164
    // determine direction
165
    if (this.startAlpha > this.stopAlpha)
166
    {
167
        var instance = this;
168
        this.timeout(function() { instance.fadeOut(); }, this.offsetTime);
169
    }
170
    else
171
    {
172
        var instance = this;
173
        this.timeout(function() { instance.fadeIn(); }, this.offsetTime);
174
    }
175
}
176
 
177
// }}}
178
// {{{ stop()
179
 
180
alphaAPI.prototype.stop = function()
181
{
182
    this.started = false;
183
    this.setAlpha(this.stopAlpha);
184
	if (this.stopAlpha == 0)
185
	{
186
		this.element.style.display = 'none';
187
	}
188
 
189
    this.stopTimer();
190
    this.command = function() {};
191
}
192
 
193
// }}}
194
// {{{ reset()
195
 
196
alphaAPI.prototype.reset = function()
197
{
198
    this.started = false;
199
    this.setAlpha(this.startAlpha);
200
    this.stopTimer();
201
    this.command = function() {};
202
}
203
 
204
// }}}
205
// {{{ pause()
206
 
207
alphaAPI.prototype.pause = function()
208
{
209
    this.paused = true;
210
    this.stopTimer();
211
}
212
 
213
// }}}
214
// {{{ unpause()
215
 
216
alphaAPI.prototype.unpause = function()
217
{
218
    this.paused = false;
219
    if (!this.started)
220
    {
221
        this.start();
222
    }
223
    else
224
    {
225
        this.command();
226
    }
227
}
228
 
229
// }}}
230
// {{{ stopTimer()
231
 
232
alphaAPI.prototype.stopTimer = function()
233
{
234
    clearTimeout(this.timer);
235
    this.timer = null;
236
}
237
 
238
// }}}
239
// {{{ fadeOut()
240
 
241
alphaAPI.prototype.fadeOut = function()
242
{
243
    this.stopTimer();
244
    if (this.getAlpha() > this.stopAlpha)
245
    {
246
        this.setAlphaBy(-1 * this.deltaAlpha);
247
        var instance = this;
248
        this.timeout(function() { instance.fadeOut(); }, this.fadeOutDelay);
249
    }
250
    else
251
    {
252
        if (this.cycle)
253
        {
254
            var instance = this;
255
            this.timeout(function() { instance.fadeIn(); }, this.fadeInDelay);
256
        }
257
        else
258
        {
259
			if (this.stopAlpha == 0)
260
			{
261
				this.element.style.display = 'none';
262
			}
263
            this.started = false;
264
        }
265
    }
266
}
267
 
268
// }}}
269
// {{{ fadeIn()
270
 
271
alphaAPI.prototype.fadeIn = function()
272
{
273
    this.stopTimer();
274
    if (this.getAlpha() < this.startAlpha)
275
    {
276
        this.setAlphaBy(this.deltaAlpha);
277
        var instance = this;
278
        this.timeout(function() { instance.fadeIn(); }, this.fadeInDelay);
279
    }
280
    else
281
    {
282
        if (this.cycle)
283
        {
284
            var instance = this;
285
            this.timeout(function() { instance.fadeOut(); }, this.fadeOutDelay);
286
        }
287
        else
288
        {
289
            this.started = false;
290
        }
291
    }
292
}
293
 
294
// }}}