Skip to content
Permalink
Browse files

Add prototype.js to new apps in javascripts/ #885

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@956 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
dhh committed Mar 21, 2005
1 parent 5d5f0ba commit ecc0e2889614ffdd22dac0c8346da0018b3c7b12
@@ -20,7 +20,7 @@ PUBLIC_DIRS = %w( images javascripts stylesheets )
TEST_DIRS = %w( fixtures unit functional mocks mocks/development mocks/test )

LOG_FILES = %w( server.log development.log test.log production.log )
HTML_FILES = %w( 404.html 500.html index.html favicon.ico )
HTML_FILES = %w( 404.html 500.html index.html favicon.ico javascripts/prototype.js )
BIN_FILES = %w( generate destroy breakpointer console server update runner )

VENDOR_LIBS = %w( actionpack activerecord actionmailer activesupport actionwebservice railties )
@@ -0,0 +1,326 @@
/* Prototype: an object-oriented Javascript library, version 1.0.0
* (c) 2005 Sam Stephenson <sam@conio.net>
*
* Prototype is freely distributable under the terms of an MIT-style license.
* For details, see http://prototype.conio.net/
*/


Prototype = {
Version = '1.0.0'
}

Class = {
create: function() {
return function() {
this.initialize.apply(this, arguments);
}
}
}

Abstract = new Object();

Object.prototype.extend = function(object) {
for (property in object) {
this[property] = object[property];
}
return this;
}

Function.prototype.bind = function(object) {
var method = this;
return function() {
method.apply(object, arguments);
}
}

Function.prototype.bindAsEventListener = function(object) {
var method = this;
return function(event) {
method.call(object, event || window.event);
}
}

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;
}
}

Toggle = {
visibility: function() {
for (i = 0; i < arguments.length; i++) {
var element = $(arguments[i]);
element.style.display =
(element.style.display == 'none' ? '' : 'none');
}
}
}

/*--------------------------------------------------------------------------*/

function $() {
var elements = new Array();

for (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;
}

function getElementsByClassName(className, element) {
var children = (element || document).getElementsByTagName('*');
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;
}

/*--------------------------------------------------------------------------*/

Ajax = {
getTransport: function() {
return Try.these(
function() {return new ActiveXObject('Msxml2.XMLHTTP')},
function() {return new ActiveXObject('Microsoft.XMLHTTP')},
function() {return new XMLHttpRequest()}
) || false;
},

emptyFunction: function() {}
}

Ajax.Base = function() {};
Ajax.Base.prototype = {
setOptions: function(options) {
this.options = {
method: 'post',
asynchronous: true,
parameters: ''
}.extend(options || {});
}
}

Ajax.Request = Class.create();
Ajax.Request.Events =
['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'];

Ajax.Request.prototype = (new Ajax.Base()).extend({
initialize: function(url, options) {
this.transport = Ajax.getTransport();
this.setOptions(options);

try {
if (this.options.method == 'get')
url += '?' + this.options.parameters + '&_=';

this.transport.open(this.options.method, url, true);

if (this.options.asynchronous)
this.transport.onreadystatechange = this.onStateChange.bind(this);

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.parameters);

} catch (e) {
}
},

onStateChange: function() {
var event = Ajax.Request.Events[this.transport.readyState];
(this.options['on' + event] || Ajax.emptyFunction)(this.transport);
}
});

Ajax.Updater = Class.create();
Ajax.Updater.prototype = (new Ajax.Base()).extend({
initialize: function(container, url, options) {
this.container = $(container);
this.setOptions(options);

if (this.options.asynchronous) {
this.onComplete = this.options.onComplete;
this.options.onComplete = this.updateContent.bind(this);
}

this.request = new Ajax.Request(url, this.options);

if (!this.options.asynchronous)
this.updateContent();
},

updateContent: function() {
this.container.innerHTML = this.request.transport.responseText;
if (this.onComplete) this.onComplete(this.request);
}
});

Field = {
clear: function() {
for (i = 0; i < arguments.length; i++)
$(arguments[i]).value = '';
},

focus: function(element) {
$(element).focus();
},

present: function() {
for (i = 0; i < arguments.length; i++)
if ($(arguments[i]).value == '') return false;
return true;
}
}

/*--------------------------------------------------------------------------*/

Form = {
serialize: function(form) {
var elements = Form.getElements($(form));
var queryComponents = new Array();

for (var i = 0; i < elements.length; i++) {
var queryComponent = Form.Element.serialize(elements[i]);
if (queryComponent)
queryComponents.push(queryComponent);
}

return queryComponents.join('&');
},

getElements: function(form) {
form = $(form);
var elements = new Array();

for (tagName in Form.Element.Serializers) {
var tagElements = form.getElementsByTagName(tagName);
for (var j = 0; j < tagElements.length; j++)
elements.push(tagElements[j]);
}
return elements;
}
}

Form.Element = {
serialize: function(element) {
element = $(element);
var method = element.tagName.toLowerCase();
var parameter = Form.Element.Serializers[method](element);

if (parameter)
return encodeURIComponent(parameter[0]) + '=' +
encodeURIComponent(parameter[1]);
},

getValue: function(element) {
element = $(element);
var method = element.tagName.toLowerCase();
var parameter = Form.Element.Serializers[method](element);

if (parameter)
return parameter[1];
}
}

Form.Element.Serializers = {
input: function(element) {
switch (element.type.toLowerCase()) {
case 'hidden':
case 'text':
return Form.Element.Serializers.textarea(element);
case 'checkbox':
case 'radio':
return Form.Element.Serializers.inputSelector(element);
}
},

inputSelector: function(element) {
if (element.checked)
return [element.name, element.value];
},

textarea: function(element) {
return [element.name, element.value];
},

select: function(element) {
var index = element.selectedIndex;
return [element.name, element.options[index].value];
}
}

/*--------------------------------------------------------------------------*/

Abstract.TimedObserver = function() {}
Abstract.TimedObserver.prototype = {
initialize: function(element, frequency, callback) {
this.frequency = frequency;
this.element = $(element);
this.callback = callback;

this.lastValue = this.getValue();
this.registerCallback();
},

registerCallback: function() {
setTimeout(this.onTimerEvent.bind(this), this.frequency * 1000);
},

onTimerEvent: function() {
var value = this.getValue();
if (this.lastValue != value) {
this.callback(this.element, value);
this.lastValue = value;
}

this.registerCallback();
}
}

Form.Element.Observer = Class.create();
Form.Element.Observer.prototype = (new Abstract.TimedObserver()).extend({
getValue: function() {
return Form.Element.getValue(this.element);
}
});

Form.Observer = Class.create();
Form.Observer.prototype = (new Abstract.TimedObserver()).extend({
getValue: function() {
return Form.serialize(this.element);
}
});

@@ -64,6 +64,9 @@ def manifest

m.template "html/favicon.ico", "public/favicon.ico"

# Javascripts
m.file "javascripts/prototype.js", "public/javascripts/prototype.js"

# Docs
m.file "doc/README_FOR_APP", "doc/README_FOR_APP"

0 comments on commit ecc0e28

Please sign in to comment.
You can’t perform that action at this time.