nitro 0.19.0 → 0.20.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. data/CHANGELOG +187 -0
  2. data/INSTALL +5 -0
  3. data/README +11 -5
  4. data/doc/AUTHORS +11 -1
  5. data/doc/RELEASES +217 -0
  6. data/doc/tutorial.txt +1 -2
  7. data/lib/nitro.rb +9 -6
  8. data/lib/nitro/adapter/webrick.rb +13 -2
  9. data/lib/nitro/builder/form.rb +11 -9
  10. data/lib/nitro/builder/rss.rb +2 -2
  11. data/lib/nitro/builder/xhtml.rb +15 -0
  12. data/lib/nitro/caching.rb +15 -10
  13. data/lib/nitro/conf.rb +0 -5
  14. data/lib/nitro/controller.rb +118 -81
  15. data/lib/nitro/cookie.rb +6 -6
  16. data/lib/nitro/dispatcher.rb +62 -18
  17. data/lib/nitro/element.rb +4 -1
  18. data/lib/nitro/element/java_script.rb +15 -0
  19. data/lib/nitro/localization.rb +3 -4
  20. data/lib/nitro/markup.rb +4 -4
  21. data/lib/nitro/mixin/debug.rb +30 -0
  22. data/lib/nitro/mixin/helper.rb +14 -0
  23. data/lib/nitro/mixin/javascript.rb +137 -0
  24. data/lib/nitro/{ui → mixin}/pager.rb +110 -82
  25. data/lib/nitro/render.rb +20 -8
  26. data/lib/nitro/request.rb +6 -0
  27. data/lib/nitro/routing.rb +6 -5
  28. data/lib/nitro/runner.rb +21 -9
  29. data/lib/nitro/server.rb +95 -0
  30. data/lib/nitro/service.rb +0 -1
  31. data/lib/nitro/session.rb +4 -5
  32. data/lib/nitro/shaders.rb +2 -2
  33. data/lib/nitro/template.rb +1 -1
  34. data/lib/nitro/testing/assertions.rb +2 -4
  35. data/lib/nitro/testing/context.rb +4 -6
  36. data/proto/public/js/behaviour.js +254 -0
  37. data/proto/public/js/controls.js +446 -0
  38. data/proto/public/js/dragdrop.js +537 -0
  39. data/proto/public/js/effects.js +612 -0
  40. data/proto/public/js/prototype.js +644 -370
  41. data/proto/public/settings.xhtml +64 -0
  42. data/test/nitro/adapter/tc_cgi.rb +2 -2
  43. data/test/nitro/builder/tc_rss.rb +1 -1
  44. data/test/nitro/mixin/tc_pager.rb +35 -0
  45. data/test/nitro/tc_controller.rb +1 -1
  46. data/test/nitro/tc_cookie.rb +14 -0
  47. data/test/nitro/tc_dispatcher.rb +11 -6
  48. data/test/nitro/tc_server.rb +35 -0
  49. metadata +20 -15
  50. data/lib/nitro/builder/atom.rb +0 -74
  51. data/lib/nitro/part.rb +0 -22
  52. data/lib/nitro/simple.rb +0 -11
  53. data/lib/nitro/ui/popup.rb +0 -41
  54. data/lib/nitro/ui/tabs.rb +0 -25
  55. data/lib/nitro/uri.rb +0 -193
  56. data/test/nitro/builder/tc_atom.rb +0 -24
  57. data/test/nitro/tc_uri.rb +0 -97
  58. data/test/nitro/ui/tc_pager.rb +0 -49
data/lib/nitro/service.rb CHANGED
@@ -11,7 +11,6 @@ class Service < Nitro::Controller
11
11
 
12
12
  response.content_type = 'text/xml'
13
13
 
14
- puts '--', encode_response(method, res)
15
14
  print encode_response(method, res)
16
15
 
17
16
  return :stop
data/lib/nitro/session.rb CHANGED
@@ -22,11 +22,11 @@ class Session < Hash
22
22
 
23
23
  # Session id salt.
24
24
 
25
- cattr_accessor :session_id_salt, 'SALT'
25
+ setting :session_id_salt, :default => 'SALT', :doc => 'Session id salt'
26
26
 
27
27
  # The name of the cookie that stores the session id.
28
28
 
