Commit 36bb78bd4e22a0a4ad748875ed497ef5c5b55f9a

Added scriptaculous 1.8.1

Commit diff

Rakefile

 
1require 'rubygems'
2require 'hoe'
3require 'spec/rake/spectask'
4require './lib/kipling/version.rb'
5
6Hoe.new('kipling', Kipling::VERSION) do |p|
7 p.developer('Aslak Hellesøy', 'aslak.hellesoy@gmail.com')
8end
9
10Spec::Rake::SpecTask.new do |t|
11 t.spec_opts = ['--colour']
12 t.rcov = true
13 t.rcov_opts = ['--exclude', 'spec', '--text-report']
14end
15
16desc 'Run stories'
17task :stories do
18 ruby "stories/console.rb -- --colour --diff"
19end
20
21task :clear_rcov do
22 rm 'coverage.data' rescue nil
23end
24
25Rake::Task[:default].prerequisites.clear
26task :default => [:clear_rcov, :spec, :stories]
1require 'rubygems'
2require 'hoe'
3require 'spec/rake/spectask'
4require './lib/kipling/version.rb'
5
6Hoe.new('kipling', Kipling::VERSION) do |p|
7 p.developer('Aslak Hellesøy', 'aslak.hellesoy@gmail.com')
8end
9
10Spec::Rake::SpecTask.new do |t|
11 t.spec_opts = ['--colour']
12 t.rcov = true
13 t.rcov_opts = ['--exclude', 'spec', '--text-report']
14end
15
16desc 'Run stories'
17task :stories do
18 ruby "stories/console.rb -- --colour --diff"
19end
20
21task :clear_rcov do
22 rm 'coverage.data' rescue nil
23end
24
25Rake::Task[:default].prerequisites.clear
26task :default => [:clear_rcov, :spec, :stories]
toggle raw diff

