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