Browse files

Update to Prototype 1.3.0; closes #1441, #1348

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1530 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent c9f2389 commit 264a7e7c46339e07be98a9f49db31d83c09e7733 @sstephenson sstephenson committed Jun 27, 2005
Showing with 435 additions and 389 deletions.
  1. +435 −389 actionpack/lib/action_view/helpers/javascripts/prototype.js
View
824 actionpack/lib/action_view/helpers/javascripts/prototype.js
@@ -1,4 +1,4 @@
-/* Prototype: an object-oriented Javascript library, version 1.2.1
+/* Prototype: an object-oriented Javascript library, version 1.3.0
* (c) 2005 Sam Stephenson <sam@conio.net>
*
* THIS FILE IS AUTOMATICALLY GENERATED. When sending patches, please diff
@@ -11,7 +11,8 @@
/*--------------------------------------------------------------------------*/
var Prototype = {
- Version: '1.2.1'
+ Version: '1.3.0',
+ emptyFunction: function() {}
}
var Class = {
@@ -32,16 +33,16 @@ Object.prototype.extend = function(object) {
}
Function.prototype.bind = function(object) {
- var method = this;
+ var __method = this;
return function() {
- method.apply(object, arguments);
+ __method.apply(object, arguments);
}
}
Function.prototype.bindAsEventListener = function(object) {
- var method = this;
+ var __method = this;
return function(event) {
- method.call(object, event || window.event);
+ __method.call(object, event || window.event);
}
}
@@ -54,15 +55,15 @@ Number.prototype.toColorPart = function() {
var Try = {
these: function() {
var returnValue;
-
+
for (var i = 0; i < arguments.length; i++) {
var lambda = arguments[i];
try {
returnValue = lambda();
break;
} catch (e) {}
}
-
+
return returnValue;
}
}
@@ -75,14 +76,14 @@ PeriodicalExecuter.prototype = {
this.callback = callback;
this.frequency = frequency;
this.currentlyExecuting = false;
-
+
this.registerCallback();
},
-
+
registerCallback: function() {
- setTimeout(this.onTimerEvent.bind(this), this.frequency * 1000);
+ setInterval(this.onTimerEvent.bind(this), this.frequency * 1000);
},
-
+
onTimerEvent: function() {
if (!this.currentlyExecuting) {
try {
@@ -92,32 +93,28 @@ PeriodicalExecuter.prototype = {
this.currentlyExecuting = false;
}
}
-
- this.registerCallback();
}
}
/*--------------------------------------------------------------------------*/
function $() {
var elements = new Array();
-
+
for (var i = 0; i < arguments.length; i++) {
var element = arguments[i];
if (typeof element == 'string')
element = document.getElementById(element);
if (arguments.length == 1)
return element;
-
+
elements.push(element);
}
-
+
return elements;
}
-/*--------------------------------------------------------------------------*/
-
if (!Array.prototype.push) {
Array.prototype.push = function() {
var startLength = this.length;
@@ -138,15 +135,25 @@ if (!Function.prototype.apply) {
parameterStrings[i] = 'x[' + i + ']';
object.__apply__ = this;
- var result = eval('obj.__apply__(' +
+ var result = eval('object.__apply__(' +
parameterStrings[i].join(', ') + ')');
object.__apply__ = null;
return result;
}
}
-/*--------------------------------------------------------------------------*/
+String.prototype.extend({
+ stripTags: function() {
+ return this.replace(/<\/?[^>]+>/gi, '');
+ },
+
+ unescapeHTML: function() {
+ return this.replace(/&lt;/gi,"<").replace(/&gt;/gi,">").
+ replace(/&quot;/gi,'"').replace(/&apos;/gi,"'").
+ replace(/&amp;/gi,"&").replace(/[\n\r]/gi,"");
+ }
+});
var Ajax = {
getTransport: function() {
@@ -155,9 +162,7 @@ var Ajax = {
function() {return new ActiveXObject('Microsoft.XMLHTTP')},
function() {return new XMLHttpRequest()}
) || false;
- },
-
- emptyFunction: function() {}
+ }
}
Ajax.Base = function() {};
@@ -191,22 +196,33 @@ Ajax.Request.prototype = (new Ajax.Base()).extend({
this.transport.onreadystatechange = this.onStateChange.bind(this);
setTimeout((function() {this.respondToReadyState(1)}).bind(this), 10);
}
-
- this.transport.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
- this.transport.setRequestHeader('X-Prototype-Version', Prototype.Version);
-
- if (this.options.method == 'post') {
- this.transport.setRequestHeader('Connection', 'close');
- this.transport.setRequestHeader('Content-type',
- 'application/x-www-form-urlencoded');
- }
- this.transport.send(this.options.method == 'post' ?
- this.options.parameters + '&_=' : null);
+ this.setRequestHeaders();
+
+ var sendData = this.options.postBody ? this.options.postBody
+ : this.options.parameters ? this.options.parameters + '&_='
+ : null;
+
+ this.transport.send(this.options.method == 'post' ? sendData : null);
} catch (e) {
}
},
+
+ setRequestHeaders: function() {
+ var requestHeaders = ['X-Requested-With', 'XMLHttpRequest',
+ 'X-Prototype-Version', Prototype.Version];
+
+ if (this.options.method == 'post')
+ requestHeaders.push('Connection', 'close',
+ 'Content-type', 'application/x-www-form-urlencoded');
+
+ if (this.options.requestHeaders)
+ requestHeaders.push.apply(requestHeaders, this.options.requestHeaders);
+
+ for (var i = 0; i < requestHeaders.length; i += 2)
+ this.transport.setRequestHeader(requestHeader[i], requestHeader[i+1]);
+ },
onStateChange: function() {
var readyState = this.transport.readyState;
@@ -216,7 +232,7 @@ Ajax.Request.prototype = (new Ajax.Base()).extend({
respondToReadyState: function(readyState) {
var event = Ajax.Request.Events[readyState];
- (this.options['on' + event] || Ajax.emptyFunction)(this.transport);
+ (this.options['on' + event] || Prototype.emptyFunction)(this.transport);
}
});
@@ -238,21 +254,198 @@ Ajax.Updater.prototype = (new Ajax.Base()).extend({
},
updateContent: function() {
- if (this.options.insertion) {
- new this.options.insertion(this.container,
+ if (this.request.transport.status == 200) {
+ if (this.options.insertion) {
+ new this.options.insertion(this.container,
this.request.transport.responseText);
- } else {
- this.container.innerHTML = this.request.transport.responseText;
- }
+ } else {
+ this.container.innerHTML = this.request.transport.responseText;
+ }
+ }
if (this.onComplete) {
- setTimeout((function() {this.onComplete(this.request)}).bind(this), 10);
+ setTimeout((function() {this.onComplete(
+ this.request.transport)}).bind(this), 10);
+ }
+ }
+});
+
+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;
+}
+
+/*--------------------------------------------------------------------------*/
+
+if (!window.Element) {
+ var Element = new Object();
+}
+
+Element.extend({
+ toggle: function() {
+ for (var i = 0; i < arguments.length; i++) {
+ var element = $(arguments[i]);
+ element.style.display =
+ (element.style.display == 'none' ? '' : 'none');
+ }
+ },
+
+ hide: function() {
+ for (var i = 0; i < arguments.length; i++) {
+ var element = $(arguments[i]);
+ element.style.display = 'none';
+ }
+ },
+
+ show: function() {
+ for (var i = 0; i < arguments.length; i++) {
+ var element = $(arguments[i]);
+ element.style.display = '';
+ }
+ },
+
+ remove: function(element) {
+ element = $(element);
+ element.parentNode.removeChild(element);
+ },
+
+ getHeight: function(element) {
+ element = $(element);
+ return element.offsetHeight;
+ },
+
+ 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;
+ },
+
+ addClassName: function(element, className) {
+ element = $(element);
+ Element.removeClassName(element, className);
+ element.className += ' ' + 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;
+ },
+
+ // removes whitespace-only text node children
+ cleanWhitespace: function(element) {
+ var 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);
}
}
});
+var Toggle = new Object();
+Toggle.display = Element.toggle;
+
/*--------------------------------------------------------------------------*/
+Abstract.Insertion = function(adjacency) {
+ this.adjacency = adjacency;
+}
+
+Abstract.Insertion.prototype = {
+ initialize: function(element, content) {
+ this.element = $(element);
+ this.content = content;
+
+ if (this.adjacency && this.element.insertAdjacentHTML) {
+ this.element.insertAdjacentHTML(this.adjacency, this.content);
+ } else {
+ this.range = this.element.ownerDocument.createRange();
+ if (this.initializeRange) this.initializeRange();
+ this.fragment = this.range.createContextualFragment(this.content);
+ this.insertContent();
+ }
+ }
+}
+
+var Insertion = new Object();
+
+Insertion.Before = Class.create();
+Insertion.Before.prototype = (new Abstract.Insertion('beforeBegin')).extend({
+ initializeRange: function() {
+ this.range.setStartBefore(this.element);
+ },
+
+ insertContent: function() {
+ this.element.parentNode.insertBefore(this.fragment, this.element);
+ }
+});
+
+Insertion.Top = Class.create();
+Insertion.Top.prototype = (new Abstract.Insertion('afterBegin')).extend({
+ initializeRange: function() {
+ this.range.selectNodeContents(this.element);
+ this.range.collapse(true);
+ },
+
+ insertContent: function() {
+ this.element.insertBefore(this.fragment, this.element.firstChild);
+ }
+});
+
+Insertion.Bottom = Class.create();
+Insertion.Bottom.prototype = (new Abstract.Insertion('beforeEnd')).extend({
+ initializeRange: function() {
+ this.range.selectNodeContents(this.element);
+ this.range.collapse(this.element);
+ },
+
+ insertContent: function() {
+ this.element.appendChild(this.fragment);
+ }
+});
+
+Insertion.After = Class.create();
+Insertion.After.prototype = (new Abstract.Insertion('afterEnd')).extend({
+ initializeRange: function() {
+ this.range.setStartAfter(this.element);
+ },
+
+ insertContent: function() {
+ this.element.parentNode.insertBefore(this.fragment,
+ this.element.nextSibling);
+ }
+});
+
var Field = {
clear: function() {
for (var i = 0; i < arguments.length; i++)
@@ -296,7 +489,7 @@ var Form = {
},
getElements: function(form) {
- form = $(form);
+ var form = $(form);
var elements = new Array();
for (tagName in Form.Element.Serializers) {
@@ -307,6 +500,25 @@ var Form = {
return elements;
},
+ getInputs: function(form, typeName, name) {
+ var form = $(form);
+ var inputs = form.getElementsByTagName('input');
+
+ if (!typeName && !name)
+ return inputs;
+
+ var matchingInputs = new Array();
+ for (var i = 0; i < inputs.length; i++) {
+ var input = inputs[i];
+ if ((typeName && input.type != typeName) ||
+ (name && input.name != name))
+ continue;
+ matchingInputs.push(input);
+ }
+
+ return matchingInputs;
+ },
+
disable: function(form) {
var elements = Form.getElements(form);
for (var i = 0; i < elements.length; i++) {
@@ -316,8 +528,16 @@ var Form = {
}
},
+ enable: function(form) {
+ var elements = Form.getElements(form);
+ for (var i = 0; i < elements.length; i++) {
+ var element = elements[i];
+ element.disabled = '';
+ }
+ },
+
focusFirstElement: function(form) {
- form = $(form);
+ var form = $(form);
var elements = Form.getElements(form);
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
@@ -335,7 +555,7 @@ var Form = {
Form.Element = {
serialize: function(element) {
- element = $(element);
+ var element = $(element);
var method = element.tagName.toLowerCase();
var parameter = Form.Element.Serializers[method](element);
@@ -345,7 +565,7 @@ Form.Element = {
},
getValue: function(element) {
- element = $(element);
+ var element = $(element);
var method = element.tagName.toLowerCase();
var parameter = Form.Element.Serializers[method](element);
@@ -357,11 +577,14 @@ Form.Element = {
Form.Element.Serializers = {
input: function(element) {
switch (element.type.toLowerCase()) {
+ case 'submit':
+ case 'hidden':
+ case 'password':
+ case 'text':
+ return Form.Element.Serializers.textarea(element);
case 'checkbox':
case 'radio':
return Form.Element.Serializers.inputSelector(element);
- default:
- return Form.Element.Serializers.textarea(element);
}
return false;
},
@@ -376,9 +599,20 @@ Form.Element.Serializers = {
},
select: function(element) {
- var index = element.selectedIndex;
- var value = element.options[index].value || element.options[index].text;
- return [element.name, (index >= 0) ? value : ''];
+ var value = '';
+ if (element.type == 'select-one') {
+ var index = element.selectedIndex;
+ if (index >= 0)
+ value = element.options[index].value || element.options[index].text;
+ } else {
+ value = new Array();
+ for (var i = 0; i < element.length; i++) {
+ var opt = element.options[i];
+ if (opt.selected)
+ value.push(opt.value || opt.text);
+ }
+ }
+ return [element.name, value];
}
}
@@ -400,7 +634,7 @@ Abstract.TimedObserver.prototype = {
},
registerCallback: function() {
- setTimeout(this.onTimerEvent.bind(this), this.frequency * 1000);
+ setInterval(this.onTimerEvent.bind(this), this.frequency * 1000);
},
onTimerEvent: function() {
@@ -409,8 +643,6 @@ Abstract.TimedObserver.prototype = {
this.callback(this.element, value);
this.lastValue = value;
}
-
- this.registerCallback();
}
}
@@ -428,164 +660,84 @@ Form.Observer.prototype = (new Abstract.TimedObserver()).extend({
}
});
-
-/*--------------------------------------------------------------------------*/
-
-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;
-}
-
/*--------------------------------------------------------------------------*/
-var Element = {
- toggle: function() {
- for (var i = 0; i < arguments.length; i++) {
- var element = $(arguments[i]);
- element.style.display =
- (element.style.display == 'none' ? '' : 'none');
- }
- },
-
- hide: function() {
- for (var i = 0; i < arguments.length; i++) {
- var element = $(arguments[i]);
- element.style.display = 'none';
- }
- },
-
- show: function() {
- for (var i = 0; i < arguments.length; i++) {
- var element = $(arguments[i]);
- element.style.display = '';
- }
- },
-
- remove: function(element) {
- element = $(element);
- element.parentNode.removeChild(element);
- },
-
- getHeight: function(element) {
- element = $(element);
- return element.offsetHeight;
- }
-}
-
-var Toggle = new Object();
-Toggle.display = Element.toggle;
-
-/*--------------------------------------------------------------------------*/
-
-Abstract.Insertion = function(adjacency) {
- this.adjacency = adjacency;
-}
-
-Abstract.Insertion.prototype = {
- initialize: function(element, content) {
- this.element = $(element);
- this.content = content;
+Abstract.EventObserver = function() {}
+Abstract.EventObserver.prototype = {
+ initialize: function(element, callback) {
+ this.element = $(element);
+ this.callback = callback;
- if (this.adjacency && this.element.insertAdjacentHTML) {
- this.element.insertAdjacentHTML(this.adjacency, this.content);
- } else {
- this.range = this.element.ownerDocument.createRange();
- if (this.initializeRange) this.initializeRange();
- this.fragment = this.range.createContextualFragment(this.content);
- this.insertContent();
+ this.lastValue = this.getValue();
+ if (this.element.tagName.toLowerCase() == 'form')
+ this.registerFormCallbacks();
+ else
+ this.registerCallback(this.element);
+ },
+
+ onElementEvent: function() {
+ var value = this.getValue();
+ if (this.lastValue != value) {
+ this.callback(this.element, value);
+ this.lastValue = value;
}
- }
-}
-
-var Insertion = new Object();
-
-Insertion.Before = Class.create();
-Insertion.Before.prototype = (new Abstract.Insertion('beforeBegin')).extend({
- initializeRange: function() {
- this.range.setStartBefore(this.element);
},
- insertContent: function() {
- this.element.parentNode.insertBefore(this.fragment, this.element);
- }
-});
-
-Insertion.Top = Class.create();
-Insertion.Top.prototype = (new Abstract.Insertion('afterBegin')).extend({
- initializeRange: function() {
- this.range.selectNodeContents(this.element);
- this.range.collapse(true);
+ registerFormCallbacks: function() {
+ var elements = Form.getElements(this.element);
+ for (var i = 0; i < elements.length; i++)
+ this.registerCallback(elements[i]);
},
- insertContent: function() {
- this.element.insertBefore(this.fragment, this.element.firstChild);
+ registerCallback: function(element) {
+ if (element.type) {
+ switch (element.type.toLowerCase()) {
+ case 'checkbox':
+ case 'radio':
+ element.target = this;
+ element.prev_onclick = element.onclick || Prototype.emptyFunction;
+ element.onclick = function() {
+ this.prev_onclick();
+ this.target.onElementEvent();
+ }
+ break;
+ case 'password':
+ case 'text':
+ case 'textarea':
+ case 'select-one':
+ case 'select-multiple':
+ element.target = this;
+ element.prev_onchange = element.onchange || Prototype.emptyFunction;
+ element.onchange = function() {
+ this.prev_onchange();
+ this.target.onElementEvent();
+ }
+ break;
+ }
+ }
}
-});
+}
-Insertion.Bottom = Class.create();
-Insertion.Bottom.prototype = (new Abstract.Insertion('beforeEnd')).extend({
- initializeRange: function() {
- this.range.selectNodeContents(this.element);
- this.range.collapse(this.element);
- },
-
- insertContent: function() {
- this.element.appendChild(this.fragment);
+Form.Element.EventObserver = Class.create();
+Form.Element.EventObserver.prototype = (new Abstract.EventObserver()).extend({
+ getValue: function() {
+ return Form.Element.getValue(this.element);
}
});
-Insertion.After = Class.create();
-Insertion.After.prototype = (new Abstract.Insertion('afterEnd')).extend({
- initializeRange: function() {
- this.range.setStartAfter(this.element);
- },
-
- insertContent: function() {
- this.element.parentNode.insertBefore(this.fragment,
- this.element.nextSibling);
+Form.EventObserver = Class.create();
+Form.EventObserver.prototype = (new Abstract.EventObserver()).extend({
+ getValue: function() {
+ return Form.serialize(this.element);
}
});
-// === Prototype Extension ====================================================
-
-// Copyright (c) 2005 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
-//
-// Element.Class part Copyright (c) 2005 by Rick Olson
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-
-var Event = {
+if (!window.Event) {
+ var Event = new Object();
+}
+
+Event.extend({
KEY_BACKSPACE: 8,
KEY_TAB: 9,
KEY_RETURN: 13,
@@ -599,247 +751,141 @@ var Event = {
element: function(event) {
return event.srcElement || event.currentTarget;
},
-
+
isLeftClick: function(event) {
return (((event.which) && (event.which == 1)) ||
((event.button) && (event.button == 1)));
},
-
+
pointerX: function(event) {
- return event.pageX || (event.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft));
+ return event.pageX || (event.clientX +
+ (document.documentElement.scrollLeft || document.body.scrollLeft));
},
-
+
pointerY: function(event) {
- return event.pageY || (event.clientY + (document.documentElement.scrollTop || document.body.scrollTop));
+ return event.pageY || (event.clientY +
+ (document.documentElement.scrollTop || document.body.scrollTop));
},
stop: function(event) {
- if(event.preventDefault)
- { event.preventDefault(); event.stopPropagation(); }
- else
+ if (event.preventDefault) {
+ event.preventDefault();
+ event.stopPropagation();
+ } else {
event.returnValue = false;
+ }
},
// find the first node with the given tagName, starting from the
- // node the event was triggered on, traverses the DOM upwards
+ // node the event was triggered on; traverses the DOM upwards
findElement: function(event, tagName) {
element = Event.element(event);
- while(element.tagName.toUpperCase() != tagName.toUpperCase() && element.parentNode)
- element = element.parentNode;
+ while (element.tagName.toUpperCase() != tagName.toUpperCase()
+ && element.parentNode)
+ element = element.parentNode;
return element;
},
-
- observe: function(element, name, observer) {
- if(name=='keypress') {
- if(navigator.appVersion.indexOf('AppleWebKit')>0)
- { $(element).addEventListener("keydown",observer,false); return; }
- if($(element).addEventListener) $(element).addEventListener("keypress",observer,false)
- else if($(element).attachEvent) $(element).attachEvent("onkeydown",observer);
- } else {
- if($(element).addEventListener) $(element).addEventListener(name,observer,false)
- else if($(element).attachEvent) $(element).attachEvent("on" + name,observer);
- }
- }
-}
-/*--------------------------------------------------------------------------*/
-
-// removes whitespace-only text node children
-// needed to make Gecko-based browsers happy
-Element.cleanWhitespace = function(element) {
- var 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);
- }
-}
-
-Element.collectTextNodesIgnoreClass = function(element, ignoreclass) {
- var children = $(element).childNodes;
- var text = "";
- var classtest = new RegExp("^([^ ]+ )*" + ignoreclass+ "( [^ ]+)*$","i");
-
- for (var i = 0; i < children.length; i++) {
- if(children[i].nodeType==3) {
- text+=children[i].nodeValue;
- } else {
- if((!children[i].className.match(classtest)) && children[i].hasChildNodes())
- text += Element.collectTextNodesIgnoreClass(children[i], ignoreclass);
- }
- }
-
- return text;
-}
-
-/*--------------------------------------------------------------------------*/
-
-Text = {
- stripTags: function(htmlstr) {
- return htmlstr.replace(/<\/?[^>]+>/gi,"");
- },
- decodeHTML: function(htmlstr) {
- return htmlstr.replace(/&lt;/gi,"<").replace(/&gt;/gi,">").replace(/&quot;/gi,'"').replace(/&apos;/gi,"'").replace(/&amp;/gi,"&").replace(/[\n\r]/gi,"");
- }
-}
-
-/*--------------------------------------------------------------------------*/
-
-Element.Class = {
- // Element.toggleClass(element, className) toggles the class being on/off
- // Element.toggleClass(element, className1, className2) toggles between both classes,
- // defaulting to className1 if neither exist
- toggle: function(element, className) {
- if(Element.Class.has(element, className)) {
- Element.Class.remove(element, className);
- if(arguments.length == 3) Element.Class.add(element, arguments[2]);
- } else {
- Element.Class.add(element, className);
- if(arguments.length == 3) Element.Class.remove(element, arguments[2]);
- }
- },
-
- // gets space-delimited classnames of an element as an array
- get: function(element) {
- element = $(element);
- return element.className.split(' ');
- },
-
- // functions adapted from original functions by Gavin Kistner
- remove: function(element) {
- element = $(element);
- var regEx;
- for(var i = 1; i < arguments.length; i++) {
- regEx = new RegExp("^" + arguments[i] + "\\b\\s*|\\s*\\b" + arguments[i] + "\\b", 'g');
- element.className = element.className.replace(regEx, '')
- }
- },
-
- add: function(element) {
- element = $(element);
- for(var i = 1; i < arguments.length; i++) {
- Element.Class.remove(element, arguments[i]);
- element.className += (element.className.length > 0 ? ' ' : '') + arguments[i];
- }
- },
-
- // returns true if all given classes exist in said element
- has: function(element) {
- element = $(element);
- if(!element || !element.className) return false;
- var regEx;
- for(var i = 1; i < arguments.length; i++) {
- regEx = new RegExp("\\b" + arguments[i] + "\\b");
- if(!regEx.test(element.className)) return false;
+ observe: function(element, name, observer) {
+ var element = $(element);
+ if (name == 'keypress') {
+ if (navigator.appVersion.indexOf('AppleWebKit') > 0) {
+ element.addEventListener('keydown', observer, false);
+ return;
}
- return true;
- },
-
- // expects arrays of strings and/or strings as optional paramters
- // Element.Class.has_any(element, ['classA','classB','classC'], 'classD')
- has_any: function(element) {
- element = $(element);
- if(!element || !element.className) return false;
- var regEx;
- for(var i = 1; i < arguments.length; i++) {
- if((typeof arguments[i] == 'object') &&
- (arguments[i].constructor == Array)) {
- for(var j = 0; j < arguments[i].length; j++) {
- regEx = new RegExp("\\b" + arguments[i][j] + "\\b");
- if(regEx.test(element.className)) return true;
- }
- } else {
- regEx = new RegExp("\\b" + arguments[i] + "\\b");
- if(regEx.test(element.className)) return true;
- }
+ if (element.addEventListener) {
+ element.addEventListener('keypress', observer, false);
+ } else if (element.attachEvent) {
+ element.attachEvent('onkeydown', observer);
}
- return false;
- },
-
- childrenWith: function(element, className) {
- var children = $(element).getElementsByTagName('*');
- var elements = new Array();
-
- for (var i = 0; i < children.length; i++) {
- if (Element.Class.has(children[i], className)) {
- elements.push(children[i]);
- break;
- }
+ } else {
+ if (element.addEventListener) {
+ element.addEventListener(name, observer, false);
+ } else if (element.attachEvent) {
+ element.attachEvent('on' + name, observer);
}
-
- return elements;
}
-}
-
-/*--------------------------------------------------------------------------*/
+ }
+});
var Position = {
- // must be called before calling within_including_scrolloffset, every time the page is scrolled
+ // must be called before calling withinIncludingScrolloffset, every time the
+ // page is scrolled
prepare: function() {
- this.deltaX = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0;
- this.deltaY = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
- this.include_scroll_offsets = true;
- },
-
- real_offset: function(element) {
- var valueT = 0; var valueL = 0;
+ this.deltaX = window.pageXOffset
+ || document.documentElement.scrollLeft
+ || document.body.scrollLeft
+ || 0;
+ this.deltaY = window.pageYOffset
+ || document.documentElement.scrollTop
+ || document.body.scrollTop
+ || 0;
+ this.includeScrollOffsets = true;
+ },
+
+ realOffset: function(element) {
+ var valueT = 0, valueL = 0;
do {
- valueT += element.scrollTop || 0;
+ valueT += element.scrollTop || 0;
valueL += element.scrollLeft || 0;
element = element.parentNode;
- } while(element);
+ } while (element);
return [valueL, valueT];
},
-
- cumulative_offset: function(element) {
- var valueT = 0; var valueL = 0;
+
+ cumulativeOffset: function(element) {
+ var valueT = 0, valueL = 0;
do {
- valueT += element.offsetTop || 0;
+ valueT += element.offsetTop || 0;
valueL += element.offsetLeft || 0;
element = element.offsetParent;
- } while(element);
+ } while (element);
return [valueL, valueT];
},
-
+
// caches x/y coordinate pair to use with overlap
within: function(element, x, y) {
- if(this.include_scroll_offsets)
- return this.within_including_scrolloffsets(element, x, y);
+ if (this.includeScrollOffsets)
+ return this.withinIncludingScrolloffsets(element, x, y);
this.xcomp = x;
this.ycomp = y;
- this.offset = this.cumulative_offset(element);
+ this.offset = this.cumulativeOffset(element);
- return (y>=this.offset[1] &&
- y<this.offset[1]+element.offsetHeight &&
- x>=this.offset[0] &&
- x<this.offset[0]+element.offsetWidth);
+ return (y >= this.offset[1] &&
+ y < this.offset[1] + element.offsetHeight &&
+ x >= this.offset[0] &&
+ x < this.offset[0] + element.offsetWidth);
},
-
- within_including_scrolloffsets: function(element, x, y) {
- var offsetcache = this.real_offset(element);
- this.offset = this.cumulative_offset(element);
+
+ withinIncludingScrolloffsets: function(element, x, y) {
+ var offsetcache = this.realOffset(element);
+ this.offset = this.cumulativeOffset(element);
this.xcomp = x + offsetcache[0] - this.deltaX + this.offset[0];
this.ycomp = y + offsetcache[1] - this.deltaY + this.offset[1];
-
- return (this.ycomp>=this.offset[1] &&
- this.ycomp<this.offset[1]+element.offsetHeight &&
- this.xcomp>=this.offset[0] &&
- this.xcomp<this.offset[0]+element.offsetWidth);
+
+ return (this.ycomp >= this.offset[1] &&
+ this.ycomp < this.offset[1] + element.offsetHeight &&
+ this.xcomp >= this.offset[0] &&
+ this.xcomp < this.offset[0] + element.offsetWidth);
},
-
+
// within must be called directly before
overlap: function(mode, element) {
- if(!mode) return 0;
- if(mode == 'vertical')
- return ((this.offset[1]+element.offsetHeight)-this.ycomp) / element.offsetHeight;
- if(mode == 'horizontal')
- return ((this.offset[0]+element.offsetWidth)-this.xcomp) / element.offsetWidth;
+ if (!mode) return 0;
+ if (mode == 'vertical')
+ return ((this.offset[1] + element.offsetHeight) - this.ycomp) /
+ element.offsetHeight;
+ if (mode == 'horizontal')
+ return ((this.offset[0] + element.offsetWidth) - this.xcomp) /
+ element.offsetWidth;
},
-
+
clone: function(source, target) {
- $(target).style.top = $(source).style.top;
- $(target).style.left = $(source).style.left;
- $(target).style.width = $(source).offsetWidth + "px";
- $(target).style.height = $(source).offsetHeight + "px";
+ source = $(source);
+ target = $(target);
+ target.style.top = source.style.top;
+ target.style.left = source.style.left;
+ target.style.width = source.offsetWidth + 'px';
+ target.style.height = source.offsetHeight + 'px';
}
-}
+}

0 comments on commit 264a7e7

Please sign in to comment.