1 // Copyright (c) 2005-2007 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
3 // Justin Palmer (http://encytemedia.com/)
4 // Mark Pilgrim (http://diveintomark.org/)
7 // script.aculo.us is freely distributable under the terms of an MIT-style license.
8 // For details, see the script.aculo.us web site: http://script.aculo.us/
10 // converts rgb() and #xxx to #xxxxxx format,
11 // returns self (or first argument) if not convertable
13 JSAN.use('Prototype');
15 String.prototype.parseColor = function() {
17 if (this.slice(0,4) == 'rgb(') {
18 var cols = this.slice(4,this.length-1).split(',');
19 var i=0; do { color += parseInt(cols[i]).toColorPart() } while (++i<3);
21 if (this.slice(0,1) == '#') {
22 if (this.length==4) for(var i=1;i<4;i++) color += (this.charAt(i) + this.charAt(i)).toLowerCase();
23 if (this.length==7) color = this.toLowerCase();
26 return (color.length==7 ? color : (arguments[0] || this));
29 /*--------------------------------------------------------------------------*/
31 Element.collectTextNodes = function(element) {
32 return $A($(element).childNodes).collect( function(node) {
33 return (node.nodeType==3 ? node.nodeValue :
34 (node.hasChildNodes() ? Element.collectTextNodes(node) : ''));
35 }).flatten().join('');
38 Element.collectTextNodesIgnoreClass = function(element, className) {
39 return $A($(element).childNodes).collect( function(node) {
40 return (node.nodeType==3 ? node.nodeValue :
41 ((node.hasChildNodes() && !Element.hasClassName(node,className)) ?
42 Element.collectTextNodesIgnoreClass(node, className) : ''));
43 }).flatten().join('');
46 Element.setContentZoom = function(element, percent) {
48 element.setStyle({fontSize: (percent/100) + 'em'});
49 if (Prototype.Browser.WebKit) window.scrollBy(0,0);
53 Element.getInlineOpacity = function(element){
54 return $(element).style.opacity || '';
57 Element.forceRerendering = function(element) {
60 var n = document.createTextNode(' ');
61 element.appendChild(n);
62 element.removeChild(n);
66 /*--------------------------------------------------------------------------*/
69 _elementDoesNotExistError: {
70 name: 'ElementDoesNotExistError',
71 message: 'The specified DOM element does not exist, but is required for this effect to operate'
75 sinoidal: function(pos) {
76 return (-Math.cos(pos*Math.PI)/2) + 0.5;
78 reverse: function(pos) {
81 flicker: function(pos) {
82 var pos = ((-Math.cos(pos*Math.PI)/4) + 0.75) + Math.random()/4;
83 return pos > 1 ? 1 : pos;
85 wobble: function(pos) {
86 return (-Math.cos(pos*Math.PI*(9*pos))/2) + 0.5;
88 pulse: function(pos, pulses) {
91 ((pos % (1/pulses)) * pulses).round() == 0 ?
92 ((pos * pulses * 2) - (pos * pulses * 2).floor()) :
93 1 - ((pos * pulses * 2) - (pos * pulses * 2).floor())
96 spring: function(pos) {
97 return 1 - (Math.cos(pos * 4.5 * Math.PI) * Math.exp(-pos * 6));
102 full: function(pos) {
107 duration: 1.0, // seconds
108 fps: 100, // 100= assume 66fps max.
109 sync: false, // true for combining
115 tagifyText: function(element) {
116 var tagifyStyle = 'position:relative';
117 if (Prototype.Browser.IE) tagifyStyle += ';zoom:1';
119 element = $(element);
120 $A(element.childNodes).each( function(child) {
121 if (child.nodeType==3) {
122 child.nodeValue.toArray().each( function(character) {
123 element.insertBefore(
124 new Element('span', {style: tagifyStyle}).update(
125 character == ' ' ? String.fromCharCode(160) : character),
128 Element.remove(child);
132 multiple: function(element, effect) {
134 if (((typeof element == 'object') ||
135 Object.isFunction(element)) &&
139 elements = $(element).childNodes;
141 var options = Object.extend({
144 }, arguments[2] || { });
145 var masterDelay = options.delay;
147 $A(elements).each( function(element, index) {
148 new effect(element, Object.extend(options, { delay: index * options.speed + masterDelay }));
152 'slide': ['SlideDown','SlideUp'],
153 'blind': ['BlindDown','BlindUp'],
154 'appear': ['Appear','Fade']
156 toggle: function(element, effect) {
157 element = $(element);
158 effect = (effect || 'appear').toLowerCase();
159 var options = Object.extend({
160 queue: { position:'end', scope:(element.id || 'global'), limit: 1 }
161 }, arguments[2] || { });
162 Effect[element.visible() ?
163 Effect.PAIRS[effect][1] : Effect.PAIRS[effect][0]](element, options);
167 Effect.DefaultOptions.transition = Effect.Transitions.sinoidal;
169 /* ------------- core effects ------------- */
171 Effect.ScopedQueue = Class.create(Enumerable, {
172 initialize: function() {
174 this.interval = null;
176 _each: function(iterator) {
177 this.effects._each(iterator);
179 add: function(effect) {
180 var timestamp = new Date().getTime();
182 var position = Object.isString(effect.options.queue) ?
183 effect.options.queue : effect.options.queue.position;
187 // move unstarted effects after this effect
188 this.effects.findAll(function(e){ return e.state=='idle' }).each( function(e) {
189 e.startOn += effect.finishOn;
190 e.finishOn += effect.finishOn;
194 timestamp = this.effects.pluck('startOn').max() || timestamp;
197 // start effect after last queued effect has finished
198 timestamp = this.effects.pluck('finishOn').max() || timestamp;
202 effect.startOn += timestamp;
203 effect.finishOn += timestamp;
205 if (!effect.options.queue.limit || (this.effects.length < effect.options.queue.limit))
206 this.effects.push(effect);
209 this.interval = setInterval(this.loop.bind(this), 15);
211 remove: function(effect) {
212 this.effects = this.effects.reject(function(e) { return e==effect });
213 if (this.effects.length == 0) {
214 clearInterval(this.interval);
215 this.interval = null;
219 var timePos = new Date().getTime();
220 for(var i=0, len=this.effects.length;i<len;i++)
221 this.effects[i] && this.effects[i].loop(timePos);
227 get: function(queueName) {
228 if (!Object.isString(queueName)) return queueName;
230 return this.instances.get(queueName) ||
231 this.instances.set(queueName, new Effect.ScopedQueue());
234 Effect.Queue = Effect.Queues.get('global');
236 Effect.Base = Class.create({
238 start: function(options) {
239 function codeForEvent(options,eventName){
241 (options[eventName+'Internal'] ? 'this.options.'+eventName+'Internal(this);' : '') +
242 (options[eventName] ? 'this.options.'+eventName+'(this);' : '')
245 if (options && options.transition === false) options.transition = Effect.Transitions.linear;
246 this.options = Object.extend(Object.extend({ },Effect.DefaultOptions), options || { });
247 this.currentFrame = 0;
249 this.startOn = this.options.delay*1000;
250 this.finishOn = this.startOn+(this.options.duration*1000);
251 this.fromToDelta = this.options.to-this.options.from;
252 this.totalTime = this.finishOn-this.startOn;
253 this.totalFrames = this.options.fps*this.options.duration;
255 eval('this.render = function(pos){ '+
256 'if (this.state=="idle"){this.state="running";'+
257 codeForEvent(this.options,'beforeSetup')+
258 (this.setup ? 'this.setup();':'')+
259 codeForEvent(this.options,'afterSetup')+
260 '};if (this.state=="running"){'+
261 'pos=this.options.transition(pos)*'+this.fromToDelta+'+'+this.options.from+';'+
262 'this.position=pos;'+
263 codeForEvent(this.options,'beforeUpdate')+
264 (this.update ? 'this.update(pos);':'')+
265 codeForEvent(this.options,'afterUpdate')+
268 this.event('beforeStart');
269 if (!this.options.sync)
270 Effect.Queues.get(Object.isString(this.options.queue) ?
271 'global' : this.options.queue.scope).add(this);
273 loop: function(timePos) {
274 if (timePos >= this.startOn) {
275 if (timePos >= this.finishOn) {
278 this.event('beforeFinish');
279 if (this.finish) this.finish();
280 this.event('afterFinish');
283 var pos = (timePos - this.startOn) / this.totalTime,
284 frame = (pos * this.totalFrames).round();
285 if (frame > this.currentFrame) {
287 this.currentFrame = frame;
292 if (!this.options.sync)
293 Effect.Queues.get(Object.isString(this.options.queue) ?
294 'global' : this.options.queue.scope).remove(this);
295 this.state = 'finished';
297 event: function(eventName) {
298 if (this.options[eventName + 'Internal']) this.options[eventName + 'Internal'](this);
299 if (this.options[eventName]) this.options[eventName](this);
301 inspect: function() {
303 for(property in this)
304 if (!Object.isFunction(this[property])) data.set(property, this[property]);
305 return '#<Effect:' + data.inspect() + ',options:' + $H(this.options).inspect() + '>';
309 Effect.Parallel = Class.create(Effect.Base, {
310 initialize: function(effects) {
311 this.effects = effects || [];
312 this.start(arguments[1]);
314 update: function(position) {
315 this.effects.invoke('render', position);
317 finish: function(position) {
318 this.effects.each( function(effect) {
321 effect.event('beforeFinish');
322 if (effect.finish) effect.finish(position);
323 effect.event('afterFinish');
328 Effect.Tween = Class.create(Effect.Base, {
329 initialize: function(object, from, to) {
330 object = Object.isString(object) ? $(object) : object;
331 var args = $A(arguments), method = args.last(),
332 options = args.length == 5 ? args[3] : null;
333 this.method = Object.isFunction(method) ? method.bind(object) :
334 Object.isFunction(object[method]) ? object[method].bind(object) :
335 function(value) { object[method] = value };
336 this.start(Object.extend({ from: from, to: to }, options || { }));
338 update: function(position) {
339 this.method(position);
343 Effect.Event = Class.create(Effect.Base, {
344 initialize: function() {
345 this.start(Object.extend({ duration: 0 }, arguments[0] || { }));
347 update: Prototype.emptyFunction
350 Effect.Opacity = Class.create(Effect.Base, {
351 initialize: function(element) {
352 this.element = $(element);
353 if (!this.element) throw(Effect._elementDoesNotExistError);
354 // make this work on IE on elements without 'layout'
355 if (Prototype.Browser.IE && (!this.element.currentStyle.hasLayout))
356 this.element.setStyle({zoom: 1});
357 var options = Object.extend({
358 from: this.element.getOpacity() || 0.0,
360 }, arguments[1] || { });
363 update: function(position) {
364 this.element.setOpacity(position);
368 Effect.Move = Class.create(Effect.Base, {
369 initialize: function(element) {
370 this.element = $(element);
371 if (!this.element) throw(Effect._elementDoesNotExistError);
372 var options = Object.extend({
376 }, arguments[1] || { });
380 this.element.makePositioned();
381 this.originalLeft = parseFloat(this.element.getStyle('left') || '0');
382 this.originalTop = parseFloat(this.element.getStyle('top') || '0');
383 if (this.options.mode == 'absolute') {
384 this.options.x = this.options.x - this.originalLeft;
385 this.options.y = this.options.y - this.originalTop;
388 update: function(position) {
389 this.element.setStyle({
390 left: (this.options.x * position + this.originalLeft).round() + 'px',
391 top: (this.options.y * position + this.originalTop).round() + 'px'
396 // for backwards compatibility
397 Effect.MoveBy = function(element, toTop, toLeft) {
398 return new Effect.Move(element,
399 Object.extend({ x: toLeft, y: toTop }, arguments[3] || { }));
402 Effect.Scale = Class.create(Effect.Base, {
403 initialize: function(element, percent) {
404 this.element = $(element);
405 if (!this.element) throw(Effect._elementDoesNotExistError);
406 var options = Object.extend({
410 scaleFromCenter: false,
411 scaleMode: 'box', // 'box' or 'contents' or { } with provided values
414 }, arguments[2] || { });
418 this.restoreAfterFinish = this.options.restoreAfterFinish || false;
419 this.elementPositioning = this.element.getStyle('position');
421 this.originalStyle = { };
422 ['top','left','width','height','fontSize'].each( function(k) {
423 this.originalStyle[k] = this.element.style[k];
426 this.originalTop = this.element.offsetTop;
427 this.originalLeft = this.element.offsetLeft;
429 var fontSize = this.element.getStyle('font-size') || '100%';
430 ['em','px','%','pt'].each( function(fontSizeType) {
431 if (fontSize.indexOf(fontSizeType)>0) {
432 this.fontSize = parseFloat(fontSize);
433 this.fontSizeType = fontSizeType;
437 this.factor = (this.options.scaleTo - this.options.scaleFrom)/100;
440 if (this.options.scaleMode=='box')
441 this.dims = [this.element.offsetHeight, this.element.offsetWidth];
442 if (/^content/.test(this.options.scaleMode))
443 this.dims = [this.element.scrollHeight, this.element.scrollWidth];
445 this.dims = [this.options.scaleMode.originalHeight,
446 this.options.scaleMode.originalWidth];
448 update: function(position) {
449 var currentScale = (this.options.scaleFrom/100.0) + (this.factor * position);
450 if (this.options.scaleContent && this.fontSize)
451 this.element.setStyle({fontSize: this.fontSize * currentScale + this.fontSizeType });
452 this.setDimensions(this.dims[0] * currentScale, this.dims[1] * currentScale);
454 finish: function(position) {
455 if (this.restoreAfterFinish) this.element.setStyle(this.originalStyle);
457 setDimensions: function(height, width) {
459 if (this.options.scaleX) d.width = width.round() + 'px';
460 if (this.options.scaleY) d.height = height.round() + 'px';
461 if (this.options.scaleFromCenter) {
462 var topd = (height - this.dims[0])/2;
463 var leftd = (width - this.dims[1])/2;
464 if (this.elementPositioning == 'absolute') {
465 if (this.options.scaleY) d.top = this.originalTop-topd + 'px';
466 if (this.options.scaleX) d.left = this.originalLeft-leftd + 'px';
468 if (this.options.scaleY) d.top = -topd + 'px';
469 if (this.options.scaleX) d.left = -leftd + 'px';
472 this.element.setStyle(d);
476 Effect.Highlight = Class.create(Effect.Base, {
477 initialize: function(element) {
478 this.element = $(element);
479 if (!this.element) throw(Effect._elementDoesNotExistError);
480 var options = Object.extend({ startcolor: '#ffff99' }, arguments[1] || { });
484 // Prevent executing on elements not in the layout flow
485 if (this.element.getStyle('display')=='none') { this.cancel(); return; }
486 // Disable background image during the effect
488 if (!this.options.keepBackgroundImage) {
489 this.oldStyle.backgroundImage = this.element.getStyle('background-image');
490 this.element.setStyle({backgroundImage: 'none'});
492 if (!this.options.endcolor)
493 this.options.endcolor = this.element.getStyle('background-color').parseColor('#ffffff');
494 if (!this.options.restorecolor)
495 this.options.restorecolor = this.element.getStyle('background-color');
496 // init color calculations
497 this._base = $R(0,2).map(function(i){ return parseInt(this.options.startcolor.slice(i*2+1,i*2+3),16) }.bind(this));
498 this._delta = $R(0,2).map(function(i){ return parseInt(this.options.endcolor.slice(i*2+1,i*2+3),16)-this._base[i] }.bind(this));
500 update: function(position) {
501 this.element.setStyle({backgroundColor: $R(0,2).inject('#',function(m,v,i){
502 return m+((this._base[i]+(this._delta[i]*position)).round().toColorPart()); }.bind(this)) });
505 this.element.setStyle(Object.extend(this.oldStyle, {
506 backgroundColor: this.options.restorecolor
511 Effect.ScrollTo = function(element) {
512 var options = arguments[1] || { },
513 scrollOffsets = document.viewport.getScrollOffsets(),
514 elementOffsets = $(element).cumulativeOffset(),
515 max = (window.height || document.body.scrollHeight) - document.viewport.getHeight();
517 if (options.offset) elementOffsets[1] += options.offset;
519 return new Effect.Tween(null,
521 elementOffsets[1] > max ? max : elementOffsets[1],
523 function(p){ scrollTo(scrollOffsets.left, p.round()) }
527 /* ------------- combination effects ------------- */
529 Effect.Fade = function(element) {
530 element = $(element);
531 var oldOpacity = element.getInlineOpacity();
532 var options = Object.extend({
533 from: element.getOpacity() || 1.0,
535 afterFinishInternal: function(effect) {
536 if (effect.options.to!=0) return;
537 effect.element.hide().setStyle({opacity: oldOpacity});
539 }, arguments[1] || { });
540 return new Effect.Opacity(element,options);
543 Effect.Appear = function(element) {
544 element = $(element);
545 var options = Object.extend({
546 from: (element.getStyle('display') == 'none' ? 0.0 : element.getOpacity() || 0.0),
548 // force Safari to render floated elements properly
549 afterFinishInternal: function(effect) {
550 effect.element.forceRerendering();
552 beforeSetup: function(effect) {
553 effect.element.setOpacity(effect.options.from).show();
554 }}, arguments[1] || { });
555 return new Effect.Opacity(element,options);
558 Effect.Puff = function(element) {
559 element = $(element);
561 opacity: element.getInlineOpacity(),
562 position: element.getStyle('position'),
563 top: element.style.top,
564 left: element.style.left,
565 width: element.style.width,
566 height: element.style.height
568 return new Effect.Parallel(
569 [ new Effect.Scale(element, 200,
570 { sync: true, scaleFromCenter: true, scaleContent: true, restoreAfterFinish: true }),
571 new Effect.Opacity(element, { sync: true, to: 0.0 } ) ],
572 Object.extend({ duration: 1.0,
573 beforeSetupInternal: function(effect) {
574 Position.absolutize(effect.effects[0].element)
576 afterFinishInternal: function(effect) {
577 effect.effects[0].element.hide().setStyle(oldStyle); }
578 }, arguments[1] || { })
582 Effect.BlindUp = function(element) {
583 element = $(element);
584 element.makeClipping();
585 return new Effect.Scale(element, 0,
586 Object.extend({ scaleContent: false,
588 restoreAfterFinish: true,
589 afterFinishInternal: function(effect) {
590 effect.element.hide().undoClipping();
592 }, arguments[1] || { })
596 Effect.BlindDown = function(element) {
597 element = $(element);
598 var elementDimensions = element.getDimensions();
599 return new Effect.Scale(element, 100, Object.extend({
603 scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width},
604 restoreAfterFinish: true,
605 afterSetup: function(effect) {
606 effect.element.makeClipping().setStyle({height: '0px'}).show();
608 afterFinishInternal: function(effect) {
609 effect.element.undoClipping();
611 }, arguments[1] || { }));
614 Effect.SwitchOff = function(element) {
615 element = $(element);
616 var oldOpacity = element.getInlineOpacity();
617 return new Effect.Appear(element, Object.extend({
620 transition: Effect.Transitions.flicker,
621 afterFinishInternal: function(effect) {
622 new Effect.Scale(effect.element, 1, {
623 duration: 0.3, scaleFromCenter: true,
624 scaleX: false, scaleContent: false, restoreAfterFinish: true,
625 beforeSetup: function(effect) {
626 effect.element.makePositioned().makeClipping();
628 afterFinishInternal: function(effect) {
629 effect.element.hide().undoClipping().undoPositioned().setStyle({opacity: oldOpacity});
633 }, arguments[1] || { }));
636 Effect.DropOut = function(element) {
637 element = $(element);
639 top: element.getStyle('top'),
640 left: element.getStyle('left'),
641 opacity: element.getInlineOpacity() };
642 return new Effect.Parallel(
643 [ new Effect.Move(element, {x: 0, y: 100, sync: true }),
644 new Effect.Opacity(element, { sync: true, to: 0.0 }) ],
647 beforeSetup: function(effect) {
648 effect.effects[0].element.makePositioned();
650 afterFinishInternal: function(effect) {
651 effect.effects[0].element.hide().undoPositioned().setStyle(oldStyle);
653 }, arguments[1] || { }));
656 Effect.Shake = function(element) {
657 element = $(element);
658 var options = Object.extend({
661 }, arguments[1] || {});
662 var distance = parseFloat(options.distance);
663 var split = parseFloat(options.duration) / 10.0;
665 top: element.getStyle('top'),
666 left: element.getStyle('left') };
667 return new Effect.Move(element,
668 { x: distance, y: 0, duration: split, afterFinishInternal: function(effect) {
669 new Effect.Move(effect.element,
670 { x: -distance*2, y: 0, duration: split*2, afterFinishInternal: function(effect) {
671 new Effect.Move(effect.element,
672 { x: distance*2, y: 0, duration: split*2, afterFinishInternal: function(effect) {
673 new Effect.Move(effect.element,
674 { x: -distance*2, y: 0, duration: split*2, afterFinishInternal: function(effect) {
675 new Effect.Move(effect.element,
676 { x: distance*2, y: 0, duration: split*2, afterFinishInternal: function(effect) {
677 new Effect.Move(effect.element,
678 { x: -distance, y: 0, duration: split, afterFinishInternal: function(effect) {
679 effect.element.undoPositioned().setStyle(oldStyle);
680 }}) }}) }}) }}) }}) }});
683 Effect.SlideDown = function(element) {
684 element = $(element).cleanWhitespace();
685 // SlideDown need to have the content of the element wrapped in a container element with fixed height!
686 var oldInnerBottom = element.down().getStyle('bottom');
687 var elementDimensions = element.getDimensions();
688 return new Effect.Scale(element, 100, Object.extend({
691 scaleFrom: window.opera ? 0 : 1,
692 scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width},
693 restoreAfterFinish: true,
694 afterSetup: function(effect) {
695 effect.element.makePositioned();
696 effect.element.down().makePositioned();
697 if (window.opera) effect.element.setStyle({top: ''});
698 effect.element.makeClipping().setStyle({height: '0px'}).show();
700 afterUpdateInternal: function(effect) {
701 effect.element.down().setStyle({bottom:
702 (effect.dims[0] - effect.element.clientHeight) + 'px' });
704 afterFinishInternal: function(effect) {
705 effect.element.undoClipping().undoPositioned();
706 effect.element.down().undoPositioned().setStyle({bottom: oldInnerBottom}); }
707 }, arguments[1] || { })
711 Effect.SlideUp = function(element) {
712 element = $(element).cleanWhitespace();
713 var oldInnerBottom = element.down().getStyle('bottom');
714 var elementDimensions = element.getDimensions();
715 return new Effect.Scale(element, window.opera ? 0 : 1,
716 Object.extend({ scaleContent: false,
720 scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width},
721 restoreAfterFinish: true,
722 afterSetup: function(effect) {
723 effect.element.makePositioned();
724 effect.element.down().makePositioned();
725 if (window.opera) effect.element.setStyle({top: ''});
726 effect.element.makeClipping().show();
728 afterUpdateInternal: function(effect) {
729 effect.element.down().setStyle({bottom:
730 (effect.dims[0] - effect.element.clientHeight) + 'px' });
732 afterFinishInternal: function(effect) {
733 effect.element.hide().undoClipping().undoPositioned();
734 effect.element.down().undoPositioned().setStyle({bottom: oldInnerBottom});
736 }, arguments[1] || { })
740 // Bug in opera makes the TD containing this element expand for a instance after finish
741 Effect.Squish = function(element) {
742 return new Effect.Scale(element, window.opera ? 1 : 0, {
743 restoreAfterFinish: true,
744 beforeSetup: function(effect) {
745 effect.element.makeClipping();
747 afterFinishInternal: function(effect) {
748 effect.element.hide().undoClipping();
753 Effect.Grow = function(element) {
754 element = $(element);
755 var options = Object.extend({
757 moveTransition: Effect.Transitions.sinoidal,
758 scaleTransition: Effect.Transitions.sinoidal,
759 opacityTransition: Effect.Transitions.full
760 }, arguments[1] || { });
762 top: element.style.top,
763 left: element.style.left,
764 height: element.style.height,
765 width: element.style.width,
766 opacity: element.getInlineOpacity() };
768 var dims = element.getDimensions();
769 var initialMoveX, initialMoveY;
772 switch (options.direction) {
774 initialMoveX = initialMoveY = moveX = moveY = 0;
777 initialMoveX = dims.width;
778 initialMoveY = moveY = 0;
782 initialMoveX = moveX = 0;
783 initialMoveY = dims.height;
784 moveY = -dims.height;
787 initialMoveX = dims.width;
788 initialMoveY = dims.height;
790 moveY = -dims.height;
793 initialMoveX = dims.width / 2;
794 initialMoveY = dims.height / 2;
795 moveX = -dims.width / 2;
796 moveY = -dims.height / 2;
800 return new Effect.Move(element, {
804 beforeSetup: function(effect) {
805 effect.element.hide().makeClipping().makePositioned();
807 afterFinishInternal: function(effect) {
809 [ new Effect.Opacity(effect.element, { sync: true, to: 1.0, from: 0.0, transition: options.opacityTransition }),
810 new Effect.Move(effect.element, { x: moveX, y: moveY, sync: true, transition: options.moveTransition }),
811 new Effect.Scale(effect.element, 100, {
812 scaleMode: { originalHeight: dims.height, originalWidth: dims.width },
813 sync: true, scaleFrom: window.opera ? 1 : 0, transition: options.scaleTransition, restoreAfterFinish: true})
815 beforeSetup: function(effect) {
816 effect.effects[0].element.setStyle({height: '0px'}).show();
818 afterFinishInternal: function(effect) {
819 effect.effects[0].element.undoClipping().undoPositioned().setStyle(oldStyle);
827 Effect.Shrink = function(element) {
828 element = $(element);
829 var options = Object.extend({
831 moveTransition: Effect.Transitions.sinoidal,
832 scaleTransition: Effect.Transitions.sinoidal,
833 opacityTransition: Effect.Transitions.none
834 }, arguments[1] || { });
836 top: element.style.top,
837 left: element.style.left,
838 height: element.style.height,
839 width: element.style.width,
840 opacity: element.getInlineOpacity() };
842 var dims = element.getDimensions();
845 switch (options.direction) {
862 moveX = dims.width / 2;
863 moveY = dims.height / 2;
867 return new Effect.Parallel(
868 [ new Effect.Opacity(element, { sync: true, to: 0.0, from: 1.0, transition: options.opacityTransition }),
869 new Effect.Scale(element, window.opera ? 1 : 0, { sync: true, transition: options.scaleTransition, restoreAfterFinish: true}),
870 new Effect.Move(element, { x: moveX, y: moveY, sync: true, transition: options.moveTransition })
872 beforeStartInternal: function(effect) {
873 effect.effects[0].element.makePositioned().makeClipping();
875 afterFinishInternal: function(effect) {
876 effect.effects[0].element.hide().undoClipping().undoPositioned().setStyle(oldStyle); }
881 Effect.Pulsate = function(element) {
882 element = $(element);
883 var options = arguments[1] || { };
884 var oldOpacity = element.getInlineOpacity();
885 var transition = options.transition || Effect.Transitions.sinoidal;
886 var reverser = function(pos){ return transition(1-Effect.Transitions.pulse(pos, options.pulses)) };
887 reverser.bind(transition);
888 return new Effect.Opacity(element,
889 Object.extend(Object.extend({ duration: 2.0, from: 0,
890 afterFinishInternal: function(effect) { effect.element.setStyle({opacity: oldOpacity}); }
891 }, options), {transition: reverser}));
894 Effect.Fold = function(element) {
895 element = $(element);
897 top: element.style.top,
898 left: element.style.left,
899 width: element.style.width,
900 height: element.style.height };
901 element.makeClipping();
902 return new Effect.Scale(element, 5, Object.extend({
905 afterFinishInternal: function(effect) {
906 new Effect.Scale(element, 1, {
909 afterFinishInternal: function(effect) {
910 effect.element.hide().undoClipping().setStyle(oldStyle);
912 }}, arguments[1] || { }));
915 Effect.Morph = Class.create(Effect.Base, {
916 initialize: function(element) {
917 this.element = $(element);
918 if (!this.element) throw(Effect._elementDoesNotExistError);
919 var options = Object.extend({
921 }, arguments[1] || { });
923 if (!Object.isString(options.style)) this.style = $H(options.style);
925 if (options.style.include(':'))
926 this.style = options.style.parseStyle();
928 this.element.addClassName(options.style);
929 this.style = $H(this.element.getStyles());
930 this.element.removeClassName(options.style);
931 var css = this.element.getStyles();
932 this.style = this.style.reject(function(style) {
933 return style.value == css[style.key];
935 options.afterFinishInternal = function(effect) {
936 effect.element.addClassName(effect.options.style);
937 effect.transforms.each(function(transform) {
938 effect.element.style[transform.style] = '';
947 function parseColor(color){
948 if (!color || ['rgba(0, 0, 0, 0)','transparent'].include(color)) color = '#ffffff';
949 color = color.parseColor();
950 return $R(0,2).map(function(i){
951 return parseInt( color.slice(i*2+1,i*2+3), 16 )
954 this.transforms = this.style.map(function(pair){
955 var property = pair[0], value = pair[1], unit = null;
957 if (value.parseColor('#zzzzzz') != '#zzzzzz') {
958 value = value.parseColor();
960 } else if (property == 'opacity') {
961 value = parseFloat(value);
962 if (Prototype.Browser.IE && (!this.element.currentStyle.hasLayout))
963 this.element.setStyle({zoom: 1});
964 } else if (Element.CSS_LENGTH.test(value)) {
965 var components = value.match(/^([\+\-]?[0-9\.]+)(.*)$/);
966 value = parseFloat(components[1]);
967 unit = (components.length == 3) ? components[2] : null;
970 var originalValue = this.element.getStyle(property);
972 style: property.camelize(),
973 originalValue: unit=='color' ? parseColor(originalValue) : parseFloat(originalValue || 0),
974 targetValue: unit=='color' ? parseColor(value) : value,
977 }.bind(this)).reject(function(transform){
979 (transform.originalValue == transform.targetValue) ||
981 transform.unit != 'color' &&
982 (isNaN(transform.originalValue) || isNaN(transform.targetValue))
987 update: function(position) {
988 var style = { }, transform, i = this.transforms.length;
990 style[(transform = this.transforms[i]).style] =
991 transform.unit=='color' ? '#'+
992 (Math.round(transform.originalValue[0]+
993 (transform.targetValue[0]-transform.originalValue[0])*position)).toColorPart() +
994 (Math.round(transform.originalValue[1]+
995 (transform.targetValue[1]-transform.originalValue[1])*position)).toColorPart() +
996 (Math.round(transform.originalValue[2]+
997 (transform.targetValue[2]-transform.originalValue[2])*position)).toColorPart() :
998 (transform.originalValue +
999 (transform.targetValue - transform.originalValue) * position).toFixed(3) +
1000 (transform.unit === null ? '' : transform.unit);
1001 this.element.setStyle(style, true);
1005 Effect.Transform = Class.create({
1006 initialize: function(tracks){
1008 this.options = arguments[1] || { };
1009 this.addTracks(tracks);
1011 addTracks: function(tracks){
1012 tracks.each(function(track){
1014 var data = track.values().first();
1015 this.tracks.push($H({
1016 ids: track.keys().first(),
1017 effect: Effect.Morph,
1018 options: { style: data }
1024 return new Effect.Parallel(
1025 this.tracks.map(function(track){
1026 var ids = track.get('ids'), effect = track.get('effect'), options = track.get('options');
1027 var elements = [$(ids) || $$(ids)].flatten();
1028 return elements.map(function(e){ return new effect(e, Object.extend({ sync:true }, options)) });
1035 Element.CSS_PROPERTIES = $w(
1036 'backgroundColor backgroundPosition borderBottomColor borderBottomStyle ' +
1037 'borderBottomWidth borderLeftColor borderLeftStyle borderLeftWidth ' +
1038 'borderRightColor borderRightStyle borderRightWidth borderSpacing ' +
1039 'borderTopColor borderTopStyle borderTopWidth bottom clip color ' +
1040 'fontSize fontWeight height left letterSpacing lineHeight ' +
1041 'marginBottom marginLeft marginRight marginTop markerOffset maxHeight '+
1042 'maxWidth minHeight minWidth opacity outlineColor outlineOffset ' +
1043 'outlineWidth paddingBottom paddingLeft paddingRight paddingTop ' +
1044 'right textIndent top width wordSpacing zIndex');
1046 Element.CSS_LENGTH = /^(([\+\-]?[0-9\.]+)(em|ex|px|in|cm|mm|pt|pc|\%))|0$/;
1048 String.__parseStyleElement = document.createElement('div');
1049 String.prototype.parseStyle = function(){
1050 var style, styleRules = $H();
1051 if (Prototype.Browser.WebKit)
1052 style = new Element('div',{style:this}).style;
1054 String.__parseStyleElement.innerHTML = '<div style="' + this + '"></div>';
1055 style = String.__parseStyleElement.childNodes[0].style;
1058 Element.CSS_PROPERTIES.each(function(property){
1059 if (style[property]) styleRules.set(property, style[property]);
1062 if (Prototype.Browser.IE && this.include('opacity'))
1063 styleRules.set('opacity', this.match(/opacity:\s*((?:0|1)?(?:\.\d*)?)/)[1]);
1068 if (document.defaultView && document.defaultView.getComputedStyle) {
1069 Element.getStyles = function(element) {
1070 var css = document.defaultView.getComputedStyle($(element), null);
1071 return Element.CSS_PROPERTIES.inject({ }, function(styles, property) {
1072 styles[property] = css[property];
1077 Element.getStyles = function(element) {
1078 element = $(element);
1079 var css = element.currentStyle, styles;
1080 styles = Element.CSS_PROPERTIES.inject({ }, function(hash, property) {
1081 hash.set(property, css[property]);
1084 if (!styles.opacity) styles.set('opacity', element.getOpacity());
1090 morph: function(element, style) {
1091 element = $(element);
1092 new Effect.Morph(element, Object.extend({ style: style }, arguments[2] || { }));
1095 visualEffect: function(element, effect, options) {
1096 element = $(element)
1097 var s = effect.dasherize().camelize(), klass = s.charAt(0).toUpperCase() + s.substring(1);
1098 new Effect[klass](element, options);
1101 highlight: function(element, options) {
1102 element = $(element);
1103 new Effect.Highlight(element, options);
1108 $w('fade appear grow shrink fold blindUp blindDown slideUp slideDown '+
1109 'pulsate shake puff squish switchOff dropOut').each(
1111 Effect.Methods[effect] = function(element, options){
1112 element = $(element);
1113 Effect[effect.charAt(0).toUpperCase() + effect.substring(1)](element, options);
1119 $w('getInlineOpacity forceRerendering setContentZoom collectTextNodes collectTextNodesIgnoreClass getStyles').each(
1120 function(f) { Effect.Methods[f] = Element[f]; }
1123 Element.addMethods(Effect.Methods);