lib/kipling/camping/javascripts/builder.js

 
1// script.aculo.us builder.js v1.8.1, Thu Jan 03 22:07:12 -0500 2008
2
3// Copyright (c) 2005-2007 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
4//
5// script.aculo.us is freely distributable under the terms of an MIT-style license.
6// For details, see the script.aculo.us web site: http://script.aculo.us/
7
8var Builder = {
9 NODEMAP: {
10 AREA: 'map',
11 CAPTION: 'table',
12 COL: 'table',
13 COLGROUP: 'table',
14 LEGEND: 'fieldset',
15 OPTGROUP: 'select',
16 OPTION: 'select',
17 PARAM: 'object',
18 TBODY: 'table',
19 TD: 'table',
20 TFOOT: 'table',
21 TH: 'table',
22 THEAD: 'table',
23 TR: 'table'
24 },
25 // note: For Firefox < 1.5, OPTION and OPTGROUP tags are currently broken,
26 // due to a Firefox bug
27 node: function(elementName) {
28 elementName = elementName.toUpperCase();
29
30 // try innerHTML approach
31 var parentTag = this.NODEMAP[elementName] || 'div';
32 var parentElement = document.createElement(parentTag);
33 try { // prevent IE "feature": http://dev.rubyonrails.org/ticket/2707
34 parentElement.innerHTML = "<" + elementName + "></" + elementName + ">";
35 } catch(e) {}
36 var element = parentElement.firstChild || null;
37
38 // see if browser added wrapping tags
39 if(element && (element.tagName.toUpperCase() != elementName))
40 element = element.getElementsByTagName(elementName)[0];
41
42 // fallback to createElement approach
43 if(!element) element = document.createElement(elementName);
44
45 // abort if nothing could be created
46 if(!element) return;
47
48 // attributes (or text)
49 if(arguments[1])
50 if(this._isStringOrNumber(arguments[1]) ||
51 (arguments[1] instanceof Array) ||
52 arguments[1].tagName) {
53 this._children(element, arguments[1]);
54 } else {
55 var attrs = this._attributes(arguments[1]);
56 if(attrs.length) {
57 try { // prevent IE "feature": http://dev.rubyonrails.org/ticket/2707
58 parentElement.innerHTML = "<" +elementName + " " +
59 attrs + "></" + elementName + ">";
60 } catch(e) {}
61 element = parentElement.firstChild || null;
62 // workaround firefox 1.0.X bug
63 if(!element) {
64 element = document.createElement(elementName);
65 for(attr in arguments[1])
66 element[attr == 'class' ? 'className' : attr] = arguments[1][attr];
67 }
68 if(element.tagName.toUpperCase() != elementName)
69 element = parentElement.getElementsByTagName(elementName)[0];
70 }
71 }
72
73 // text, or array of children
74 if(arguments[2])
75 this._children(element, arguments[2]);
76
77 return element;
78 },
79 _text: function(text) {
80 return document.createTextNode(text);
81 },
82
83 ATTR_MAP: {
84 'className': 'class',
85 'htmlFor': 'for'
86 },
87
88 _attributes: function(attributes) {
89 var attrs = [];
90 for(attribute in attributes)
91 attrs.push((attribute in this.ATTR_MAP ? this.ATTR_MAP[attribute] : attribute) +
92 '="' + attributes[attribute].toString().escapeHTML().gsub(/"/,'&quot;') + '"');
93 return attrs.join(" ");
94 },
95 _children: function(element, children) {
96 if(children.tagName) {
97 element.appendChild(children);
98 return;
99 }
100 if(typeof children=='object') { // array can hold nodes and text
101 children.flatten().each( function(e) {
102 if(typeof e=='object')
103 element.appendChild(e)
104 else
105 if(Builder._isStringOrNumber(e))
106 element.appendChild(Builder._text(e));
107 });
108 } else
109 if(Builder._isStringOrNumber(children))
110 element.appendChild(Builder._text(children));
111 },
112 _isStringOrNumber: function(param) {
113 return(typeof param=='string' || typeof param=='number');
114 },
115 build: function(html) {
116 var element = this.node('div');
117 $(element).update(html.strip());
118 return element.down();
119 },
120 dump: function(scope) {
121 if(typeof scope != 'object' && typeof scope != 'function') scope = window; //global scope
122
123 var tags = ("A ABBR ACRONYM ADDRESS APPLET AREA B BASE BASEFONT BDO BIG BLOCKQUOTE BODY " +
124 "BR BUTTON CAPTION CENTER CITE CODE COL COLGROUP DD DEL DFN DIR DIV DL DT EM FIELDSET " +
125 "FONT FORM FRAME FRAMESET H1 H2 H3 H4 H5 H6 HEAD HR HTML I IFRAME IMG INPUT INS ISINDEX "+
126 "KBD LABEL LEGEND LI LINK MAP MENU META NOFRAMES NOSCRIPT OBJECT OL OPTGROUP OPTION P "+
127 "PARAM PRE Q S SAMP SCRIPT SELECT SMALL SPAN STRIKE STRONG STYLE SUB SUP TABLE TBODY TD "+
128 "TEXTAREA TFOOT TH THEAD TITLE TR TT U UL VAR").split(/\s+/);
129
130 tags.each( function(tag){
131 scope[tag] = function() {
132 return Builder.node.apply(Builder, [tag].concat($A(arguments)));
133 }
134 });
135 }
136}
toggle raw diff

lib/kipling/camping/javascripts/controls.js

 
1// script.aculo.us controls.js v1.8.1, Thu Jan 03 22:07:12 -0500 2008
2
3// Copyright (c) 2005-2007 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
4// (c) 2005-2007 Ivan Krstic (http://blogs.law.harvard.edu/ivan)
5// (c) 2005-2007 Jon Tirsen (http://www.tirsen.com)
6// Contributors:
7// Richard Livsey
8// Rahul Bhargava
9// Rob Wills
10//
11// script.aculo.us is freely distributable under the terms of an MIT-style license.
12// For details, see the script.aculo.us web site: http://script.aculo.us/
13
14// Autocompleter.Base handles all the autocompletion functionality
15// that's independent of the data source for autocompletion. This
16// includes drawing the autocompletion menu, observing keyboard
17// and mouse events, and similar.
18//
19// Specific autocompleters need to provide, at the very least,
20// a getUpdatedChoices function that will be invoked every time
21// the text inside the monitored textbox changes. This method
22// should get the text for which to provide autocompletion by
23// invoking this.getToken(), NOT by directly accessing
24// this.element.value. This is to allow incremental tokenized
25// autocompletion. Specific auto-completion logic (AJAX, etc)
26// belongs in getUpdatedChoices.
27//
28// Tokenized incremental autocompletion is enabled automatically
29// when an autocompleter is instantiated with the 'tokens' option
30// in the options parameter, e.g.:
31// new Ajax.Autocompleter('id','upd', '/url/', { tokens: ',' });
32// will incrementally autocomplete with a comma as the token.
33// Additionally, ',' in the above example can be replaced with
34// a token array, e.g. { tokens: [',', '\n'] } which
35// enables autocompletion on multiple tokens. This is most
36// useful when one of the tokens is \n (a newline), as it
37// allows smart autocompletion after linebreaks.
38
39if(typeof Effect == 'undefined')
40 throw("controls.js requires including script.aculo.us' effects.js library");
41
42var Autocompleter = { }
43Autocompleter.Base = Class.create({
44 baseInitialize: function(element, update, options) {
45 element = $(element)
46 this.element = element;
47 this.update = $(update);
48 this.hasFocus = false;
49 this.changed = false;
50 this.active = false;
51 this.index = 0;
52 this.entryCount = 0;
53 this.oldElementValue = this.element.value;
54
55 if(this.setOptions)
56 this.setOptions(options);
57 else
58 this.options = options || { };
59
60 this.options.paramName = this.options.paramName || this.element.name;
61 this.options.tokens = this.options.tokens || [];
62 this.options.frequency = this.options.frequency || 0.4;
63 this.options.minChars = this.options.minChars || 1;
64 this.options.onShow = this.options.onShow ||
65 function(element, update){
66 if(!update.style.position || update.style.position=='absolute') {
67 update.style.position = 'absolute';
68 Position.clone(element, update, {
69 setHeight: false,
70 offsetTop: element.offsetHeight
71 });
72 }
73 Effect.Appear(update,{duration:0.15});
74 };
75 this.options.onHide = this.options.onHide ||
76 function(element, update){ new Effect.Fade(update,{duration:0.15}) };
77
78 if(typeof(this.options.tokens) == 'string')
79 this.options.tokens = new Array(this.options.tokens);
80 // Force carriage returns as token delimiters anyway
81 if (!this.options.tokens.include('\n'))
82 this.options.tokens.push('\n');
83
84 this.observer = null;
85
86 this.element.setAttribute('autocomplete','off');
87
88 Element.hide(this.update);
89
90 Event.observe(this.element, 'blur', this.onBlur.bindAsEventListener(this));
91 Event.observe(this.element, 'keydown', this.onKeyPress.bindAsEventListener(this));
92 },
93
94 show: function() {
95 if(Element.getStyle(this.update, 'display')=='none') this.options.onShow(this.element, this.update);
96 if(!this.iefix &&
97 (Prototype.Browser.IE) &&
98 (Element.getStyle(this.update, 'position')=='absolute')) {
99 new Insertion.After(this.update,
100 '<iframe id="' + this.update.id + '_iefix" '+
101 'style="display:none;position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" ' +
102 'src="javascript:false;" frameborder="0" scrolling="no"></iframe>');
103 this.iefix = $(this.update.id+'_iefix');
104 }
105 if(this.iefix) setTimeout(this.fixIEOverlapping.bind(this), 50);
106 },
107
108 fixIEOverlapping: function() {
109 Position.clone(this.update, this.iefix, {setTop:(!this.update.style.height)});
110 this.iefix.style.zIndex = 1;
111 this.update.style.zIndex = 2;
112 Element.show(this.iefix);
113 },
114
115 hide: function() {
116 this.stopIndicator();
117 if(Element.getStyle(this.update, 'display')!='none') this.options.onHide(this.element, this.update);
118 if(this.iefix) Element.hide(this.iefix);
119 },
120
121 startIndicator: function() {
122 if(this.options.indicator) Element.show(this.options.indicator);
123 },
124
125 stopIndicator: function() {
126 if(this.options.indicator) Element.hide(this.options.indicator);
127 },
128
129 onKeyPress: function(event) {
130 if(this.active)
131 switch(event.keyCode) {
132 case Event.KEY_TAB:
133 case Event.KEY_RETURN:
134 this.selectEntry();
135 Event.stop(event);
136 case Event.KEY_ESC:
137 this.hide();
138 this.active = false;
139 Event.stop(event);
140 return;
141 case Event.KEY_LEFT:
142 case Event.KEY_RIGHT:
143 return;
144 case Event.KEY_UP:
145 this.markPrevious();
146 this.render();
147 Event.stop(event);
148 return;
149 case Event.KEY_DOWN:
150 this.markNext();
151 this.render();
152 Event.stop(event);
153 return;
154 }
155 else
156 if(event.keyCode==Event.KEY_TAB || event.keyCode==Event.KEY_RETURN ||
157 (Prototype.Browser.WebKit > 0 && event.keyCode == 0)) return;
158
159 this.changed = true;
160 this.hasFocus = true;
161
162 if(this.observer) clearTimeout(this.observer);
163 this.observer =
164 setTimeout(this.onObserverEvent.bind(this), this.options.frequency*1000);
165 },
166
167 activate: function() {
168 this.changed = false;
169 this.hasFocus = true;
170 this.getUpdatedChoices();
171 },
172
173 onHover: function(event) {
174 var element = Event.findElement(event, 'LI');
175 if(this.index != element.autocompleteIndex)
176 {
177 this.index = element.autocompleteIndex;
178 this.render();
179 }
180 Event.stop(event);
181 },
182
183 onClick: function(event) {
184 var element = Event.findElement(event, 'LI');
185 this.index = element.autocompleteIndex;
186 this.selectEntry();
187 this.hide();
188 },
189
190 onBlur: function(event) {
191 // needed to make click events working
192 setTimeout(this.hide.bind(this), 250);
193 this.hasFocus = false;
194 this.active = false;
195 },
196
197 render: function() {
198 if(this.entryCount > 0) {
199 for (var i = 0; i < this.entryCount; i++)
200 this.index==i ?
201 Element.addClassName(this.getEntry(i),"selected") :
202 Element.removeClassName(this.getEntry(i),"selected");
203 if(this.hasFocus) {
204 this.show();
205 this.active = true;
206 }
207 } else {
208 this.active = false;
209 this.hide();
210 }
211 },
212
213 markPrevious: function() {
214 if(this.index > 0) this.index--
215 else this.index = this.entryCount-1;
216 this.getEntry(this.index).scrollIntoView(true);
217 },
218
219 markNext: function() {
220 if(this.index < this.entryCount-1) this.index++
221 else this.index = 0;
222 this.getEntry(this.index).scrollIntoView(false);
223 },
224
225 getEntry: function(index) {
226 return this.update.firstChild.childNodes[index];
227 },
228
229 getCurrentEntry: function() {
230 return this.getEntry(this.index);
231 },
232
233 selectEntry: function() {
234 this.active = false;
235 this.updateElement(this.getCurrentEntry());
236 },
237
238 updateElement: function(selectedElement) {
239 if (this.options.updateElement) {
240 this.options.updateElement(selectedElement);
241 return;
242 }
243 var value = '';
244 if (this.options.select) {
245 var nodes = $(selectedElement).select('.' + this.options.select) || [];
246 if(nodes.length>0) value = Element.collectTextNodes(nodes[0], this.options.select);
247 } else
248 value = Element.collectTextNodesIgnoreClass(selectedElement, 'informal');
249
250 var bounds = this.getTokenBounds();
251 if (bounds[0] != -1) {
252 var newValue = this.element.value.substr(0, bounds[0]);
253 var whitespace = this.element.value.substr(bounds[0]).match(/^\s+/);
254 if (whitespace)
255 newValue += whitespace[0];
256 this.element.value = newValue + value + this.element.value.substr(bounds[1]);
257 } else {
258 this.element.value = value;
259 }
260 this.oldElementValue = this.element.value;
261 this.element.focus();
262
263 if (this.options.afterUpdateElement)
264 this.options.afterUpdateElement(this.element, selectedElement);
265 },
266
267 updateChoices: function(choices) {
268 if(!this.changed && this.hasFocus) {
269 this.update.innerHTML = choices;
270 Element.cleanWhitespace(this.update);
271 Element.cleanWhitespace(this.update.down());
272
273 if(this.update.firstChild && this.update.down().childNodes) {
274 this.entryCount =
275 this.update.down().childNodes.length;
276 for (var i = 0; i < this.entryCount; i++) {
277 var entry = this.getEntry(i);
278 entry.autocompleteIndex = i;
279 this.addObservers(entry);
280 }
281 } else {
282 this.entryCount = 0;
283 }
284
285 this.stopIndicator();
286 this.index = 0;
287
288 if(this.entryCount==1 && this.options.autoSelect) {
289 this.selectEntry();
290 this.hide();
291 } else {
292 this.render();
293 }
294 }
295 },
296
297 addObservers: function(element) {
298 Event.observe(element, "mouseover", this.onHover.bindAsEventListener(this));
299 Event.observe(element, "click", this.onClick.bindAsEventListener(this));
300 },
301
302 onObserverEvent: function() {
303 this.changed = false;
304 this.tokenBounds = null;
305 if(this.getToken().length>=this.options.minChars) {
306 this.getUpdatedChoices();
307 } else {
308 this.active = false;
309 this.hide();
310 }
311 this.oldElementValue = this.element.value;
312 },
313
314 getToken: function() {
315 var bounds = this.getTokenBounds();
316 return this.element.value.substring(bounds[0], bounds[1]).strip();
317 },
318
319 getTokenBounds: function() {
320 if (null != this.tokenBounds) return this.tokenBounds;
321 var value = this.element.value;
322 if (value.strip().empty()) return [-1, 0];
323 var diff = arguments.callee.getFirstDifferencePos(value, this.oldElementValue);
324 var offset = (diff == this.oldElementValue.length ? 1 : 0);
325 var prevTokenPos = -1, nextTokenPos = value.length;
326 var tp;
327 for (var index = 0, l = this.options.tokens.length; index < l; ++index) {
328 tp = value.lastIndexOf(this.options.tokens[index], diff + offset - 1);
329 if (tp > prevTokenPos) prevTokenPos = tp;
330 tp = value.indexOf(this.options.tokens[index], diff + offset);
331 if (-1 != tp && tp < nextTokenPos) nextTokenPos = tp;
332 }
333 return (this.tokenBounds = [prevTokenPos + 1, nextTokenPos]);
334 }
335});
336
337Autocompleter.Base.prototype.getTokenBounds.getFirstDifferencePos = function(newS, oldS) {
338 var boundary = Math.min(newS.length, oldS.length);
339 for (var index = 0; index < boundary; ++index)
340 if (newS[index] != oldS[index])
341 return index;
342 return boundary;
343};
344
345Ajax.Autocompleter = Class.create(Autocompleter.Base, {
346 initialize: function(element, update, url, options) {
347 this.baseInitialize(element, update, options);
348 this.options.asynchronous = true;
349 this.options.onComplete = this.onComplete.bind(this);
350 this.options.defaultParams = this.options.parameters || null;
351 this.url = url;
352 },
353
354 getUpdatedChoices: function() {
355 this.startIndicator();
356
357 var entry = encodeURIComponent(this.options.paramName) + '=' +
358 encodeURIComponent(this.getToken());
359
360 this.options.parameters = this.options.callback ?
361 this.options.callback(this.element, entry) : entry;
362
363 if(this.options.defaultParams)
364 this.options.parameters += '&' + this.options.defaultParams;
365
366 new Ajax.Request(this.url, this.options);
367 },
368
369 onComplete: function(request) {
370 this.updateChoices(request.responseText);
371 }
372});
373
374// The local array autocompleter. Used when you'd prefer to
375// inject an array of autocompletion options into the page, rather
376// than sending out Ajax queries, which can be quite slow sometimes.
377//
378// The constructor takes four parameters. The first two are, as usual,
379// the id of the monitored textbox, and id of the autocompletion menu.
380// The third is the array you want to autocomplete from, and the fourth
381// is the options block.
382//
383// Extra local autocompletion options:
384// - choices - How many autocompletion choices to offer
385//
386// - partialSearch - If false, the autocompleter will match entered
387// text only at the beginning of strings in the
388// autocomplete array. Defaults to true, which will
389// match text at the beginning of any *word* in the
390// strings in the autocomplete array. If you want to
391// search anywhere in the string, additionally set
392// the option fullSearch to true (default: off).
393//
394// - fullSsearch - Search anywhere in autocomplete array strings.
395//
396// - partialChars - How many characters to enter before triggering
397// a partial match (unlike minChars, which defines
398// how many characters are required to do any match
399// at all). Defaults to 2.
400//
401// - ignoreCase - Whether to ignore case when autocompleting.
402// Defaults to true.
403//
404// It's possible to pass in a custom function as the 'selector'
405// option, if you prefer to write your own autocompletion logic.
406// In that case, the other options above will not apply unless
407// you support them.
408
409Autocompleter.Local = Class.create(Autocompleter.Base, {
410 initialize: function(element, update, array, options) {
411 this.baseInitialize(element, update, options);
412 this.options.array = array;
413 },
414
415 getUpdatedChoices: function() {
416 this.updateChoices(this.options.selector(this));
417 },
418
419 setOptions: function(options) {
420 this.options = Object.extend({
421 choices: 10,
422 partialSearch: true,
423 partialChars: 2,
424 ignoreCase: true,
425 fullSearch: false,
426 selector: function(instance) {
427 var ret = []; // Beginning matches
428 var partial = []; // Inside matches
429 var entry = instance.getToken();
430 var count = 0;
431
432 for (var i = 0; i < instance.options.array.length &&
433 ret.length < instance.options.choices ; i++) {
434
435 var elem = instance.options.array[i];
436 var foundPos = instance.options.ignoreCase ?
437 elem.toLowerCase().indexOf(entry.toLowerCase()) :
438 elem.indexOf(entry);
439
440 while (foundPos != -1) {
441 if (foundPos == 0 && elem.length != entry.length) {
442 ret.push("<li><strong>" + elem.substr(0, entry.length) + "</strong>" +
443 elem.substr(entry.length) + "</li>");
444 break;
445 } else if (entry.length >= instance.options.partialChars &&
446 instance.options.partialSearch && foundPos != -1) {
447 if (instance.options.fullSearch || /\s/.test(elem.substr(foundPos-1,1))) {
448 partial.push("<li>" + elem.substr(0, foundPos) + "<strong>" +
449 elem.substr(foundPos, entry.length) + "</strong>" + elem.substr(
450 foundPos + entry.length) + "</li>");
451 break;
452 }
453 }
454
455 foundPos = instance.options.ignoreCase ?
456 elem.toLowerCase().indexOf(entry.toLowerCase(), foundPos + 1) :
457 elem.indexOf(entry, foundPos + 1);
458
459 }
460 }
461 if (partial.length)
462 ret = ret.concat(partial.slice(0, instance.options.choices - ret.length))
463 return "<ul>" + ret.join('') + "</ul>";
464 }
465 }, options || { });
466 }
467});
468
469// AJAX in-place editor and collection editor
470// Full rewrite by Christophe Porteneuve <tdd@tddsworld.com> (April 2007).
471
472// Use this if you notice weird scrolling problems on some browsers,
473// the DOM might be a bit confused when this gets called so do this
474// waits 1 ms (with setTimeout) until it does the activation
475Field.scrollFreeActivate = function(field) {
476 setTimeout(function() {
477 Field.activate(field);
478 }, 1);
479}
480
481Ajax.InPlaceEditor = Class.create({
482 initialize: function(element, url, options) {
483 this.url = url;
484 this.element = element = $(element);
485 this.prepareOptions();
486 this._controls = { };
487 arguments.callee.dealWithDeprecatedOptions(options); // DEPRECATION LAYER!!!
488 Object.extend(this.options, options || { });
489 if (!this.options.formId && this.element.id) {
490 this.options.formId = this.element.id + '-inplaceeditor';
491 if ($(this.options.formId))
492 this.options.formId = '';
493 }
494 if (this.options.externalControl)
495 this.options.externalControl = $(this.options.externalControl);
496 if (!this.options.externalControl)
497 this.options.externalControlOnly = false;
498 this._originalBackground = this.element.getStyle('background-color') || 'transparent';
499 this.element.title = this.options.clickToEditText;
500 this._boundCancelHandler = this.handleFormCancellation.bind(this);
501 this._boundComplete = (this.options.onComple