michael@0: // script.aculo.us effects.js v1.7.1_beta2, Tue May 15 15:15:45 EDT 2007 michael@0: michael@0: // Copyright (c) 2005-2007 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) michael@0: // Contributors: michael@0: // Justin Palmer (http://encytemedia.com/) michael@0: // Mark Pilgrim (http://diveintomark.org/) michael@0: // Martin Bialasinki michael@0: // michael@0: // script.aculo.us is freely distributable under the terms of an MIT-style license. michael@0: // For details, see the script.aculo.us web site: http://script.aculo.us/ michael@0: michael@0: // converts rgb() and #xxx to #xxxxxx format, michael@0: // returns self (or first argument) if not convertable michael@0: String.prototype.parseColor = function() { michael@0: var color = '#'; michael@0: if(this.slice(0,4) == 'rgb(') { michael@0: var cols = this.slice(4,this.length-1).split(','); michael@0: var i=0; do { color += parseInt(cols[i]).toColorPart() } while (++i<3); michael@0: } else { michael@0: if(this.slice(0,1) == '#') { michael@0: if(this.length==4) for(var i=1;i<4;i++) color += (this.charAt(i) + this.charAt(i)).toLowerCase(); michael@0: if(this.length==7) color = this.toLowerCase(); michael@0: } michael@0: } michael@0: return(color.length==7 ? color : (arguments[0] || this)); michael@0: } michael@0: michael@0: /*--------------------------------------------------------------------------*/ michael@0: michael@0: Element.collectTextNodes = function(element) { michael@0: return $A($(element).childNodes).collect( function(node) { michael@0: return (node.nodeType==3 ? node.nodeValue : michael@0: (node.hasChildNodes() ? Element.collectTextNodes(node) : '')); michael@0: }).flatten().join(''); michael@0: } michael@0: michael@0: Element.collectTextNodesIgnoreClass = function(element, className) { michael@0: return $A($(element).childNodes).collect( function(node) { michael@0: return (node.nodeType==3 ? node.nodeValue : michael@0: ((node.hasChildNodes() && !Element.hasClassName(node,className)) ? michael@0: Element.collectTextNodesIgnoreClass(node, className) : '')); michael@0: }).flatten().join(''); michael@0: } michael@0: michael@0: Element.setContentZoom = function(element, percent) { michael@0: element = $(element); michael@0: element.setStyle({fontSize: (percent/100) + 'em'}); michael@0: if(Prototype.Browser.WebKit) window.scrollBy(0,0); michael@0: return element; michael@0: } michael@0: michael@0: Element.getInlineOpacity = function(element){ michael@0: return $(element).style.opacity || ''; michael@0: } michael@0: michael@0: Element.forceRerendering = function(element) { michael@0: try { michael@0: element = $(element); michael@0: var n = document.createTextNode(' '); michael@0: element.appendChild(n); michael@0: element.removeChild(n); michael@0: } catch(e) { } michael@0: }; michael@0: michael@0: /*--------------------------------------------------------------------------*/ michael@0: michael@0: Array.prototype.call = function() { michael@0: var args = arguments; michael@0: this.each(function(f){ f.apply(this, args) }); michael@0: } michael@0: michael@0: /*--------------------------------------------------------------------------*/ michael@0: michael@0: var Effect = { michael@0: _elementDoesNotExistError: { michael@0: name: 'ElementDoesNotExistError', michael@0: message: 'The specified DOM element does not exist, but is required for this effect to operate' michael@0: }, michael@0: tagifyText: function(element) { michael@0: if(typeof Builder == 'undefined') michael@0: throw("Effect.tagifyText requires including script.aculo.us' builder.js library"); michael@0: michael@0: var tagifyStyle = 'position:relative'; michael@0: if(Prototype.Browser.IE) tagifyStyle += ';zoom:1'; michael@0: michael@0: element = $(element); michael@0: $A(element.childNodes).each( function(child) { michael@0: if(child.nodeType==3) { michael@0: child.nodeValue.toArray().each( function(character) { michael@0: element.insertBefore( michael@0: Builder.node('span',{style: tagifyStyle}, michael@0: character == ' ' ? String.fromCharCode(160) : character), michael@0: child); michael@0: }); michael@0: Element.remove(child); michael@0: } michael@0: }); michael@0: }, michael@0: multiple: function(element, effect) { michael@0: var elements; michael@0: if(((typeof element == 'object') || michael@0: (typeof element == 'function')) && michael@0: (element.length)) michael@0: elements = element; michael@0: else michael@0: elements = $(element).childNodes; michael@0: michael@0: var options = Object.extend({ michael@0: speed: 0.1, michael@0: delay: 0.0 michael@0: }, arguments[2] || {}); michael@0: var masterDelay = options.delay; michael@0: michael@0: $A(elements).each( function(element, index) { michael@0: new effect(element, Object.extend(options, { delay: index * options.speed + masterDelay })); michael@0: }); michael@0: }, michael@0: PAIRS: { michael@0: 'slide': ['SlideDown','SlideUp'], michael@0: 'blind': ['BlindDown','BlindUp'], michael@0: 'appear': ['Appear','Fade'] michael@0: }, michael@0: toggle: function(element, effect) { michael@0: element = $(element); michael@0: effect = (effect || 'appear').toLowerCase(); michael@0: var options = Object.extend({ michael@0: queue: { position:'end', scope:(element.id || 'global'), limit: 1 } michael@0: }, arguments[2] || {}); michael@0: Effect[element.visible() ? michael@0: Effect.PAIRS[effect][1] : Effect.PAIRS[effect][0]](element, options); michael@0: } michael@0: }; michael@0: michael@0: var Effect2 = Effect; // deprecated michael@0: michael@0: /* ------------- transitions ------------- */ michael@0: michael@0: Effect.Transitions = { michael@0: linear: Prototype.K, michael@0: sinoidal: function(pos) { michael@0: return (-Math.cos(pos*Math.PI)/2) + 0.5; michael@0: }, michael@0: reverse: function(pos) { michael@0: return 1-pos; michael@0: }, michael@0: flicker: function(pos) { michael@0: var pos = ((-Math.cos(pos*Math.PI)/4) + 0.75) + Math.random()/4; michael@0: return (pos > 1 ? 1 : pos); michael@0: }, michael@0: wobble: function(pos) { michael@0: return (-Math.cos(pos*Math.PI*(9*pos))/2) + 0.5; michael@0: }, michael@0: pulse: function(pos, pulses) { michael@0: pulses = pulses || 5; michael@0: return ( michael@0: Math.round((pos % (1/pulses)) * pulses) == 0 ? michael@0: ((pos * pulses * 2) - Math.floor(pos * pulses * 2)) : michael@0: 1 - ((pos * pulses * 2) - Math.floor(pos * pulses * 2)) michael@0: ); michael@0: }, michael@0: none: function(pos) { michael@0: return 0; michael@0: }, michael@0: full: function(pos) { michael@0: return 1; michael@0: } michael@0: }; michael@0: michael@0: /* ------------- core effects ------------- */ michael@0: michael@0: Effect.ScopedQueue = Class.create(); michael@0: Object.extend(Object.extend(Effect.ScopedQueue.prototype, Enumerable), { michael@0: initialize: function() { michael@0: this.effects = []; michael@0: this.interval = null; michael@0: }, michael@0: _each: function(iterator) { michael@0: this.effects._each(iterator); michael@0: }, michael@0: add: function(effect) { michael@0: var timestamp = new Date().getTime(); michael@0: michael@0: var position = (typeof effect.options.queue == 'string') ? michael@0: effect.options.queue : effect.options.queue.position; michael@0: michael@0: switch(position) { michael@0: case 'front': michael@0: // move unstarted effects after this effect michael@0: this.effects.findAll(function(e){ return e.state=='idle' }).each( function(e) { michael@0: e.startOn += effect.finishOn; michael@0: e.finishOn += effect.finishOn; michael@0: }); michael@0: break; michael@0: case 'with-last': michael@0: timestamp = this.effects.pluck('startOn').max() || timestamp; michael@0: break; michael@0: case 'end': michael@0: // start effect after last queued effect has finished michael@0: timestamp = this.effects.pluck('finishOn').max() || timestamp; michael@0: break; michael@0: } michael@0: michael@0: effect.startOn += timestamp; michael@0: effect.finishOn += timestamp; michael@0: michael@0: if(!effect.options.queue.limit || (this.effects.length < effect.options.queue.limit)) michael@0: this.effects.push(effect); michael@0: michael@0: if(!this.interval) michael@0: this.interval = setInterval(this.loop.bind(this), 15); michael@0: }, michael@0: remove: function(effect) { michael@0: this.effects = this.effects.reject(function(e) { return e==effect }); michael@0: if(this.effects.length == 0) { michael@0: clearInterval(this.interval); michael@0: this.interval = null; michael@0: } michael@0: }, michael@0: loop: function() { michael@0: var timePos = new Date().getTime(); michael@0: for(var i=0, len=this.effects.length;i= this.startOn) { michael@0: if(timePos >= this.finishOn) { michael@0: this.render(1.0); michael@0: this.cancel(); michael@0: this.event('beforeFinish'); michael@0: if(this.finish) this.finish(); michael@0: this.event('afterFinish'); michael@0: return; michael@0: } michael@0: var pos = (timePos - this.startOn) / this.totalTime, michael@0: frame = Math.round(pos * this.totalFrames); michael@0: if(frame > this.currentFrame) { michael@0: this.render(pos); michael@0: this.currentFrame = frame; michael@0: } michael@0: } michael@0: }, michael@0: cancel: function() { michael@0: if(!this.options.sync) michael@0: Effect.Queues.get(typeof this.options.queue == 'string' ? michael@0: 'global' : this.options.queue.scope).remove(this); michael@0: this.state = 'finished'; michael@0: }, michael@0: event: function(eventName) { michael@0: if(this.options[eventName + 'Internal']) this.options[eventName + 'Internal'](this); michael@0: if(this.options[eventName]) this.options[eventName](this); michael@0: }, michael@0: inspect: function() { michael@0: var data = $H(); michael@0: for(property in this) michael@0: if(typeof this[property] != 'function') data[property] = this[property]; michael@0: return '#'; michael@0: } michael@0: } michael@0: michael@0: Effect.Parallel = Class.create(); michael@0: Object.extend(Object.extend(Effect.Parallel.prototype, Effect.Base.prototype), { michael@0: initialize: function(effects) { michael@0: this.effects = effects || []; michael@0: this.start(arguments[1]); michael@0: }, michael@0: update: function(position) { michael@0: this.effects.invoke('render', position); michael@0: }, michael@0: finish: function(position) { michael@0: this.effects.each( function(effect) { michael@0: effect.render(1.0); michael@0: effect.cancel(); michael@0: effect.event('beforeFinish'); michael@0: if(effect.finish) effect.finish(position); michael@0: effect.event('afterFinish'); michael@0: }); michael@0: } michael@0: }); michael@0: michael@0: Effect.Event = Class.create(); michael@0: Object.extend(Object.extend(Effect.Event.prototype, Effect.Base.prototype), { michael@0: initialize: function() { michael@0: var options = Object.extend({ michael@0: duration: 0 michael@0: }, arguments[0] || {}); michael@0: this.start(options); michael@0: }, michael@0: update: Prototype.emptyFunction michael@0: }); michael@0: michael@0: Effect.Opacity = Class.create(); michael@0: Object.extend(Object.extend(Effect.Opacity.prototype, Effect.Base.prototype), { michael@0: initialize: function(element) { michael@0: this.element = $(element); michael@0: if(!this.element) throw(Effect._elementDoesNotExistError); michael@0: // make this work on IE on elements without 'layout' michael@0: if(Prototype.Browser.IE && (!this.element.currentStyle.hasLayout)) michael@0: this.element.setStyle({zoom: 1}); michael@0: var options = Object.extend({ michael@0: from: this.element.getOpacity() || 0.0, michael@0: to: 1.0 michael@0: }, arguments[1] || {}); michael@0: this.start(options); michael@0: }, michael@0: update: function(position) { michael@0: this.element.setOpacity(position); michael@0: } michael@0: }); michael@0: michael@0: Effect.Move = Class.create(); michael@0: Object.extend(Object.extend(Effect.Move.prototype, Effect.Base.prototype), { michael@0: initialize: function(element) { michael@0: this.element = $(element); michael@0: if(!this.element) throw(Effect._elementDoesNotExistError); michael@0: var options = Object.extend({ michael@0: x: 0, michael@0: y: 0, michael@0: mode: 'relative' michael@0: }, arguments[1] || {}); michael@0: this.start(options); michael@0: }, michael@0: setup: function() { michael@0: // Bug in Opera: Opera returns the "real" position of a static element or michael@0: // relative element that does not have top/left explicitly set. michael@0: // ==> Always set top and left for position relative elements in your stylesheets michael@0: // (to 0 if you do not need them) michael@0: this.element.makePositioned(); michael@0: this.originalLeft = parseFloat(this.element.getStyle('left') || '0'); michael@0: this.originalTop = parseFloat(this.element.getStyle('top') || '0'); michael@0: if(this.options.mode == 'absolute') { michael@0: // absolute movement, so we need to calc deltaX and deltaY michael@0: this.options.x = this.options.x - this.originalLeft; michael@0: this.options.y = this.options.y - this.originalTop; michael@0: } michael@0: }, michael@0: update: function(position) { michael@0: this.element.setStyle({ michael@0: left: Math.round(this.options.x * position + this.originalLeft) + 'px', michael@0: top: Math.round(this.options.y * position + this.originalTop) + 'px' michael@0: }); michael@0: } michael@0: }); michael@0: michael@0: // for backwards compatibility michael@0: Effect.MoveBy = function(element, toTop, toLeft) { michael@0: return new Effect.Move(element, michael@0: Object.extend({ x: toLeft, y: toTop }, arguments[3] || {})); michael@0: }; michael@0: michael@0: Effect.Scale = Class.create(); michael@0: Object.extend(Object.extend(Effect.Scale.prototype, Effect.Base.prototype), { michael@0: initialize: function(element, percent) { michael@0: this.element = $(element); michael@0: if(!this.element) throw(Effect._elementDoesNotExistError); michael@0: var options = Object.extend({ michael@0: scaleX: true, michael@0: scaleY: true, michael@0: scaleContent: true, michael@0: scaleFromCenter: false, michael@0: scaleMode: 'box', // 'box' or 'contents' or {} with provided values michael@0: scaleFrom: 100.0, michael@0: scaleTo: percent michael@0: }, arguments[2] || {}); michael@0: this.start(options); michael@0: }, michael@0: setup: function() { michael@0: this.restoreAfterFinish = this.options.restoreAfterFinish || false; michael@0: this.elementPositioning = this.element.getStyle('position'); michael@0: michael@0: this.originalStyle = {}; michael@0: ['top','left','width','height','fontSize'].each( function(k) { michael@0: this.originalStyle[k] = this.element.style[k]; michael@0: }.bind(this)); michael@0: michael@0: this.originalTop = this.element.offsetTop; michael@0: this.originalLeft = this.element.offsetLeft; michael@0: michael@0: var fontSize = this.element.getStyle('font-size') || '100%'; michael@0: ['em','px','%','pt'].each( function(fontSizeType) { michael@0: if(fontSize.indexOf(fontSizeType)>0) { michael@0: this.fontSize = parseFloat(fontSize); michael@0: this.fontSizeType = fontSizeType; michael@0: } michael@0: }.bind(this)); michael@0: michael@0: this.factor = (this.options.scaleTo - this.options.scaleFrom)/100; michael@0: michael@0: this.dims = null; michael@0: if(this.options.scaleMode=='box') michael@0: this.dims = [this.element.offsetHeight, this.element.offsetWidth]; michael@0: if(/^content/.test(this.options.scaleMode)) michael@0: this.dims = [this.element.scrollHeight, this.element.scrollWidth]; michael@0: if(!this.dims) michael@0: this.dims = [this.options.scaleMode.originalHeight, michael@0: this.options.scaleMode.originalWidth]; michael@0: }, michael@0: update: function(position) { michael@0: var currentScale = (this.options.scaleFrom/100.0) + (this.factor * position); michael@0: if(this.options.scaleContent && this.fontSize) michael@0: this.element.setStyle({fontSize: this.fontSize * currentScale + this.fontSizeType }); michael@0: this.setDimensions(this.dims[0] * currentScale, this.dims[1] * currentScale); michael@0: }, michael@0: finish: function(position) { michael@0: if(this.restoreAfterFinish) this.element.setStyle(this.originalStyle); michael@0: }, michael@0: setDimensions: function(height, width) { michael@0: var d = {}; michael@0: if(this.options.scaleX) d.width = Math.round(width) + 'px'; michael@0: if(this.options.scaleY) d.height = Math.round(height) + 'px'; michael@0: if(this.options.scaleFromCenter) { michael@0: var topd = (height - this.dims[0])/2; michael@0: var leftd = (width - this.dims[1])/2; michael@0: if(this.elementPositioning == 'absolute') { michael@0: if(this.options.scaleY) d.top = this.originalTop-topd + 'px'; michael@0: if(this.options.scaleX) d.left = this.originalLeft-leftd + 'px'; michael@0: } else { michael@0: if(this.options.scaleY) d.top = -topd + 'px'; michael@0: if(this.options.scaleX) d.left = -leftd + 'px'; michael@0: } michael@0: } michael@0: this.element.setStyle(d); michael@0: } michael@0: }); michael@0: michael@0: Effect.Highlight = Class.create(); michael@0: Object.extend(Object.extend(Effect.Highlight.prototype, Effect.Base.prototype), { michael@0: initialize: function(element) { michael@0: this.element = $(element); michael@0: if(!this.element) throw(Effect._elementDoesNotExistError); michael@0: var options = Object.extend({ startcolor: '#ffff99' }, arguments[1] || {}); michael@0: this.start(options); michael@0: }, michael@0: setup: function() { michael@0: // Prevent executing on elements not in the layout flow michael@0: if(this.element.getStyle('display')=='none') { this.cancel(); return; } michael@0: // Disable background image during the effect michael@0: this.oldStyle = {}; michael@0: if (!this.options.keepBackgroundImage) { michael@0: this.oldStyle.backgroundImage = this.element.getStyle('background-image'); michael@0: this.element.setStyle({backgroundImage: 'none'}); michael@0: } michael@0: if(!this.options.endcolor) michael@0: this.options.endcolor = this.element.getStyle('background-color').parseColor('#ffffff'); michael@0: if(!this.options.restorecolor) michael@0: this.options.restorecolor = this.element.getStyle('background-color'); michael@0: // init color calculations michael@0: this._base = $R(0,2).map(function(i){ return parseInt(this.options.startcolor.slice(i*2+1,i*2+3),16) }.bind(this)); michael@0: 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)); michael@0: }, michael@0: update: function(position) { michael@0: this.element.setStyle({backgroundColor: $R(0,2).inject('#',function(m,v,i){ michael@0: return m+(Math.round(this._base[i]+(this._delta[i]*position)).toColorPart()); }.bind(this)) }); michael@0: }, michael@0: finish: function() { michael@0: this.element.setStyle(Object.extend(this.oldStyle, { michael@0: backgroundColor: this.options.restorecolor michael@0: })); michael@0: } michael@0: }); michael@0: michael@0: Effect.ScrollTo = Class.create(); michael@0: Object.extend(Object.extend(Effect.ScrollTo.prototype, Effect.Base.prototype), { michael@0: initialize: function(element) { michael@0: this.element = $(element); michael@0: this.start(arguments[1] || {}); michael@0: }, michael@0: setup: function() { michael@0: Position.prepare(); michael@0: var offsets = Position.cumulativeOffset(this.element); michael@0: if(this.options.offset) offsets[1] += this.options.offset; michael@0: var max = window.innerHeight ? michael@0: window.height - window.innerHeight : michael@0: document.body.scrollHeight - michael@0: (document.documentElement.clientHeight ? michael@0: document.documentElement.clientHeight : document.body.clientHeight); michael@0: this.scrollStart = Position.deltaY; michael@0: this.delta = (offsets[1] > max ? max : offsets[1]) - this.scrollStart; michael@0: }, michael@0: update: function(position) { michael@0: Position.prepare(); michael@0: window.scrollTo(Position.deltaX, michael@0: this.scrollStart + (position*this.delta)); michael@0: } michael@0: }); michael@0: michael@0: /* ------------- combination effects ------------- */ michael@0: michael@0: Effect.Fade = function(element) { michael@0: element = $(element); michael@0: var oldOpacity = element.getInlineOpacity(); michael@0: var options = Object.extend({ michael@0: from: element.getOpacity() || 1.0, michael@0: to: 0.0, michael@0: afterFinishInternal: function(effect) { michael@0: if(effect.options.to!=0) return; michael@0: effect.element.hide().setStyle({opacity: oldOpacity}); michael@0: }}, arguments[1] || {}); michael@0: return new Effect.Opacity(element,options); michael@0: } michael@0: michael@0: Effect.Appear = function(element) { michael@0: element = $(element); michael@0: var options = Object.extend({ michael@0: from: (element.getStyle('display') == 'none' ? 0.0 : element.getOpacity() || 0.0), michael@0: to: 1.0, michael@0: // force Safari to render floated elements properly michael@0: afterFinishInternal: function(effect) { michael@0: effect.element.forceRerendering(); michael@0: }, michael@0: beforeSetup: function(effect) { michael@0: effect.element.setOpacity(effect.options.from).show(); michael@0: }}, arguments[1] || {}); michael@0: return new Effect.Opacity(element,options); michael@0: } michael@0: michael@0: Effect.Puff = function(element) { michael@0: element = $(element); michael@0: var oldStyle = { michael@0: opacity: element.getInlineOpacity(), michael@0: position: element.getStyle('position'), michael@0: top: element.style.top, michael@0: left: element.style.left, michael@0: width: element.style.width, michael@0: height: element.style.height michael@0: }; michael@0: return new Effect.Parallel( michael@0: [ new Effect.Scale(element, 200, michael@0: { sync: true, scaleFromCenter: true, scaleContent: true, restoreAfterFinish: true }), michael@0: new Effect.Opacity(element, { sync: true, to: 0.0 } ) ], michael@0: Object.extend({ duration: 1.0, michael@0: beforeSetupInternal: function(effect) { michael@0: Position.absolutize(effect.effects[0].element) michael@0: }, michael@0: afterFinishInternal: function(effect) { michael@0: effect.effects[0].element.hide().setStyle(oldStyle); } michael@0: }, arguments[1] || {}) michael@0: ); michael@0: } michael@0: michael@0: Effect.BlindUp = function(element) { michael@0: element = $(element); michael@0: element.makeClipping(); michael@0: return new Effect.Scale(element, 0, michael@0: Object.extend({ scaleContent: false, michael@0: scaleX: false, michael@0: restoreAfterFinish: true, michael@0: afterFinishInternal: function(effect) { michael@0: effect.element.hide().undoClipping(); michael@0: } michael@0: }, arguments[1] || {}) michael@0: ); michael@0: } michael@0: michael@0: Effect.BlindDown = function(element) { michael@0: element = $(element); michael@0: var elementDimensions = element.getDimensions(); michael@0: return new Effect.Scale(element, 100, Object.extend({ michael@0: scaleContent: false, michael@0: scaleX: false, michael@0: scaleFrom: 0, michael@0: scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width}, michael@0: restoreAfterFinish: true, michael@0: afterSetup: function(effect) { michael@0: effect.element.makeClipping().setStyle({height: '0px'}).show(); michael@0: }, michael@0: afterFinishInternal: function(effect) { michael@0: effect.element.undoClipping(); michael@0: } michael@0: }, arguments[1] || {})); michael@0: } michael@0: michael@0: Effect.SwitchOff = function(element) { michael@0: element = $(element); michael@0: var oldOpacity = element.getInlineOpacity(); michael@0: return new Effect.Appear(element, Object.extend({ michael@0: duration: 0.4, michael@0: from: 0, michael@0: transition: Effect.Transitions.flicker, michael@0: afterFinishInternal: function(effect) { michael@0: new Effect.Scale(effect.element, 1, { michael@0: duration: 0.3, scaleFromCenter: true, michael@0: scaleX: false, scaleContent: false, restoreAfterFinish: true, michael@0: beforeSetup: function(effect) { michael@0: effect.element.makePositioned().makeClipping(); michael@0: }, michael@0: afterFinishInternal: function(effect) { michael@0: effect.element.hide().undoClipping().undoPositioned().setStyle({opacity: oldOpacity}); michael@0: } michael@0: }) michael@0: } michael@0: }, arguments[1] || {})); michael@0: } michael@0: michael@0: Effect.DropOut = function(element) { michael@0: element = $(element); michael@0: var oldStyle = { michael@0: top: element.getStyle('top'), michael@0: left: element.getStyle('left'), michael@0: opacity: element.getInlineOpacity() }; michael@0: return new Effect.Parallel( michael@0: [ new Effect.Move(element, {x: 0, y: 100, sync: true }), michael@0: new Effect.Opacity(element, { sync: true, to: 0.0 }) ], michael@0: Object.extend( michael@0: { duration: 0.5, michael@0: beforeSetup: function(effect) { michael@0: effect.effects[0].element.makePositioned(); michael@0: }, michael@0: afterFinishInternal: function(effect) { michael@0: effect.effects[0].element.hide().undoPositioned().setStyle(oldStyle); michael@0: } michael@0: }, arguments[1] || {})); michael@0: } michael@0: michael@0: Effect.Shake = function(element) { michael@0: element = $(element); michael@0: var oldStyle = { michael@0: top: element.getStyle('top'), michael@0: left: element.getStyle('left') }; michael@0: return new Effect.Move(element, michael@0: { x: 20, y: 0, duration: 0.05, afterFinishInternal: function(effect) { michael@0: new Effect.Move(effect.element, michael@0: { x: -40, y: 0, duration: 0.1, afterFinishInternal: function(effect) { michael@0: new Effect.Move(effect.element, michael@0: { x: 40, y: 0, duration: 0.1, afterFinishInternal: function(effect) { michael@0: new Effect.Move(effect.element, michael@0: { x: -40, y: 0, duration: 0.1, afterFinishInternal: function(effect) { michael@0: new Effect.Move(effect.element, michael@0: { x: 40, y: 0, duration: 0.1, afterFinishInternal: function(effect) { michael@0: new Effect.Move(effect.element, michael@0: { x: -20, y: 0, duration: 0.05, afterFinishInternal: function(effect) { michael@0: effect.element.undoPositioned().setStyle(oldStyle); michael@0: }}) }}) }}) }}) }}) }}); michael@0: } michael@0: michael@0: Effect.SlideDown = function(element) { michael@0: element = $(element).cleanWhitespace(); michael@0: // SlideDown need to have the content of the element wrapped in a container element with fixed height! michael@0: var oldInnerBottom = element.down().getStyle('bottom'); michael@0: var elementDimensions = element.getDimensions(); michael@0: return new Effect.Scale(element, 100, Object.extend({ michael@0: scaleContent: false, michael@0: scaleX: false, michael@0: scaleFrom: window.opera ? 0 : 1, michael@0: scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width}, michael@0: restoreAfterFinish: true, michael@0: afterSetup: function(effect) { michael@0: effect.element.makePositioned(); michael@0: effect.element.down().makePositioned(); michael@0: if(window.opera) effect.element.setStyle({top: ''}); michael@0: effect.element.makeClipping().setStyle({height: '0px'}).show(); michael@0: }, michael@0: afterUpdateInternal: function(effect) { michael@0: effect.element.down().setStyle({bottom: michael@0: (effect.dims[0] - effect.element.clientHeight) + 'px' }); michael@0: }, michael@0: afterFinishInternal: function(effect) { michael@0: effect.element.undoClipping().undoPositioned(); michael@0: effect.element.down().undoPositioned().setStyle({bottom: oldInnerBottom}); } michael@0: }, arguments[1] || {}) michael@0: ); michael@0: } michael@0: michael@0: Effect.SlideUp = function(element) { michael@0: element = $(element).cleanWhitespace(); michael@0: var oldInnerBottom = element.down().getStyle('bottom'); michael@0: return new Effect.Scale(element, window.opera ? 0 : 1, michael@0: Object.extend({ scaleContent: false, michael@0: scaleX: false, michael@0: scaleMode: 'box', michael@0: scaleFrom: 100, michael@0: restoreAfterFinish: true, michael@0: beforeStartInternal: function(effect) { michael@0: effect.element.makePositioned(); michael@0: effect.element.down().makePositioned(); michael@0: if(window.opera) effect.element.setStyle({top: ''}); michael@0: effect.element.makeClipping().show(); michael@0: }, michael@0: afterUpdateInternal: function(effect) { michael@0: effect.element.down().setStyle({bottom: michael@0: (effect.dims[0] - effect.element.clientHeight) + 'px' }); michael@0: }, michael@0: afterFinishInternal: function(effect) { michael@0: effect.element.hide().undoClipping().undoPositioned().setStyle({bottom: oldInnerBottom}); michael@0: effect.element.down().undoPositioned(); michael@0: } michael@0: }, arguments[1] || {}) michael@0: ); michael@0: } michael@0: michael@0: // Bug in opera makes the TD containing this element expand for a instance after finish michael@0: Effect.Squish = function(element) { michael@0: return new Effect.Scale(element, window.opera ? 1 : 0, { michael@0: restoreAfterFinish: true, michael@0: beforeSetup: function(effect) { michael@0: effect.element.makeClipping(); michael@0: }, michael@0: afterFinishInternal: function(effect) { michael@0: effect.element.hide().undoClipping(); michael@0: } michael@0: }); michael@0: } michael@0: michael@0: Effect.Grow = function(element) { michael@0: element = $(element); michael@0: var options = Object.extend({ michael@0: direction: 'center', michael@0: moveTransition: Effect.Transitions.sinoidal, michael@0: scaleTransition: Effect.Transitions.sinoidal, michael@0: opacityTransition: Effect.Transitions.full michael@0: }, arguments[1] || {}); michael@0: var oldStyle = { michael@0: top: element.style.top, michael@0: left: element.style.left, michael@0: height: element.style.height, michael@0: width: element.style.width, michael@0: opacity: element.getInlineOpacity() }; michael@0: michael@0: var dims = element.getDimensions(); michael@0: var initialMoveX, initialMoveY; michael@0: var moveX, moveY; michael@0: michael@0: switch (options.direction) { michael@0: case 'top-left': michael@0: initialMoveX = initialMoveY = moveX = moveY = 0; michael@0: break; michael@0: case 'top-right': michael@0: initialMoveX = dims.width; michael@0: initialMoveY = moveY = 0; michael@0: moveX = -dims.width; michael@0: break; michael@0: case 'bottom-left': michael@0: initialMoveX = moveX = 0; michael@0: initialMoveY = dims.height; michael@0: moveY = -dims.height; michael@0: break; michael@0: case 'bottom-right': michael@0: initialMoveX = dims.width; michael@0: initialMoveY = dims.height; michael@0: moveX = -dims.width; michael@0: moveY = -dims.height; michael@0: break; michael@0: case 'center': michael@0: initialMoveX = dims.width / 2; michael@0: initialMoveY = dims.height / 2; michael@0: moveX = -dims.width / 2; michael@0: moveY = -dims.height / 2; michael@0: break; michael@0: } michael@0: michael@0: return new Effect.Move(element, { michael@0: x: initialMoveX, michael@0: y: initialMoveY, michael@0: duration: 0.01, michael@0: beforeSetup: function(effect) { michael@0: effect.element.hide().makeClipping().makePositioned(); michael@0: }, michael@0: afterFinishInternal: function(effect) { michael@0: new Effect.Parallel( michael@0: [ new Effect.Opacity(effect.element, { sync: true, to: 1.0, from: 0.0, transition: options.opacityTransition }), michael@0: new Effect.Move(effect.element, { x: moveX, y: moveY, sync: true, transition: options.moveTransition }), michael@0: new Effect.Scale(effect.element, 100, { michael@0: scaleMode: { originalHeight: dims.height, originalWidth: dims.width }, michael@0: sync: true, scaleFrom: window.opera ? 1 : 0, transition: options.scaleTransition, restoreAfterFinish: true}) michael@0: ], Object.extend({ michael@0: beforeSetup: function(effect) { michael@0: effect.effects[0].element.setStyle({height: '0px'}).show(); michael@0: }, michael@0: afterFinishInternal: function(effect) { michael@0: effect.effects[0].element.undoClipping().undoPositioned().setStyle(oldStyle); michael@0: } michael@0: }, options) michael@0: ) michael@0: } michael@0: }); michael@0: } michael@0: michael@0: Effect.Shrink = function(element) { michael@0: element = $(element); michael@0: var options = Object.extend({ michael@0: direction: 'center', michael@0: moveTransition: Effect.Transitions.sinoidal, michael@0: scaleTransition: Effect.Transitions.sinoidal, michael@0: opacityTransition: Effect.Transitions.none michael@0: }, arguments[1] || {}); michael@0: var oldStyle = { michael@0: top: element.style.top, michael@0: left: element.style.left, michael@0: height: element.style.height, michael@0: width: element.style.width, michael@0: opacity: element.getInlineOpacity() }; michael@0: michael@0: var dims = element.getDimensions(); michael@0: var moveX, moveY; michael@0: michael@0: switch (options.direction) { michael@0: case 'top-left': michael@0: moveX = moveY = 0; michael@0: break; michael@0: case 'top-right': michael@0: moveX = dims.width; michael@0: moveY = 0; michael@0: break; michael@0: case 'bottom-left': michael@0: moveX = 0; michael@0: moveY = dims.height; michael@0: break; michael@0: case 'bottom-right': michael@0: moveX = dims.width; michael@0: moveY = dims.height; michael@0: break; michael@0: case 'center': michael@0: moveX = dims.width / 2; michael@0: moveY = dims.height / 2; michael@0: break; michael@0: } michael@0: michael@0: return new Effect.Parallel( michael@0: [ new Effect.Opacity(element, { sync: true, to: 0.0, from: 1.0, transition: options.opacityTransition }), michael@0: new Effect.Scale(element, window.opera ? 1 : 0, { sync: true, transition: options.scaleTransition, restoreAfterFinish: true}), michael@0: new Effect.Move(element, { x: moveX, y: moveY, sync: true, transition: options.moveTransition }) michael@0: ], Object.extend({ michael@0: beforeStartInternal: function(effect) { michael@0: effect.effects[0].element.makePositioned().makeClipping(); michael@0: }, michael@0: afterFinishInternal: function(effect) { michael@0: effect.effects[0].element.hide().undoClipping().undoPositioned().setStyle(oldStyle); } michael@0: }, options) michael@0: ); michael@0: } michael@0: michael@0: Effect.Pulsate = function(element) { michael@0: element = $(element); michael@0: var options = arguments[1] || {}; michael@0: var oldOpacity = element.getInlineOpacity(); michael@0: var transition = options.transition || Effect.Transitions.sinoidal; michael@0: var reverser = function(pos){ return transition(1-Effect.Transitions.pulse(pos, options.pulses)) }; michael@0: reverser.bind(transition); michael@0: return new Effect.Opacity(element, michael@0: Object.extend(Object.extend({ duration: 2.0, from: 0, michael@0: afterFinishInternal: function(effect) { effect.element.setStyle({opacity: oldOpacity}); } michael@0: }, options), {transition: reverser})); michael@0: } michael@0: michael@0: Effect.Fold = function(element) { michael@0: element = $(element); michael@0: var oldStyle = { michael@0: top: element.style.top, michael@0: left: element.style.left, michael@0: width: element.style.width, michael@0: height: element.style.height }; michael@0: element.makeClipping(); michael@0: return new Effect.Scale(element, 5, Object.extend({ michael@0: scaleContent: false, michael@0: scaleX: false, michael@0: afterFinishInternal: function(effect) { michael@0: new Effect.Scale(element, 1, { michael@0: scaleContent: false, michael@0: scaleY: false, michael@0: afterFinishInternal: function(effect) { michael@0: effect.element.hide().undoClipping().setStyle(oldStyle); michael@0: } }); michael@0: }}, arguments[1] || {})); michael@0: }; michael@0: michael@0: Effect.Morph = Class.create(); michael@0: Object.extend(Object.extend(Effect.Morph.prototype, Effect.Base.prototype), { michael@0: initialize: function(element) { michael@0: this.element = $(element); michael@0: if(!this.element) throw(Effect._elementDoesNotExistError); michael@0: var options = Object.extend({ michael@0: style: {} michael@0: }, arguments[1] || {}); michael@0: if (typeof options.style == 'string') { michael@0: if(options.style.indexOf(':') == -1) { michael@0: var cssText = '', selector = '.' + options.style; michael@0: $A(document.styleSheets).reverse().each(function(styleSheet) { michael@0: if (styleSheet.cssRules) cssRules = styleSheet.cssRules; michael@0: else if (styleSheet.rules) cssRules = styleSheet.rules; michael@0: $A(cssRules).reverse().each(function(rule) { michael@0: if (selector == rule.selectorText) { michael@0: cssText = rule.style.cssText; michael@0: throw $break; michael@0: } michael@0: }); michael@0: if (cssText) throw $break; michael@0: }); michael@0: this.style = cssText.parseStyle(); michael@0: options.afterFinishInternal = function(effect){ michael@0: effect.element.addClassName(effect.options.style); michael@0: effect.transforms.each(function(transform) { michael@0: if(transform.style != 'opacity') michael@0: effect.element.style[transform.style] = ''; michael@0: }); michael@0: } michael@0: } else this.style = options.style.parseStyle(); michael@0: } else this.style = $H(options.style) michael@0: this.start(options); michael@0: }, michael@0: setup: function(){ michael@0: function parseColor(color){ michael@0: if(!color || ['rgba(0, 0, 0, 0)','transparent'].include(color)) color = '#ffffff'; michael@0: color = color.parseColor(); michael@0: return $R(0,2).map(function(i){ michael@0: return parseInt( color.slice(i*2+1,i*2+3), 16 ) michael@0: }); michael@0: } michael@0: this.transforms = this.style.map(function(pair){ michael@0: var property = pair[0], value = pair[1], unit = null; michael@0: michael@0: if(value.parseColor('#zzzzzz') != '#zzzzzz') { michael@0: value = value.parseColor(); michael@0: unit = 'color'; michael@0: } else if(property == 'opacity') { michael@0: value = parseFloat(value); michael@0: if(Prototype.Browser.IE && (!this.element.currentStyle.hasLayout)) michael@0: this.element.setStyle({zoom: 1}); michael@0: } else if(Element.CSS_LENGTH.test(value)) { michael@0: var components = value.match(/^([\+\-]?[0-9\.]+)(.*)$/); michael@0: value = parseFloat(components[1]); michael@0: unit = (components.length == 3) ? components[2] : null; michael@0: } michael@0: michael@0: var originalValue = this.element.getStyle(property); michael@0: return { michael@0: style: property.camelize(), michael@0: originalValue: unit=='color' ? parseColor(originalValue) : parseFloat(originalValue || 0), michael@0: targetValue: unit=='color' ? parseColor(value) : value, michael@0: unit: unit michael@0: }; michael@0: }.bind(this)).reject(function(transform){ michael@0: return ( michael@0: (transform.originalValue == transform.targetValue) || michael@0: ( michael@0: transform.unit != 'color' && michael@0: (isNaN(transform.originalValue) || isNaN(transform.targetValue)) michael@0: ) michael@0: ) michael@0: }); michael@0: }, michael@0: update: function(position) { michael@0: var style = {}, transform, i = this.transforms.length; michael@0: while(i--) michael@0: style[(transform = this.transforms[i]).style] = michael@0: transform.unit=='color' ? '#'+ michael@0: (Math.round(transform.originalValue[0]+ michael@0: (transform.targetValue[0]-transform.originalValue[0])*position)).toColorPart() + michael@0: (Math.round(transform.originalValue[1]+ michael@0: (transform.targetValue[1]-transform.originalValue[1])*position)).toColorPart() + michael@0: (Math.round(transform.originalValue[2]+ michael@0: (transform.targetValue[2]-transform.originalValue[2])*position)).toColorPart() : michael@0: transform.originalValue + Math.round( michael@0: ((transform.targetValue - transform.originalValue) * position) * 1000)/1000 + transform.unit; michael@0: this.element.setStyle(style, true); michael@0: } michael@0: }); michael@0: michael@0: Effect.Transform = Class.create(); michael@0: Object.extend(Effect.Transform.prototype, { michael@0: initialize: function(tracks){ michael@0: this.tracks = []; michael@0: this.options = arguments[1] || {}; michael@0: this.addTracks(tracks); michael@0: }, michael@0: addTracks: function(tracks){ michael@0: tracks.each(function(track){ michael@0: var data = $H(track).values().first(); michael@0: this.tracks.push($H({ michael@0: ids: $H(track).keys().first(), michael@0: effect: Effect.Morph, michael@0: options: { style: data } michael@0: })); michael@0: }.bind(this)); michael@0: return this; michael@0: }, michael@0: play: function(){ michael@0: return new Effect.Parallel( michael@0: this.tracks.map(function(track){ michael@0: var elements = [$(track.ids) || $$(track.ids)].flatten(); michael@0: return elements.map(function(e){ return new track.effect(e, Object.extend({ sync:true }, track.options)) }); michael@0: }).flatten(), michael@0: this.options michael@0: ); michael@0: } michael@0: }); michael@0: michael@0: Element.CSS_PROPERTIES = $w( michael@0: 'backgroundColor backgroundPosition borderBottomColor borderBottomStyle ' + michael@0: 'borderBottomWidth borderLeftColor borderLeftStyle borderLeftWidth ' + michael@0: 'borderRightColor borderRightStyle borderRightWidth borderSpacing ' + michael@0: 'borderTopColor borderTopStyle borderTopWidth bottom clip color ' + michael@0: 'fontSize fontWeight height left letterSpacing lineHeight ' + michael@0: 'marginBottom marginLeft marginRight marginTop markerOffset maxHeight '+ michael@0: 'maxWidth minHeight minWidth opacity outlineColor outlineOffset ' + michael@0: 'outlineWidth paddingBottom paddingLeft paddingRight paddingTop ' + michael@0: 'right textIndent top width wordSpacing zIndex'); michael@0: michael@0: Element.CSS_LENGTH = /^(([\+\-]?[0-9\.]+)(em|ex|px|in|cm|mm|pt|pc|\%))|0$/; michael@0: michael@0: String.prototype.parseStyle = function(){ michael@0: var element = document.createElement('div'); michael@0: element.innerHTML = '
'; michael@0: var style = element.childNodes[0].style, styleRules = $H(); michael@0: michael@0: Element.CSS_PROPERTIES.each(function(property){ michael@0: if(style[property]) styleRules[property] = style[property]; michael@0: }); michael@0: if(Prototype.Browser.IE && this.indexOf('opacity') > -1) { michael@0: styleRules.opacity = this.match(/opacity:\s*((?:0|1)?(?:\.\d*)?)/)[1]; michael@0: } michael@0: return styleRules; michael@0: }; michael@0: michael@0: Element.morph = function(element, style) { michael@0: new Effect.Morph(element, Object.extend({ style: style }, arguments[2] || {})); michael@0: return element; michael@0: }; michael@0: michael@0: ['getInlineOpacity','forceRerendering','setContentZoom', michael@0: 'collectTextNodes','collectTextNodesIgnoreClass','morph'].each( michael@0: function(f) { Element.Methods[f] = Element[f]; } michael@0: ); michael@0: michael@0: Element.Methods.visualEffect = function(element, effect, options) { michael@0: s = effect.dasherize().camelize(); michael@0: effect_class = s.charAt(0).toUpperCase() + s.substring(1); michael@0: new Effect[effect_class](element, options); michael@0: return $(element); michael@0: }; michael@0: michael@0: Element.addMethods();