From 7d101d58400acaff232091485c8e140f0a37cbaf Mon Sep 17 00:00:00 2001 From: Thomas Fuchs Date: Sun, 9 Oct 2005 16:56:23 +0000 Subject: Update Prototype to V1.4.0_pre11, script.aculo.us to [2502] and fix the rails generator to include the new .js files [Thomas Fuchs] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2503 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../action_view/helpers/javascripts/prototype.js | 339 ++++++++++++++------- 1 file changed, 236 insertions(+), 103 deletions(-) (limited to 'actionpack/lib/action_view/helpers/javascripts/prototype.js') diff --git a/actionpack/lib/action_view/helpers/javascripts/prototype.js b/actionpack/lib/action_view/helpers/javascripts/prototype.js index c0ec65450d..9f5180315d 100644 --- a/actionpack/lib/action_view/helpers/javascripts/prototype.js +++ b/actionpack/lib/action_view/helpers/javascripts/prototype.js @@ -1,4 +1,4 @@ -/* Prototype JavaScript framework, version 1.4.0_pre7 +/* Prototype JavaScript framework, version 1.4.0_pre11 * (c) 2005 Sam Stephenson * * THIS FILE IS AUTOMATICALLY GENERATED. When sending patches, please diff @@ -11,7 +11,7 @@ /*--------------------------------------------------------------------------*/ var Prototype = { - Version: '1.4.0_pre7', + Version: '1.4.0_pre11', emptyFunction: function() {}, K: function(x) {return x} @@ -34,6 +34,17 @@ Object.extend = function(destination, source) { return destination; } +Object.inspect = function(object) { + try { + if (object == undefined) return 'undefined'; + if (object == null) return 'null'; + return object.inspect ? object.inspect() : object.toString(); + } catch (e) { + if (e instanceof RangeError) return '...'; + throw e; + } +} + Function.prototype.bind = function(object) { var __method = this; return function() { @@ -128,34 +139,6 @@ function $() { return elements; } -if (!Array.prototype.push) { - Array.prototype.push = function() { - var startLength = this.length; - for (var i = 0; i < arguments.length; i++) - this[startLength + i] = arguments[i]; - return this.length; - } -} - -if (!Function.prototype.apply) { - // Based on code from http://www.youngpup.net/ - Function.prototype.apply = function(object, parameters) { - var parameterStrings = new Array(); - if (!object) object = window; - if (!parameters) parameters = new Array(); - - for (var i = 0; i < parameters.length; i++) - parameterStrings[i] = 'parameters[' + i + ']'; - - object.__apply__ = this; - var result = eval('object.__apply__(' + - parameterStrings.join(', ') + ')'); - object.__apply__ = null; - - return result; - } -} - Object.extend(String.prototype, { stripTags: function() { return this.replace(/<\/?[^>]+>/gi, ''); @@ -171,27 +154,28 @@ Object.extend(String.prototype, { unescapeHTML: function() { var div = document.createElement('div'); div.innerHTML = this.stripTags(); - return div.childNodes[0].nodeValue; + return div.childNodes[0] ? div.childNodes[0].nodeValue : ''; }, - parseQuery: function() { - var str = this; - if (str.substring(0,1) == '?') { - str = this.substring(1); - } - var result = {}; - var pairs = str.split('&'); - for (var i = 0; i < pairs.length; i++) { - var pair = pairs[i].split('='); - result[pair[0]] = pair[1]; - } - return result; + toQueryParams: function() { + var pairs = this.match(/^\??(.*)$/)[1].split('&'); + return pairs.inject({}, function(params, pairString) { + var pair = pairString.split('='); + params[pair[0]] = pair[1]; + return params; + }); + }, + + inspect: function() { + return "'" + this.replace('\\', '\\\\').replace("'", '\\\'') + "'"; } }); +String.prototype.parseQuery = String.prototype.toQueryParams; -var _break = new Object(); -var _continue = new Object(); + +var $break = new Object(); +var $continue = new Object(); var Enumerable = { each: function(iterator) { @@ -201,11 +185,11 @@ var Enumerable = { try { iterator(value, index++); } catch (e) { - if (e != _continue) throw e; + if (e != $continue) throw e; } }); } catch (e) { - if (e != _break) throw e; + if (e != $break) throw e; } }, @@ -213,7 +197,7 @@ var Enumerable = { var result = true; this.each(function(value, index) { if (!(result &= (iterator || Prototype.K)(value, index))) - throw _break; + throw $break; }); return result; }, @@ -222,7 +206,7 @@ var Enumerable = { var result = true; this.each(function(value, index) { if (result &= (iterator || Prototype.K)(value, index)) - throw _break; + throw $break; }); return result; }, @@ -240,7 +224,7 @@ var Enumerable = { this.each(function(value, index) { if (iterator(value, index)) { result = value; - throw _break; + throw $break; } }); return result; @@ -270,7 +254,7 @@ var Enumerable = { this.each(function(value) { if (value == object) { found = true; - throw _break; + throw $break; } }); return found; @@ -359,6 +343,10 @@ var Enumerable = { iterator(value = collections.pluck(index)); return value; }); + }, + + inspect: function() { + return '#'; } } @@ -381,6 +369,8 @@ var $A = Array.from = function(iterable) { } } +Object.extend(Array.prototype, Enumerable); + Object.extend(Array.prototype, { _each: function(iterator) { for (var i = 0; i < this.length; i++) @@ -393,10 +383,80 @@ Object.extend(Array.prototype, { last: function() { return this[this.length - 1]; + }, + + compact: function() { + return this.select(function(value) { + return value != undefined || value != null; + }); + }, + + flatten: function() { + return this.inject([], function(array, value) { + return array.concat(value.constructor == Array ? + value.flatten() : [value]); + }); + }, + + without: function() { + var values = $A(arguments); + return this.select(function(value) { + return !values.include(value); + }); + }, + + inspect: function() { + return '[' + this.map(Object.inspect).join(', ') + ']'; } }); -Object.extend(Array.prototype, Enumerable); +var Hash = { + _each: function(iterator) { + for (key in this) { + var value = this[key]; + if (typeof value == 'function') continue; + + var pair = [key, value]; + pair.key = key; + pair.value = value; + iterator(pair); + } + }, + + keys: function() { + return this.pluck('key'); + }, + + values: function() { + return this.pluck('value'); + }, + + merge: function(hash) { + return $H(hash).inject($H(this), function(mergedHash, pair) { + mergedHash[pair.key] = pair.value; + return mergedHash; + }); + }, + + toQueryString: function() { + return this.map(function(pair) { + return pair.map(encodeURIComponent).join('='); + }).join('&'); + }, + + inspect: function() { + return '#'; + } +} + +function $H(object) { + var hash = Object.extend({}, object || {}); + Object.extend(hash, Enumerable); + Object.extend(hash, Hash); + return hash; +} var Range = Class.create(); Object.extend(Range.prototype, Enumerable); @@ -435,9 +495,51 @@ var Ajax = { function() {return new ActiveXObject('Microsoft.XMLHTTP')}, function() {return new XMLHttpRequest()} ) || false; - } + }, + + activeRequestCount: 0 } +Ajax.Responders = { + responders: [], + + _each: function(iterator) { + this.responders._each(iterator); + }, + + register: function(responderToAdd) { + if (!this.include(responderToAdd)) + this.responders.push(responderToAdd); + }, + + unregister: function(responderToRemove) { + this.responders = this.responders.without(responderToRemove); + }, + + dispatch: function(callback, request, transport, json) { + this.each(function(responder) { + if (responder[callback] && typeof responder[callback] == 'function') { + try { + responder[callback].apply(responder, [request, transport, json]); + } catch (e) { + } + } + }); + } +}; + +Object.extend(Ajax.Responders, Enumerable); + +Ajax.Responders.register({ + onCreate: function() { + Ajax.activeRequestCount++; + }, + + onComplete: function() { + Ajax.activeRequestCount--; + } +}); + Ajax.Base = function() {}; Ajax.Base.prototype = { setOptions: function(options) { @@ -476,10 +578,13 @@ Ajax.Request.prototype = Object.extend(new Ajax.Base(), { if (parameters.length > 0) parameters += '&_='; try { - if (this.options.method == 'get') - url += '?' + parameters; - - this.transport.open(this.options.method, url, + this.url = url; + if (this.options.method == 'get') + this.url += '?' + parameters; + + Ajax.Responders.dispatch('onCreate', this, this.transport); + + this.transport.open(this.options.method, this.url, this.options.asynchronous); if (this.options.asynchronous) { @@ -545,6 +650,7 @@ Ajax.Request.prototype = Object.extend(new Ajax.Base(), { || Prototype.emptyFunction)(transport, json); (this.options['on' + event] || Prototype.emptyFunction)(transport, json); + Ajax.Responders.dispatch('on' + event, this, transport, json); /* Avoid memory leak in MSIE: clean up the oncomplete event handler */ if (event == 'Complete') @@ -649,22 +755,13 @@ Ajax.PeriodicalUpdater.prototype = Object.extend(new Ajax.Base(), { } }); -document.getElementsByClassName = function(className) { - var children = document.getElementsByTagName('*') || document.all; - var elements = new Array(); - - for (var i = 0; i < children.length; i++) { - var child = children[i]; - var classNames = child.className.split(' '); - for (var j = 0; j < classNames.length; j++) { - if (classNames[j] == className) { - elements.push(child); - break; - } - } - } - - return elements; +document.getElementsByClassName = function(className, parentElement) { + var children = (document.body || $(parentElement)).getElementsByTagName('*'); + return $A(children).inject([], function(elements, child) { + if (Element.hasClassName(child, className)) + elements.push(child); + return elements; + }); } /*--------------------------------------------------------------------------*/ @@ -674,11 +771,14 @@ if (!window.Element) { } Object.extend(Element, { + visible: function(element) { + return $(element).style.display != 'none'; + }, + toggle: function() { for (var i = 0; i < arguments.length; i++) { var element = $(arguments[i]); - element.style.display = - (element.style.display == 'none' ? '' : 'none'); + Element[Element.visible(element) ? 'show' : 'hide'](element); } }, @@ -688,7 +788,7 @@ Object.extend(Element, { element.style.display = 'none'; } }, - + show: function() { for (var i = 0; i < arguments.length; i++) { var element = $(arguments[i]); @@ -700,54 +800,50 @@ Object.extend(Element, { element = $(element); element.parentNode.removeChild(element); }, - + getHeight: function(element) { element = $(element); return element.offsetHeight; }, + + classNames: function(element) { + return new Element.ClassNames(element); + }, hasClassName: function(element, className) { - element = $(element); - if (!element) - return; - var a = element.className.split(' '); - for (var i = 0; i < a.length; i++) { - if (a[i] == className) - return true; - } - return false; + if (!(element = $(element))) return; + return Element.classNames(element).include(className); }, addClassName: function(element, className) { - element = $(element); - Element.removeClassName(element, className); - element.className += ' ' + className; + if (!(element = $(element))) return; + return Element.classNames(element).add(className); }, removeClassName: function(element, className) { - element = $(element); - if (!element) - return; - var newClassName = ''; - var a = element.className.split(' '); - for (var i = 0; i < a.length; i++) { - if (a[i] != className) { - if (i > 0) - newClassName += ' '; - newClassName += a[i]; - } - } - element.className = newClassName; + if (!(element = $(element))) return; + return Element.classNames(element).remove(className); }, // removes whitespace-only text node children cleanWhitespace: function(element) { - var element = $(element); + element = $(element); for (var i = 0; i < element.childNodes.length; i++) { var node = element.childNodes[i]; if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) Element.remove(node); } + }, + + empty: function(element) { + return $(element).innerHTML.match(/^\s*$/); + }, + + scrollTo: function(element) { + element = $(element); + var x = element.x ? element.x : element.offsetLeft, + y = element.y ? element.y : element.offsetTop; + window.scrollTo(x, y); } }); @@ -840,6 +936,43 @@ Insertion.After.prototype = Object.extend(new Abstract.Insertion('afterEnd'), { } }); +/*--------------------------------------------------------------------------*/ + +Element.ClassNames = Class.create(); +Element.ClassNames.prototype = { + initialize: function(element) { + this.element = $(element); + }, + + _each: function(iterator) { + this.element.className.split(/\s+/).select(function(name) { + return name.length > 0; + })._each(iterator); + }, + + set: function(className) { + this.element.className = className; + }, + + add: function(classNameToAdd) { + if (this.include(classNameToAdd)) return; + this.set(this.toArray().concat(classNameToAdd).join(' ')); + }, + + remove: function(classNameToRemove) { + if (!this.include(classNameToRemove)) return; + this.set(this.select(function(className) { + return className != classNameToRemove; + })); + }, + + toString: function() { + return this.toArray().join(' '); + } +} + +Object.extend(Element.ClassNames.prototype, Enumerable); + var Field = { clear: function() { for (var i = 0; i < arguments.length; i++) -- cgit v1.2.3