Skip to content

Commit ecc0e28

Browse files
author
David Heinemeier Hansson
committed
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
1 parent 5d5f0ba commit ecc0e28

File tree

3 files changed

+330
-1
lines changed

3 files changed

+330
-1
lines changed

railties/Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ PUBLIC_DIRS = %w( images javascripts stylesheets )
2020
TEST_DIRS = %w( fixtures unit functional mocks mocks/development mocks/test )
2121

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

2626
VENDOR_LIBS = %w( actionpack activerecord actionmailer activesupport actionwebservice railties )
Lines changed: 326 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,326 @@
1+
/* Prototype: an object-oriented Javascript library, version 1.0.0
2+
* (c) 2005 Sam Stephenson <[email protected]>
3+
*
4+
* Prototype is freely distributable under the terms of an MIT-style license.
5+
* For details, see http://prototype.conio.net/
6+
*/
7+
8+
9+
Prototype = {
10+
Version = '1.0.0'
11+
}
12+
13+
Class = {
14+
create: function() {
15+
return function() {
16+
this.initialize.apply(this, arguments);
17+
}
18+
}
19+
}
20+
21+
Abstract = new Object();
22+
23+
Object.prototype.extend = function(object) {
24+
for (property in object) {
25+
this[property] = object[property];
26+
}
27+
return this;
28+
}
29+
30+
Function.prototype.bind = function(object) {
31+
var method = this;
32+
return function() {
33+
method.apply(object, arguments);
34+
}
35+
}
36+
37+
Function.prototype.bindAsEventListener = function(object) {
38+
var method = this;
39+
return function(event) {
40+
method.call(object, event || window.event);
41+
}
42+
}
43+
44+
Try = {
45+
these: function() {
46+
var returnValue;
47+
48+
for (var i = 0; i < arguments.length; i++) {
49+
var lambda = arguments[i];
50+
try {
51+
returnValue = lambda();
52+
break;
53+
} catch (e) {}
54+
}
55+
56+
return returnValue;
57+
}
58+
}
59+
60+
Toggle = {
61+
visibility: function() {
62+
for (i = 0; i < arguments.length; i++) {
63+
var element = $(arguments[i]);
64+
element.style.display =
65+
(element.style.display == 'none' ? '' : 'none');
66+
}
67+
}
68+
}
69+
70+
/*--------------------------------------------------------------------------*/
71+
72+
function $() {
73+
var elements = new Array();
74+
75+
for (i = 0; i < arguments.length; i++) {
76+
var element = arguments[i];
77+
if (typeof element == 'string')
78+
element = document.getElementById(element);
79+
80+
if (arguments.length == 1)
81+
return element;
82+
83+
elements.push(element);
84+
}
85+
86+
return elements;
87+
}
88+
89+
function getElementsByClassName(className, element) {
90+
var children = (element || document).getElementsByTagName('*');
91+
var elements = new Array();
92+
93+
for (var i = 0; i < children.length; i++) {
94+
var child = children[i];
95+
var classNames = child.className.split(' ');
96+
for (var j = 0; j < classNames.length; j++) {
97+
if (classNames[j] == className) {
98+
elements.push(child);
99+
break;
100+
}
101+
}
102+
}
103+
104+
return elements;
105+
}
106+
107+
/*--------------------------------------------------------------------------*/
108+
109+
Ajax = {
110+
getTransport: function() {
111+
return Try.these(
112+
function() {return new ActiveXObject('Msxml2.XMLHTTP')},
113+
function() {return new ActiveXObject('Microsoft.XMLHTTP')},
114+
function() {return new XMLHttpRequest()}
115+
) || false;
116+
},
117+
118+
emptyFunction: function() {}
119+
}
120+
121+
Ajax.Base = function() {};
122+
Ajax.Base.prototype = {
123+
setOptions: function(options) {
124+
this.options = {
125+
method: 'post',
126+
asynchronous: true,
127+
parameters: ''
128+
}.extend(options || {});
129+
}
130+
}
131+
132+
Ajax.Request = Class.create();
133+
Ajax.Request.Events =
134+
['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'];
135+
136+
Ajax.Request.prototype = (new Ajax.Base()).extend({
137+
initialize: function(url, options) {
138+
this.transport = Ajax.getTransport();
139+
this.setOptions(options);
140+
141+
try {
142+
if (this.options.method == 'get')
143+
url += '?' + this.options.parameters + '&_=';
144+
145+
this.transport.open(this.options.method, url, true);
146+
147+
if (this.options.asynchronous)
148+
this.transport.onreadystatechange = this.onStateChange.bind(this);
149+
150+
if (this.options.method == 'post') {
151+
this.transport.setRequestHeader('Connection', 'close');
152+
this.transport.setRequestHeader('Content-type',
153+
'application/x-www-form-urlencoded');
154+
}
155+
156+
this.transport.send(this.options.parameters);
157+
158+
} catch (e) {
159+
}
160+
},
161+
162+
onStateChange: function() {
163+
var event = Ajax.Request.Events[this.transport.readyState];
164+
(this.options['on' + event] || Ajax.emptyFunction)(this.transport);
165+
}
166+
});
167+
168+
Ajax.Updater = Class.create();
169+
Ajax.Updater.prototype = (new Ajax.Base()).extend({
170+
initialize: function(container, url, options) {
171+
this.container = $(container);
172+
this.setOptions(options);
173+
174+
if (this.options.asynchronous) {
175+
this.onComplete = this.options.onComplete;
176+
this.options.onComplete = this.updateContent.bind(this);
177+
}
178+
179+
this.request = new Ajax.Request(url, this.options);
180+
181+
if (!this.options.asynchronous)
182+
this.updateContent();
183+
},
184+
185+
updateContent: function() {
186+
this.container.innerHTML = this.request.transport.responseText;
187+
if (this.onComplete) this.onComplete(this.request);
188+
}
189+
});
190+
191+
Field = {
192+
clear: function() {
193+
for (i = 0; i < arguments.length; i++)
194+
$(arguments[i]).value = '';
195+
},
196+
197+
focus: function(element) {
198+
$(element).focus();
199+
},
200+
201+
present: function() {
202+
for (i = 0; i < arguments.length; i++)
203+
if ($(arguments[i]).value == '') return false;
204+
return true;
205+
}
206+
}
207+
208+
/*--------------------------------------------------------------------------*/
209+
210+
Form = {
211+
serialize: function(form) {
212+
var elements = Form.getElements($(form));
213+
var queryComponents = new Array();
214+
215+
for (var i = 0; i < elements.length; i++) {
216+
var queryComponent = Form.Element.serialize(elements[i]);
217+
if (queryComponent)
218+
queryComponents.push(queryComponent);
219+
}
220+
221+
return queryComponents.join('&');
222+
},
223+
224+
getElements: function(form) {
225+
form = $(form);
226+
var elements = new Array();
227+
228+
for (tagName in Form.Element.Serializers) {
229+
var tagElements = form.getElementsByTagName(tagName);
230+
for (var j = 0; j < tagElements.length; j++)
231+
elements.push(tagElements[j]);
232+
}
233+
return elements;
234+
}
235+
}
236+
237+
Form.Element = {
238+
serialize: function(element) {
239+
element = $(element);
240+
var method = element.tagName.toLowerCase();
241+
var parameter = Form.Element.Serializers[method](element);
242+
243+
if (parameter)
244+
return encodeURIComponent(parameter[0]) + '=' +
245+
encodeURIComponent(parameter[1]);
246+
},
247+
248+
getValue: function(element) {
249+
element = $(element);
250+
var method = element.tagName.toLowerCase();
251+
var parameter = Form.Element.Serializers[method](element);
252+
253+
if (parameter)
254+
return parameter[1];
255+
}
256+
}
257+
258+
Form.Element.Serializers = {
259+
input: function(element) {
260+
switch (element.type.toLowerCase()) {
261+
case 'hidden':
262+
case 'text':
263+
return Form.Element.Serializers.textarea(element);
264+
case 'checkbox':
265+
case 'radio':
266+
return Form.Element.Serializers.inputSelector(element);
267+
}
268+
},
269+
270+
inputSelector: function(element) {
271+
if (element.checked)
272+
return [element.name, element.value];
273+
},
274+
275+
textarea: function(element) {
276+
return [element.name, element.value];
277+
},
278+
279+
select: function(element) {
280+
var index = element.selectedIndex;
281+
return [element.name, element.options[index].value];
282+
}
283+
}
284+
285+
/*--------------------------------------------------------------------------*/
286+
287+
Abstract.TimedObserver = function() {}
288+
Abstract.TimedObserver.prototype = {
289+
initialize: function(element, frequency, callback) {
290+
this.frequency = frequency;
291+
this.element = $(element);
292+
this.callback = callback;
293+
294+
this.lastValue = this.getValue();
295+
this.registerCallback();
296+
},
297+
298+
registerCallback: function() {
299+
setTimeout(this.onTimerEvent.bind(this), this.frequency * 1000);
300+
},
301+
302+
onTimerEvent: function() {
303+
var value = this.getValue();
304+
if (this.lastValue != value) {
305+
this.callback(this.element, value);
306+
this.lastValue = value;
307+
}
308+
309+
this.registerCallback();
310+
}
311+
}
312+
313+
Form.Element.Observer = Class.create();
314+
Form.Element.Observer.prototype = (new Abstract.TimedObserver()).extend({
315+
getValue: function() {
316+
return Form.Element.getValue(this.element);
317+
}
318+
});
319+
320+
Form.Observer = Class.create();
321+
Form.Observer.prototype = (new Abstract.TimedObserver()).extend({
322+
getValue: function() {
323+
return Form.serialize(this.element);
324+
}
325+
});
326+

railties/lib/rails_generator/generators/applications/app/app_generator.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ def manifest
6464

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

67+
# Javascripts
68+
m.file "javascripts/prototype.js", "public/javascripts/prototype.js"
69+
6770
# Docs
6871
m.file "doc/README_FOR_APP", "doc/README_FOR_APP"
6972

0 commit comments

Comments
 (0)