29
- cattr_accessor :cookie_name, 'nsid'
29
+ setting :cookie_name, :default => 'nsid', :doc => 'The name of the cookie that stores the session id'
30
30
 
31
31
  # The sessions store. By default sessions are
32
32
  # stored in memory.
@@ -62,9 +62,8 @@ class Session < Hash
62
62
  unless session
63
63
  # Create new session.
64
64
  session = Session.new(context)
65
- context.add_cookie(
66
- Cookie.new(Session.cookie_name, session.session_id)
67
- )
65
+ cookie = Cookie.new(Session.cookie_name, session.session_id)
66
+ context.add_cookie(cookie)
68
67
  Session.store[session.session_id] = session
69
68
  else
70
69
  # Access ('touch') the existing session.
data/lib/nitro/shaders.rb CHANGED
@@ -1,5 +1,3 @@
1
- # $Id: shaders.rb 56 2005-05-13 14:16:47Z gmosx $
2
-
3
1
  require 'nitro/template'
4
2
  require 'nitro/element'
5
3
 
@@ -204,3 +202,5 @@ class ElementsShader < Shader
204
202
  end
205
203
 
206
204
  end
205
+
206
+ # * George Moschovitis <gm@navel.gr>
@@ -105,7 +105,7 @@ module TemplateMixin
105
105
  # Compile time ruby code. This code is evaluated when
106
106
  # compiling the template and the result injected directly
107
107
  # into the result. Usefull for example to prevaluate
108
- # localization.
108
+ # localization. Just use the #[] marker instead of #{}.
109
109
 
