/[webpac2]/Webpacus/root/js/controls.js
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /Webpacus/root/js/controls.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 203 - (hide annotations)
Fri Dec 2 23:01:25 2005 UTC (18 years, 6 months ago) by dpavlin
File MIME type: text/cpp
File size: 25574 byte(s)
 r11427@llin:  dpavlin | 2005-12-02 23:58:03 +0100
 huge update: new upstream HTML::Prototype and handling of Ajax.Autocompleter mode: enter
 now *ALWAYS* trigger search, while accepting suggestions is made using tab.
 Also, list doesn't wrap. [0.12]
 

1 dpavlin 83 // 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 dpavlin 203 // (c) 2005 Jon Tirsen (http://www.tirsen.com)
4     // Contributors:
5     // Richard Livsey
6     // Rahul Bhargava
7     // Rob Wills
8     //
9     // See scriptaculous.js for full license.
10 dpavlin 83
11 dpavlin 203 // Autocompleter.Base handles all the autocompletion functionality
12 dpavlin 83 // that's independent of the data source for autocompletion. This
13     // includes drawing the autocompletion menu, observing keyboard
14     // and mouse events, and similar.
15     //
16 dpavlin 203 // Specific autocompleters need to provide, at the very least,
17 dpavlin 83 // a getUpdatedChoices function that will be invoked every time
18 dpavlin 203 // the text inside the monitored textbox changes. This method
19 dpavlin 83 // should get the text for which to provide autocompletion by
20 dpavlin 203 // invoking this.getToken(), NOT by directly accessing
21 dpavlin 83 // this.element.value. This is to allow incremental tokenized
22     // autocompletion. Specific auto-completion logic (AJAX, etc)
23     // belongs in getUpdatedChoices.
24     //
25     // Tokenized incremental autocompletion is enabled automatically
26     // when an autocompleter is instantiated with the 'tokens' option
27     // in the options parameter, e.g.:
28     // new Ajax.Autocompleter('id','upd', '/url/', { tokens: ',' });
29     // will incrementally autocomplete with a comma as the token.
30     // Additionally, ',' in the above example can be replaced with
31 dpavlin 203 // a token array, e.g. { tokens: [',', '\n'] } which
32     // enables autocompletion on multiple tokens. This is most
33     // useful when one of the tokens is \n (a newline), as it
34 dpavlin 83 // allows smart autocompletion after linebreaks.
35    
36     var Autocompleter = {}
37     Autocompleter.Base = function() {};
38     Autocompleter.Base.prototype = {
39 dpavlin 203 baseInitialize: function(element, update, options) {
40     this.element = $(element);
41     this.update = $(update);
42     this.hasFocus = false;
43     this.changed = false;
44     this.active = false;
45     this.index = 0;
46     this.entryCount = 0;
47 dpavlin 83
48     if (this.setOptions)
49     this.setOptions(options);
50     else
51 dpavlin 203 this.options = options || {};
52    
53     this.options.paramName = this.options.paramName || this.element.name;
54     this.options.tokens = this.options.tokens || [];
55 dpavlin 83 this.options.frequency = this.options.frequency || 0.4;
56 dpavlin 203 this.options.minChars = this.options.minChars || 1;
57     this.options.onShow = this.options.onShow ||
58     function(element, update){
59 dpavlin 83 if(!update.style.position || update.style.position=='absolute') {
60     update.style.position = 'absolute';
61 dpavlin 203 Position.clone(element, update, {setHeight: false, offsetTop: element.offsetHeight});
62 dpavlin 83 }
63 dpavlin 203 Effect.Appear(update,{duration:0.15});
64 dpavlin 83 };
65 dpavlin 203 this.options.onHide = this.options.onHide ||
66 dpavlin 83 function(element, update){ new Effect.Fade(update,{duration:0.15}) };
67    
68 dpavlin 203 if (typeof(this.options.tokens) == 'string')
69 dpavlin 83 this.options.tokens = new Array(this.options.tokens);
70 dpavlin 203
71 dpavlin 83 this.observer = null;
72 dpavlin 203
73     this.element.setAttribute('autocomplete','off');
74    
75 dpavlin 83 Element.hide(this.update);
76 dpavlin 203
77 dpavlin 83 Event.observe(this.element, "blur", this.onBlur.bindAsEventListener(this));
78     Event.observe(this.element, "keypress", this.onKeyPress.bindAsEventListener(this));
79     },
80    
81     show: function() {
82 dpavlin 203 if(Element.getStyle(this.update, 'display')=='none') this.options.onShow(this.element, this.update);
83     if(!this.iefix &&
84     (navigator.appVersion.indexOf('MSIE')>0) &&
85     (navigator.userAgent.indexOf('Opera')<0) &&
86     (Element.getStyle(this.update, 'position')=='absolute')) {
87     new Insertion.After(this.update,
88 dpavlin 83 '<iframe id="' + this.update.id + '_iefix" '+
89 dpavlin 203 'style="display:none;position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" ' +
90 dpavlin 83 'src="javascript:false;" frameborder="0" scrolling="no"></iframe>');
91     this.iefix = $(this.update.id+'_iefix');
92     }
93 dpavlin 203 if(this.iefix) setTimeout(this.fixIEOverlapping.bind(this), 50);
94 dpavlin 83 },
95 dpavlin 203
96     fixIEOverlapping: function() {
97     Position.clone(this.update, this.iefix);
98     this.iefix.style.zIndex = 1;
99     this.update.style.zIndex = 2;
100     Element.show(this.iefix);
101     },
102    
103 dpavlin 83 hide: function() {
104 dpavlin 203 this.stopIndicator();
105     if(Element.getStyle(this.update, 'display')!='none') this.options.onHide(this.element, this.update);
106 dpavlin 83 if(this.iefix) Element.hide(this.iefix);
107     },
108 dpavlin 203
109 dpavlin 83 startIndicator: function() {
110 dpavlin 203 if(this.options.indicator) Element.show(this.options.indicator);
111 dpavlin 83 },
112 dpavlin 203
113 dpavlin 83 stopIndicator: function() {
114 dpavlin 203 if(this.options.indicator) Element.hide(this.options.indicator);
115 dpavlin 83 },
116    
117     onKeyPress: function(event) {
118     if(this.active)
119     switch(event.keyCode) {
120     case Event.KEY_TAB:
121 dpavlin 203 this.selectEntry();
122     Event.stop(event);
123 dpavlin 83 case Event.KEY_RETURN:
124 dpavlin 203 if (this.ignoreReturn) {
125     this.hide();
126     this.active = false;
127     return;
128     } else {
129     this.selectEntry();
130     Event.stop(event);
131     }
132 dpavlin 83 case Event.KEY_ESC:
133     this.hide();
134     this.active = false;
135 dpavlin 203 Event.stop(event);
136 dpavlin 83 return;
137     case Event.KEY_LEFT:
138     case Event.KEY_RIGHT:
139     return;
140     case Event.KEY_UP:
141 dpavlin 203 this.markPrevious();
142 dpavlin 83 this.render();
143     if(navigator.appVersion.indexOf('AppleWebKit')>0) Event.stop(event);
144     return;
145     case Event.KEY_DOWN:
146 dpavlin 203 this.markNext();
147 dpavlin 83 this.render();
148     if(navigator.appVersion.indexOf('AppleWebKit')>0) Event.stop(event);
149     return;
150     }
151 dpavlin 203 else
152     if(event.keyCode==Event.KEY_TAB || event.keyCode==Event.KEY_RETURN)
153 dpavlin 83 return;
154 dpavlin 203
155 dpavlin 83 this.changed = true;
156 dpavlin 203 this.hasFocus = true;
157    
158 dpavlin 83 if(this.observer) clearTimeout(this.observer);
159 dpavlin 203 this.observer =
160 dpavlin 83 setTimeout(this.onObserverEvent.bind(this), this.options.frequency*1000);
161     },
162 dpavlin 203
163 dpavlin 83 onHover: function(event) {
164     var element = Event.findElement(event, 'LI');
165 dpavlin 203 if(this.index != element.autocompleteIndex)
166 dpavlin 83 {
167     this.index = element.autocompleteIndex;
168     this.render();
169     }
170     Event.stop(event);
171     },
172 dpavlin 203
173 dpavlin 83 onClick: function(event) {
174     var element = Event.findElement(event, 'LI');
175     this.index = element.autocompleteIndex;
176 dpavlin 203 this.selectEntry();
177     this.hide();
178 dpavlin 83 },
179 dpavlin 203
180 dpavlin 83 onBlur: function(event) {
181     // needed to make click events working
182     setTimeout(this.hide.bind(this), 250);
183 dpavlin 203 this.hasFocus = false;
184     this.active = false;
185     },
186    
187 dpavlin 83 render: function() {
188 dpavlin 203 if(this.entryCount > 0) {
189     for (var i = 0; i < this.entryCount; i++)
190     this.index==i ?
191     Element.addClassName(this.getEntry(i),"selected") :
192     Element.removeClassName(this.getEntry(i),"selected");
193    
194     if(this.hasFocus) {
195 dpavlin 83 this.show();
196     this.active = true;
197     }
198 dpavlin 203 } else {
199     this.active = false;
200     this.hide();
201     }
202 dpavlin 83 },
203 dpavlin 203
204     markPrevious: function() {
205 dpavlin 83 if(this.index > 0) this.index--
206 dpavlin 203 else this.index = this.entryCount-1;
207 dpavlin 83 },
208 dpavlin 203
209     markNext: function() {
210     if(this.index < this.entryCount-1) this.index++
211 dpavlin 83 else this.index = 0;
212     },
213 dpavlin 203
214     getEntry: function(index) {
215 dpavlin 83 return this.update.firstChild.childNodes[index];
216     },
217 dpavlin 203
218     getCurrentEntry: function() {
219     return this.getEntry(this.index);
220 dpavlin 83 },
221 dpavlin 203
222     selectEntry: function() {
223 dpavlin 83 this.active = false;
224 dpavlin 203 this.updateElement(this.getCurrentEntry());
225 dpavlin 83 },
226    
227 dpavlin 203 updateElement: function(selectedElement) {
228     if (this.options.updateElement) {
229     this.options.updateElement(selectedElement);
230     return;
231     }
232    
233     var value = Element.collectTextNodesIgnoreClass(selectedElement, 'informal');
234     var lastTokenPos = this.findLastToken();
235     if (lastTokenPos != -1) {
236     var newValue = this.element.value.substr(0, lastTokenPos + 1);
237     var whitespace = this.element.value.substr(lastTokenPos + 1).match(/^\s+/);
238 dpavlin 83 if (whitespace)
239 dpavlin 203 newValue += whitespace[0];
240     this.element.value = newValue + value;
241 dpavlin 83 } else {
242     this.element.value = value;
243 dpavlin 203 }
244     this.element.focus();
245    
246     if (this.options.afterUpdateElement)
247     this.options.afterUpdateElement(this.element, selectedElement);
248 dpavlin 83 },
249 dpavlin 203
250 dpavlin 83 updateChoices: function(choices) {
251 dpavlin 203 if(!this.changed && this.hasFocus) {
252 dpavlin 83 this.update.innerHTML = choices;
253     Element.cleanWhitespace(this.update);
254     Element.cleanWhitespace(this.update.firstChild);
255    
256     if(this.update.firstChild && this.update.firstChild.childNodes) {
257 dpavlin 203 this.entryCount =
258 dpavlin 83 this.update.firstChild.childNodes.length;
259 dpavlin 203 for (var i = 0; i < this.entryCount; i++) {
260     var entry = this.getEntry(i);
261 dpavlin 83 entry.autocompleteIndex = i;
262     this.addObservers(entry);
263     }
264 dpavlin 203 } else {
265     this.entryCount = 0;
266 dpavlin 83 }
267 dpavlin 203
268 dpavlin 83 this.stopIndicator();
269 dpavlin 203
270 dpavlin 83 this.index = 0;
271     this.render();
272     }
273     },
274    
275     addObservers: function(element) {
276     Event.observe(element, "mouseover", this.onHover.bindAsEventListener(this));
277     Event.observe(element, "click", this.onClick.bindAsEventListener(this));
278     },
279    
280     onObserverEvent: function() {
281 dpavlin 203 this.changed = false;
282     if(this.getToken().length>=this.options.minChars) {
283 dpavlin 83 this.startIndicator();
284     this.getUpdatedChoices();
285     } else {
286     this.active = false;
287     this.hide();
288     }
289     },
290    
291 dpavlin 203 getToken: function() {
292     var tokenPos = this.findLastToken();
293     if (tokenPos != -1)
294     var ret = this.element.value.substr(tokenPos + 1).replace(/^\s+/,'').replace(/\s+$/,'');
295 dpavlin 83 else
296     var ret = this.element.value;
297 dpavlin 203
298 dpavlin 83 return /\n/.test(ret) ? '' : ret;
299     },
300    
301     findLastToken: function() {
302 dpavlin 203 var lastTokenPos = -1;
303 dpavlin 83
304     for (var i=0; i<this.options.tokens.length; i++) {
305 dpavlin 203 var thisTokenPos = this.element.value.lastIndexOf(this.options.tokens[i]);
306     if (thisTokenPos > lastTokenPos)
307     lastTokenPos = thisTokenPos;
308 dpavlin 83 }
309 dpavlin 203 return lastTokenPos;
310 dpavlin 83 }
311     }
312    
313     Ajax.Autocompleter = Class.create();
314 dpavlin 203 Object.extend(Object.extend(Ajax.Autocompleter.prototype, Autocompleter.Base.prototype), {
315 dpavlin 83 initialize: function(element, update, url, options) {
316 dpavlin 203 this.baseInitialize(element, update, options);
317 dpavlin 83 this.options.asynchronous = true;
318 dpavlin 203 this.options.onComplete = this.onComplete.bind(this);
319 dpavlin 83 this.options.defaultParams = this.options.parameters || null;
320     this.url = url;
321     },
322 dpavlin 203
323 dpavlin 83 getUpdatedChoices: function() {
324 dpavlin 203 entry = encodeURIComponent(this.options.paramName) + '=' +
325     encodeURIComponent(this.getToken());
326    
327 dpavlin 83 this.options.parameters = this.options.callback ?
328     this.options.callback(this.element, entry) : entry;
329 dpavlin 203
330     if(this.options.defaultParams)
331 dpavlin 83 this.options.parameters += '&' + this.options.defaultParams;
332 dpavlin 203
333 dpavlin 83 new Ajax.Request(this.url, this.options);
334     },
335 dpavlin 203
336 dpavlin 83 onComplete: function(request) {
337     this.updateChoices(request.responseText);
338     }
339    
340 dpavlin 203 });
341 dpavlin 83
342     // The local array autocompleter. Used when you'd prefer to
343     // inject an array of autocompletion options into the page, rather
344     // than sending out Ajax queries, which can be quite slow sometimes.
345     //
346     // The constructor takes four parameters. The first two are, as usual,
347     // the id of the monitored textbox, and id of the autocompletion menu.
348     // The third is the array you want to autocomplete from, and the fourth
349     // is the options block.
350     //
351     // Extra local autocompletion options:
352     // - choices - How many autocompletion choices to offer
353     //
354 dpavlin 203 // - partialSearch - If false, the autocompleter will match entered
355     // text only at the beginning of strings in the
356 dpavlin 83 // autocomplete array. Defaults to true, which will
357     // match text at the beginning of any *word* in the
358     // strings in the autocomplete array. If you want to
359     // search anywhere in the string, additionally set
360 dpavlin 203 // the option fullSearch to true (default: off).
361 dpavlin 83 //
362 dpavlin 203 // - fullSsearch - Search anywhere in autocomplete array strings.
363 dpavlin 83 //
364 dpavlin 203 // - partialChars - How many characters to enter before triggering
365     // a partial match (unlike minChars, which defines
366 dpavlin 83 // how many characters are required to do any match
367     // at all). Defaults to 2.
368     //
369 dpavlin 203 // - ignoreCase - Whether to ignore case when autocompleting.
370 dpavlin 83 // Defaults to true.
371     //
372 dpavlin 203 // It's possible to pass in a custom function as the 'selector'
373 dpavlin 83 // option, if you prefer to write your own autocompletion logic.
374     // In that case, the other options above will not apply unless
375     // you support them.
376    
377     Autocompleter.Local = Class.create();
378     Autocompleter.Local.prototype = Object.extend(new Autocompleter.Base(), {
379     initialize: function(element, update, array, options) {
380 dpavlin 203 this.baseInitialize(element, update, options);
381 dpavlin 83 this.options.array = array;
382     },
383    
384     getUpdatedChoices: function() {
385     this.updateChoices(this.options.selector(this));
386     },
387    
388     setOptions: function(options) {
389     this.options = Object.extend({
390     choices: 10,
391 dpavlin 203 partialSearch: true,
392     partialChars: 2,
393     ignoreCase: true,
394     fullSearch: false,
395 dpavlin 83 selector: function(instance) {
396 dpavlin 203 var ret = []; // Beginning matches
397     var partial = []; // Inside matches
398     var entry = instance.getToken();
399 dpavlin 83 var count = 0;
400 dpavlin 203
401     for (var i = 0; i < instance.options.array.length &&
402     ret.length < instance.options.choices ; i++) {
403    
404 dpavlin 83 var elem = instance.options.array[i];
405 dpavlin 203 var foundPos = instance.options.ignoreCase ?
406     elem.toLowerCase().indexOf(entry.toLowerCase()) :
407 dpavlin 83 elem.indexOf(entry);
408    
409 dpavlin 203 while (foundPos != -1) {
410     if (foundPos == 0 && elem.length != entry.length) {
411     ret.push("<li><strong>" + elem.substr(0, entry.length) + "</strong>" +
412 dpavlin 83 elem.substr(entry.length) + "</li>");
413     break;
414 dpavlin 203 } else if (entry.length >= instance.options.partialChars &&
415     instance.options.partialSearch && foundPos != -1) {
416     if (instance.options.fullSearch || /\s/.test(elem.substr(foundPos-1,1))) {
417     partial.push("<li>" + elem.substr(0, foundPos) + "<strong>" +
418     elem.substr(foundPos, entry.length) + "</strong>" + elem.substr(
419     foundPos + entry.length) + "</li>");
420 dpavlin 83 break;
421     }
422     }
423    
424 dpavlin 203 foundPos = instance.options.ignoreCase ?
425     elem.toLowerCase().indexOf(entry.toLowerCase(), foundPos + 1) :
426     elem.indexOf(entry, foundPos + 1);
427 dpavlin 83
428     }
429     }
430     if (partial.length)
431     ret = ret.concat(partial.slice(0, instance.options.choices - ret.length))
432     return "<ul>" + ret.join('') + "</ul>";
433     }
434     }, options || {});
435     }
436     });
437 dpavlin 203
438     // AJAX in-place editor
439     //
440     // see documentation on http://wiki.script.aculo.us/scriptaculous/show/Ajax.InPlaceEditor
441    
442     // Use this if you notice weird scrolling problems on some browsers,
443     // the DOM might be a bit confused when this gets called so do this
444     // waits 1 ms (with setTimeout) until it does the activation
445     Field.scrollFreeActivate = function(field) {
446     setTimeout(function() {
447     Field.activate(field);
448     }, 1);
449     }
450    
451     Ajax.InPlaceEditor = Class.create();
452     Ajax.InPlaceEditor.defaultHighlightColor = "#FFFF99";
453     Ajax.InPlaceEditor.prototype = {
454     initialize: function(element, url, options) {
455     this.url = url;
456     this.element = $(element);
457    
458     this.options = Object.extend({
459     okText: "ok",
460     cancelText: "cancel",
461     savingText: "Saving...",
462     clickToEditText: "Click to edit",
463     okText: "ok",
464     rows: 1,
465     onComplete: function(transport, element) {
466     new Effect.Highlight(element, {startcolor: this.options.highlightcolor});
467     },
468     onFailure: function(transport) {
469     alert("Error communicating with the server: " + transport.responseText.stripTags());
470     },
471     callback: function(form) {
472     return Form.serialize(form);
473     },
474     handleLineBreaks: true,
475     loadingText: 'Loading...',
476     savingClassName: 'inplaceeditor-saving',
477     loadingClassName: 'inplaceeditor-loading',
478     formClassName: 'inplaceeditor-form',
479     highlightcolor: Ajax.InPlaceEditor.defaultHighlightColor,
480     highlightendcolor: "#FFFFFF",
481     externalControl: null,
482     ajaxOptions: {}
483     }, options || {});
484    
485     if(!this.options.formId && this.element.id) {
486     this.options.formId = this.element.id + "-inplaceeditor";
487     if ($(this.options.formId)) {
488     // there's already a form with that name, don't specify an id
489     this.options.formId = null;
490     }
491     }
492    
493     if (this.options.externalControl) {
494     this.options.externalControl = $(this.options.externalControl);
495     }
496    
497     this.originalBackground = Element.getStyle(this.element, 'background-color');
498     if (!this.originalBackground) {
499     this.originalBackground = "transparent";
500     }
501    
502     this.element.title = this.options.clickToEditText;
503    
504     this.onclickListener = this.enterEditMode.bindAsEventListener(this);
505     this.mouseoverListener = this.enterHover.bindAsEventListener(this);
506     this.mouseoutListener = this.leaveHover.bindAsEventListener(this);
507     Event.observe(this.element, 'click', this.onclickListener);
508     Event.observe(this.element, 'mouseover', this.mouseoverListener);
509     Event.observe(this.element, 'mouseout', this.mouseoutListener);
510     if (this.options.externalControl) {
511     Event.observe(this.options.externalControl, 'click', this.onclickListener);
512     Event.observe(this.options.externalControl, 'mouseover', this.mouseoverListener);
513     Event.observe(this.options.externalControl, 'mouseout', this.mouseoutListener);
514     }
515     },
516     enterEditMode: function(evt) {
517     if (this.saving) return;
518     if (this.editing) return;
519     this.editing = true;
520     this.onEnterEditMode();
521     if (this.options.externalControl) {
522     Element.hide(this.options.externalControl);
523     }
524     Element.hide(this.element);
525     this.createForm();
526     this.element.parentNode.insertBefore(this.form, this.element);
527     Field.scrollFreeActivate(this.editField);
528     // stop the event to avoid a page refresh in Safari
529     if (evt) {
530     Event.stop(evt);
531     }
532     return false;
533     },
534     createForm: function() {
535     this.form = document.createElement("form");
536     this.form.id = this.options.formId;
537     Element.addClassName(this.form, this.options.formClassName)
538     this.form.onsubmit = this.onSubmit.bind(this);
539    
540     this.createEditField();
541    
542     if (this.options.textarea) {
543     var br = document.createElement("br");
544     this.form.appendChild(br);
545     }
546    
547     okButton = document.createElement("input");
548     okButton.type = "submit";
549     okButton.value = this.options.okText;
550     this.form.appendChild(okButton);
551    
552     cancelLink = document.createElement("a");
553     cancelLink.href = "#";
554     cancelLink.appendChild(document.createTextNode(this.options.cancelText));
555     cancelLink.onclick = this.onclickCancel.bind(this);
556     this.form.appendChild(cancelLink);
557     },
558     hasHTMLLineBreaks: function(string) {
559     if (!this.options.handleLineBreaks) return false;
560     return string.match(/<br/i) || string.match(/<p>/i);
561     },
562     convertHTMLLineBreaks: function(string) {
563     return string.replace(/<br>/gi, "\n").replace(/<br\/>/gi, "\n").replace(/<\/p>/gi, "\n").replace(/<p>/gi, "");
564     },
565     createEditField: function() {
566     var text;
567     if(this.options.loadTextURL) {
568     text = this.options.loadingText;
569     } else {
570     text = this.getText();
571     }
572    
573     if (this.options.rows == 1 && !this.hasHTMLLineBreaks(text)) {
574     this.options.textarea = false;
575     var textField = document.createElement("input");
576     textField.type = "text";
577     textField.name = "value";
578     textField.value = text;
579     textField.style.backgroundColor = this.options.highlightcolor;
580     var size = this.options.size || this.options.cols || 0;
581     if (size != 0) textField.size = size;
582     this.editField = textField;
583     } else {
584     this.options.textarea = true;
585     var textArea = document.createElement("textarea");
586     textArea.name = "value";
587     textArea.value = this.convertHTMLLineBreaks(text);
588     textArea.rows = this.options.rows;
589     textArea.cols = this.options.cols || 40;
590     this.editField = textArea;
591     }
592    
593     if(this.options.loadTextURL) {
594     this.loadExternalText();
595     }
596     this.form.appendChild(this.editField);
597     },
598     getText: function() {
599     return this.element.innerHTML;
600     },
601     loadExternalText: function() {
602     Element.addClassName(this.form, this.options.loadingClassName);
603     this.editField.disabled = true;
604     new Ajax.Request(
605     this.options.loadTextURL,
606     Object.extend({
607     asynchronous: true,
608     onComplete: this.onLoadedExternalText.bind(this)
609     }, this.options.ajaxOptions)
610     );
611     },
612     onLoadedExternalText: function(transport) {
613     Element.removeClassName(this.form, this.options.loadingClassName);
614     this.editField.disabled = false;
615     this.editField.value = transport.responseText.stripTags();
616     },
617     onclickCancel: function() {
618     this.onComplete();
619     this.leaveEditMode();
620     return false;
621     },
622     onFailure: function(transport) {
623     this.options.onFailure(transport);
624     if (this.oldInnerHTML) {
625     this.element.innerHTML = this.oldInnerHTML;
626     this.oldInnerHTML = null;
627     }
628     return false;
629     },
630     onSubmit: function() {
631     // onLoading resets these so we need to save them away for the Ajax call
632     var form = this.form;
633     var value = this.editField.value;
634    
635     // do this first, sometimes the ajax call returns before we get a chance to switch on Saving...
636     // which means this will actually switch on Saving... *after* we've left edit mode causing Saving...
637     // to be displayed indefinitely
638     this.onLoading();
639    
640     new Ajax.Updater(
641     {
642     success: this.element,
643     // don't update on failure (this could be an option)
644     failure: null
645     },
646     this.url,
647     Object.extend({
648     parameters: this.options.callback(form, value),
649     onComplete: this.onComplete.bind(this),
650     onFailure: this.onFailure.bind(this)
651     }, this.options.ajaxOptions)
652     );
653     // stop the event to avoid a page refresh in Safari
654     if (arguments.length > 1) {
655     Event.stop(arguments[0]);
656     }
657     return false;
658     },
659     onLoading: function() {
660     this.saving = true;
661     this.removeForm();
662     this.leaveHover();
663     this.showSaving();
664     },
665     showSaving: function() {
666     this.oldInnerHTML = this.element.innerHTML;
667     this.element.innerHTML = this.options.savingText;
668     Element.addClassName(this.element, this.options.savingClassName);
669     this.element.style.backgroundColor = this.originalBackground;
670     Element.show(this.element);
671     },
672     removeForm: function() {
673     if(this.form) {
674     if (this.form.parentNode) Element.remove(this.form);
675     this.form = null;
676     }
677     },
678     enterHover: function() {
679     if (this.saving) return;
680     this.element.style.backgroundColor = this.options.highlightcolor;
681     if (this.effect) {
682     this.effect.cancel();
683     }
684     Element.addClassName(this.element, this.options.hoverClassName)
685     },
686     leaveHover: function() {
687     if (this.options.backgroundColor) {
688     this.element.style.backgroundColor = this.oldBackground;
689     }
690     Element.removeClassName(this.element, this.options.hoverClassName)
691     if (this.saving) return;
692     this.effect = new Effect.Highlight(this.element, {
693     startcolor: this.options.highlightcolor,
694     endcolor: this.options.highlightendcolor,
695     restorecolor: this.originalBackground
696     });
697     },
698     leaveEditMode: function() {
699     Element.removeClassName(this.element, this.options.savingClassName);
700     this.removeForm();
701     this.leaveHover();
702     this.element.style.backgroundColor = this.originalBackground;
703     Element.show(this.element);
704     if (this.options.externalControl) {
705     Element.show(this.options.externalControl);
706     }
707     this.editing = false;
708     this.saving = false;
709     this.oldInnerHTML = null;
710     this.onLeaveEditMode();
711     },
712     onComplete: function(transport) {
713     this.leaveEditMode();
714     this.options.onComplete.bind(this)(transport, this.element);
715     },
716     onEnterEditMode: function() {},
717     onLeaveEditMode: function() {},
718     dispose: function() {
719     if (this.oldInnerHTML) {
720     this.element.innerHTML = this.oldInnerHTML;
721     }
722     this.leaveEditMode();
723     Event.stopObserving(this.element, 'click', this.onclickListener);
724     Event.stopObserving(this.element, 'mouseover', this.mouseoverListener);
725     Event.stopObserving(this.element, 'mouseout', this.mouseoutListener);
726     if (this.options.externalControl) {
727     Event.stopObserving(this.options.externalControl, 'click', this.onclickListener);
728     Event.stopObserving(this.options.externalControl, 'mouseover', this.mouseoverListener);
729     Event.stopObserving(this.options.externalControl, 'mouseout', this.mouseoutListener);
730     }
731     }
732     };
733    
734     // Delayed observer, like Form.Element.Observer,
735     // but waits for delay after last key input
736     // Ideal for live-search fields
737    
738     Form.Element.DelayedObserver = Class.create();
739     Form.Element.DelayedObserver.prototype = {
740     initialize: function(element, delay, callback) {
741     this.delay = delay || 0.5;
742     this.element = $(element);
743     this.callback = callback;
744     this.timer = null;
745     this.lastValue = $F(this.element);
746     Event.observe(this.element,'keyup',this.delayedListener.bindAsEventListener(this));
747     },
748     delayedListener: function(event) {
749     if(this.lastValue == $F(this.element)) return;
750     if(this.timer) clearTimeout(this.timer);
751     this.timer = setTimeout(this.onTimerEvent.bind(this), this.delay * 1000);
752     this.lastValue = $F(this.element);
753     },
754     onTimerEvent: function() {
755     this.timer = null;
756     this.callback(this.element, $F(this.element));
757     }
758     };

Properties

Name Value
svn:mime-type text/cpp

  ViewVC Help
Powered by ViewVC 1.1.26