From bf921dee92be3b6c0d7cac902ef2fc0dde21793c Mon Sep 17 00:00:00 2001 From: efyx Date: Fri, 27 May 2011 18:40:42 +0200 Subject: [PATCH] Update to mootools 1.2.5 for demos --- Clients/mootools-core.js | 4399 +++++++++++++++++++------------------- 1 file changed, 2179 insertions(+), 2220 deletions(-) mode change 100755 => 100644 Clients/mootools-core.js diff --git a/Clients/mootools-core.js b/Clients/mootools-core.js old mode 100755 new mode 100644 index 0098571..384c393 --- a/Clients/mootools-core.js +++ b/Clients/mootools-core.js @@ -1,7 +1,7 @@ /* --- -script: Core.js +name: Core description: The core of MooTools, contains all the base functions and the Native and Hash implementations. Required by all the other scripts. @@ -12,17 +12,17 @@ copyright: Copyright (c) 2006-2008 [Valerio Proietti](http://mad4milk.net/). authors: The MooTools production team (http://mootools.net/developers/) inspiration: -- Class implementation inspired by [Base.js](http://dean.edwards.name/weblog/2006/03/base/) Copyright (c) 2006 Dean Edwards, [GNU Lesser General Public License](http://opensource.org/licenses/lgpl-license.php) -- Some functionality inspired by [Prototype.js](http://prototypejs.org) Copyright (c) 2005-2007 Sam Stephenson, [MIT License](http://opensource.org/licenses/mit-license.php) + - Class implementation inspired by [Base.js](http://dean.edwards.name/weblog/2006/03/base/) Copyright (c) 2006 Dean Edwards, [GNU Lesser General Public License](http://opensource.org/licenses/lgpl-license.php) + - Some functionality inspired by [Prototype.js](http://prototypejs.org) Copyright (c) 2005-2007 Sam Stephenson, [MIT License](http://opensource.org/licenses/mit-license.php) -provides: [Mootools, Native, Hash.base, Array.each, $util] +provides: [MooTools, Native, Hash.base, Array.each, $util] ... */ var MooTools = { - 'version': '1.2.4', - 'build': '0d9113241a90b9cd5643b926795852a2026710d4' + 'version': '1.2.5', + 'build': '008d8f0f2fcc2044e54fdd3635341aaab274e757' }; var Native = function(options){ @@ -294,187 +294,15 @@ function $unlink(object){ /* --- -script: Browser.js - -description: The Browser Core. Contains Browser initialization, Window and Document, and the Browser Hash. - -license: MIT-style license. - -requires: -- /Native -- /$util - -provides: [Browser, Window, Document, $exec] - -... -*/ - -var Browser = $merge({ - - Engine: {name: 'unknown', version: 0}, - - Platform: {name: (window.orientation != undefined) ? 'ipod' : (navigator.platform.match(/mac|win|linux/i) || ['other'])[0].toLowerCase()}, - - Features: {xpath: !!(document.evaluate), air: !!(window.runtime), query: !!(document.querySelector)}, - - Plugins: {}, - - Engines: { - - presto: function(){ - return (!window.opera) ? false : ((arguments.callee.caller) ? 960 : ((document.getElementsByClassName) ? 950 : 925)); - }, - - trident: function(){ - return (!window.ActiveXObject) ? false : ((window.XMLHttpRequest) ? ((document.querySelectorAll) ? 6 : 5) : 4); - }, - - webkit: function(){ - return (navigator.taintEnabled) ? false : ((Browser.Features.xpath) ? ((Browser.Features.query) ? 525 : 420) : 419); - }, - - gecko: function(){ - return (!document.getBoxObjectFor && window.mozInnerScreenX == null) ? false : ((document.getElementsByClassName) ? 19 : 18); - } - - } - -}, Browser || {}); - -Browser.Platform[Browser.Platform.name] = true; - -Browser.detect = function(){ - - for (var engine in this.Engines){ - var version = this.Engines[engine](); - if (version){ - this.Engine = {name: engine, version: version}; - this.Engine[engine] = this.Engine[engine + version] = true; - break; - } - } - - return {name: engine, version: version}; - -}; - -Browser.detect(); - -Browser.Request = function(){ - return $try(function(){ - return new XMLHttpRequest(); - }, function(){ - return new ActiveXObject('MSXML2.XMLHTTP'); - }, function(){ - return new ActiveXObject('Microsoft.XMLHTTP'); - }); -}; - -Browser.Features.xhr = !!(Browser.Request()); - -Browser.Plugins.Flash = (function(){ - var version = ($try(function(){ - return navigator.plugins['Shockwave Flash'].description; - }, function(){ - return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version'); - }) || '0 r0').match(/\d+/g); - return {version: parseInt(version[0] || 0 + '.' + version[1], 10) || 0, build: parseInt(version[2], 10) || 0}; -})(); - -function $exec(text){ - if (!text) return text; - if (window.execScript){ - window.execScript(text); - } else { - var script = document.createElement('script'); - script.setAttribute('type', 'text/javascript'); - script[(Browser.Engine.webkit && Browser.Engine.version < 420) ? 'innerText' : 'text'] = text; - document.head.appendChild(script); - document.head.removeChild(script); - } - return text; -}; - -Native.UID = 1; - -var $uid = (Browser.Engine.trident) ? function(item){ - return (item.uid || (item.uid = [Native.UID++]))[0]; -} : function(item){ - return item.uid || (item.uid = Native.UID++); -}; - -var Window = new Native({ - - name: 'Window', - - legacy: (Browser.Engine.trident) ? null: window.Window, - - initialize: function(win){ - $uid(win); - if (!win.Element){ - win.Element = $empty; - if (Browser.Engine.webkit) win.document.createElement("iframe"); //fixes safari 2 - win.Element.prototype = (Browser.Engine.webkit) ? window["[[DOMElement.prototype]]"] : {}; - } - win.document.window = win; - return $extend(win, Window.Prototype); - }, - - afterImplement: function(property, value){ - window[property] = Window.Prototype[property] = value; - } - -}); - -Window.Prototype = {$family: {name: 'window'}}; - -new Window(window); - -var Document = new Native({ - - name: 'Document', - - legacy: (Browser.Engine.trident) ? null: window.Document, - - initialize: function(doc){ - $uid(doc); - doc.head = doc.getElementsByTagName('head')[0]; - doc.html = doc.getElementsByTagName('html')[0]; - if (Browser.Engine.trident && Browser.Engine.version <= 4) $try(function(){ - doc.execCommand("BackgroundImageCache", false, true); - }); - if (Browser.Engine.trident) doc.window.attachEvent('onunload', function(){ - doc.window.detachEvent('onunload', arguments.callee); - doc.head = doc.html = doc.window = null; - }); - return $extend(doc, Document.Prototype); - }, - - afterImplement: function(property, value){ - document[property] = Document.Prototype[property] = value; - } - -}); - -Document.Prototype = {$family: {name: 'document'}}; - -new Document(document); - - -/* ---- - -script: Array.js +name: Array description: Contains Array Prototypes like each, contains, and erase. license: MIT-style license. -requires: -- /$util -- /Array.each +requires: [$util, Array.each] -provides: [Array] +provides: Array ... */ @@ -616,241 +444,239 @@ Array.implement({ /* --- -script: Function.js +name: String -description: Contains Function Prototypes like create, bind, pass, and delay. +description: Contains String Prototypes like camelCase, capitalize, test, and toInt. license: MIT-style license. -requires: -- /Native -- /$util +requires: Native -provides: [Function] +provides: String ... */ -Function.implement({ +String.implement({ - extend: function(properties){ - for (var property in properties) this[property] = properties[property]; - return this; + test: function(regex, params){ + return ((typeof regex == 'string') ? new RegExp(regex, params) : regex).test(this); }, - create: function(options){ - var self = this; - options = options || {}; - return function(event){ - var args = options.arguments; - args = (args != undefined) ? $splat(args) : Array.slice(arguments, (options.event) ? 1 : 0); - if (options.event) args = [event || window.event].extend(args); - var returns = function(){ - return self.apply(options.bind || null, args); - }; - if (options.delay) return setTimeout(returns, options.delay); - if (options.periodical) return setInterval(returns, options.periodical); - if (options.attempt) return $try(returns); - return returns(); - }; + contains: function(string, separator){ + return (separator) ? (separator + this + separator).indexOf(separator + string + separator) > -1 : this.indexOf(string) > -1; }, - run: function(args, bind){ - return this.apply(bind, $splat(args)); + trim: function(){ + return this.replace(/^\s+|\s+$/g, ''); }, - pass: function(args, bind){ - return this.create({bind: bind, arguments: args}); + clean: function(){ + return this.replace(/\s+/g, ' ').trim(); }, - bind: function(bind, args){ - return this.create({bind: bind, arguments: args}); + camelCase: function(){ + return this.replace(/-\D/g, function(match){ + return match.charAt(1).toUpperCase(); + }); }, - bindWithEvent: function(bind, args){ - return this.create({bind: bind, arguments: args, event: true}); + hyphenate: function(){ + return this.replace(/[A-Z]/g, function(match){ + return ('-' + match.charAt(0).toLowerCase()); + }); }, - attempt: function(args, bind){ - return this.create({bind: bind, arguments: args, attempt: true})(); + capitalize: function(){ + return this.replace(/\b[a-z]/g, function(match){ + return match.toUpperCase(); + }); }, - delay: function(delay, bind, args){ - return this.create({bind: bind, arguments: args, delay: delay})(); + escapeRegExp: function(){ + return this.replace(/([-.*+?^${}()|[\]\/\\])/g, '\\$1'); }, - periodical: function(periodical, bind, args){ - return this.create({bind: bind, arguments: args, periodical: periodical})(); - } - -}); - - -/* ---- - -script: Number.js - -description: Contains Number Prototypes like limit, round, times, and ceil. - -license: MIT-style license. - -requires: -- /Native -- /$util - -provides: [Number] - -... -*/ - -Number.implement({ + toInt: function(base){ + return parseInt(this, base || 10); + }, - limit: function(min, max){ - return Math.min(max, Math.max(min, this)); + toFloat: function(){ + return parseFloat(this); }, - round: function(precision){ - precision = Math.pow(10, precision || 0); - return Math.round(this * precision) / precision; + hexToRgb: function(array){ + var hex = this.match(/^#?(\w{1,2})(\w{1,2})(\w{1,2})$/); + return (hex) ? hex.slice(1).hexToRgb(array) : null; }, - times: function(fn, bind){ - for (var i = 0; i < this; i++) fn.call(bind, i, this); + rgbToHex: function(array){ + var rgb = this.match(/\d{1,3}/g); + return (rgb) ? rgb.rgbToHex(array) : null; }, - toFloat: function(){ - return parseFloat(this); + stripScripts: function(option){ + var scripts = ''; + var text = this.replace(/]*>([\s\S]*?)<\/script>/gi, function(){ + scripts += arguments[1] + '\n'; + return ''; + }); + if (option === true) $exec(scripts); + else if ($type(option) == 'function') option(scripts, text); + return text; }, - toInt: function(base){ - return parseInt(this, base || 10); + substitute: function(object, regexp){ + return this.replace(regexp || (/\\?\{([^{}]+)\}/g), function(match, name){ + if (match.charAt(0) == '\\') return match.slice(1); + return (object[name] != undefined) ? object[name] : ''; + }); } }); -Number.alias('times', 'each'); - -(function(math){ - var methods = {}; - math.each(function(name){ - if (!Number[name]) methods[name] = function(){ - return Math[name].apply(null, [this].concat($A(arguments))); - }; - }); - Number.implement(methods); -})(['abs', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'exp', 'floor', 'log', 'max', 'min', 'pow', 'sin', 'sqrt', 'tan']); - /* --- -script: String.js +name: Function -description: Contains String Prototypes like camelCase, capitalize, test, and toInt. +description: Contains Function Prototypes like create, bind, pass, and delay. license: MIT-style license. -requires: -- /Native +requires: [Native, $util] -provides: [String] +provides: Function ... */ -String.implement({ +try { + delete Function.prototype.bind; +} catch(e){} - test: function(regex, params){ - return ((typeof regex == 'string') ? new RegExp(regex, params) : regex).test(this); - }, +Function.implement({ - contains: function(string, separator){ - return (separator) ? (separator + this + separator).indexOf(separator + string + separator) > -1 : this.indexOf(string) > -1; + extend: function(properties){ + for (var property in properties) this[property] = properties[property]; + return this; }, - trim: function(){ - return this.replace(/^\s+|\s+$/g, ''); + create: function(options){ + var self = this; + options = options || {}; + return function(event){ + var args = options.arguments; + args = (args != undefined) ? $splat(args) : Array.slice(arguments, (options.event) ? 1 : 0); + if (options.event) args = [event || window.event].extend(args); + var returns = function(){ + return self.apply(options.bind || null, args); + }; + if (options.delay) return setTimeout(returns, options.delay); + if (options.periodical) return setInterval(returns, options.periodical); + if (options.attempt) return $try(returns); + return returns(); + }; }, - clean: function(){ - return this.replace(/\s+/g, ' ').trim(); + run: function(args, bind){ + return this.apply(bind, $splat(args)); }, - camelCase: function(){ - return this.replace(/-\D/g, function(match){ - return match.charAt(1).toUpperCase(); - }); + pass: function(args, bind){ + return this.create({bind: bind, arguments: args}); }, - hyphenate: function(){ - return this.replace(/[A-Z]/g, function(match){ - return ('-' + match.charAt(0).toLowerCase()); - }); + bind: function(bind, args){ + return this.create({bind: bind, arguments: args}); }, - capitalize: function(){ - return this.replace(/\b[a-z]/g, function(match){ - return match.toUpperCase(); - }); + bindWithEvent: function(bind, args){ + return this.create({bind: bind, arguments: args, event: true}); }, - escapeRegExp: function(){ - return this.replace(/([-.*+?^${}()|[\]\/\\])/g, '\\$1'); + attempt: function(args, bind){ + return this.create({bind: bind, arguments: args, attempt: true})(); }, - toInt: function(base){ - return parseInt(this, base || 10); + delay: function(delay, bind, args){ + return this.create({bind: bind, arguments: args, delay: delay})(); }, - toFloat: function(){ - return parseFloat(this); + periodical: function(periodical, bind, args){ + return this.create({bind: bind, arguments: args, periodical: periodical})(); + } + +}); + + +/* +--- + +name: Number + +description: Contains Number Prototypes like limit, round, times, and ceil. + +license: MIT-style license. + +requires: [Native, $util] + +provides: Number + +... +*/ + +Number.implement({ + + limit: function(min, max){ + return Math.min(max, Math.max(min, this)); }, - hexToRgb: function(array){ - var hex = this.match(/^#?(\w{1,2})(\w{1,2})(\w{1,2})$/); - return (hex) ? hex.slice(1).hexToRgb(array) : null; + round: function(precision){ + precision = Math.pow(10, precision || 0); + return Math.round(this * precision) / precision; }, - rgbToHex: function(array){ - var rgb = this.match(/\d{1,3}/g); - return (rgb) ? rgb.rgbToHex(array) : null; + times: function(fn, bind){ + for (var i = 0; i < this; i++) fn.call(bind, i, this); }, - stripScripts: function(option){ - var scripts = ''; - var text = this.replace(/]*>([\s\S]*?)<\/script>/gi, function(){ - scripts += arguments[1] + '\n'; - return ''; - }); - if (option === true) $exec(scripts); - else if ($type(option) == 'function') option(scripts, text); - return text; + toFloat: function(){ + return parseFloat(this); }, - substitute: function(object, regexp){ - return this.replace(regexp || (/\\?\{([^{}]+)\}/g), function(match, name){ - if (match.charAt(0) == '\\') return match.slice(1); - return (object[name] != undefined) ? object[name] : ''; - }); + toInt: function(base){ + return parseInt(this, base || 10); } }); +Number.alias('times', 'each'); + +(function(math){ + var methods = {}; + math.each(function(name){ + if (!Number[name]) methods[name] = function(){ + return Math[name].apply(null, [this].concat($A(arguments))); + }; + }); + Number.implement(methods); +})(['abs', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'exp', 'floor', 'log', 'max', 'min', 'pow', 'sin', 'sqrt', 'tan']); + /* --- -script: Hash.js +name: Hash description: Contains Hash Prototypes. Provides a means for overcoming the JavaScript practical impossibility of extending native Objects. license: MIT-style license. -requires: -- /Hash.base +requires: Hash.base -provides: [Hash] +provides: Hash ... */ @@ -986,205 +812,68 @@ Hash.alias({keyOf: 'indexOf', hasValue: 'contains'}); /* --- -script: Event.js +name: Class -description: Contains the Event Class, to make the event object cross-browser. +description: Contains the Class Function for easily creating, extending, and implementing reusable Classes. license: MIT-style license. -requires: -- /Window -- /Document -- /Hash -- /Array -- /Function -- /String +requires: [$util, Native, Array, String, Function, Number, Hash] -provides: [Event] +provides: Class ... */ -var Event = new Native({ +function Class(params){ + + if (params instanceof Function) params = {initialize: params}; + + var newClass = function(){ + Object.reset(this); + if (newClass._prototyping) return this; + this._current = $empty; + var value = (this.initialize) ? this.initialize.apply(this, arguments) : this; + delete this._current; delete this.caller; + return value; + }.extend(this); + + newClass.implement(params); + + newClass.constructor = Class; + newClass.prototype.constructor = newClass; - name: 'Event', + return newClass; - initialize: function(event, win){ - win = win || window; - var doc = win.document; - event = event || win.event; - if (event.$extended) return event; - this.$extended = true; - var type = event.type; - var target = event.target || event.srcElement; - while (target && target.nodeType == 3) target = target.parentNode; +}; - if (type.test(/key/)){ - var code = event.which || event.keyCode; - var key = Event.Keys.keyOf(code); - if (type == 'keydown'){ - var fKey = code - 111; - if (fKey > 0 && fKey < 13) key = 'f' + fKey; - } - key = key || String.fromCharCode(code).toLowerCase(); - } else if (type.match(/(click|mouse|menu)/i)){ - doc = (!doc.compatMode || doc.compatMode == 'CSS1Compat') ? doc.html : doc.body; - var page = { - x: event.pageX || event.clientX + doc.scrollLeft, - y: event.pageY || event.clientY + doc.scrollTop - }; - var client = { - x: (event.pageX) ? event.pageX - win.pageXOffset : event.clientX, - y: (event.pageY) ? event.pageY - win.pageYOffset : event.clientY - }; - if (type.match(/DOMMouseScroll|mousewheel/)){ - var wheel = (event.wheelDelta) ? event.wheelDelta / 120 : -(event.detail || 0) / 3; - } - var rightClick = (event.which == 3) || (event.button == 2); - var related = null; - if (type.match(/over|out/)){ - switch (type){ - case 'mouseover': related = event.relatedTarget || event.fromElement; break; - case 'mouseout': related = event.relatedTarget || event.toElement; - } - if (!(function(){ - while (related && related.nodeType == 3) related = related.parentNode; - return true; - }).create({attempt: Browser.Engine.gecko})()) related = false; - } - } +Function.prototype.protect = function(){ + this._protected = true; + return this; +}; - return $extend(this, { - event: event, - type: type, - - page: page, - client: client, - rightClick: rightClick, - - wheel: wheel, - - relatedTarget: related, - target: target, - - code: code, - key: key, - - shift: event.shiftKey, - control: event.ctrlKey, - alt: event.altKey, - meta: event.metaKey - }); - } - -}); - -Event.Keys = new Hash({ - 'enter': 13, - 'up': 38, - 'down': 40, - 'left': 37, - 'right': 39, - 'esc': 27, - 'space': 32, - 'backspace': 8, - 'tab': 9, - 'delete': 46 -}); - -Event.implement({ - - stop: function(){ - return this.stopPropagation().preventDefault(); - }, - - stopPropagation: function(){ - if (this.event.stopPropagation) this.event.stopPropagation(); - else this.event.cancelBubble = true; - return this; - }, - - preventDefault: function(){ - if (this.event.preventDefault) this.event.preventDefault(); - else this.event.returnValue = false; - return this; - } - -}); - - -/* ---- - -script: Class.js - -description: Contains the Class Function for easily creating, extending, and implementing reusable Classes. - -license: MIT-style license. - -requires: -- /$util -- /Native -- /Array -- /String -- /Function -- /Number -- /Hash - -provides: [Class] - -... -*/ - -function Class(params){ - - if (params instanceof Function) params = {initialize: params}; - - var newClass = function(){ - Object.reset(this); - if (newClass._prototyping) return this; - this._current = $empty; - var value = (this.initialize) ? this.initialize.apply(this, arguments) : this; - delete this._current; delete this.caller; - return value; - }.extend(this); - - newClass.implement(params); - - newClass.constructor = Class; - newClass.prototype.constructor = newClass; - - return newClass; - -}; - -Function.prototype.protect = function(){ - this._protected = true; - return this; -}; - -Object.reset = function(object, key){ - - if (key == null){ - for (var p in object) Object.reset(object, p); - return object; - } - - delete object[key]; - - switch ($type(object[key])){ - case 'object': - var F = function(){}; - F.prototype = object[key]; - var i = new F; - object[key] = Object.reset(i); - break; - case 'array': object[key] = $unlink(object[key]); break; - } - - return object; - -}; +Object.reset = function(object, key){ + + if (key == null){ + for (var p in object) Object.reset(object, p); + return object; + } + + delete object[key]; + + switch ($type(object[key])){ + case 'object': + var F = function(){}; + F.prototype = object[key]; + var i = new F; + object[key] = Object.reset(i); + break; + case 'array': object[key] = $unlink(object[key]); break; + } + + return object; + +}; new Native({name: 'Class', initialize: Class}).extend({ @@ -1285,16 +974,15 @@ Class.Mutators = { /* --- -script: Class.Extras.js +name: Class.Extras description: Contains Utility Classes that can be implemented into your own Classes to ease the execution of many common tasks. license: MIT-style license. -requires: -- /Class +requires: Class -provides: [Chain, Events, Options] +provides: [Chain, Events, Options, Class.Extras] ... */ @@ -1396,141 +1084,319 @@ var Options = new Class({ /* --- -script: Element.js +name: Browser -description: One of the most important items in MooTools. Contains the dollar function, the dollars function, and an handful of cross-browser, time-saver methods to let you easily work with HTML Elements. +description: The Browser Core. Contains Browser initialization, Window and Document, and the Browser Hash. license: MIT-style license. -requires: -- /Window -- /Document -- /Array -- /String -- /Function -- /Number -- /Hash +requires: [Native, $util] -provides: [Element, Elements, $, $$, Iframe] +provides: [Browser, Window, Document, $exec] ... */ -var Element = new Native({ - - name: 'Element', +var Browser = $merge({ - legacy: window.Element, + Engine: {name: 'unknown', version: 0}, - initialize: function(tag, props){ - var konstructor = Element.Constructors.get(tag); - if (konstructor) return konstructor(props); - if (typeof tag == 'string') return document.newElement(tag, props); - return document.id(tag).set(props); - }, + Platform: {name: (window.orientation != undefined) ? 'ipod' : (navigator.platform.match(/mac|win|linux/i) || ['other'])[0].toLowerCase()}, - afterImplement: function(key, value){ - Element.Prototype[key] = value; - if (Array[key]) return; - Elements.implement(key, function(){ - var items = [], elements = true; - for (var i = 0, j = this.length; i < j; i++){ - var returns = this[i][key].apply(this[i], arguments); - items.push(returns); - if (elements) elements = ($type(returns) == 'element'); - } - return (elements) ? new Elements(items) : items; - }); - } + Features: {xpath: !!(document.evaluate), air: !!(window.runtime), query: !!(document.querySelector)}, -}); + Plugins: {}, -Element.Prototype = {$family: {name: 'element'}}; + Engines: { -Element.Constructors = new Hash; + presto: function(){ + return (!window.opera) ? false : ((arguments.callee.caller) ? 960 : ((document.getElementsByClassName) ? 950 : 925)); + }, -var IFrame = new Native({ + trident: function(){ + return (!window.ActiveXObject) ? false : ((window.XMLHttpRequest) ? ((document.querySelectorAll) ? 6 : 5) : 4); + }, - name: 'IFrame', + webkit: function(){ + return (navigator.taintEnabled) ? false : ((Browser.Features.xpath) ? ((Browser.Features.query) ? 525 : 420) : 419); + }, - generics: false, + gecko: function(){ + return (!document.getBoxObjectFor && window.mozInnerScreenX == null) ? false : ((document.getElementsByClassName) ? 19 : 18); + } - initialize: function(){ - var params = Array.link(arguments, {properties: Object.type, iframe: $defined}); - var props = params.properties || {}; - var iframe = document.id(params.iframe); - var onload = props.onload || $empty; - delete props.onload; - props.id = props.name = $pick(props.id, props.name, iframe ? (iframe.id || iframe.name) : 'IFrame_' + $time()); - iframe = new Element(iframe || 'iframe', props); - var onFrameLoad = function(){ - var host = $try(function(){ - return iframe.contentWindow.location.host; - }); - if (!host || host == window.location.host){ - var win = new Window(iframe.contentWindow); - new Document(iframe.contentWindow.document); - $extend(win.Element.prototype, Element.Prototype); - } - onload.call(iframe.contentWindow, iframe.contentWindow.document); - }; - var contentWindow = $try(function(){ - return iframe.contentWindow; - }); - ((contentWindow && contentWindow.document.body) || window.frames[props.id]) ? onFrameLoad() : iframe.addListener('load', onFrameLoad); - return iframe; } -}); +}, Browser || {}); -var Elements = new Native({ +Browser.Platform[Browser.Platform.name] = true; - initialize: function(elements, options){ - options = $extend({ddup: true, cash: true}, options); - elements = elements || []; - if (options.ddup || options.cash){ - var uniques = {}, returned = []; - for (var i = 0, l = elements.length; i < l; i++){ - var el = document.id(elements[i], !options.cash); - if (options.ddup){ - if (uniques[el.uid]) continue; - uniques[el.uid] = true; - } - if (el) returned.push(el); - } - elements = returned; +Browser.detect = function(){ + + for (var engine in this.Engines){ + var version = this.Engines[engine](); + if (version){ + this.Engine = {name: engine, version: version}; + this.Engine[engine] = this.Engine[engine + version] = true; + break; } - return (options.cash) ? $extend(elements, this) : elements; } -}); + return {name: engine, version: version}; -Elements.implement({ +}; - filter: function(filter, bind){ - if (!filter) return this; - return new Elements(Array.filter(this, (typeof filter == 'string') ? function(item){ - return item.match(filter); - } : filter, bind)); - } +Browser.detect(); -}); +Browser.Request = function(){ + return $try(function(){ + return new XMLHttpRequest(); + }, function(){ + return new ActiveXObject('MSXML2.XMLHTTP'); + }, function(){ + return new ActiveXObject('Microsoft.XMLHTTP'); + }); +}; -Document.implement({ +Browser.Features.xhr = !!(Browser.Request()); - newElement: function(tag, props){ - if (Browser.Engine.trident && props){ - ['name', 'type', 'checked'].each(function(attribute){ - if (!props[attribute]) return; - tag += ' ' + attribute + '="' + props[attribute] + '"'; - if (attribute != 'checked') delete props[attribute]; - }); - tag = '<' + tag + '>'; - } - return document.id(this.createElement(tag)).set(props); - }, +Browser.Plugins.Flash = (function(){ + var version = ($try(function(){ + return navigator.plugins['Shockwave Flash'].description; + }, function(){ + return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version'); + }) || '0 r0').match(/\d+/g); + return {version: parseInt(version[0] || 0 + '.' + version[1], 10) || 0, build: parseInt(version[2], 10) || 0}; +})(); - newTextNode: function(text){ +function $exec(text){ + if (!text) return text; + if (window.execScript){ + window.execScript(text); + } else { + var script = document.createElement('script'); + script.setAttribute('type', 'text/javascript'); + script[(Browser.Engine.webkit && Browser.Engine.version < 420) ? 'innerText' : 'text'] = text; + document.head.appendChild(script); + document.head.removeChild(script); + } + return text; +}; + +Native.UID = 1; + +var $uid = (Browser.Engine.trident) ? function(item){ + return (item.uid || (item.uid = [Native.UID++]))[0]; +} : function(item){ + return item.uid || (item.uid = Native.UID++); +}; + +var Window = new Native({ + + name: 'Window', + + legacy: (Browser.Engine.trident) ? null: window.Window, + + initialize: function(win){ + $uid(win); + if (!win.Element){ + win.Element = $empty; + if (Browser.Engine.webkit) win.document.createElement("iframe"); //fixes safari 2 + win.Element.prototype = (Browser.Engine.webkit) ? window["[[DOMElement.prototype]]"] : {}; + } + win.document.window = win; + return $extend(win, Window.Prototype); + }, + + afterImplement: function(property, value){ + window[property] = Window.Prototype[property] = value; + } + +}); + +Window.Prototype = {$family: {name: 'window'}}; + +new Window(window); + +var Document = new Native({ + + name: 'Document', + + legacy: (Browser.Engine.trident) ? null: window.Document, + + initialize: function(doc){ + $uid(doc); + doc.head = doc.getElementsByTagName('head')[0]; + doc.html = doc.getElementsByTagName('html')[0]; + if (Browser.Engine.trident && Browser.Engine.version <= 4) $try(function(){ + doc.execCommand("BackgroundImageCache", false, true); + }); + if (Browser.Engine.trident) doc.window.attachEvent('onunload', function(){ + doc.window.detachEvent('onunload', arguments.callee); + doc.head = doc.html = doc.window = null; + }); + return $extend(doc, Document.Prototype); + }, + + afterImplement: function(property, value){ + document[property] = Document.Prototype[property] = value; + } + +}); + +Document.Prototype = {$family: {name: 'document'}}; + +new Document(document); + + +/* +--- + +name: Element + +description: One of the most important items in MooTools. Contains the dollar function, the dollars function, and an handful of cross-browser, time-saver methods to let you easily work with HTML Elements. + +license: MIT-style license. + +requires: [Window, Document, Array, String, Function, Number, Hash] + +provides: [Element, Elements, $, $$, Iframe] + +... +*/ + +var Element = new Native({ + + name: 'Element', + + legacy: window.Element, + + initialize: function(tag, props){ + var konstructor = Element.Constructors.get(tag); + if (konstructor) return konstructor(props); + if (typeof tag == 'string') return document.newElement(tag, props); + return document.id(tag).set(props); + }, + + afterImplement: function(key, value){ + Element.Prototype[key] = value; + if (Array[key]) return; + Elements.implement(key, function(){ + var items = [], elements = true; + for (var i = 0, j = this.length; i < j; i++){ + var returns = this[i][key].apply(this[i], arguments); + items.push(returns); + if (elements) elements = ($type(returns) == 'element'); + } + return (elements) ? new Elements(items) : items; + }); + } + +}); + +Element.Prototype = {$family: {name: 'element'}}; + +Element.Constructors = new Hash; + +var IFrame = new Native({ + + name: 'IFrame', + + generics: false, + + initialize: function(){ + var params = Array.link(arguments, {properties: Object.type, iframe: $defined}); + var props = params.properties || {}; + var iframe = document.id(params.iframe); + var onload = props.onload || $empty; + delete props.onload; + props.id = props.name = $pick(props.id, props.name, iframe ? (iframe.id || iframe.name) : 'IFrame_' + $time()); + iframe = new Element(iframe || 'iframe', props); + var onFrameLoad = function(){ + var host = $try(function(){ + return iframe.contentWindow.location.host; + }); + if (!host || host == window.location.host){ + var win = new Window(iframe.contentWindow); + new Document(iframe.contentWindow.document); + $extend(win.Element.prototype, Element.Prototype); + } + onload.call(iframe.contentWindow, iframe.contentWindow.document); + }; + var contentWindow = $try(function(){ + return iframe.contentWindow; + }); + ((contentWindow && contentWindow.document.body) || window.frames[props.id]) ? onFrameLoad() : iframe.addListener('load', onFrameLoad); + return iframe; + } + +}); + +var Elements = new Native({ + + initialize: function(elements, options){ + options = $extend({ddup: true, cash: true}, options); + elements = elements || []; + if (options.ddup || options.cash){ + var uniques = {}, returned = []; + for (var i = 0, l = elements.length; i < l; i++){ + var el = document.id(elements[i], !options.cash); + if (options.ddup){ + if (uniques[el.uid]) continue; + uniques[el.uid] = true; + } + if (el) returned.push(el); + } + elements = returned; + } + return (options.cash) ? $extend(elements, this) : elements; + } + +}); + +Elements.implement({ + + filter: function(filter, bind){ + if (!filter) return this; + return new Elements(Array.filter(this, (typeof filter == 'string') ? function(item){ + return item.match(filter); + } : filter, bind)); + } + +}); + +(function(){ + +/**/ +var createElementAcceptsHTML; +try { + var x = document.createElement(''); + createElementAcceptsHTML = (x.name == 'x'); +} catch(e){} + +var escapeQuotes = function(html){ + return ('' + html).replace(/&/g,'&').replace(/"/g,'"'); +}; +/**/ + +Document.implement({ + + newElement: function(tag, props){ + if (props && props.checked != null) props.defaultChecked = props.checked; + /**/// Fix for readonly name and type properties in IE < 8 + if (createElementAcceptsHTML && props){ + tag = '<' + tag; + if (props.name) tag += ' name="' + escapeQuotes(props.name) + '"'; + if (props.type) tag += ' type="' + escapeQuotes(props.type) + '"'; + tag += '>'; + delete props.name; + delete props.type; + } + /**/ + return this.id(this.createElement(tag)).set(props); + }, + + newTextNode: function(text){ return this.createTextNode(text); }, @@ -1579,6 +1445,8 @@ Document.implement({ }); +})(); + if (window.$ == null) Window.implement({ $: function(el, nc){ return document.id(el, nc, this.document); @@ -1642,6 +1510,7 @@ var get = function(uid){ var clean = function(item, retain){ if (!item) return; var uid = item.uid; + if (retain !== true) retain = false; if (Browser.Engine.trident){ if (item.clearAttributes){ var clone = retain && item.cloneNode(false); @@ -2107,947 +1976,978 @@ if (Browser.Engine.webkit && Browser.Engine.version < 420) Element.Properties.te /* --- -script: Element.Event.js +name: Element.Dimensions -description: Contains Element methods for dealing with events. This file also includes mouseenter and mouseleave custom Element Events. +description: Contains methods to work with size, scroll, or positioning of Elements and the window object. license: MIT-style license. -requires: -- /Element -- /Event +credits: + - Element positioning based on the [qooxdoo](http://qooxdoo.org/) code and smart browser fixes, [LGPL License](http://www.gnu.org/licenses/lgpl.html). + - Viewport dimensions based on [YUI](http://developer.yahoo.com/yui/) code, [BSD License](http://developer.yahoo.com/yui/license.html). + +requires: Element -provides: [Element.Event] +provides: Element.Dimensions ... */ -Element.Properties.events = {set: function(events){ - this.addEvents(events); -}}; +(function(){ -Native.implement([Element, Window, Document], { +Element.implement({ - addEvent: function(type, fn){ - var events = this.retrieve('events', {}); - events[type] = events[type] || {'keys': [], 'values': []}; - if (events[type].keys.contains(fn)) return this; - events[type].keys.push(fn); - var realType = type, custom = Element.Events.get(type), condition = fn, self = this; - if (custom){ - if (custom.onAdd) custom.onAdd.call(this, fn); - if (custom.condition){ - condition = function(event){ - if (custom.condition.call(this, event)) return fn.call(this, event); - return true; - }; - } - realType = custom.base || realType; - } - var defn = function(){ - return fn.call(self); - }; - var nativeEvent = Element.NativeEvents[realType]; - if (nativeEvent){ - if (nativeEvent == 2){ - defn = function(event){ - event = new Event(event, self.getWindow()); - if (condition.call(self, event) === false) event.stop(); - }; - } - this.addListener(realType, defn); + scrollTo: function(x, y){ + if (isBody(this)){ + this.getWindow().scrollTo(x, y); + } else { + this.scrollLeft = x; + this.scrollTop = y; } - events[type].values.push(defn); return this; }, - removeEvent: function(type, fn){ - var events = this.retrieve('events'); - if (!events || !events[type]) return this; - var pos = events[type].keys.indexOf(fn); - if (pos == -1) return this; - events[type].keys.splice(pos, 1); - var value = events[type].values.splice(pos, 1)[0]; - var custom = Element.Events.get(type); - if (custom){ - if (custom.onRemove) custom.onRemove.call(this, fn); - type = custom.base || type; - } - return (Element.NativeEvents[type]) ? this.removeListener(type, value) : this; + getSize: function(){ + if (isBody(this)) return this.getWindow().getSize(); + return {x: this.offsetWidth, y: this.offsetHeight}; }, - addEvents: function(events){ - for (var event in events) this.addEvent(event, events[event]); - return this; + getScrollSize: function(){ + if (isBody(this)) return this.getWindow().getScrollSize(); + return {x: this.scrollWidth, y: this.scrollHeight}; }, - removeEvents: function(events){ - var type; - if ($type(events) == 'object'){ - for (type in events) this.removeEvent(type, events[type]); - return this; - } - var attached = this.retrieve('events'); - if (!attached) return this; - if (!events){ - for (type in attached) this.removeEvents(type); - this.eliminate('events'); - } else if (attached[events]){ - while (attached[events].keys[0]) this.removeEvent(events, attached[events].keys[0]); - attached[events] = null; + getScroll: function(){ + if (isBody(this)) return this.getWindow().getScroll(); + return {x: this.scrollLeft, y: this.scrollTop}; + }, + + getScrolls: function(){ + var element = this, position = {x: 0, y: 0}; + while (element && !isBody(element)){ + position.x += element.scrollLeft; + position.y += element.scrollTop; + element = element.parentNode; } - return this; + return position; }, - fireEvent: function(type, args, delay){ - var events = this.retrieve('events'); - if (!events || !events[type]) return this; - events[type].keys.each(function(fn){ - fn.create({'bind': this, 'delay': delay, 'arguments': args})(); - }, this); - return this; + getOffsetParent: function(){ + var element = this; + if (isBody(element)) return null; + if (!Browser.Engine.trident) return element.offsetParent; + while ((element = element.parentNode) && !isBody(element)){ + if (styleString(element, 'position') != 'static') return element; + } + return null; }, - cloneEvents: function(from, type){ - from = document.id(from); - var fevents = from.retrieve('events'); - if (!fevents) return this; - if (!type){ - for (var evType in fevents) this.cloneEvents(from, evType); - } else if (fevents[type]){ - fevents[type].keys.each(function(fn){ - this.addEvent(type, fn); - }, this); + getOffsets: function(){ + if (this.getBoundingClientRect){ + var bound = this.getBoundingClientRect(), + html = document.id(this.getDocument().documentElement), + htmlScroll = html.getScroll(), + elemScrolls = this.getScrolls(), + elemScroll = this.getScroll(), + isFixed = (styleString(this, 'position') == 'fixed'); + + return { + x: bound.left.toInt() + elemScrolls.x - elemScroll.x + ((isFixed) ? 0 : htmlScroll.x) - html.clientLeft, + y: bound.top.toInt() + elemScrolls.y - elemScroll.y + ((isFixed) ? 0 : htmlScroll.y) - html.clientTop + }; } - return this; - } -}); + var element = this, position = {x: 0, y: 0}; + if (isBody(this)) return position; -Element.NativeEvents = { - click: 2, dblclick: 2, mouseup: 2, mousedown: 2, contextmenu: 2, //mouse buttons - mousewheel: 2, DOMMouseScroll: 2, //mouse wheel - mouseover: 2, mouseout: 2, mousemove: 2, selectstart: 2, selectend: 2, //mouse movement - keydown: 2, keypress: 2, keyup: 2, //keyboard - focus: 2, blur: 2, change: 2, reset: 2, select: 2, submit: 2, //form elements - load: 1, unload: 1, beforeunload: 2, resize: 1, move: 1, DOMContentLoaded: 1, readystatechange: 1, //window - error: 1, abort: 1, scroll: 1 //misc -}; + while (element && !isBody(element)){ + position.x += element.offsetLeft; + position.y += element.offsetTop; -(function(){ + if (Browser.Engine.gecko){ + if (!borderBox(element)){ + position.x += leftBorder(element); + position.y += topBorder(element); + } + var parent = element.parentNode; + if (parent && styleString(parent, 'overflow') != 'visible'){ + position.x += leftBorder(parent); + position.y += topBorder(parent); + } + } else if (element != this && Browser.Engine.webkit){ + position.x += leftBorder(element); + position.y += topBorder(element); + } -var $check = function(event){ - var related = event.relatedTarget; - if (related == undefined) return true; - if (related === false) return false; - return ($type(this) != 'document' && related != this && related.prefix != 'xul' && !this.hasChild(related)); -}; + element = element.offsetParent; + } + if (Browser.Engine.gecko && !borderBox(this)){ + position.x -= leftBorder(this); + position.y -= topBorder(this); + } + return position; + }, -Element.Events = new Hash({ + getPosition: function(relative){ + if (isBody(this)) return {x: 0, y: 0}; + var offset = this.getOffsets(), + scroll = this.getScrolls(); + var position = { + x: offset.x - scroll.x, + y: offset.y - scroll.y + }; + var relativePosition = (relative && (relative = document.id(relative))) ? relative.getPosition() : {x: 0, y: 0}; + return {x: position.x - relativePosition.x, y: position.y - relativePosition.y}; + }, - mouseenter: { - base: 'mouseover', - condition: $check + getCoordinates: function(element){ + if (isBody(this)) return this.getWindow().getCoordinates(); + var position = this.getPosition(element), + size = this.getSize(); + var obj = { + left: position.x, + top: position.y, + width: size.x, + height: size.y + }; + obj.right = obj.left + obj.width; + obj.bottom = obj.top + obj.height; + return obj; }, - mouseleave: { - base: 'mouseout', - condition: $check + computePosition: function(obj){ + return { + left: obj.x - styleNumber(this, 'margin-left'), + top: obj.y - styleNumber(this, 'margin-top') + }; }, - mousewheel: { - base: (Browser.Engine.gecko) ? 'DOMMouseScroll' : 'mousewheel' + setPosition: function(obj){ + return this.setStyles(this.computePosition(obj)); } }); -})(); +Native.implement([Document, Window], { -/* ---- + getSize: function(){ + if (Browser.Engine.presto || Browser.Engine.webkit){ + var win = this.getWindow(); + return {x: win.innerWidth, y: win.innerHeight}; + } + var doc = getCompatElement(this); + return {x: doc.clientWidth, y: doc.clientHeight}; + }, -script: Element.Style.js + getScroll: function(){ + var win = this.getWindow(), doc = getCompatElement(this); + return {x: win.pageXOffset || doc.scrollLeft, y: win.pageYOffset || doc.scrollTop}; + }, -description: Contains methods for interacting with the styles of Elements in a fashionable way. + getScrollSize: function(){ + var doc = getCompatElement(this), min = this.getSize(); + return {x: Math.max(doc.scrollWidth, min.x), y: Math.max(doc.scrollHeight, min.y)}; + }, -license: MIT-style license. + getPosition: function(){ + return {x: 0, y: 0}; + }, -requires: -- /Element + getCoordinates: function(){ + var size = this.getSize(); + return {top: 0, left: 0, bottom: size.y, right: size.x, height: size.y, width: size.x}; + } -provides: [Element.Style] +}); -... -*/ +// private methods -Element.Properties.styles = {set: function(styles){ - this.setStyles(styles); -}}; +var styleString = Element.getComputedStyle; -Element.Properties.opacity = { +function styleNumber(element, style){ + return styleString(element, style).toInt() || 0; +}; - set: function(opacity, novisibility){ - if (!novisibility){ - if (opacity == 0){ - if (this.style.visibility != 'hidden') this.style.visibility = 'hidden'; - } else { - if (this.style.visibility != 'visible') this.style.visibility = 'visible'; - } - } - if (!this.currentStyle || !this.currentStyle.hasLayout) this.style.zoom = 1; - if (Browser.Engine.trident) this.style.filter = (opacity == 1) ? '' : 'alpha(opacity=' + opacity * 100 + ')'; - this.style.opacity = opacity; - this.store('opacity', opacity); - }, +function borderBox(element){ + return styleString(element, '-moz-box-sizing') == 'border-box'; +}; - get: function(){ - return this.retrieve('opacity', 1); - } +function topBorder(element){ + return styleNumber(element, 'border-top-width'); +}; +function leftBorder(element){ + return styleNumber(element, 'border-left-width'); }; -Element.implement({ +function isBody(element){ + return (/^(?:body|html)$/i).test(element.tagName); +}; - setOpacity: function(value){ - return this.set('opacity', value, true); - }, +function getCompatElement(element){ + var doc = element.getDocument(); + return (!doc.compatMode || doc.compatMode == 'CSS1Compat') ? doc.html : doc.body; +}; - getOpacity: function(){ - return this.get('opacity'); - }, +})(); - setStyle: function(property, value){ - switch (property){ - case 'opacity': return this.set('opacity', parseFloat(value)); - case 'float': property = (Browser.Engine.trident) ? 'styleFloat' : 'cssFloat'; - } - property = property.camelCase(); - if ($type(value) != 'string'){ - var map = (Element.Styles.get(property) || '@').split(' '); - value = $splat(value).map(function(val, i){ - if (!map[i]) return ''; - return ($type(val) == 'number') ? map[i].replace('@', Math.round(val)) : val; - }).join(' '); - } else if (value == String(Number(value))){ - value = Math.round(value); - } - this.style[property] = value; - return this; +//aliases +Element.alias('setPosition', 'position'); //compatability + +Native.implement([Window, Document, Element], { + + getHeight: function(){ + return this.getSize().y; }, - getStyle: function(property){ - switch (property){ - case 'opacity': return this.get('opacity'); - case 'float': property = (Browser.Engine.trident) ? 'styleFloat' : 'cssFloat'; - } - property = property.camelCase(); - var result = this.style[property]; - if (!$chk(result)){ - result = []; - for (var style in Element.ShortStyles){ - if (property != style) continue; - for (var s in Element.ShortStyles[style]) result.push(this.getStyle(s)); - return result.join(' '); - } - result = this.getComputedStyle(property); - } - if (result){ - result = String(result); - var color = result.match(/rgba?\([\d\s,]+\)/); - if (color) result = result.replace(color[0], color[0].rgbToHex()); - } - if (Browser.Engine.presto || (Browser.Engine.trident && !$chk(parseInt(result, 10)))){ - if (property.test(/^(height|width)$/)){ - var values = (property == 'width') ? ['left', 'right'] : ['top', 'bottom'], size = 0; - values.each(function(value){ - size += this.getStyle('border-' + value + '-width').toInt() + this.getStyle('padding-' + value).toInt(); - }, this); - return this['offset' + property.capitalize()] - size + 'px'; - } - if ((Browser.Engine.presto) && String(result).test('px')) return result; - if (property.test(/(border(.+)Width|margin|padding)/)) return '0px'; - } - return result; + getWidth: function(){ + return this.getSize().x; }, - setStyles: function(styles){ - for (var style in styles) this.setStyle(style, styles[style]); - return this; + getScrollTop: function(){ + return this.getScroll().y; }, - getStyles: function(){ - var result = {}; - Array.flatten(arguments).each(function(key){ - result[key] = this.getStyle(key); - }, this); - return result; - } + getScrollLeft: function(){ + return this.getScroll().x; + }, -}); + getScrollHeight: function(){ + return this.getScrollSize().y; + }, -Element.Styles = new Hash({ - left: '@px', top: '@px', bottom: '@px', right: '@px', - width: '@px', height: '@px', maxWidth: '@px', maxHeight: '@px', minWidth: '@px', minHeight: '@px', - backgroundColor: 'rgb(@, @, @)', backgroundPosition: '@px @px', color: 'rgb(@, @, @)', - fontSize: '@px', letterSpacing: '@px', lineHeight: '@px', clip: 'rect(@px @px @px @px)', - margin: '@px @px @px @px', padding: '@px @px @px @px', border: '@px @ rgb(@, @, @) @px @ rgb(@, @, @) @px @ rgb(@, @, @)', - borderWidth: '@px @px @px @px', borderStyle: '@ @ @ @', borderColor: 'rgb(@, @, @) rgb(@, @, @) rgb(@, @, @) rgb(@, @, @)', - zIndex: '@', 'zoom': '@', fontWeight: '@', textIndent: '@px', opacity: '@' -}); + getScrollWidth: function(){ + return this.getScrollSize().x; + }, -Element.ShortStyles = {margin: {}, padding: {}, border: {}, borderWidth: {}, borderStyle: {}, borderColor: {}}; + getTop: function(){ + return this.getPosition().y; + }, + + getLeft: function(){ + return this.getPosition().x; + } -['Top', 'Right', 'Bottom', 'Left'].each(function(direction){ - var Short = Element.ShortStyles; - var All = Element.Styles; - ['margin', 'padding'].each(function(style){ - var sd = style + direction; - Short[style][sd] = All[sd] = '@px'; - }); - var bd = 'border' + direction; - Short.border[bd] = All[bd] = '@px @ rgb(@, @, @)'; - var bdw = bd + 'Width', bds = bd + 'Style', bdc = bd + 'Color'; - Short[bd] = {}; - Short.borderWidth[bdw] = Short[bd][bdw] = All[bdw] = '@px'; - Short.borderStyle[bds] = Short[bd][bds] = All[bds] = '@'; - Short.borderColor[bdc] = Short[bd][bdc] = All[bdc] = 'rgb(@, @, @)'; }); /* --- -script: Element.Dimensions.js +name: Event -description: Contains methods to work with size, scroll, or positioning of Elements and the window object. +description: Contains the Event Class, to make the event object cross-browser. license: MIT-style license. -credits: -- Element positioning based on the [qooxdoo](http://qooxdoo.org/) code and smart browser fixes, [LGPL License](http://www.gnu.org/licenses/lgpl.html). -- Viewport dimensions based on [YUI](http://developer.yahoo.com/yui/) code, [BSD License](http://developer.yahoo.com/yui/license.html). - -requires: -- /Element +requires: [Window, Document, Hash, Array, Function, String] -provides: [Element.Dimensions] +provides: Event ... */ -(function(){ +var Event = new Native({ -Element.implement({ + name: 'Event', - scrollTo: function(x, y){ - if (isBody(this)){ - this.getWindow().scrollTo(x, y); - } else { - this.scrollLeft = x; - this.scrollTop = y; + initialize: function(event, win){ + win = win || window; + var doc = win.document; + event = event || win.event; + if (event.$extended) return event; + this.$extended = true; + var type = event.type; + var target = event.target || event.srcElement; + while (target && target.nodeType == 3) target = target.parentNode; + + if (type.test(/key/)){ + var code = event.which || event.keyCode; + var key = Event.Keys.keyOf(code); + if (type == 'keydown'){ + var fKey = code - 111; + if (fKey > 0 && fKey < 13) key = 'f' + fKey; + } + key = key || String.fromCharCode(code).toLowerCase(); + } else if (type.match(/(click|mouse|menu)/i)){ + doc = (!doc.compatMode || doc.compatMode == 'CSS1Compat') ? doc.html : doc.body; + var page = { + x: event.pageX || event.clientX + doc.scrollLeft, + y: event.pageY || event.clientY + doc.scrollTop + }; + var client = { + x: (event.pageX) ? event.pageX - win.pageXOffset : event.clientX, + y: (event.pageY) ? event.pageY - win.pageYOffset : event.clientY + }; + if (type.match(/DOMMouseScroll|mousewheel/)){ + var wheel = (event.wheelDelta) ? event.wheelDelta / 120 : -(event.detail || 0) / 3; + } + var rightClick = (event.which == 3) || (event.button == 2); + var related = null; + if (type.match(/over|out/)){ + switch (type){ + case 'mouseover': related = event.relatedTarget || event.fromElement; break; + case 'mouseout': related = event.relatedTarget || event.toElement; + } + if (!(function(){ + while (related && related.nodeType == 3) related = related.parentNode; + return true; + }).create({attempt: Browser.Engine.gecko})()) related = false; + } } - return this; - }, - getSize: function(){ - if (isBody(this)) return this.getWindow().getSize(); - return {x: this.offsetWidth, y: this.offsetHeight}; - }, + return $extend(this, { + event: event, + type: type, - getScrollSize: function(){ - if (isBody(this)) return this.getWindow().getScrollSize(); - return {x: this.scrollWidth, y: this.scrollHeight}; - }, + page: page, + client: client, + rightClick: rightClick, - getScroll: function(){ - if (isBody(this)) return this.getWindow().getScroll(); - return {x: this.scrollLeft, y: this.scrollTop}; - }, + wheel: wheel, - getScrolls: function(){ - var element = this, position = {x: 0, y: 0}; - while (element && !isBody(element)){ - position.x += element.scrollLeft; - position.y += element.scrollTop; - element = element.parentNode; - } - return position; - }, + relatedTarget: related, + target: target, - getOffsetParent: function(){ - var element = this; - if (isBody(element)) return null; - if (!Browser.Engine.trident) return element.offsetParent; - while ((element = element.parentNode) && !isBody(element)){ - if (styleString(element, 'position') != 'static') return element; - } - return null; - }, + code: code, + key: key, - getOffsets: function(){ - if (this.getBoundingClientRect){ - var bound = this.getBoundingClientRect(), - html = document.id(this.getDocument().documentElement), - htmlScroll = html.getScroll(), - elemScrolls = this.getScrolls(), - elemScroll = this.getScroll(), - isFixed = (styleString(this, 'position') == 'fixed'); + shift: event.shiftKey, + control: event.ctrlKey, + alt: event.altKey, + meta: event.metaKey + }); + } - return { - x: bound.left.toInt() + elemScrolls.x - elemScroll.x + ((isFixed) ? 0 : htmlScroll.x) - html.clientLeft, - y: bound.top.toInt() + elemScrolls.y - elemScroll.y + ((isFixed) ? 0 : htmlScroll.y) - html.clientTop - }; - } +}); - var element = this, position = {x: 0, y: 0}; - if (isBody(this)) return position; +Event.Keys = new Hash({ + 'enter': 13, + 'up': 38, + 'down': 40, + 'left': 37, + 'right': 39, + 'esc': 27, + 'space': 32, + 'backspace': 8, + 'tab': 9, + 'delete': 46 +}); - while (element && !isBody(element)){ - position.x += element.offsetLeft; - position.y += element.offsetTop; +Event.implement({ - if (Browser.Engine.gecko){ - if (!borderBox(element)){ - position.x += leftBorder(element); - position.y += topBorder(element); - } - var parent = element.parentNode; - if (parent && styleString(parent, 'overflow') != 'visible'){ - position.x += leftBorder(parent); - position.y += topBorder(parent); - } - } else if (element != this && Browser.Engine.webkit){ - position.x += leftBorder(element); - position.y += topBorder(element); - } - - element = element.offsetParent; - } - if (Browser.Engine.gecko && !borderBox(this)){ - position.x -= leftBorder(this); - position.y -= topBorder(this); - } - return position; - }, - - getPosition: function(relative){ - if (isBody(this)) return {x: 0, y: 0}; - var offset = this.getOffsets(), - scroll = this.getScrolls(); - var position = { - x: offset.x - scroll.x, - y: offset.y - scroll.y - }; - var relativePosition = (relative && (relative = document.id(relative))) ? relative.getPosition() : {x: 0, y: 0}; - return {x: position.x - relativePosition.x, y: position.y - relativePosition.y}; - }, - - getCoordinates: function(element){ - if (isBody(this)) return this.getWindow().getCoordinates(); - var position = this.getPosition(element), - size = this.getSize(); - var obj = { - left: position.x, - top: position.y, - width: size.x, - height: size.y - }; - obj.right = obj.left + obj.width; - obj.bottom = obj.top + obj.height; - return obj; + stop: function(){ + return this.stopPropagation().preventDefault(); }, - computePosition: function(obj){ - return { - left: obj.x - styleNumber(this, 'margin-left'), - top: obj.y - styleNumber(this, 'margin-top') - }; + stopPropagation: function(){ + if (this.event.stopPropagation) this.event.stopPropagation(); + else this.event.cancelBubble = true; + return this; }, - setPosition: function(obj){ - return this.setStyles(this.computePosition(obj)); + preventDefault: function(){ + if (this.event.preventDefault) this.event.preventDefault(); + else this.event.returnValue = false; + return this; } }); -Native.implement([Document, Window], { - - getSize: function(){ - if (Browser.Engine.presto || Browser.Engine.webkit){ - var win = this.getWindow(); - return {x: win.innerWidth, y: win.innerHeight}; - } - var doc = getCompatElement(this); - return {x: doc.clientWidth, y: doc.clientHeight}; - }, - - getScroll: function(){ - var win = this.getWindow(), doc = getCompatElement(this); - return {x: win.pageXOffset || doc.scrollLeft, y: win.pageYOffset || doc.scrollTop}; - }, +/* +--- - getScrollSize: function(){ - var doc = getCompatElement(this), min = this.getSize(); - return {x: Math.max(doc.scrollWidth, min.x), y: Math.max(doc.scrollHeight, min.y)}; - }, +name: Element.Event - getPosition: function(){ - return {x: 0, y: 0}; - }, +description: Contains Element methods for dealing with events. This file also includes mouseenter and mouseleave custom Element Events. - getCoordinates: function(){ - var size = this.getSize(); - return {top: 0, left: 0, bottom: size.y, right: size.x, height: size.y, width: size.x}; - } +license: MIT-style license. -}); +requires: [Element, Event] -// private methods +provides: Element.Event -var styleString = Element.getComputedStyle; +... +*/ -function styleNumber(element, style){ - return styleString(element, style).toInt() || 0; -}; +Element.Properties.events = {set: function(events){ + this.addEvents(events); +}}; -function borderBox(element){ - return styleString(element, '-moz-box-sizing') == 'border-box'; -}; +Native.implement([Element, Window, Document], { -function topBorder(element){ - return styleNumber(element, 'border-top-width'); -}; + addEvent: function(type, fn){ + var events = this.retrieve('events', {}); + events[type] = events[type] || {'keys': [], 'values': []}; + if (events[type].keys.contains(fn)) return this; + events[type].keys.push(fn); + var realType = type, custom = Element.Events.get(type), condition = fn, self = this; + if (custom){ + if (custom.onAdd) custom.onAdd.call(this, fn); + if (custom.condition){ + condition = function(event){ + if (custom.condition.call(this, event)) return fn.call(this, event); + return true; + }; + } + realType = custom.base || realType; + } + var defn = function(){ + return fn.call(self); + }; + var nativeEvent = Element.NativeEvents[realType]; + if (nativeEvent){ + if (nativeEvent == 2){ + defn = function(event){ + event = new Event(event, self.getWindow()); + if (condition.call(self, event) === false) event.stop(); + }; + } + this.addListener(realType, defn); + } + events[type].values.push(defn); + return this; + }, -function leftBorder(element){ - return styleNumber(element, 'border-left-width'); -}; + removeEvent: function(type, fn){ + var events = this.retrieve('events'); + if (!events || !events[type]) return this; + var pos = events[type].keys.indexOf(fn); + if (pos == -1) return this; + events[type].keys.splice(pos, 1); + var value = events[type].values.splice(pos, 1)[0]; + var custom = Element.Events.get(type); + if (custom){ + if (custom.onRemove) custom.onRemove.call(this, fn); + type = custom.base || type; + } + return (Element.NativeEvents[type]) ? this.removeListener(type, value) : this; + }, -function isBody(element){ - return (/^(?:body|html)$/i).test(element.tagName); -}; + addEvents: function(events){ + for (var event in events) this.addEvent(event, events[event]); + return this; + }, -function getCompatElement(element){ - var doc = element.getDocument(); - return (!doc.compatMode || doc.compatMode == 'CSS1Compat') ? doc.html : doc.body; -}; + removeEvents: function(events){ + var type; + if ($type(events) == 'object'){ + for (type in events) this.removeEvent(type, events[type]); + return this; + } + var attached = this.retrieve('events'); + if (!attached) return this; + if (!events){ + for (type in attached) this.removeEvents(type); + this.eliminate('events'); + } else if (attached[events]){ + while (attached[events].keys[0]) this.removeEvent(events, attached[events].keys[0]); + attached[events] = null; + } + return this; + }, -})(); + fireEvent: function(type, args, delay){ + var events = this.retrieve('events'); + if (!events || !events[type]) return this; + events[type].keys.each(function(fn){ + fn.create({'bind': this, 'delay': delay, 'arguments': args})(); + }, this); + return this; + }, -//aliases -Element.alias('setPosition', 'position'); //compatability + cloneEvents: function(from, type){ + from = document.id(from); + var fevents = from.retrieve('events'); + if (!fevents) return this; + if (!type){ + for (var evType in fevents) this.cloneEvents(from, evType); + } else if (fevents[type]){ + fevents[type].keys.each(function(fn){ + this.addEvent(type, fn); + }, this); + } + return this; + } -Native.implement([Window, Document, Element], { +}); - getHeight: function(){ - return this.getSize().y; - }, +// IE9 +try { + if (typeof HTMLElement != 'undefined') + HTMLElement.prototype.fireEvent = Element.prototype.fireEvent; +} catch(e){} - getWidth: function(){ - return this.getSize().x; - }, +Element.NativeEvents = { + click: 2, dblclick: 2, mouseup: 2, mousedown: 2, contextmenu: 2, //mouse buttons + mousewheel: 2, DOMMouseScroll: 2, //mouse wheel + mouseover: 2, mouseout: 2, mousemove: 2, selectstart: 2, selectend: 2, //mouse movement + keydown: 2, keypress: 2, keyup: 2, //keyboard + focus: 2, blur: 2, change: 2, reset: 2, select: 2, submit: 2, //form elements + load: 1, unload: 1, beforeunload: 2, resize: 1, move: 1, DOMContentLoaded: 1, readystatechange: 1, //window + error: 1, abort: 1, scroll: 1 //misc +}; - getScrollTop: function(){ - return this.getScroll().y; - }, +(function(){ - getScrollLeft: function(){ - return this.getScroll().x; - }, +var $check = function(event){ + var related = event.relatedTarget; + if (related == undefined) return true; + if (related === false) return false; + return ($type(this) != 'document' && related != this && related.prefix != 'xul' && !this.hasChild(related)); +}; - getScrollHeight: function(){ - return this.getScrollSize().y; - }, +Element.Events = new Hash({ - getScrollWidth: function(){ - return this.getScrollSize().x; + mouseenter: { + base: 'mouseover', + condition: $check }, - getTop: function(){ - return this.getPosition().y; + mouseleave: { + base: 'mouseout', + condition: $check }, - getLeft: function(){ - return this.getPosition().x; + mousewheel: { + base: (Browser.Engine.gecko) ? 'DOMMouseScroll' : 'mousewheel' } }); +})(); + /* --- -script: Selectors.js +name: Element.Style -description: Adds advanced CSS-style querying capabilities for targeting HTML Elements. Includes pseudo selectors. +description: Contains methods for interacting with the styles of Elements in a fashionable way. license: MIT-style license. -requires: -- /Element +requires: Element -provides: [Selectors] +provides: Element.Style ... */ -Native.implement([Document, Element], { - - getElements: function(expression, nocash){ - expression = expression.split(','); - var items, local = {}; - for (var i = 0, l = expression.length; i < l; i++){ - var selector = expression[i], elements = Selectors.Utils.search(this, selector, local); - if (i != 0 && elements.item) elements = $A(elements); - items = (i == 0) ? elements : (items.item) ? $A(items).concat(elements) : items.concat(elements); - } - return new Elements(items, {ddup: (expression.length > 1), cash: !nocash}); - } +Element.Properties.styles = {set: function(styles){ + this.setStyles(styles); +}}; -}); +Element.Properties.opacity = { -Element.implement({ + set: function(opacity, novisibility){ + if (!novisibility){ + if (opacity == 0){ + if (this.style.visibility != 'hidden') this.style.visibility = 'hidden'; + } else { + if (this.style.visibility != 'visible') this.style.visibility = 'visible'; + } + } + if (!this.currentStyle || !this.currentStyle.hasLayout) this.style.zoom = 1; + if (Browser.Engine.trident) this.style.filter = (opacity == 1) ? '' : 'alpha(opacity=' + opacity * 100 + ')'; + this.style.opacity = opacity; + this.store('opacity', opacity); + }, - match: function(selector){ - if (!selector || (selector == this)) return true; - var tagid = Selectors.Utils.parseTagAndID(selector); - var tag = tagid[0], id = tagid[1]; - if (!Selectors.Filters.byID(this, id) || !Selectors.Filters.byTag(this, tag)) return false; - var parsed = Selectors.Utils.parseSelector(selector); - return (parsed) ? Selectors.Utils.filter(this, parsed, {}) : true; + get: function(){ + return this.retrieve('opacity', 1); } -}); - -var Selectors = {Cache: {nth: {}, parsed: {}}}; - -Selectors.RegExps = { - id: (/#([\w-]+)/), - tag: (/^(\w+|\*)/), - quick: (/^(\w+|\*)$/), - splitter: (/\s*([+>~\s])\s*([a-zA-Z#.*:\[])/g), - combined: (/\.([\w-]+)|\[(\w+)(?:([!*^$~|]?=)(["']?)([^\4]*?)\4)?\]|:([\w-]+)(?:\(["']?(.*?)?["']?\)|$)/g) }; -Selectors.Utils = { +Element.implement({ - chk: function(item, uniques){ - if (!uniques) return true; - var uid = $uid(item); - if (!uniques[uid]) return uniques[uid] = true; - return false; + setOpacity: function(value){ + return this.set('opacity', value, true); }, - parseNthArgument: function(argument){ - if (Selectors.Cache.nth[argument]) return Selectors.Cache.nth[argument]; - var parsed = argument.match(/^([+-]?\d*)?([a-z]+)?([+-]?\d*)?$/); - if (!parsed) return false; - var inta = parseInt(parsed[1], 10); - var a = (inta || inta === 0) ? inta : 1; - var special = parsed[2] || false; - var b = parseInt(parsed[3], 10) || 0; - if (a != 0){ - b--; - while (b < 1) b += a; - while (b >= a) b -= a; - } else { - a = b; - special = 'index'; - } - switch (special){ - case 'n': parsed = {a: a, b: b, special: 'n'}; break; - case 'odd': parsed = {a: 2, b: 0, special: 'n'}; break; - case 'even': parsed = {a: 2, b: 1, special: 'n'}; break; - case 'first': parsed = {a: 0, special: 'index'}; break; - case 'last': parsed = {special: 'last-child'}; break; - case 'only': parsed = {special: 'only-child'}; break; - default: parsed = {a: (a - 1), special: 'index'}; - } - - return Selectors.Cache.nth[argument] = parsed; + getOpacity: function(){ + return this.get('opacity'); }, - parseSelector: function(selector){ - if (Selectors.Cache.parsed[selector]) return Selectors.Cache.parsed[selector]; - var m, parsed = {classes: [], pseudos: [], attributes: []}; - while ((m = Selectors.RegExps.combined.exec(selector))){ - var cn = m[1], an = m[2], ao = m[3], av = m[5], pn = m[6], pa = m[7]; - if (cn){ - parsed.classes.push(cn); - } else if (pn){ - var parser = Selectors.Pseudo.get(pn); - if (parser) parsed.pseudos.push({parser: parser, argument: pa}); - else parsed.attributes.push({name: pn, operator: '=', value: pa}); - } else if (an){ - parsed.attributes.push({name: an, operator: ao, value: av}); - } + setStyle: function(property, value){ + switch (property){ + case 'opacity': return this.set('opacity', parseFloat(value)); + case 'float': property = (Browser.Engine.trident) ? 'styleFloat' : 'cssFloat'; } - if (!parsed.classes.length) delete parsed.classes; - if (!parsed.attributes.length) delete parsed.attributes; - if (!parsed.pseudos.length) delete parsed.pseudos; - if (!parsed.classes && !parsed.attributes && !parsed.pseudos) parsed = null; - return Selectors.Cache.parsed[selector] = parsed; - }, - - parseTagAndID: function(selector){ - var tag = selector.match(Selectors.RegExps.tag); - var id = selector.match(Selectors.RegExps.id); - return [(tag) ? tag[1] : '*', (id) ? id[1] : false]; + property = property.camelCase(); + if ($type(value) != 'string'){ + var map = (Element.Styles.get(property) || '@').split(' '); + value = $splat(value).map(function(val, i){ + if (!map[i]) return ''; + return ($type(val) == 'number') ? map[i].replace('@', Math.round(val)) : val; + }).join(' '); + } else if (value == String(Number(value))){ + value = Math.round(value); + } + this.style[property] = value; + return this; }, - filter: function(item, parsed, local){ - var i; - if (parsed.classes){ - for (i = parsed.classes.length; i--; i){ - var cn = parsed.classes[i]; - if (!Selectors.Filters.byClass(item, cn)) return false; - } + getStyle: function(property){ + switch (property){ + case 'opacity': return this.get('opacity'); + case 'float': property = (Browser.Engine.trident) ? 'styleFloat' : 'cssFloat'; } - if (parsed.attributes){ - for (i = parsed.attributes.length; i--; i){ - var att = parsed.attributes[i]; - if (!Selectors.Filters.byAttribute(item, att.name, att.operator, att.value)) return false; + property = property.camelCase(); + var result = this.style[property]; + if (!$chk(result)){ + result = []; + for (var style in Element.ShortStyles){ + if (property != style) continue; + for (var s in Element.ShortStyles[style]) result.push(this.getStyle(s)); + return result.join(' '); } + result = this.getComputedStyle(property); } - if (parsed.pseudos){ - for (i = parsed.pseudos.length; i--; i){ - var psd = parsed.pseudos[i]; - if (!Selectors.Filters.byPseudo(item, psd.parser, psd.argument, local)) return false; + if (result){ + result = String(result); + var color = result.match(/rgba?\([\d\s,]+\)/); + if (color) result = result.replace(color[0], color[0].rgbToHex()); + } + if (Browser.Engine.presto || (Browser.Engine.trident && !$chk(parseInt(result, 10)))){ + if (property.test(/^(height|width)$/)){ + var values = (property == 'width') ? ['left', 'right'] : ['top', 'bottom'], size = 0; + values.each(function(value){ + size += this.getStyle('border-' + value + '-width').toInt() + this.getStyle('padding-' + value).toInt(); + }, this); + return this['offset' + property.capitalize()] - size + 'px'; } + if ((Browser.Engine.presto) && String(result).test('px')) return result; + if (property.test(/(border(.+)Width|margin|padding)/)) return '0px'; } - return true; + return result; }, - getByTagAndID: function(ctx, tag, id){ - if (id){ - var item = (ctx.getElementById) ? ctx.getElementById(id, true) : Element.getElementById(ctx, id, true); - return (item && Selectors.Filters.byTag(item, tag)) ? [item] : []; - } else { - return ctx.getElementsByTagName(tag); - } + setStyles: function(styles){ + for (var style in styles) this.setStyle(style, styles[style]); + return this; }, - search: function(self, expression, local){ - var splitters = []; + getStyles: function(){ + var result = {}; + Array.flatten(arguments).each(function(key){ + result[key] = this.getStyle(key); + }, this); + return result; + } - var selectors = expression.trim().replace(Selectors.RegExps.splitter, function(m0, m1, m2){ - splitters.push(m1); - return ':)' + m2; - }).split(':)'); +}); - var items, filtered, item; +Element.Styles = new Hash({ + left: '@px', top: '@px', bottom: '@px', right: '@px', + width: '@px', height: '@px', maxWidth: '@px', maxHeight: '@px', minWidth: '@px', minHeight: '@px', + backgroundColor: 'rgb(@, @, @)', backgroundPosition: '@px @px', color: 'rgb(@, @, @)', + fontSize: '@px', letterSpacing: '@px', lineHeight: '@px', clip: 'rect(@px @px @px @px)', + margin: '@px @px @px @px', padding: '@px @px @px @px', border: '@px @ rgb(@, @, @) @px @ rgb(@, @, @) @px @ rgb(@, @, @)', + borderWidth: '@px @px @px @px', borderStyle: '@ @ @ @', borderColor: 'rgb(@, @, @) rgb(@, @, @) rgb(@, @, @) rgb(@, @, @)', + zIndex: '@', 'zoom': '@', fontWeight: '@', textIndent: '@px', opacity: '@' +}); - for (var i = 0, l = selectors.length; i < l; i++){ +Element.ShortStyles = {margin: {}, padding: {}, border: {}, borderWidth: {}, borderStyle: {}, borderColor: {}}; - var selector = selectors[i]; +['Top', 'Right', 'Bottom', 'Left'].each(function(direction){ + var Short = Element.ShortStyles; + var All = Element.Styles; + ['margin', 'padding'].each(function(style){ + var sd = style + direction; + Short[style][sd] = All[sd] = '@px'; + }); + var bd = 'border' + direction; + Short.border[bd] = All[bd] = '@px @ rgb(@, @, @)'; + var bdw = bd + 'Width', bds = bd + 'Style', bdc = bd + 'Color'; + Short[bd] = {}; + Short.borderWidth[bdw] = Short[bd][bdw] = All[bdw] = '@px'; + Short.borderStyle[bds] = Short[bd][bds] = All[bds] = '@'; + Short.borderColor[bdc] = Short[bd][bdc] = All[bdc] = 'rgb(@, @, @)'; +}); - if (i == 0 && Selectors.RegExps.quick.test(selector)){ - items = self.getElementsByTagName(selector); - continue; - } - var splitter = splitters[i - 1]; +/* +--- - var tagid = Selectors.Utils.parseTagAndID(selector); - var tag = tagid[0], id = tagid[1]; +name: Fx - if (i == 0){ - items = Selectors.Utils.getByTagAndID(self, tag, id); - } else { - var uniques = {}, found = []; - for (var j = 0, k = items.length; j < k; j++) found = Selectors.Getters[splitter](found, items[j], tag, id, uniques); - items = found; - } +description: Contains the basic animation logic to be extended by all other Fx Classes. - var parsed = Selectors.Utils.parseSelector(selector); +license: MIT-style license. - if (parsed){ - filtered = []; - for (var m = 0, n = items.length; m < n; m++){ - item = items[m]; - if (Selectors.Utils.filter(item, parsed, local)) filtered.push(item); - } - items = filtered; - } +requires: [Chain, Events, Options] + +provides: Fx + +... +*/ + +var Fx = new Class({ + + Implements: [Chain, Events, Options], + + options: { + /* + onStart: $empty, + onCancel: $empty, + onComplete: $empty, + */ + fps: 50, + unit: false, + duration: 500, + link: 'ignore' + }, + + initialize: function(options){ + this.subject = this.subject || this; + this.setOptions(options); + this.options.duration = Fx.Durations[this.options.duration] || this.options.duration.toInt(); + var wait = this.options.wait; + if (wait === false) this.options.link = 'cancel'; + }, + + getTransition: function(){ + return function(p){ + return -(Math.cos(Math.PI * p) - 1) / 2; + }; + }, + step: function(){ + var time = $time(); + if (time < this.time + this.options.duration){ + var delta = this.transition((time - this.time) / this.options.duration); + this.set(this.compute(this.from, this.to, delta)); + } else { + this.set(this.compute(this.from, this.to, 1)); + this.complete(); } + }, - return items; - - } - -}; + set: function(now){ + return now; + }, -Selectors.Getters = { + compute: function(from, to, delta){ + return Fx.compute(from, to, delta); + }, - ' ': function(found, self, tag, id, uniques){ - var items = Selectors.Utils.getByTagAndID(self, tag, id); - for (var i = 0, l = items.length; i < l; i++){ - var item = items[i]; - if (Selectors.Utils.chk(item, uniques)) found.push(item); + check: function(){ + if (!this.timer) return true; + switch (this.options.link){ + case 'cancel': this.cancel(); return true; + case 'chain': this.chain(this.caller.bind(this, arguments)); return false; } - return found; + return false; }, - '>': function(found, self, tag, id, uniques){ - var children = Selectors.Utils.getByTagAndID(self, tag, id); - for (var i = 0, l = children.length; i < l; i++){ - var child = children[i]; - if (child.parentNode == self && Selectors.Utils.chk(child, uniques)) found.push(child); - } - return found; + start: function(from, to){ + if (!this.check(from, to)) return this; + this.from = from; + this.to = to; + this.time = 0; + this.transition = this.getTransition(); + this.startTimer(); + this.onStart(); + return this; }, - '+': function(found, self, tag, id, uniques){ - while ((self = self.nextSibling)){ - if (self.nodeType == 1){ - if (Selectors.Utils.chk(self, uniques) && Selectors.Filters.byTag(self, tag) && Selectors.Filters.byID(self, id)) found.push(self); - break; - } - } - return found; + complete: function(){ + if (this.stopTimer()) this.onComplete(); + return this; }, - '~': function(found, self, tag, id, uniques){ - while ((self = self.nextSibling)){ - if (self.nodeType == 1){ - if (!Selectors.Utils.chk(self, uniques)) break; - if (Selectors.Filters.byTag(self, tag) && Selectors.Filters.byID(self, id)) found.push(self); - } - } - return found; - } + cancel: function(){ + if (this.stopTimer()) this.onCancel(); + return this; + }, -}; + onStart: function(){ + this.fireEvent('start', this.subject); + }, -Selectors.Filters = { + onComplete: function(){ + this.fireEvent('complete', this.subject); + if (!this.callChain()) this.fireEvent('chainComplete', this.subject); + }, - byTag: function(self, tag){ - return (tag == '*' || (self.tagName && self.tagName.toLowerCase() == tag)); + onCancel: function(){ + this.fireEvent('cancel', this.subject).clearChain(); }, - byID: function(self, id){ - return (!id || (self.id && self.id == id)); + pause: function(){ + this.stopTimer(); + return this; }, - byClass: function(self, klass){ - return (self.className && self.className.contains && self.className.contains(klass, ' ')); + resume: function(){ + this.startTimer(); + return this; }, - byPseudo: function(self, parser, argument, local){ - return parser.call(self, argument, local); + stopTimer: function(){ + if (!this.timer) return false; + this.time = $time() - this.time; + this.timer = $clear(this.timer); + return true; }, - byAttribute: function(self, name, operator, value){ - var result = Element.prototype.getProperty.call(self, name); - if (!result) return (operator == '!='); - if (!operator || value == undefined) return true; - switch (operator){ - case '=': return (result == value); - case '*=': return (result.contains(value)); - case '^=': return (result.substr(0, value.length) == value); - case '$=': return (result.substr(result.length - value.length) == value); - case '!=': return (result != value); - case '~=': return result.contains(value, ' '); - case '|=': return result.contains(value, '-'); - } - return false; + startTimer: function(){ + if (this.timer) return false; + this.time = $time() - this.time; + this.timer = this.step.periodical(Math.round(1000 / this.options.fps), this); + return true; } +}); + +Fx.compute = function(from, to, delta){ + return (to - from) * delta + from; }; -Selectors.Pseudo = new Hash({ +Fx.Durations = {'short': 250, 'normal': 500, 'long': 1000}; - // w3c pseudo selectors - checked: function(){ - return this.checked; - }, - - empty: function(){ - return !(this.innerText || this.textContent || '').length; - }, +/* +--- - not: function(selector){ - return !Element.match(this, selector); - }, +name: Fx.CSS - contains: function(text){ - return (this.innerText || this.textContent || '').contains(text); +description: Contains the CSS animation logic. Used by Fx.Tween, Fx.Morph, Fx.Elements. + +license: MIT-style license. + +requires: [Fx, Element.Style] + +provides: Fx.CSS + +... +*/ + +Fx.CSS = new Class({ + + Extends: Fx, + + //prepares the base from/to object + + prepare: function(element, property, values){ + values = $splat(values); + var values1 = values[1]; + if (!$chk(values1)){ + values[1] = values[0]; + values[0] = element.getStyle(property); + } + var parsed = values.map(this.parse); + return {from: parsed[0], to: parsed[1]}; }, - 'first-child': function(){ - return Selectors.Pseudo.index.call(this, 0); + //parses a value into an array + + parse: function(value){ + value = $lambda(value)(); + value = (typeof value == 'string') ? value.split(' ') : $splat(value); + return value.map(function(val){ + val = String(val); + var found = false; + Fx.CSS.Parsers.each(function(parser, key){ + if (found) return; + var parsed = parser.parse(val); + if ($chk(parsed)) found = {value: parsed, parser: parser}; + }); + found = found || {value: val, parser: Fx.CSS.Parsers.String}; + return found; + }); }, - 'last-child': function(){ - var element = this; - while ((element = element.nextSibling)){ - if (element.nodeType == 1) return false; - } - return true; + //computes by a from and to prepared objects, using their parsers. + + compute: function(from, to, delta){ + var computed = []; + (Math.min(from.length, to.length)).times(function(i){ + computed.push({value: from[i].parser.compute(from[i].value, to[i].value, delta), parser: from[i].parser}); + }); + computed.$family = {name: 'fx:css:value'}; + return computed; }, - 'only-child': function(){ - var prev = this; - while ((prev = prev.previousSibling)){ - if (prev.nodeType == 1) return false; - } - var next = this; - while ((next = next.nextSibling)){ - if (next.nodeType == 1) return false; - } - return true; + //serves the value as settable + + serve: function(value, unit){ + if ($type(value) != 'fx:css:value') value = this.parse(value); + var returned = []; + value.each(function(bit){ + returned = returned.concat(bit.parser.serve(bit.value, unit)); + }); + return returned; }, - 'nth-child': function(argument, local){ - argument = (argument == undefined) ? 'n' : argument; - var parsed = Selectors.Utils.parseNthArgument(argument); - if (parsed.special != 'n') return Selectors.Pseudo[parsed.special].call(this, parsed.a, local); - var count = 0; - local.positions = local.positions || {}; - var uid = $uid(this); - if (!local.positions[uid]){ - var self = this; - while ((self = self.previousSibling)){ - if (self.nodeType != 1) continue; - count ++; - var position = local.positions[$uid(self)]; - if (position != undefined){ - count = position + count; - break; - } - } - local.positions[uid] = count; - } - return (local.positions[uid] % parsed.a == parsed.b); + //renders the change to an element + + render: function(element, property, value, unit){ + element.setStyle(property, this.serve(value, unit)); }, - // custom pseudo selectors + //searches inside the page css to find the values for a selector + + search: function(selector){ + if (Fx.CSS.Cache[selector]) return Fx.CSS.Cache[selector]; + var to = {}; + Array.each(document.styleSheets, function(sheet, j){ + var href = sheet.href; + if (href && href.contains('://') && !href.contains(document.domain)) return; + var rules = sheet.rules || sheet.cssRules; + Array.each(rules, function(rule, i){ + if (!rule.style) return; + var selectorText = (rule.selectorText) ? rule.selectorText.replace(/^\w+/, function(m){ + return m.toLowerCase(); + }) : null; + if (!selectorText || !selectorText.test('^' + selector + '$')) return; + Element.Styles.each(function(value, style){ + if (!rule.style[style] || Element.ShortStyles[style]) return; + value = String(rule.style[style]); + to[style] = (value.test(/^rgb/)) ? value.rgbToHex() : value; + }); + }); + }); + return Fx.CSS.Cache[selector] = to; + } + +}); - index: function(index){ - var element = this, count = 0; - while ((element = element.previousSibling)){ - if (element.nodeType == 1 && ++count > index) return false; +Fx.CSS.Cache = {}; + +Fx.CSS.Parsers = new Hash({ + + Color: { + parse: function(value){ + if (value.match(/^#[0-9a-f]{3,6}$/i)) return value.hexToRgb(true); + return ((value = value.match(/(\d+),\s*(\d+),\s*(\d+)/))) ? [value[1], value[2], value[3]] : false; + }, + compute: function(from, to, delta){ + return from.map(function(value, i){ + return Math.round(Fx.compute(from[i], to[i], delta)); + }); + }, + serve: function(value){ + return value.map(Number); } - return (count == index); }, - even: function(argument, local){ - return Selectors.Pseudo['nth-child'].call(this, '2n+1', local); + Number: { + parse: parseFloat, + compute: Fx.compute, + serve: function(value, unit){ + return (unit) ? value + unit : value; + } }, - odd: function(argument, local){ - return Selectors.Pseudo['nth-child'].call(this, '2n', local); - }, - - selected: function(){ - return this.selected; - }, - - enabled: function(){ - return (this.disabled === false); + String: { + parse: $lambda(false), + compute: $arguments(1), + serve: $arguments(0) } }); @@ -3056,599 +2956,522 @@ Selectors.Pseudo = new Hash({ /* --- -script: DomReady.js +name: Fx.Morph -description: Contains the custom event domready. +description: Formerly Fx.Styles, effect to transition any number of CSS properties for an element using an object of rules, or CSS based selector rules. license: MIT-style license. -requires: -- /Element.Event +requires: Fx.CSS -provides: [DomReady] +provides: Fx.Morph ... */ -Element.Events.domready = { +Fx.Morph = new Class({ - onAdd: function(fn){ - if (Browser.loaded) fn.call(this); + Extends: Fx.CSS, + + initialize: function(element, options){ + this.element = this.subject = document.id(element); + this.parent(options); + }, + + set: function(now){ + if (typeof now == 'string') now = this.search(now); + for (var p in now) this.render(this.element, p, now[p], this.options.unit); + return this; + }, + + compute: function(from, to, delta){ + var now = {}; + for (var p in from) now[p] = this.parent(from[p], to[p], delta); + return now; + }, + + start: function(properties){ + if (!this.check(properties)) return this; + if (typeof properties == 'string') properties = this.search(properties); + var from = {}, to = {}; + for (var p in properties){ + var parsed = this.prepare(this.element, p, properties[p]); + from[p] = parsed.from; + to[p] = parsed.to; + } + return this.parent(from, to); } -}; +}); -(function(){ +Element.Properties.morph = { - var domready = function(){ - if (Browser.loaded) return; - Browser.loaded = true; - window.fireEvent('domready'); - document.fireEvent('domready'); - }; - - window.addEvent('load', domready); + set: function(options){ + var morph = this.retrieve('morph'); + if (morph) morph.cancel(); + return this.eliminate('morph').store('morph:options', $extend({link: 'cancel'}, options)); + }, - if (Browser.Engine.trident){ - var temp = document.createElement('div'); - (function(){ - ($try(function(){ - temp.doScroll(); // Technique by Diego Perini - return document.id(temp).inject(document.body).set('html', 'temp').dispose(); - })) ? domready() : arguments.callee.delay(50); - })(); - } else if (Browser.Engine.webkit && Browser.Engine.version < 525){ - (function(){ - (['loaded', 'complete'].contains(document.readyState)) ? domready() : arguments.callee.delay(50); - })(); - } else { - document.addEvent('DOMContentLoaded', domready); + get: function(options){ + if (options || !this.retrieve('morph')){ + if (options || !this.retrieve('morph:options')) this.set('morph', options); + this.store('morph', new Fx.Morph(this, this.retrieve('morph:options'))); + } + return this.retrieve('morph'); } -})(); +}; + +Element.implement({ + + morph: function(props){ + this.get('morph').start(props); + return this; + } + +}); /* --- -script: JSON.js +name: Fx.Transitions -description: JSON encoder and decoder. +description: Contains a set of advanced transitions to be used with any of the Fx Classes. license: MIT-style license. -See Also: +credits: Easing Equations by Robert Penner, , modified and optimized to be used with MooTools. -requires: -- /Array -- /String -- /Number -- /Function -- /Hash +requires: Fx -provides: [JSON] +provides: Fx.Transitions ... */ -var JSON = new Hash(this.JSON && { - stringify: JSON.stringify, - parse: JSON.parse -}).extend({ - - $specialChars: {'\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"' : '\\"', '\\': '\\\\'}, - - $replaceChars: function(chr){ - return JSON.$specialChars[chr] || '\\u00' + Math.floor(chr.charCodeAt() / 16).toString(16) + (chr.charCodeAt() % 16).toString(16); - }, +Fx.implement({ - encode: function(obj){ - switch ($type(obj)){ - case 'string': - return '"' + obj.replace(/[\x00-\x1f\\"]/g, JSON.$replaceChars) + '"'; - case 'array': - return '[' + String(obj.map(JSON.encode).clean()) + ']'; - case 'object': case 'hash': - var string = []; - Hash.each(obj, function(value, key){ - var json = JSON.encode(value); - if (json) string.push(JSON.encode(key) + ':' + json); - }); - return '{' + string + '}'; - case 'number': case 'boolean': return String(obj); - case false: return 'null'; + getTransition: function(){ + var trans = this.options.transition || Fx.Transitions.Sine.easeInOut; + if (typeof trans == 'string'){ + var data = trans.split(':'); + trans = Fx.Transitions; + trans = trans[data[0]] || trans[data[0].capitalize()]; + if (data[1]) trans = trans['ease' + data[1].capitalize() + (data[2] ? data[2].capitalize() : '')]; } - return null; - }, - - decode: function(string, secure){ - if ($type(string) != 'string' || !string.length) return null; - if (secure && !(/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(string.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, ''))) return null; - return eval('(' + string + ')'); - } - -}); - -Native.implement([Hash, Array, String, Number], { - - toJSON: function(){ - return JSON.encode(this); + return trans; } }); +Fx.Transition = function(transition, params){ + params = $splat(params); + return $extend(transition, { + easeIn: function(pos){ + return transition(pos, params); + }, + easeOut: function(pos){ + return 1 - transition(1 - pos, params); + }, + easeInOut: function(pos){ + return (pos <= 0.5) ? transition(2 * pos, params) / 2 : (2 - transition(2 * (1 - pos), params)) / 2; + } + }); +}; -/* ---- - -script: Cookie.js - -description: Class for creating, reading, and deleting browser Cookies. - -license: MIT-style license. +Fx.Transitions = new Hash({ -credits: -- Based on the functions by Peter-Paul Koch (http://quirksmode.org). + linear: $arguments(0) -requires: -- /Options +}); -provides: [Cookie] +Fx.Transitions.extend = function(transitions){ + for (var transition in transitions) Fx.Transitions[transition] = new Fx.Transition(transitions[transition]); +}; -... -*/ +Fx.Transitions.extend({ -var Cookie = new Class({ + Pow: function(p, x){ + return Math.pow(p, x[0] || 6); + }, - Implements: Options, + Expo: function(p){ + return Math.pow(2, 8 * (p - 1)); + }, - options: { - path: false, - domain: false, - duration: false, - secure: false, - document: document + Circ: function(p){ + return 1 - Math.sin(Math.acos(p)); }, - initialize: function(key, options){ - this.key = key; - this.setOptions(options); + Sine: function(p){ + return 1 - Math.sin((1 - p) * Math.PI / 2); }, - write: function(value){ - value = encodeURIComponent(value); - if (this.options.domain) value += '; domain=' + this.options.domain; - if (this.options.path) value += '; path=' + this.options.path; - if (this.options.duration){ - var date = new Date(); - date.setTime(date.getTime() + this.options.duration * 24 * 60 * 60 * 1000); - value += '; expires=' + date.toGMTString(); - } - if (this.options.secure) value += '; secure'; - this.options.document.cookie = this.key + '=' + value; - return this; + Back: function(p, x){ + x = x[0] || 1.618; + return Math.pow(p, 2) * ((x + 1) * p - x); }, - read: function(){ - var value = this.options.document.cookie.match('(?:^|;)\\s*' + this.key.escapeRegExp() + '=([^;]*)'); - return (value) ? decodeURIComponent(value[1]) : null; + Bounce: function(p){ + var value; + for (var a = 0, b = 1; 1; a += b, b /= 2){ + if (p >= (7 - 4 * a) / 11){ + value = b * b - Math.pow((11 - 6 * a - 11 * p) / 4, 2); + break; + } + } + return value; }, - dispose: function(){ - new Cookie(this.key, $merge(this.options, {duration: -1})).write(''); - return this; + Elastic: function(p, x){ + return Math.pow(2, 10 * --p) * Math.cos(20 * p * Math.PI * (x[0] || 1) / 3); } }); -Cookie.write = function(key, value, options){ - return new Cookie(key, options).write(value); -}; - -Cookie.read = function(key){ - return new Cookie(key).read(); -}; - -Cookie.dispose = function(key, options){ - return new Cookie(key, options).dispose(); -}; +['Quad', 'Cubic', 'Quart', 'Quint'].each(function(transition, i){ + Fx.Transitions[transition] = new Fx.Transition(function(p){ + return Math.pow(p, [i + 2]); + }); +}); /* --- -script: Swiff.js +name: Fx.Tween -description: Wrapper for embedding SWF movies. Supports External Interface Communication. +description: Formerly Fx.Style, effect to transition any CSS property for an element. license: MIT-style license. -credits: -- Flash detection & Internet Explorer + Flash Player 9 fix inspired by SWFObject. +requires: Fx.CSS -requires: -- /Options -- /$util - -provides: [Swiff] +provides: [Fx.Tween, Element.fade, Element.highlight] ... */ -var Swiff = new Class({ +Fx.Tween = new Class({ - Implements: [Options], + Extends: Fx.CSS, - options: { - id: null, - height: 1, - width: 1, - container: null, - properties: {}, - params: { - quality: 'high', - allowScriptAccess: 'always', - wMode: 'transparent', - swLiveConnect: true - }, - callBacks: {}, - vars: {} + initialize: function(element, options){ + this.element = this.subject = document.id(element); + this.parent(options); }, - toElement: function(){ - return this.object; + set: function(property, now){ + if (arguments.length == 1){ + now = property; + property = this.property || this.options.property; + } + this.render(this.element, property, now, this.options.unit); + return this; }, - initialize: function(path, options){ - this.instance = 'Swiff_' + $time(); - - this.setOptions(options); - options = this.options; - var id = this.id = options.id || this.instance; - var container = document.id(options.container); + start: function(property, from, to){ + if (!this.check(property, from, to)) return this; + var args = Array.flatten(arguments); + this.property = this.options.property || args.shift(); + var parsed = this.prepare(this.element, this.property, args); + return this.parent(parsed.from, parsed.to); + } - Swiff.CallBacks[this.instance] = {}; +}); - var params = options.params, vars = options.vars, callBacks = options.callBacks; - var properties = $extend({height: options.height, width: options.width}, options.properties); +Element.Properties.tween = { - var self = this; + set: function(options){ + var tween = this.retrieve('tween'); + if (tween) tween.cancel(); + return this.eliminate('tween').store('tween:options', $extend({link: 'cancel'}, options)); + }, - for (var callBack in callBacks){ - Swiff.CallBacks[this.instance][callBack] = (function(option){ - return function(){ - return option.apply(self.object, arguments); - }; - })(callBacks[callBack]); - vars[callBack] = 'Swiff.CallBacks.' + this.instance + '.' + callBack; + get: function(options){ + if (options || !this.retrieve('tween')){ + if (options || !this.retrieve('tween:options')) this.set('tween', options); + this.store('tween', new Fx.Tween(this, this.retrieve('tween:options'))); } + return this.retrieve('tween'); + } - params.flashVars = Hash.toQueryString(vars); - if (Browser.Engine.trident){ - properties.classid = 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'; - params.movie = path; - } else { - properties.type = 'application/x-shockwave-flash'; - properties.data = path; - } - var build = ''; - } - build += ''; - this.object = ((container) ? container.empty() : new Element('div')).set('html', build).firstChild; - }, +}; - replaces: function(element){ - element = document.id(element, true); - element.parentNode.replaceChild(this.toElement(), element); +Element.implement({ + + tween: function(property, from, to){ + this.get('tween').start(arguments); return this; }, - inject: function(element){ - document.id(element, true).appendChild(this.toElement()); + fade: function(how){ + var fade = this.get('tween'), o = 'opacity', toggle; + how = $pick(how, 'toggle'); + switch (how){ + case 'in': fade.start(o, 1); break; + case 'out': fade.start(o, 0); break; + case 'show': fade.set(o, 1); break; + case 'hide': fade.set(o, 0); break; + case 'toggle': + var flag = this.retrieve('fade:flag', this.get('opacity') == 1); + fade.start(o, (flag) ? 0 : 1); + this.store('fade:flag', !flag); + toggle = true; + break; + default: fade.start(o, arguments); + } + if (!toggle) this.eliminate('fade:flag'); return this; }, - remote: function(){ - return Swiff.remote.apply(Swiff, [this.toElement()].extend(arguments)); + highlight: function(start, end){ + if (!end){ + end = this.retrieve('highlight:original', this.getStyle('background-color')); + end = (end == 'transparent') ? '#fff' : end; + } + var tween = this.get('tween'); + tween.start('background-color', start || '#ffff88', end).chain(function(){ + this.setStyle('background-color', this.retrieve('highlight:original')); + tween.callChain(); + }.bind(this)); + return this; } }); -Swiff.CallBacks = {}; - -Swiff.remote = function(obj, fn){ - var rs = obj.CallFunction('' + __flash__argumentsToXML(arguments, 2) + ''); - return eval(rs); -}; - /* --- -script: Fx.js +name: Request -description: Contains the basic animation logic to be extended by all other Fx Classes. +description: Powerful all purpose Request Class. Uses XMLHTTPRequest. license: MIT-style license. -requires: -- /Chain -- /Events -- /Options +requires: [Element, Chain, Events, Options, Browser] -provides: [Fx] +provides: Request ... */ -var Fx = new Class({ +var Request = new Class({ Implements: [Chain, Events, Options], - options: { - /* - onStart: $empty, - onCancel: $empty, + options: {/* + onRequest: $empty, onComplete: $empty, - */ - fps: 50, - unit: false, - duration: 500, - link: 'ignore' + onCancel: $empty, + onSuccess: $empty, + onFailure: $empty, + onException: $empty,*/ + url: '', + data: '', + headers: { + 'X-Requested-With': 'XMLHttpRequest', + 'Accept': 'text/javascript, text/html, application/xml, text/xml, */*' + }, + async: true, + format: false, + method: 'post', + link: 'ignore', + isSuccess: null, + emulation: true, + urlEncoded: true, + encoding: 'utf-8', + evalScripts: false, + evalResponse: false, + noCache: false }, initialize: function(options){ - this.subject = this.subject || this; + this.xhr = new Browser.Request(); this.setOptions(options); - this.options.duration = Fx.Durations[this.options.duration] || this.options.duration.toInt(); - var wait = this.options.wait; - if (wait === false) this.options.link = 'cancel'; - }, - - getTransition: function(){ - return function(p){ - return -(Math.cos(Math.PI * p) - 1) / 2; - }; + this.options.isSuccess = this.options.isSuccess || this.isSuccess; + this.headers = new Hash(this.options.headers); }, - step: function(){ - var time = $time(); - if (time < this.time + this.options.duration){ - var delta = this.transition((time - this.time) / this.options.duration); - this.set(this.compute(this.from, this.to, delta)); + onStateChange: function(){ + if (this.xhr.readyState != 4 || !this.running) return; + this.running = false; + this.status = 0; + $try(function(){ + this.status = this.xhr.status; + }.bind(this)); + this.xhr.onreadystatechange = $empty; + if (this.options.isSuccess.call(this, this.status)){ + this.response = {text: this.xhr.responseText, xml: this.xhr.responseXML}; + this.success(this.response.text, this.response.xml); } else { - this.set(this.compute(this.from, this.to, 1)); - this.complete(); - } - }, - - set: function(now){ - return now; - }, - - compute: function(from, to, delta){ - return Fx.compute(from, to, delta); - }, - - check: function(){ - if (!this.timer) return true; - switch (this.options.link){ - case 'cancel': this.cancel(); return true; - case 'chain': this.chain(this.caller.bind(this, arguments)); return false; + this.response = {text: null, xml: null}; + this.failure(); } - return false; }, - start: function(from, to){ - if (!this.check(from, to)) return this; - this.from = from; - this.to = to; - this.time = 0; - this.transition = this.getTransition(); - this.startTimer(); - this.onStart(); - return this; + isSuccess: function(){ + return ((this.status >= 200) && (this.status < 300)); }, - complete: function(){ - if (this.stopTimer()) this.onComplete(); - return this; + processScripts: function(text){ + if (this.options.evalResponse || (/(ecma|java)script/).test(this.getHeader('Content-type'))) return $exec(text); + return text.stripScripts(this.options.evalScripts); }, - cancel: function(){ - if (this.stopTimer()) this.onCancel(); - return this; + success: function(text, xml){ + this.onSuccess(this.processScripts(text), xml); }, - onStart: function(){ - this.fireEvent('start', this.subject); + onSuccess: function(){ + this.fireEvent('complete', arguments).fireEvent('success', arguments).callChain(); }, - onComplete: function(){ - this.fireEvent('complete', this.subject); - if (!this.callChain()) this.fireEvent('chainComplete', this.subject); + failure: function(){ + this.onFailure(); }, - onCancel: function(){ - this.fireEvent('cancel', this.subject).clearChain(); + onFailure: function(){ + this.fireEvent('complete').fireEvent('failure', this.xhr); }, - pause: function(){ - this.stopTimer(); + setHeader: function(name, value){ + this.headers.set(name, value); return this; }, - resume: function(){ - this.startTimer(); - return this; + getHeader: function(name){ + return $try(function(){ + return this.xhr.getResponseHeader(name); + }.bind(this)); }, - stopTimer: function(){ - if (!this.timer) return false; - this.time = $time() - this.time; - this.timer = $clear(this.timer); - return true; + check: function(){ + if (!this.running) return true; + switch (this.options.link){ + case 'cancel': this.cancel(); return true; + case 'chain': this.chain(this.caller.bind(this, arguments)); return false; + } + return false; }, - startTimer: function(){ - if (this.timer) return false; - this.time = $time() - this.time; - this.timer = this.step.periodical(Math.round(1000 / this.options.fps), this); - return true; - } - -}); - -Fx.compute = function(from, to, delta){ - return (to - from) * delta + from; -}; - -Fx.Durations = {'short': 250, 'normal': 500, 'long': 1000}; - - -/* ---- - -script: Fx.CSS.js - -description: Contains the CSS animation logic. Used by Fx.Tween, Fx.Morph, Fx.Elements. - -license: MIT-style license. + send: function(options){ + if (!this.check(options)) return this; + this.running = true; -requires: -- /Fx -- /Element.Style + var type = $type(options); + if (type == 'string' || type == 'element') options = {data: options}; -provides: [Fx.CSS] + var old = this.options; + options = $extend({data: old.data, url: old.url, method: old.method}, options); + var data = options.data, url = String(options.url), method = options.method.toLowerCase(); -... -*/ + switch ($type(data)){ + case 'element': data = document.id(data).toQueryString(); break; + case 'object': case 'hash': data = Hash.toQueryString(data); + } -Fx.CSS = new Class({ + if (this.options.format){ + var format = 'format=' + this.options.format; + data = (data) ? format + '&' + data : format; + } - Extends: Fx, + if (this.options.emulation && !['get', 'post'].contains(method)){ + var _method = '_method=' + method; + data = (data) ? _method + '&' + data : _method; + method = 'post'; + } - //prepares the base from/to object + if (this.options.urlEncoded && method == 'post'){ + var encoding = (this.options.encoding) ? '; charset=' + this.options.encoding : ''; + this.headers.set('Content-type', 'application/x-www-form-urlencoded' + encoding); + } - prepare: function(element, property, values){ - values = $splat(values); - var values1 = values[1]; - if (!$chk(values1)){ - values[1] = values[0]; - values[0] = element.getStyle(property); + if (this.options.noCache){ + var noCache = 'noCache=' + new Date().getTime(); + data = (data) ? noCache + '&' + data : noCache; } - var parsed = values.map(this.parse); - return {from: parsed[0], to: parsed[1]}; - }, - //parses a value into an array + var trimPosition = url.lastIndexOf('/'); + if (trimPosition > -1 && (trimPosition = url.indexOf('#')) > -1) url = url.substr(0, trimPosition); - parse: function(value){ - value = $lambda(value)(); - value = (typeof value == 'string') ? value.split(' ') : $splat(value); - return value.map(function(val){ - val = String(val); - var found = false; - Fx.CSS.Parsers.each(function(parser, key){ - if (found) return; - var parsed = parser.parse(val); - if ($chk(parsed)) found = {value: parsed, parser: parser}; - }); - found = found || {value: val, parser: Fx.CSS.Parsers.String}; - return found; - }); - }, + if (data && method == 'get'){ + url = url + (url.contains('?') ? '&' : '?') + data; + data = null; + } - //computes by a from and to prepared objects, using their parsers. + this.xhr.open(method.toUpperCase(), url, this.options.async); - compute: function(from, to, delta){ - var computed = []; - (Math.min(from.length, to.length)).times(function(i){ - computed.push({value: from[i].parser.compute(from[i].value, to[i].value, delta), parser: from[i].parser}); - }); - computed.$family = {name: 'fx:css:value'}; - return computed; - }, + this.xhr.onreadystatechange = this.onStateChange.bind(this); - //serves the value as settable + this.headers.each(function(value, key){ + try { + this.xhr.setRequestHeader(key, value); + } catch (e){ + this.fireEvent('exception', [key, value]); + } + }, this); - serve: function(value, unit){ - if ($type(value) != 'fx:css:value') value = this.parse(value); - var returned = []; - value.each(function(bit){ - returned = returned.concat(bit.parser.serve(bit.value, unit)); - }); - return returned; + this.fireEvent('request'); + this.xhr.send(data); + if (!this.options.async) this.onStateChange(); + return this; }, - //renders the change to an element - - render: function(element, property, value, unit){ - element.setStyle(property, this.serve(value, unit)); - }, + cancel: function(){ + if (!this.running) return this; + this.running = false; + this.xhr.abort(); + this.xhr.onreadystatechange = $empty; + this.xhr = new Browser.Request(); + this.fireEvent('cancel'); + return this; + } - //searches inside the page css to find the values for a selector +}); - search: function(selector){ - if (Fx.CSS.Cache[selector]) return Fx.CSS.Cache[selector]; - var to = {}; - Array.each(document.styleSheets, function(sheet, j){ - var href = sheet.href; - if (href && href.contains('://') && !href.contains(document.domain)) return; - var rules = sheet.rules || sheet.cssRules; - Array.each(rules, function(rule, i){ - if (!rule.style) return; - var selectorText = (rule.selectorText) ? rule.selectorText.replace(/^\w+/, function(m){ - return m.toLowerCase(); - }) : null; - if (!selectorText || !selectorText.test('^' + selector + '$')) return; - Element.Styles.each(function(value, style){ - if (!rule.style[style] || Element.ShortStyles[style]) return; - value = String(rule.style[style]); - to[style] = (value.test(/^rgb/)) ? value.rgbToHex() : value; - }); - }); - }); - return Fx.CSS.Cache[selector] = to; - } +(function(){ +var methods = {}; +['get', 'post', 'put', 'delete', 'GET', 'POST', 'PUT', 'DELETE'].each(function(method){ + methods[method] = function(){ + var params = Array.link(arguments, {url: String.type, data: $defined}); + return this.send($extend(params, {method: method})); + }; }); -Fx.CSS.Cache = {}; +Request.implement(methods); -Fx.CSS.Parsers = new Hash({ +})(); - Color: { - parse: function(value){ - if (value.match(/^#[0-9a-f]{3,6}$/i)) return value.hexToRgb(true); - return ((value = value.match(/(\d+),\s*(\d+),\s*(\d+)/))) ? [value[1], value[2], value[3]] : false; - }, - compute: function(from, to, delta){ - return from.map(function(value, i){ - return Math.round(Fx.compute(from[i], to[i], delta)); - }); - }, - serve: function(value){ - return value.map(Number); - } +Element.Properties.send = { + + set: function(options){ + var send = this.retrieve('send'); + if (send) send.cancel(); + return this.eliminate('send').store('send:options', $extend({ + data: this, link: 'cancel', method: this.get('method') || 'post', url: this.get('action') + }, options)); }, - Number: { - parse: parseFloat, - compute: Fx.compute, - serve: function(value, unit){ - return (unit) ? value + unit : value; + get: function(options){ + if (options || !this.retrieve('send')){ + if (options || !this.retrieve('send:options')) this.set('send', options); + this.store('send', new Request(this.retrieve('send:options'))); } - }, + return this.retrieve('send'); + } - String: { - parse: $lambda(false), - compute: $arguments(1), - serve: $arguments(0) +}; + +Element.implement({ + + send: function(url){ + var sender = this.get('send'); + sender.send({data: this, url: url || sender.options.url}); + return this; } }); @@ -3657,104 +3480,157 @@ Fx.CSS.Parsers = new Hash({ /* --- -script: Fx.Tween.js +name: Request.HTML -description: Formerly Fx.Style, effect to transition any CSS property for an element. +description: Extends the basic Request Class with additional methods for interacting with HTML responses. license: MIT-style license. -requires: -- /Fx.CSS +requires: [Request, Element] -provides: [Fx.Tween, Element.fade, Element.highlight] +provides: Request.HTML ... */ -Fx.Tween = new Class({ +Request.HTML = new Class({ - Extends: Fx.CSS, + Extends: Request, - initialize: function(element, options){ - this.element = this.subject = document.id(element); - this.parent(options); + options: { + update: false, + append: false, + evalScripts: true, + filter: false }, - set: function(property, now){ - if (arguments.length == 1){ - now = property; - property = this.property || this.options.property; - } - this.render(this.element, property, now, this.options.unit); - return this; + processHTML: function(text){ + var match = text.match(/]*>([\s\S]*?)<\/body>/i); + text = (match) ? match[1] : text; + + var container = new Element('div'); + + return $try(function(){ + var root = '' + text + '', doc; + if (Browser.Engine.trident){ + doc = new ActiveXObject('Microsoft.XMLDOM'); + doc.async = false; + doc.loadXML(root); + } else { + doc = new DOMParser().parseFromString(root, 'text/xml'); + } + root = doc.getElementsByTagName('root')[0]; + if (!root) return null; + for (var i = 0, k = root.childNodes.length; i < k; i++){ + var child = Element.clone(root.childNodes[i], true, true); + if (child) container.grab(child); + } + return container; + }) || container.set('html', text); }, - start: function(property, from, to){ - if (!this.check(property, from, to)) return this; - var args = Array.flatten(arguments); - this.property = this.options.property || args.shift(); - var parsed = this.prepare(this.element, this.property, args); - return this.parent(parsed.from, parsed.to); + success: function(text){ + var options = this.options, response = this.response; + + response.html = text.stripScripts(function(script){ + response.javascript = script; + }); + + var temp = this.processHTML(response.html); + + response.tree = temp.childNodes; + response.elements = temp.getElements('*'); + + if (options.filter) response.tree = response.elements.filter(options.filter); + if (options.update) document.id(options.update).empty().set('html', response.html); + else if (options.append) document.id(options.append).adopt(temp.getChildren()); + if (options.evalScripts) $exec(response.javascript); + + this.onSuccess(response.tree, response.elements, response.html, response.javascript); } }); -Element.Properties.tween = { +Element.Properties.load = { set: function(options){ - var tween = this.retrieve('tween'); - if (tween) tween.cancel(); - return this.eliminate('tween').store('tween:options', $extend({link: 'cancel'}, options)); + var load = this.retrieve('load'); + if (load) load.cancel(); + return this.eliminate('load').store('load:options', $extend({data: this, link: 'cancel', update: this, method: 'get'}, options)); }, get: function(options){ - if (options || !this.retrieve('tween')){ - if (options || !this.retrieve('tween:options')) this.set('tween', options); - this.store('tween', new Fx.Tween(this, this.retrieve('tween:options'))); + if (options || ! this.retrieve('load')){ + if (options || !this.retrieve('load:options')) this.set('load', options); + this.store('load', new Request.HTML(this.retrieve('load:options'))); } - return this.retrieve('tween'); + return this.retrieve('load'); } }; Element.implement({ - tween: function(property, from, to){ - this.get('tween').start(arguments); + load: function(){ + this.get('load').send(Array.link(arguments, {data: Object.type, url: String.type})); return this; + } + +}); + + +/* +--- + +name: JSON + +description: JSON encoder and decoder. + +license: MIT-style license. + +see: + +requires: [Array, String, Number, Function, Hash] + +provides: JSON + +... +*/ + +var JSON = new Hash(this.JSON && { + stringify: JSON.stringify, + parse: JSON.parse +}).extend({ + + $specialChars: {'\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"' : '\\"', '\\': '\\\\'}, + + $replaceChars: function(chr){ + return JSON.$specialChars[chr] || '\\u00' + Math.floor(chr.charCodeAt() / 16).toString(16) + (chr.charCodeAt() % 16).toString(16); }, - fade: function(how){ - var fade = this.get('tween'), o = 'opacity', toggle; - how = $pick(how, 'toggle'); - switch (how){ - case 'in': fade.start(o, 1); break; - case 'out': fade.start(o, 0); break; - case 'show': fade.set(o, 1); break; - case 'hide': fade.set(o, 0); break; - case 'toggle': - var flag = this.retrieve('fade:flag', this.get('opacity') == 1); - fade.start(o, (flag) ? 0 : 1); - this.store('fade:flag', !flag); - toggle = true; - break; - default: fade.start(o, arguments); + encode: function(obj){ + switch ($type(obj)){ + case 'string': + return '"' + obj.replace(/[\x00-\x1f\\"]/g, JSON.$replaceChars) + '"'; + case 'array': + return '[' + String(obj.map(JSON.encode).clean()) + ']'; + case 'object': case 'hash': + var string = []; + Hash.each(obj, function(value, key){ + var json = JSON.encode(value); + if (json) string.push(JSON.encode(key) + ':' + json); + }); + return '{' + string + '}'; + case 'number': case 'boolean': return String(obj); + case false: return 'null'; } - if (!toggle) this.eliminate('fade:flag'); - return this; + return null; }, - highlight: function(start, end){ - if (!end){ - end = this.retrieve('highlight:original', this.getStyle('background-color')); - end = (end == 'transparent') ? '#fff' : end; - } - var tween = this.get('tween'); - tween.start('background-color', start || '#ffff88', end).chain(function(){ - this.setStyle('background-color', this.retrieve('highlight:original')); - tween.callChain(); - }.bind(this)); - return this; + decode: function(string, secure){ + if ($type(string) != 'string' || !string.length) return null; + if (secure && !(/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(string.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, ''))) return null; + return eval('(' + string + ')'); } }); @@ -3763,425 +3639,540 @@ Element.implement({ /* --- -script: Fx.Morph.js +name: Request.JSON -description: Formerly Fx.Styles, effect to transition any number of CSS properties for an element using an object of rules, or CSS based selector rules. +description: Extends the basic Request Class with additional methods for sending and receiving JSON data. license: MIT-style license. -requires: -- /Fx.CSS +requires: [Request, JSON] -provides: [Fx.Morph] +provides: [Request.JSON] ... */ -Fx.Morph = new Class({ - - Extends: Fx.CSS, +Request.JSON = new Class({ - initialize: function(element, options){ - this.element = this.subject = document.id(element); - this.parent(options); - }, + Extends: Request, - set: function(now){ - if (typeof now == 'string') now = this.search(now); - for (var p in now) this.render(this.element, p, now[p], this.options.unit); - return this; + options: { + secure: true }, - compute: function(from, to, delta){ - var now = {}; - for (var p in from) now[p] = this.parent(from[p], to[p], delta); - return now; + initialize: function(options){ + this.parent(options); + this.headers.extend({'Accept': 'application/json', 'X-Request': 'JSON'}); }, - start: function(properties){ - if (!this.check(properties)) return this; - if (typeof properties == 'string') properties = this.search(properties); - var from = {}, to = {}; - for (var p in properties){ - var parsed = this.prepare(this.element, p, properties[p]); - from[p] = parsed.from; - to[p] = parsed.to; - } - return this.parent(from, to); + success: function(text){ + this.response.json = JSON.decode(text, this.options.secure); + this.onSuccess(this.response.json, text); } }); -Element.Properties.morph = { - set: function(options){ - var morph = this.retrieve('morph'); - if (morph) morph.cancel(); - return this.eliminate('morph').store('morph:options', $extend({link: 'cancel'}, options)); +/* +--- + +name: Cookie + +description: Class for creating, reading, and deleting browser Cookies. + +license: MIT-style license. + +credits: Based on the functions by Peter-Paul Koch (http://quirksmode.org). + +requires: Options + +provides: Cookie + +... +*/ + +var Cookie = new Class({ + + Implements: Options, + + options: { + path: false, + domain: false, + duration: false, + secure: false, + document: document + }, + + initialize: function(key, options){ + this.key = key; + this.setOptions(options); + }, + + write: function(value){ + value = encodeURIComponent(value); + if (this.options.domain) value += '; domain=' + this.options.domain; + if (this.options.path) value += '; path=' + this.options.path; + if (this.options.duration){ + var date = new Date(); + date.setTime(date.getTime() + this.options.duration * 24 * 60 * 60 * 1000); + value += '; expires=' + date.toGMTString(); + } + if (this.options.secure) value += '; secure'; + this.options.document.cookie = this.key + '=' + value; + return this; + }, + + read: function(){ + var value = this.options.document.cookie.match('(?:^|;)\\s*' + this.key.escapeRegExp() + '=([^;]*)'); + return (value) ? decodeURIComponent(value[1]) : null; }, - get: function(options){ - if (options || !this.retrieve('morph')){ - if (options || !this.retrieve('morph:options')) this.set('morph', options); - this.store('morph', new Fx.Morph(this, this.retrieve('morph:options'))); - } - return this.retrieve('morph'); + dispose: function(){ + new Cookie(this.key, $merge(this.options, {duration: -1})).write(''); + return this; } -}; +}); -Element.implement({ +Cookie.write = function(key, value, options){ + return new Cookie(key, options).write(value); +}; - morph: function(props){ - this.get('morph').start(props); - return this; - } +Cookie.read = function(key){ + return new Cookie(key).read(); +}; -}); +Cookie.dispose = function(key, options){ + return new Cookie(key, options).dispose(); +}; /* --- -script: Fx.Transitions.js +name: DomReady -description: Contains a set of advanced transitions to be used with any of the Fx Classes. +description: Contains the custom event domready. license: MIT-style license. -credits: -- Easing Equations by Robert Penner, , modified and optimized to be used with MooTools. - -requires: -- /Fx +requires: Element.Event -provides: [Fx.Transitions] +provides: DomReady ... */ -Fx.implement({ +Element.Events.domready = { - getTransition: function(){ - var trans = this.options.transition || Fx.Transitions.Sine.easeInOut; - if (typeof trans == 'string'){ - var data = trans.split(':'); - trans = Fx.Transitions; - trans = trans[data[0]] || trans[data[0].capitalize()]; - if (data[1]) trans = trans['ease' + data[1].capitalize() + (data[2] ? data[2].capitalize() : '')]; - } - return trans; + onAdd: function(fn){ + if (Browser.loaded) fn.call(this); } -}); - -Fx.Transition = function(transition, params){ - params = $splat(params); - return $extend(transition, { - easeIn: function(pos){ - return transition(pos, params); - }, - easeOut: function(pos){ - return 1 - transition(1 - pos, params); - }, - easeInOut: function(pos){ - return (pos <= 0.5) ? transition(2 * pos, params) / 2 : (2 - transition(2 * (1 - pos), params)) / 2; - } - }); }; -Fx.Transitions = new Hash({ +(function(){ - linear: $arguments(0) + var domready = function(){ + if (Browser.loaded) return; + Browser.loaded = true; + window.fireEvent('domready'); + document.fireEvent('domready'); + }; + + window.addEvent('load', domready); -}); + if (Browser.Engine.trident){ + var temp = document.createElement('div'); + (function(){ + ($try(function(){ + temp.doScroll(); // Technique by Diego Perini + return document.id(temp).inject(document.body).set('html', 'temp').dispose(); + })) ? domready() : arguments.callee.delay(50); + })(); + } else if (Browser.Engine.webkit && Browser.Engine.version < 525){ + (function(){ + (['loaded', 'complete'].contains(document.readyState)) ? domready() : arguments.callee.delay(50); + })(); + } else { + document.addEvent('DOMContentLoaded', domready); + } -Fx.Transitions.extend = function(transitions){ - for (var transition in transitions) Fx.Transitions[transition] = new Fx.Transition(transitions[transition]); -}; +})(); -Fx.Transitions.extend({ - Pow: function(p, x){ - return Math.pow(p, x[0] || 6); - }, +/* +--- - Expo: function(p){ - return Math.pow(2, 8 * (p - 1)); - }, +name: Selectors - Circ: function(p){ - return 1 - Math.sin(Math.acos(p)); - }, +description: Adds advanced CSS-style querying capabilities for targeting HTML Elements. Includes pseudo selectors. - Sine: function(p){ - return 1 - Math.sin((1 - p) * Math.PI / 2); - }, +license: MIT-style license. - Back: function(p, x){ - x = x[0] || 1.618; - return Math.pow(p, 2) * ((x + 1) * p - x); - }, +requires: Element - Bounce: function(p){ - var value; - for (var a = 0, b = 1; 1; a += b, b /= 2){ - if (p >= (7 - 4 * a) / 11){ - value = b * b - Math.pow((11 - 6 * a - 11 * p) / 4, 2); - break; - } - } - return value; - }, +provides: Selectors - Elastic: function(p, x){ - return Math.pow(2, 10 * --p) * Math.cos(20 * p * Math.PI * (x[0] || 1) / 3); - } +... +*/ -}); +Native.implement([Document, Element], { -['Quad', 'Cubic', 'Quart', 'Quint'].each(function(transition, i){ - Fx.Transitions[transition] = new Fx.Transition(function(p){ - return Math.pow(p, [i + 2]); - }); -}); + getElements: function(expression, nocash){ + expression = expression.split(','); + var items, local = {}; + for (var i = 0, l = expression.length; i < l; i++){ + var selector = expression[i], elements = Selectors.Utils.search(this, selector, local); + if (i != 0 && elements.item) elements = $A(elements); + items = (i == 0) ? elements : (items.item) ? $A(items).concat(elements) : items.concat(elements); + } + return new Elements(items, {ddup: (expression.length > 1), cash: !nocash}); + } +}); -/* ---- +Element.implement({ -script: Request.js + match: function(selector){ + if (!selector || (selector == this)) return true; + var tagid = Selectors.Utils.parseTagAndID(selector); + var tag = tagid[0], id = tagid[1]; + if (!Selectors.Filters.byID(this, id) || !Selectors.Filters.byTag(this, tag)) return false; + var parsed = Selectors.Utils.parseSelector(selector); + return (parsed) ? Selectors.Utils.filter(this, parsed, {}) : true; + } -description: Powerful all purpose Request Class. Uses XMLHTTPRequest. +}); -license: MIT-style license. +var Selectors = {Cache: {nth: {}, parsed: {}}}; -requires: -- /Element -- /Chain -- /Events -- /Options -- /Browser +Selectors.RegExps = { + id: (/#([\w-]+)/), + tag: (/^(\w+|\*)/), + quick: (/^(\w+|\*)$/), + splitter: (/\s*([+>~\s])\s*([a-zA-Z#.*:\[])/g), + combined: (/\.([\w-]+)|\[(\w+)(?:([!*^$~|]?=)(["']?)([^\4]*?)\4)?\]|:([\w-]+)(?:\(["']?(.*?)?["']?\)|$)/g) +}; -provides: [Request] +Selectors.Utils = { -... -*/ + chk: function(item, uniques){ + if (!uniques) return true; + var uid = $uid(item); + if (!uniques[uid]) return uniques[uid] = true; + return false; + }, -var Request = new Class({ + parseNthArgument: function(argument){ + if (Selectors.Cache.nth[argument]) return Selectors.Cache.nth[argument]; + var parsed = argument.match(/^([+-]?\d*)?([a-z]+)?([+-]?\d*)?$/); + if (!parsed) return false; + var inta = parseInt(parsed[1], 10); + var a = (inta || inta === 0) ? inta : 1; + var special = parsed[2] || false; + var b = parseInt(parsed[3], 10) || 0; + if (a != 0){ + b--; + while (b < 1) b += a; + while (b >= a) b -= a; + } else { + a = b; + special = 'index'; + } + switch (special){ + case 'n': parsed = {a: a, b: b, special: 'n'}; break; + case 'odd': parsed = {a: 2, b: 0, special: 'n'}; break; + case 'even': parsed = {a: 2, b: 1, special: 'n'}; break; + case 'first': parsed = {a: 0, special: 'index'}; break; + case 'last': parsed = {special: 'last-child'}; break; + case 'only': parsed = {special: 'only-child'}; break; + default: parsed = {a: (a - 1), special: 'index'}; + } - Implements: [Chain, Events, Options], + return Selectors.Cache.nth[argument] = parsed; + }, - options: {/* - onRequest: $empty, - onComplete: $empty, - onCancel: $empty, - onSuccess: $empty, - onFailure: $empty, - onException: $empty,*/ - url: '', - data: '', - headers: { - 'X-Requested-With': 'XMLHttpRequest', - 'Accept': 'text/javascript, text/html, application/xml, text/xml, */*' - }, - async: true, - format: false, - method: 'post', - link: 'ignore', - isSuccess: null, - emulation: true, - urlEncoded: true, - encoding: 'utf-8', - evalScripts: false, - evalResponse: false, - noCache: false + parseSelector: function(selector){ + if (Selectors.Cache.parsed[selector]) return Selectors.Cache.parsed[selector]; + var m, parsed = {classes: [], pseudos: [], attributes: []}; + while ((m = Selectors.RegExps.combined.exec(selector))){ + var cn = m[1], an = m[2], ao = m[3], av = m[5], pn = m[6], pa = m[7]; + if (cn){ + parsed.classes.push(cn); + } else if (pn){ + var parser = Selectors.Pseudo.get(pn); + if (parser) parsed.pseudos.push({parser: parser, argument: pa}); + else parsed.attributes.push({name: pn, operator: '=', value: pa}); + } else if (an){ + parsed.attributes.push({name: an, operator: ao, value: av}); + } + } + if (!parsed.classes.length) delete parsed.classes; + if (!parsed.attributes.length) delete parsed.attributes; + if (!parsed.pseudos.length) delete parsed.pseudos; + if (!parsed.classes && !parsed.attributes && !parsed.pseudos) parsed = null; + return Selectors.Cache.parsed[selector] = parsed; }, - initialize: function(options){ - this.xhr = new Browser.Request(); - this.setOptions(options); - this.options.isSuccess = this.options.isSuccess || this.isSuccess; - this.headers = new Hash(this.options.headers); + parseTagAndID: function(selector){ + var tag = selector.match(Selectors.RegExps.tag); + var id = selector.match(Selectors.RegExps.id); + return [(tag) ? tag[1] : '*', (id) ? id[1] : false]; }, - onStateChange: function(){ - if (this.xhr.readyState != 4 || !this.running) return; - this.running = false; - this.status = 0; - $try(function(){ - this.status = this.xhr.status; - }.bind(this)); - this.xhr.onreadystatechange = $empty; - if (this.options.isSuccess.call(this, this.status)){ - this.response = {text: this.xhr.responseText, xml: this.xhr.responseXML}; - this.success(this.response.text, this.response.xml); - } else { - this.response = {text: null, xml: null}; - this.failure(); + filter: function(item, parsed, local){ + var i; + if (parsed.classes){ + for (i = parsed.classes.length; i--; i){ + var cn = parsed.classes[i]; + if (!Selectors.Filters.byClass(item, cn)) return false; + } + } + if (parsed.attributes){ + for (i = parsed.attributes.length; i--; i){ + var att = parsed.attributes[i]; + if (!Selectors.Filters.byAttribute(item, att.name, att.operator, att.value)) return false; + } + } + if (parsed.pseudos){ + for (i = parsed.pseudos.length; i--; i){ + var psd = parsed.pseudos[i]; + if (!Selectors.Filters.byPseudo(item, psd.parser, psd.argument, local)) return false; + } } + return true; }, - isSuccess: function(){ - return ((this.status >= 200) && (this.status < 300)); + getByTagAndID: function(ctx, tag, id){ + if (id){ + var item = (ctx.getElementById) ? ctx.getElementById(id, true) : Element.getElementById(ctx, id, true); + return (item && Selectors.Filters.byTag(item, tag)) ? [item] : []; + } else { + return ctx.getElementsByTagName(tag); + } }, - processScripts: function(text){ - if (this.options.evalResponse || (/(ecma|java)script/).test(this.getHeader('Content-type'))) return $exec(text); - return text.stripScripts(this.options.evalScripts); - }, + search: function(self, expression, local){ + var splitters = []; - success: function(text, xml){ - this.onSuccess(this.processScripts(text), xml); - }, + var selectors = expression.trim().replace(Selectors.RegExps.splitter, function(m0, m1, m2){ + splitters.push(m1); + return ':)' + m2; + }).split(':)'); - onSuccess: function(){ - this.fireEvent('complete', arguments).fireEvent('success', arguments).callChain(); - }, + var items, filtered, item; - failure: function(){ - this.onFailure(); - }, + for (var i = 0, l = selectors.length; i < l; i++){ - onFailure: function(){ - this.fireEvent('complete').fireEvent('failure', this.xhr); - }, + var selector = selectors[i]; - setHeader: function(name, value){ - this.headers.set(name, value); - return this; - }, + if (i == 0 && Selectors.RegExps.quick.test(selector)){ + items = self.getElementsByTagName(selector); + continue; + } - getHeader: function(name){ - return $try(function(){ - return this.xhr.getResponseHeader(name); - }.bind(this)); - }, + var splitter = splitters[i - 1]; - check: function(){ - if (!this.running) return true; - switch (this.options.link){ - case 'cancel': this.cancel(); return true; - case 'chain': this.chain(this.caller.bind(this, arguments)); return false; - } - return false; - }, + var tagid = Selectors.Utils.parseTagAndID(selector); + var tag = tagid[0], id = tagid[1]; - send: function(options){ - if (!this.check(options)) return this; - this.running = true; + if (i == 0){ + items = Selectors.Utils.getByTagAndID(self, tag, id); + } else { + var uniques = {}, found = []; + for (var j = 0, k = items.length; j < k; j++) found = Selectors.Getters[splitter](found, items[j], tag, id, uniques); + items = found; + } - var type = $type(options); - if (type == 'string' || type == 'element') options = {data: options}; + var parsed = Selectors.Utils.parseSelector(selector); - var old = this.options; - options = $extend({data: old.data, url: old.url, method: old.method}, options); - var data = options.data, url = String(options.url), method = options.method.toLowerCase(); + if (parsed){ + filtered = []; + for (var m = 0, n = items.length; m < n; m++){ + item = items[m]; + if (Selectors.Utils.filter(item, parsed, local)) filtered.push(item); + } + items = filtered; + } - switch ($type(data)){ - case 'element': data = document.id(data).toQueryString(); break; - case 'object': case 'hash': data = Hash.toQueryString(data); } - if (this.options.format){ - var format = 'format=' + this.options.format; - data = (data) ? format + '&' + data : format; + return items; + + } + +}; + +Selectors.Getters = { + + ' ': function(found, self, tag, id, uniques){ + var items = Selectors.Utils.getByTagAndID(self, tag, id); + for (var i = 0, l = items.length; i < l; i++){ + var item = items[i]; + if (Selectors.Utils.chk(item, uniques)) found.push(item); } + return found; + }, - if (this.options.emulation && !['get', 'post'].contains(method)){ - var _method = '_method=' + method; - data = (data) ? _method + '&' + data : _method; - method = 'post'; + '>': function(found, self, tag, id, uniques){ + var children = Selectors.Utils.getByTagAndID(self, tag, id); + for (var i = 0, l = children.length; i < l; i++){ + var child = children[i]; + if (child.parentNode == self && Selectors.Utils.chk(child, uniques)) found.push(child); } + return found; + }, - if (this.options.urlEncoded && method == 'post'){ - var encoding = (this.options.encoding) ? '; charset=' + this.options.encoding : ''; - this.headers.set('Content-type', 'application/x-www-form-urlencoded' + encoding); + '+': function(found, self, tag, id, uniques){ + while ((self = self.nextSibling)){ + if (self.nodeType == 1){ + if (Selectors.Utils.chk(self, uniques) && Selectors.Filters.byTag(self, tag) && Selectors.Filters.byID(self, id)) found.push(self); + break; + } } + return found; + }, - if (this.options.noCache){ - var noCache = 'noCache=' + new Date().getTime(); - data = (data) ? noCache + '&' + data : noCache; + '~': function(found, self, tag, id, uniques){ + while ((self = self.nextSibling)){ + if (self.nodeType == 1){ + if (!Selectors.Utils.chk(self, uniques)) break; + if (Selectors.Filters.byTag(self, tag) && Selectors.Filters.byID(self, id)) found.push(self); + } } + return found; + } - var trimPosition = url.lastIndexOf('/'); - if (trimPosition > -1 && (trimPosition = url.indexOf('#')) > -1) url = url.substr(0, trimPosition); +}; - if (data && method == 'get'){ - url = url + (url.contains('?') ? '&' : '?') + data; - data = null; - } +Selectors.Filters = { - this.xhr.open(method.toUpperCase(), url, this.options.async); + byTag: function(self, tag){ + return (tag == '*' || (self.tagName && self.tagName.toLowerCase() == tag)); + }, - this.xhr.onreadystatechange = this.onStateChange.bind(this); + byID: function(self, id){ + return (!id || (self.id && self.id == id)); + }, - this.headers.each(function(value, key){ - try { - this.xhr.setRequestHeader(key, value); - } catch (e){ - this.fireEvent('exception', [key, value]); - } - }, this); + byClass: function(self, klass){ + return (self.className && self.className.contains && self.className.contains(klass, ' ')); + }, - this.fireEvent('request'); - this.xhr.send(data); - if (!this.options.async) this.onStateChange(); - return this; + byPseudo: function(self, parser, argument, local){ + return parser.call(self, argument, local); }, - cancel: function(){ - if (!this.running) return this; - this.running = false; - this.xhr.abort(); - this.xhr.onreadystatechange = $empty; - this.xhr = new Browser.Request(); - this.fireEvent('cancel'); - return this; + byAttribute: function(self, name, operator, value){ + var result = Element.prototype.getProperty.call(self, name); + if (!result) return (operator == '!='); + if (!operator || value == undefined) return true; + switch (operator){ + case '=': return (result == value); + case '*=': return (result.contains(value)); + case '^=': return (result.substr(0, value.length) == value); + case '$=': return (result.substr(result.length - value.length) == value); + case '!=': return (result != value); + case '~=': return result.contains(value, ' '); + case '|=': return result.contains(value, '-'); + } + return false; } -}); +}; -(function(){ +Selectors.Pseudo = new Hash({ -var methods = {}; -['get', 'post', 'put', 'delete', 'GET', 'POST', 'PUT', 'DELETE'].each(function(method){ - methods[method] = function(){ - var params = Array.link(arguments, {url: String.type, data: $defined}); - return this.send($extend(params, {method: method})); - }; -}); + // w3c pseudo selectors + + checked: function(){ + return this.checked; + }, + + empty: function(){ + return !(this.innerText || this.textContent || '').length; + }, -Request.implement(methods); + not: function(selector){ + return !Element.match(this, selector); + }, -})(); + contains: function(text){ + return (this.innerText || this.textContent || '').contains(text); + }, -Element.Properties.send = { + 'first-child': function(){ + return Selectors.Pseudo.index.call(this, 0); + }, - set: function(options){ - var send = this.retrieve('send'); - if (send) send.cancel(); - return this.eliminate('send').store('send:options', $extend({ - data: this, link: 'cancel', method: this.get('method') || 'post', url: this.get('action') - }, options)); + 'last-child': function(){ + var element = this; + while ((element = element.nextSibling)){ + if (element.nodeType == 1) return false; + } + return true; }, - get: function(options){ - if (options || !this.retrieve('send')){ - if (options || !this.retrieve('send:options')) this.set('send', options); - this.store('send', new Request(this.retrieve('send:options'))); + 'only-child': function(){ + var prev = this; + while ((prev = prev.previousSibling)){ + if (prev.nodeType == 1) return false; } - return this.retrieve('send'); - } + var next = this; + while ((next = next.nextSibling)){ + if (next.nodeType == 1) return false; + } + return true; + }, -}; + 'nth-child': function(argument, local){ + argument = (argument == undefined) ? 'n' : argument; + var parsed = Selectors.Utils.parseNthArgument(argument); + if (parsed.special != 'n') return Selectors.Pseudo[parsed.special].call(this, parsed.a, local); + var count = 0; + local.positions = local.positions || {}; + var uid = $uid(this); + if (!local.positions[uid]){ + var self = this; + while ((self = self.previousSibling)){ + if (self.nodeType != 1) continue; + count ++; + var position = local.positions[$uid(self)]; + if (position != undefined){ + count = position + count; + break; + } + } + local.positions[uid] = count; + } + return (local.positions[uid] % parsed.a == parsed.b); + }, -Element.implement({ + // custom pseudo selectors - send: function(url){ - var sender = this.get('send'); - sender.send({data: this, url: url || sender.options.url}); - return this; + index: function(index){ + var element = this, count = 0; + while ((element = element.previousSibling)){ + if (element.nodeType == 1 && ++count > index) return false; + } + return (count == index); + }, + + even: function(argument, local){ + return Selectors.Pseudo['nth-child'].call(this, '2n+1', local); + }, + + odd: function(argument, local){ + return Selectors.Pseudo['nth-child'].call(this, '2n', local); + }, + + selected: function(){ + return this.selected; + }, + + enabled: function(){ + return (this.disabled === false); } }); @@ -4190,140 +4181,108 @@ Element.implement({ /* --- -script: Request.HTML.js +name: Swiff -description: Extends the basic Request Class with additional methods for interacting with HTML responses. +description: Wrapper for embedding SWF movies. Supports External Interface Communication. license: MIT-style license. -requires: -- /Request -- /Element +credits: Flash detection & Internet Explorer + Flash Player 9 fix inspired by SWFObject. + +requires: [Options, $util] -provides: [Request.HTML] +provides: Swiff ... */ -Request.HTML = new Class({ +var Swiff = new Class({ - Extends: Request, + Implements: [Options], options: { - update: false, - append: false, - evalScripts: true, - filter: false + id: null, + height: 1, + width: 1, + container: null, + properties: {}, + params: { + quality: 'high', + allowScriptAccess: 'always', + wMode: 'transparent', + swLiveConnect: true + }, + callBacks: {}, + vars: {} }, - processHTML: function(text){ - var match = text.match(/]*>([\s\S]*?)<\/body>/i); - text = (match) ? match[1] : text; - - var container = new Element('div'); - - return $try(function(){ - var root = '' + text + '', doc; - if (Browser.Engine.trident){ - doc = new ActiveXObject('Microsoft.XMLDOM'); - doc.async = false; - doc.loadXML(root); - } else { - doc = new DOMParser().parseFromString(root, 'text/xml'); - } - root = doc.getElementsByTagName('root')[0]; - if (!root) return null; - for (var i = 0, k = root.childNodes.length; i < k; i++){ - var child = Element.clone(root.childNodes[i], true, true); - if (child) container.grab(child); - } - return container; - }) || container.set('html', text); + toElement: function(){ + return this.object; }, - success: function(text){ - var options = this.options, response = this.response; - - response.html = text.stripScripts(function(script){ - response.javascript = script; - }); - - var temp = this.processHTML(response.html); - - response.tree = temp.childNodes; - response.elements = temp.getElements('*'); - - if (options.filter) response.tree = response.elements.filter(options.filter); - if (options.update) document.id(options.update).empty().set('html', response.html); - else if (options.append) document.id(options.append).adopt(temp.getChildren()); - if (options.evalScripts) $exec(response.javascript); + initialize: function(path, options){ + this.instance = 'Swiff_' + $time(); - this.onSuccess(response.tree, response.elements, response.html, response.javascript); - } + this.setOptions(options); + options = this.options; + var id = this.id = options.id || this.instance; + var container = document.id(options.container); -}); + Swiff.CallBacks[this.instance] = {}; -Element.Properties.load = { + var params = options.params, vars = options.vars, callBacks = options.callBacks; + var properties = $extend({height: options.height, width: options.width}, options.properties); - set: function(options){ - var load = this.retrieve('load'); - if (load) load.cancel(); - return this.eliminate('load').store('load:options', $extend({data: this, link: 'cancel', update: this, method: 'get'}, options)); - }, + var self = this; - get: function(options){ - if (options || ! this.retrieve('load')){ - if (options || !this.retrieve('load:options')) this.set('load', options); - this.store('load', new Request.HTML(this.retrieve('load:options'))); + for (var callBack in callBacks){ + Swiff.CallBacks[this.instance][callBack] = (function(option){ + return function(){ + return option.apply(self.object, arguments); + }; + })(callBacks[callBack]); + vars[callBack] = 'Swiff.CallBacks.' + this.instance + '.' + callBack; } - return this.retrieve('load'); - } -}; - -Element.implement({ + params.flashVars = Hash.toQueryString(vars); + if (Browser.Engine.trident){ + properties.classid = 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'; + params.movie = path; + } else { + properties.type = 'application/x-shockwave-flash'; + properties.data = path; + } + var build = ''; + } + build += ''; + this.object = ((container) ? container.empty() : new Element('div')).set('html', build).firstChild; + }, - load: function(){ - this.get('load').send(Array.link(arguments, {data: Object.type, url: String.type})); + replaces: function(element){ + element = document.id(element, true); + element.parentNode.replaceChild(this.toElement(), element); return this; - } - -}); - - -/* ---- - -script: Request.JSON.js - -description: Extends the basic Request Class with additional methods for sending and receiving JSON data. - -license: MIT-style license. - -requires: -- /Request JSON - -provides: [Request.HTML] - -... -*/ - -Request.JSON = new Class({ - - Extends: Request, - - options: { - secure: true }, - initialize: function(options){ - this.parent(options); - this.headers.extend({'Accept': 'application/json', 'X-Request': 'JSON'}); + inject: function(element){ + document.id(element, true).appendChild(this.toElement()); + return this; }, - success: function(text){ - this.response.json = JSON.decode(text, this.options.secure); - this.onSuccess(this.response.json, text); + remote: function(){ + return Swiff.remote.apply(Swiff, [this.toElement()].extend(arguments)); } }); + +Swiff.CallBacks = {}; + +Swiff.remote = function(obj, fn){ + var rs = obj.CallFunction('' + __flash__argumentsToXML(arguments, 2) + ''); + return eval(rs); +}; +