110
110
  text.gsub!(/\#\[(.*?)\]/) do |match|
111
111
  eval($1)
@@ -1,7 +1,3 @@
1
- # * George Moschovitis <gm@navel.gr>
2
- # (c) 2004-2005 Navel, all rights reserved.
3
- # $Id: assertions.rb 1 2005-04-11 11:04:30Z gmosx $
4
-
5
1
  require 'test/unit'
6
2
  require 'test/unit/assertions'
7
3
  require 'rexml/document'
@@ -100,3 +96,5 @@ module Test::Unit::Assertions
100
96
  end
101
97
 
102
98
  end
99
+
100
+ # * George Moschovitis <gm@navel.gr>
@@ -1,7 +1,3 @@
1
- # * George Moschovitis <gm@navel.gr>
2
- # (c) 2004-2005 Navel, all rights reserved.
3
- # $Id: context.rb 1 2005-04-11 11:04:30Z gmosx $
4
-
5
1
  require 'test/unit'
6
2
  require 'test/unit/assertions'
7
3
  require 'rexml/document'
@@ -40,13 +36,15 @@ module Response
40
36
 
41
37
  end
42
38
 
43
- # Override the default Response implementation
39
+ # Override the default Context implementation
44
40
  # to include methods useful for testing.
45
41
 
46
42
  class Context
47
43
  def session
48
44
  @session || @session = {}
49
- end
45
+ end
50
46
  end
51
47
 
52
48
  end
49
+
50
+ # * George Moschovitis <gm@navel.gr>
@@ -0,0 +1,254 @@
1
+ /*
2
+ Behaviour v1.0 by Ben Nolan, June 2005. Based largely on the work
3
+ of Simon Willison (see comments by Simon below).
4
+
5
+ Description:
6
+
7
+ Uses css selectors to apply javascript behaviours to enable
8
+ unobtrusive javascript in html documents.
9
+
10
+ Usage:
11
+
12
+ var myrules = {
13
+ 'b.someclass' : function(element){
14
+ element.onclick = function(){
15
+ alert(this.innerHTML);
16
+ }
17
+ },
18
+ '#someid u' : function(element){
19
+ element.onmouseover = function(){
20
+ this.innerHTML = "BLAH!";
21
+ }
22
+ }
23
+ );
24
+
25
+ Behaviour.register(myrules);
26
+
27
+ // Call Behaviour.apply() to re-apply the rules (if you
28
+ // update the dom, etc).
29
+
30
+ License:
31
+
32
+ My stuff is BSD licensed. Not sure about Simon's.
33
+
34
+ More information:
35
+
36
+ http://ripcord.co.nz/behaviour/
37
+
38
+ */
39
+
40
+ var Behaviour = {
41
+ list : new Array,
42
+
43
+ register : function(sheet){
44
+ Behaviour.list.push(sheet);
45
+ },
46
+
47
+ start : function(){
48
+ Behaviour.addLoadEvent(function(){
49
+ Behaviour.apply();
50
+ });
51
+ },
52
+
53
+ apply : function(){
54
+ for (h=0;sheet=Behaviour.list[h];h++){
55
+ for (selector in sheet){
56
+ list = document.getElementsBySelector(selector);
57
+
58
+ if (!list){
59
+ continue;
60
+ }
61
+
62
+ for (i=0;element=list[i];i++){
63
+ sheet[selector](element);
64
+ }
65
+ }
66
+ }
67
+ },
68
+
69
+ addLoadEvent : function(func){
70
+ var oldonload = window.onload;
71
+
72
+ if (typeof window.onload != 'function') {
73
+ window.onload = func;
74
+ } else {
75
+ window.onload = function() {
76
+ oldonload();
77
+ func();
78
+ }
79
+ }
80
+ }
81
+ }
82
+
83
+ Behaviour.start();
84
+
85
+ /*
86
+ The following code is Copyright (C) Simon Willison 2004.
87
+
88
+ document.getElementsBySelector(selector)
89
+ - returns an array of element objects from the current document
90
+ matching the CSS selector. Selectors can contain element names,
91
+ class names and ids and can be nested. For example:
92
+
93
+ elements = document.getElementsBySelect('div#main p a.external')
94
+
95
+ Will return an array of all 'a' elements with 'external' in their
96
+ class attribute that are contained inside 'p' elements that are
97
+ contained inside the 'div' element which has id="main"
98
+
99
+ New in version 0.4: Support for CSS2 and CSS3 attribute selectors:
100
+ See http://www.w3.org/TR/css3-selectors/#attribute-selectors
101
+
102
+ Version 0.4 - Simon Willison, March 25th 2003
103
+ -- Works in Phoenix 0.5, Mozilla 1.3, Opera 7, Internet Explorer 6, Internet Explorer 5 on Windows
104
+ -- Opera 7 fails
105
+ */
106
+
107
+ function getAllChildren(e) {
108
+ // Returns all children of element. Workaround required for IE5/Windows. Ugh.
109
+ return e.all ? e.all : e.getElementsByTagName('*');
110
+ }
111
+
112
+ document.getElementsBySelector = function(selector) {
113
+ // Attempt to fail gracefully in lesser browsers
114
+ if (!document.getElementsByTagName) {
115
+ return new Array();
116
+ }
117
+ // Split selector in to tokens
118
+ var tokens = selector.split(' ');
119
+ var currentContext = new Array(document);
120
+ for (var i = 0; i < tokens.length; i++) {
121
+ token = tokens[i].replace(/^\s+/,'').replace(/\s+$/,'');;
122
+ if (token.indexOf('#') > -1) {
123
+ // Token is an ID selector
124
+ var bits = token.split('#');
125
+ var tagName = bits[0];
126
+ var id = bits[1];
127
+ var element = document.getElementById(id);
128
+ if (tagName && element.nodeName.toLowerCase() != tagName) {
129
+ // tag with that ID not found, return false
130
+ return new Array();
131
+ }
132
+ // Set currentContext to contain just this element
133
+ currentContext = new Array(element);
134
+ continue; // Skip to next token
135
+ }
136
+ if (token.indexOf('.') > -1) {
137
+ // Token contains a class selector
138
+ var bits = token.split('.');
139
+ var tagName = bits[0];
140
+ var className = bits[1];
141
+ if (!tagName) {
142
+ tagName = '*';
143
+ }
144
+ // Get elements matching tag, filter them for class selector
145
+ var found = new Array;
146
+ var foundCount = 0;
147
+ for (var h = 0; h < currentContext.length; h++) {
148
+ var elements;
149
+ if (tagName == '*') {
150
+ elements = getAllChildren(currentContext[h]);
151
+ } else {
152
+ elements = currentContext[h].getElementsByTagName(tagName);
153
+ }
154
+ for (var j = 0; j < elements.length; j++) {
155
+ found[foundCount++] = elements[j];
156
+ }
157
+ }
158
+ currentContext = new Array;
159
+ var currentContextIndex = 0;
160
+ for (var k = 0; k < found.length; k++) {
161
+ if (found[k].className && found[k].className.match(new RegExp('\\b'+className+'\\b'))) {
162
+ currentContext[currentContextIndex++] = found[k];
163
+ }
164
+ }
165
+ continue; // Skip to next token
166
+ }
167
+ // Code to deal with attribute selectors
168
+ if (token.match(/^(\w*)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/)) {
169
+ var tagName = RegExp.$1;
170
+ var attrName = RegExp.$2;
171
+ var attrOperator = RegExp.$3;
172
+ var attrValue = RegExp.$4;
173
+ if (!tagName) {
174
+ tagName = '*';
175
+ }
176
+ // Grab all of the tagName elements within current context
177
+ var found = new Array;
178
+ var foundCount = 0;
179
+ for (var h = 0; h < currentContext.length; h++) {
180
+ var elements;
181
+ if (tagName == '*') {
182
+ elements = getAllChildren(currentContext[h]);
183
+ } else {
184
+ elements = currentContext[h].getElementsByTagName(tagName);
185
+ }
186
+ for (var j = 0; j < elements.length; j++) {
187
+ found[foundCount++] = elements[j];
188
+ }
189
+ }
190
+ currentContext = new Array;
191
+ var currentContextIndex = 0;
192
+ var checkFunction; // This function will be used to filter the elements
193
+ switch (attrOperator) {
194
+ case '=': // Equality
195
+ checkFunction = function(e) { return (e.getAttribute(attrName) == attrValue); };
196
+ break;
197
+ case '~': // Match one of space seperated words
198
+ checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('\\b'+attrValue+'\\b'))); };
199
+ break;
200
+ case '|': // Match start with value followed by optional hyphen
201
+ checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('^'+attrValue+'-?'))); };
202
+ break;
203
+ case '^': // Match starts with value
204
+ checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) == 0); };
205
+ break;
206
+ case '$': // Match ends with value - fails with "Warning" in Opera 7
207
+ checkFunction = function(e) { return (e.getAttribute(attrName).lastIndexOf(attrValue) == e.getAttribute(attrName).length - attrValue.length); };
208
+ break;
209
+ case '*': // Match ends with value
210
+ checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) > -1); };
211
+ break;
212
+ default :
213
+ // Just test for existence of attribute
214
+ checkFunction = function(e) { return e.getAttribute(attrName); };
215
+ }
216
+ currentContext = new Array;
217
+ var currentContextIndex = 0;
218
+ for (var k = 0; k < found.length; k++) {
219
+ if (checkFunction(found[k])) {
220
+ currentContext[currentContextIndex++] = found[k];
221
+ }
222
+ }
223
+ // alert('Attribute Selector: '+tagName+' '+attrName+' '+attrOperator+' '+attrValue);
224
+ continue; // Skip to next token
225
+ }
226
+
227
+ if (!currentContext[0]){
228
+ return;
229
+ }
230
+
231
+ // If we get here, token is JUST an element (not a class or ID selector)
232
+ tagName = token;
233
+ var found = new Array;
234
+ var foundCount = 0;
235
+ for (var h = 0; h < currentContext.length; h++) {
236
+ var elements = currentContext[h].getElementsByTagName(tagName);
237
+ for (var j = 0; j < elements.length; j++) {
238
+ found[foundCount++] = elements[j];
239
+ }
240
+ }
241
+ currentContext = found;
242
+ }
243
+ return currentContext;
244
+ }
245
+
246
+ /* That revolting regular expression explained
247
+ /^(\w+)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/
248
+ \---/ \---/\-------------/ \-------/
249
+ | | | |
250
+ | | | The value
251
+ | | ~,|,^,$,* or =
252
+ | Attribute
253
+ Tag
254
+ */
@@ -0,0 +1,446 @@
1
+ // Copyright (c) 2005 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
2
+ // (c) 2005 Ivan Krstic (http://blogs.law.harvard.edu/ivan)
3
+ //
4
+ // Permission is hereby granted, free of charge, to any person obtaining
5
+ // a copy of this software and associated documentation files (the
6
+ // "Software"), to deal in the Software without restriction, including
7
+ // without limitation the rights to use, copy, modify, merge, publish,
8
+ // distribute, sublicense, and/or sell copies of the Software, and to
9
+ // permit persons to whom the Software is furnished to do so, subject to
10
+ // the following conditions:
11
+ //
12
+ // The above copyright notice and this permission notice shall be
13
+ // included in all copies or substantial portions of the Software.
14
+ //
15
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+
23
+ Element.collectTextNodesIgnoreClass = function(element, ignoreclass) {
24
+ var children = $(element).childNodes;
25
+ var text = "";
26
+ var classtest = new RegExp("^([^ ]+ )*" + ignoreclass+ "( [^ ]+)*$","i");
27
+
28
+ for (var i = 0; i < children.length; i++) {
29
+ if(children[i].nodeType==3) {
30
+ text+=children[i].nodeValue;
31
+ } else {
32
+ if((!children[i].className.match(classtest)) && children[i].hasChildNodes())
33
+ text += Element.collectTextNodesIgnoreClass(children[i], ignoreclass);
34
+ }
35
+ }
36
+
37
+ return text;
38
+ }
39
+
40
+ // Autocompleter.Base handles all the autocompletion functionality
41
+ // that's independent of the data source for autocompletion. This
42
+ // includes drawing the autocompletion menu, observing keyboard
43
+ // and mouse events, and similar.
44
+ //
45
+ // Specific autocompleters need to provide, at the very least,
46
+ // a getUpdatedChoices function that will be invoked every time
47
+ // the text inside the monitored textbox changes. This method
48
+ // should get the text for which to provide autocompletion by
49
+ // invoking this.getEntry(), NOT by directly accessing
50
+ // this.element.value. This is to allow incremental tokenized
51
+ // autocompletion. Specific auto-completion logic (AJAX, etc)
52
+ // belongs in getUpdatedChoices.
53
+ //
54
+ // Tokenized incremental autocompletion is enabled automatically
55
+ // when an autocompleter is instantiated with the 'tokens' option
56
+ // in the options parameter, e.g.:
57
+ // new Ajax.Autocompleter('id','upd', '/url/', { tokens: ',' });
58
+ // will incrementally autocomplete with a comma as the token.
59
+ // Additionally, ',' in the above example can be replaced with
60
+ // a token array, e.g. { tokens: new Array (',', '\n') } which
61
+ // enables autocompletion on multiple tokens. This is most
62
+ // useful when one of the tokens is \n (a newline), as it
63
+ // allows smart autocompletion after linebreaks.
64
+
65
+ var Autocompleter = {}
66
+ Autocompleter.Base = function() {};
67
+ Autocompleter.Base.prototype = {
68
+ base_initialize: function(element, update, options) {
69
+ this.element = $(element);
70
+ this.update = $(update);
71
+ this.has_focus = false;
72
+ this.changed = false;
73
+ this.active = false;
74
+ this.index = 0;
75
+ this.entry_count = 0;
76
+
77
+ if (this.setOptions)
78
+ this.setOptions(options);
79
+ else
80
+ this.options = {}
81
+
82
+ this.options.tokens = this.options.tokens || new Array();
83
+ this.options.frequency = this.options.frequency || 0.4;
84
+ this.options.min_chars = this.options.min_chars || 1;
85
+ this.options.onShow = this.options.onShow ||
86
+ function(element, update){
87
+ if(!update.style.position || update.style.position=='absolute') {
88
+ update.style.position = 'absolute';
89
+ var offsets = Position.cumulativeOffset(element);
90
+ update.style.left = offsets[0] + 'px';
91
+ update.style.top = (offsets[1] + element.offsetHeight) + 'px';
92
+ update.style.width = element.offsetWidth + 'px';
93
+ }
94
+ new Effect.Appear(update,{duration:0.15});
95
+ };
96
+ this.options.onHide = this.options.onHide ||
97
+ function(element, update){ new Effect.Fade(update,{duration:0.15}) };
98
+
99
+ if(this.options.indicator)
100
+ this.indicator = $(this.options.indicator);
101
+
102
+ if (typeof(this.options.tokens) == 'string')
103
+ this.options.tokens = new Array(this.options.tokens);
104
+
105
+ this.observer = null;
106
+
107
+ Element.hide(this.update);
108
+
109
+ Event.observe(this.element, "blur", this.onBlur.bindAsEventListener(this));
110
+ Event.observe(this.element, "keypress", this.onKeyPress.bindAsEventListener(this));
111
+ },
112
+
113
+ show: function() {
114
+ if(this.update.style.display=='none') this.options.onShow(this.element, this.update);
115
+ if(!this.iefix && (navigator.appVersion.indexOf('MSIE')>0) && this.update.style.position=='absolute') {
116
+ new Insertion.After(this.update,
117
+ '<iframe id="' + this.update.id + '_iefix" '+
118
+ 'style="display:none;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" ' +
119
+ 'src="javascript:false;" frameborder="0" scrolling="no"></iframe>');
120
+ this.iefix = $(this.update.id+'_iefix');
121
+ }
122
+ if(this.iefix) {
123
+ Position.clone(this.update, this.iefix);
124
+ this.iefix.style.zIndex = 1;
125
+ this.update.style.zIndex = 2;
126
+ Element.show(this.iefix);
127
+ }
128
+ },
129
+
130
+ hide: function() {
131
+ if(this.update.style.display=='') this.options.onHide(this.element, this.update);
132
+ if(this.iefix) Element.hide(this.iefix);
133
+ },
134
+
135
+ startIndicator: function() {
136
+ if(this.indicator) Element.show(this.indicator);
137
+ },
138
+
139
+ stopIndicator: function() {
140
+ if(this.indicator) Element.hide(this.indicator);
141
+ },
142
+
143
+ onKeyPress: function(event) {
144
+ if(this.active)
145
+ switch(event.keyCode) {
146
+ case Event.KEY_TAB:
147
+ case Event.KEY_RETURN:
148
+ this.select_entry();
149
+ Event.stop(event);
150
+ case Event.KEY_ESC:
151
+ this.hide();
152
+ this.active = false;
153
+ return;
154
+ case Event.KEY_LEFT:
155
+ case Event.KEY_RIGHT:
156
+ return;
157
+ case Event.KEY_UP:
158
+ this.mark_previous();
159
+ this.render();
160
+ if(navigator.appVersion.indexOf('AppleWebKit')>0) Event.stop(event);
161
+ return;
162
+ case Event.KEY_DOWN:
163
+ this.mark_next();
164
+ this.render();
165
+ if(navigator.appVersion.indexOf('AppleWebKit')>0) Event.stop(event);
166
+ return;
167
+ }
168
+ else
169
+ if(event.keyCode==Event.KEY_TAB || event.keyCode==Event.KEY_RETURN)
170
+ return;
171
+
172
+ this.changed = true;
173
+ this.has_focus = true;
174
+
175
+ if(this.observer) clearTimeout(this.observer);
176
+ this.observer =
177
+ setTimeout(this.onObserverEvent.bind(this), this.options.frequency*1000);
178
+ },
179
+
180
+ onHover: function(event) {
181
+ var element = Event.findElement(event, 'LI');
182
+ if(this.index != element.autocompleteIndex)
183
+ {
184
+ this.index = element.autocompleteIndex;
185
+ this.render();
186
+ }
187
+ Event.stop(event);
188
+ },
189
+
190
+ onClick: function(event) {
191
+ var element = Event.findElement(event, 'LI');
192
+ this.index = element.autocompleteIndex;
193
+ this.select_entry();
194
+ Event.stop(event);
195
+ },
196
+
197
+ onBlur: function(event) {
198
+ // needed to make click events working
199
+ setTimeout(this.hide.bind(this), 250);
200
+ this.has_focus = false;
201
+ this.active = false;
202
+ },
203
+
204
+ render: function() {
205
+ if(this.entry_count > 0) {
206
+ for (var i = 0; i < this.entry_count; i++)
207
+ this.index==i ?
208
+ Element.addClassName(this.get_entry(i),"selected") :
209
+ Element.removeClassName(this.get_entry(i),"selected");
210
+
211
+ if(this.has_focus) {
212
+ if(this.get_current_entry().scrollIntoView)
213
+ this.get_current_entry().scrollIntoView(false);
214
+
215
+ this.show();
216
+ this.active = true;
217
+ }
218
+ } else this.hide();
219
+ },
220
+
221
+ mark_previous: function() {
222
+ if(this.index > 0) this.index--
223
+ else this.index = this.entry_count-1;
224
+ },
225
+
226
+ mark_next: function() {
227
+ if(this.index < this.entry_count-1) this.index++
228
+ else this.index = 0;
229
+ },
230
+
231
+ get_entry: function(index) {
232
+ return this.update.firstChild.childNodes[index];
233
+ },
234
+
235
+ get_current_entry: function() {
236
+ return this.get_entry(this.index);
237
+ },
238
+
239
+ select_entry: function() {
240
+ this.active = false;
241
+ value = Element.collectTextNodesIgnoreClass(this.get_current_entry(), 'informal').unescapeHTML();
242
+ this.updateElement(value);
243
+ this.element.focus();
244
+ },
245
+
246
+ updateElement: function(value) {
247
+ var last_token_pos = this.findLastToken();
248
+ if (last_token_pos != -1) {
249
+ var new_value = this.element.value.substr(0, last_token_pos + 1);
250
+ var whitespace = this.element.value.substr(last_token_pos + 1).match(/^\s+/);
251
+ if (whitespace)
252
+ new_value += whitespace[0];
253
+ this.element.value = new_value + value;
254
+ } else {
255
+ this.element.value = value;
256
+ }
257
+ },
258
+
259
+ updateChoices: function(choices) {
260
+ if(!this.changed && this.has_focus) {
261
+ this.update.innerHTML = choices;
262
+ Element.cleanWhitespace(this.update);
263
+ Element.cleanWhitespace(this.update.firstChild);
264
+
265
+ if(this.update.firstChild && this.update.firstChild.childNodes) {
266
+ this.entry_count =
267
+ this.update.firstChild.childNodes.length;
268
+ for (var i = 0; i < this.entry_count; i++) {
269
+ entry = this.get_entry(i);
270
+ entry.autocompleteIndex = i;
271
+ this.addObservers(entry);
272
+ }
273
+ } else {
274
+ this.entry_count = 0;
275
+ }
276
+
277
+ this.stopIndicator();
278
+
279
+ this.index = 0;
280
+ this.render();
281
+ }
282
+ },
283
+
284
+ addObservers: function(element) {
285
+ Event.observe(element, "mouseover", this.onHover.bindAsEventListener(this));
286
+ Event.observe(element, "click", this.onClick.bindAsEventListener(this));
287
+ },
288
+
289
+ onObserverEvent: function() {
290
+ this.changed = false;
291
+ if(this.getEntry().length>=this.options.min_chars) {
292
+ this.startIndicator();
293
+ this.getUpdatedChoices();
294
+ } else {
295
+ this.active = false;
296
+ this.hide();
297
+ }
298
+ },
299
+
300
+ getEntry: function() {
301
+ var token_pos = this.findLastToken();
302
+ if (token_pos != -1)
303
+ var ret = this.element.value.substr(token_pos + 1).replace(/^\s+/,'').replace(/\s+$/,'');
304
+ else
305
+ var ret = this.element.value;
306
+
307
+ return /\n/.test(ret) ? '' : ret;
308
+ },
309
+
310
+ findLastToken: function() {
311
+ var last_token_pos = -1;
312
+
313
+ for (var i=0; i<this.options.tokens.length; i++) {
314
+ var this_token_pos = this.element.value.lastIndexOf(this.options.tokens[i]);
315
+ if (this_token_pos > last_token_pos)
316
+ last_token_pos = this_token_pos;
317
+ }
318
+ return last_token_pos;
319
+ }
320
+ }
321
+
322
+ Ajax.Autocompleter = Class.create();
323
+ Ajax.Autocompleter.prototype = Object.extend(new Autocompleter.Base(),
324
+ Object.extend(new Ajax.Base(), {
325
+ initialize: function(element, update, url, options) {
326
+ this.base_initialize(element, update, options);
327
+ this.options.asynchronous = true;
328
+ this.options.onComplete = this.onComplete.bind(this)
329
+ this.options.method = 'post';
330
+ this.options.defaultParams = this.options.parameters || null;
331
+ this.url = url;
332
+ },
333
+
334
+ getUpdatedChoices: function() {
335
+ entry = encodeURIComponent(this.element.name) + '=' +
336
+ encodeURIComponent(this.getEntry());
337
+
338
+ this.options.parameters = this.options.callback ?
339
+ this.options.callback(this.element, entry) : entry;
340
+
341
+ if(this.options.defaultParams)
342
+ this.options.parameters += '&' + this.options.defaultParams;
343
+
344
+ new Ajax.Request(this.url, this.options);
345
+ },
346
+
347
+ onComplete: function(request) {
348
+ this.updateChoices(request.responseText);
349
+ }
350
+
351
+ }));
352
+
353
+ // The local array autocompleter. Used when you'd prefer to
354
+ // inject an array of autocompletion options into the page, rather
355
+ // than sending out Ajax queries, which can be quite slow sometimes.
356
+ //
357
+ // The constructor takes four parameters. The first two are, as usual,
358
+ // the id of the monitored textbox, and id of the autocompletion menu.
359
+ // The third is the array you want to autocomplete from, and the fourth
360
+ // is the options block.
361
+ //
362
+ // Extra local autocompletion options:
363
+ // - choices - How many autocompletion choices to offer
364
+ //
365
+ // - partial_search - If false, the autocompleter will match entered
366
+ // text only at the beginning of strings in the
367
+ // autocomplete array. Defaults to true, which will
368
+ // match text at the beginning of any *word* in the
369
+ // strings in the autocomplete array. If you want to
370
+ // search anywhere in the string, additionally set
371
+ // the option full_search to true (default: off).
372
+ //
373
+ // - full_search - Search anywhere in autocomplete array strings.
374
+ //
375
+ // - partial_chars - How many characters to enter before triggering
376
+ // a partial match (unlike min_chars, which defines
377
+ // how many characters are required to do any match
378
+ // at all). Defaults to 2.
379
+ //
380
+ // - ignore_case - Whether to ignore case when autocompleting.
381
+ // Defaults to true.
382
+ //
383
+ // It's possible to pass in a custom function as the 'selector'
384
+ // option, if you prefer to write your own autocompletion logic.
385
+ // In that case, the other options above will not apply unless
386
+ // you support them.
387
+
388
+ Autocompleter.Local = Class.create();
389
+ Autocompleter.Local.prototype = Object.extend(new Autocompleter.Base(), {
390
+ initialize: function(element, update, array, options) {
391
+ this.base_initialize(element, update, options);
392
+ this.options.array = array;
393
+ },
394
+
395
+ getUpdatedChoices: function() {
396
+ this.updateChoices(this.options.selector(this));
397
+ },
398
+
399
+ setOptions: function(options) {
400
+ this.options = Object.extend({
401
+ choices: 10,
402
+ partial_search: true,
403
+ partial_chars: 2,
404
+ ignore_case: true,
405
+ full_search: false,
406
+ selector: function(instance) {
407
+ var ret = new Array(); // Beginning matches
408
+ var partial = new Array(); // Inside matches
409
+ var entry = instance.getEntry();
410
+ var count = 0;
411
+
412
+ for (var i = 0; i < instance.options.array.length &&
413
+ ret.length < instance.options.choices ; i++) {
414
+ var elem = instance.options.array[i];
415
+ var found_pos = instance.options.ignore_case ?
416
+ elem.toLowerCase().indexOf(entry.toLowerCase()) :
417
+ elem.indexOf(entry);
418
+
419
+ while (found_pos != -1) {
420
+ if (found_pos == 0 && elem.length != entry.length) {
421
+ ret.push("<li><strong>" + elem.substr(0, entry.length) + "</strong>" +
422
+ elem.substr(entry.length) + "</li>");
423
+ break;
424
+ } else if (entry.length >= instance.options.partial_chars &&
425
+ instance.options.partial_search && found_pos != -1) {
426
+ if (instance.options.full_search || /\s/.test(elem.substr(found_pos-1,1))) {
427
+ partial.push("<li>" + elem.substr(0, found_pos) + "<strong>" +
428
+ elem.substr(found_pos, entry.length) + "</strong>" + elem.substr(
429
+ found_pos + entry.length) + "</li>");
430
+ break;
431
+ }
432
+ }
433
+
434
+ found_pos = instance.options.ignore_case ?
435
+ elem.toLowerCase().indexOf(entry.toLowerCase(), found_pos + 1) :
436
+ elem.indexOf(entry, found_pos + 1);
437
+
438
+ }
439
+ }
440
+ if (partial.length)
441
+ ret = ret.concat(partial.slice(0, instance.options.choices - ret.length))
442
+ return "<ul>" + ret.join('') + "</ul>";
443
+ }
444
+ }, options || {});
445
+ }
446
+ });