| 289 | | var tokenPos = this.findLastToken(); |
|---|
| 290 | | if (tokenPos != -1) |
|---|
| 291 | | var ret = this.element.value.substr(tokenPos + 1).replace(/^\s+/,'').replace(/\s+$/,''); |
|---|
| 292 | | else |
|---|
| 293 | | var ret = this.element.value; |
|---|
| 294 | | |
|---|
| 295 | | return /\n/.test(ret) ? '' : ret; |
|---|
| 296 | | }, |
|---|
| 297 | | |
|---|
| 298 | | findLastToken: function() { |
|---|
| 299 | | var lastTokenPos = -1; |
|---|
| 300 | | |
|---|
| 301 | | for (var i=0; i<this.options.tokens.length; i++) { |
|---|
| 302 | | var thisTokenPos = this.element.value.lastIndexOf(this.options.tokens[i]); |
|---|
| 303 | | if (thisTokenPos > lastTokenPos) |
|---|
| 304 | | lastTokenPos = thisTokenPos; |
|---|
| 305 | | } |
|---|
| 306 | | return lastTokenPos; |
|---|
| | 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]); |
|---|
| 453 | | this.element = $(element); |
|---|
| 454 | | |
|---|
| 455 | | this.options = Object.extend({ |
|---|
| 456 | | okButton: true, |
|---|
| 457 | | okText: "ok", |
|---|
| 458 | | cancelLink: true, |
|---|
| 459 | | cancelText: "cancel", |
|---|
| 460 | | savingText: "Saving...", |
|---|
| 461 | | clickToEditText: "Click to edit", |
|---|
| 462 | | okText: "ok", |
|---|
| 463 | | rows: 1, |
|---|
| 464 | | onComplete: function(transport, element) { |
|---|
| 465 | | new Effect.Highlight(element, {startcolor: this.options.highlightcolor}); |
|---|
| 466 | | }, |
|---|
| 467 | | onFailure: function(transport) { |
|---|
| 468 | | alert("Error communicating with the server: " + transport.responseText.stripTags()); |
|---|
| 469 | | }, |
|---|
| 470 | | callback: function(form) { |
|---|
| 471 | | return Form.serialize(form); |
|---|
| 472 | | }, |
|---|
| 473 | | handleLineBreaks: true, |
|---|
| 474 | | loadingText: 'Loading...', |
|---|
| 475 | | savingClassName: 'inplaceeditor-saving', |
|---|
| 476 | | loadingClassName: 'inplaceeditor-loading', |
|---|
| 477 | | formClassName: 'inplaceeditor-form', |
|---|
| 478 | | highlightcolor: Ajax.InPlaceEditor.defaultHighlightColor, |
|---|
| 479 | | highlightendcolor: "#FFFFFF", |
|---|
| 480 | | externalControl: null, |
|---|
| 481 | | submitOnBlur: false, |
|---|
| 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) { |
|---|
| | 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) |
|---|
| 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); |
|---|
| | 500 | this._boundCancelHandler = this.handleFormCancellation.bind(this); |
|---|
| | 501 | this._boundComplete = (this.options.onComplete || Prototype.emptyFunction).bind(this); |
|---|
| | 502 | this._boundFailureHandler = this.handleAJAXFailure.bind(this); |
|---|
| | 503 | this._boundSubmitHandler = this.handleFormSubmission.bind(this); |
|---|
| | 504 | this._boundWrapperHandler = this.wrapUp.bind(this); |
|---|
| | 505 | this.registerListeners(); |
|---|
| | 506 | }, |
|---|
| | 507 | checkForEscapeOrReturn: function(e) { |
|---|
| | 508 | if (!this._editing || e.ctrlKey || e.altKey || e.shiftKey) return; |
|---|
| | 509 | if (Event.KEY_ESC == e.keyCode) |
|---|
| | 510 | this.handleFormCancellation(e); |
|---|
| | 511 | else if (Event.KEY_RETURN == e.keyCode) |
|---|
| | 512 | this.handleFormSubmission(e); |
|---|
| | 513 | }, |
|---|
| | 514 | createControl: function(mode, handler, extraClasses) { |
|---|
| | 515 | var control = this.options[mode + 'Control']; |
|---|
| | 516 | var text = this.options[mode + 'Text']; |
|---|
| | 517 | if ('button' == control) { |
|---|
| | 518 | var btn = document.createElement('input'); |
|---|
| | 519 | btn.type = 'submit'; |
|---|
| | 520 | btn.value = text; |
|---|
| | 521 | btn.className = 'editor_' + mode + '_button'; |
|---|
| | 522 | if ('cancel' == mode) |
|---|
| | 523 | btn.onclick = this._boundCancelHandler; |
|---|
| | 524 | this._form.appendChild(btn); |
|---|
| | 525 | this._controls[mode] = btn; |
|---|
| | 526 | } else if ('link' == control) { |
|---|
| | 527 | var link = document.createElement('a'); |
|---|
| | 528 | link.href = '#'; |
|---|
| | 529 | link.appendChild(document.createTextNode(text)); |
|---|
| | 530 | link.onclick = 'cancel' == mode ? this._boundCancelHandler : this._boundSubmitHandler; |
|---|
| | 531 | link.className = 'editor_' + mode + '_link'; |
|---|
| | 532 | if (extraClasses) |
|---|
| | 533 | link.className += ' ' + extraClasses; |
|---|
| | 534 | this._form.appendChild(link); |
|---|
| | 535 | this._controls[mode] = link; |
|---|
| | 536 | } |
|---|
| | 537 | }, |
|---|
| | 538 | createEditField: function() { |
|---|
| | 539 | var text = (this.options.loadTextURL ? this.options.loadingText : this.getText()); |
|---|
| | 540 | var fld; |
|---|
| | 541 | if (1 >= this.options.rows && !/\r|\n/.test(this.getText())) { |
|---|
| | 542 | fld = document.createElement('input'); |
|---|
| | 543 | fld.type = 'text'; |
|---|
| | 544 | var size = this.options.size || this.options.cols || 0; |
|---|
| | 545 | if (0 < size) fld.size = size; |
|---|
| | 546 | } else { |
|---|
| | 547 | fld = document.createElement('textarea'); |
|---|
| | 548 | fld.rows = (1 >= this.options.rows ? this.options.autoRows : this.options.rows); |
|---|
| | 549 | fld.cols = this.options.cols || 40; |
|---|
| | 550 | } |
|---|
| | 551 | fld.name = this.options.paramName; |
|---|
| | 552 | fld.value = text; // No HTML breaks conversion anymore |
|---|
| | 553 | fld.className = 'editor_field'; |
|---|
| | 554 | if (this.options.submitOnBlur) |
|---|
| | 555 | fld.onblur = this._boundSubmitHandler; |
|---|
| | 556 | this._controls.editor = fld; |
|---|
| | 557 | if (this.options.loadTextURL) |
|---|
| | 558 | this.loadExternalText(); |
|---|
| | 559 | this._form.appendChild(this._controls.editor); |
|---|
| | 560 | }, |
|---|
| | 561 | createForm: function() { |
|---|
| | 562 | var ipe = this; |
|---|
| | 563 | function addText(mode, condition) { |
|---|
| | 564 | var text = ipe.options['text' + mode + 'Controls']; |
|---|
| | 565 | if (!text || condition === false) return; |
|---|
| | 566 | ipe._form.appendChild(document.createTextNode(text)); |
|---|
| | 567 | }; |
|---|
| | 568 | this._form = $(document.createElement('form')); |
|---|
| | 569 | this._form.id = this.options.formId; |
|---|
| | 570 | this._form.addClassName(this.options.formClassName); |
|---|
| | 571 | this._form.onsubmit = this._boundSubmitHandler; |
|---|
| | 572 | this.createEditField(); |
|---|
| | 573 | if ('textarea' == this._controls.editor.tagName.toLowerCase()) |
|---|
| | 574 | this._form.appendChild(document.createElement('br')); |
|---|
| | 575 | if (this.options.onFormCustomization) |
|---|
| | 576 | this.options.onFormCustomization(this, this._form); |
|---|
| | 577 | addText('Before', this.options.okControl || this.options.cancelControl); |
|---|
| | 578 | this.createControl('ok', this._boundSubmitHandler); |
|---|
| | 579 | addText('Between', this.options.okControl && this.options.cancelControl); |
|---|
| | 580 | this.createControl('cancel', this._boundCancelHandler, 'editor_cancel'); |
|---|
| | 581 | addText('After', this.options.okControl || this.options.cancelControl); |
|---|
| | 582 | }, |
|---|
| | 583 | destroy: function() { |
|---|
| | 584 | if (this._oldInnerHTML) |
|---|
| | 585 | this.element.innerHTML = this._oldInnerHTML; |
|---|
| | 586 | this.leaveEditMode(); |
|---|
| | 587 | this.unregisterListeners(); |
|---|
| | 588 | }, |
|---|
| | 589 | enterEditMode: function(e) { |
|---|
| | 590 | if (this._saving || this._editing) return; |
|---|
| | 591 | this._editing = true; |
|---|
| | 592 | this.triggerCallback('onEnterEditMode'); |
|---|
| | 593 | if (this.options.externalControl) |
|---|
| | 594 | this.options.externalControl.hide(); |
|---|
| | 595 | this.element.hide(); |
|---|
| 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 | | if (this.options.okButton) { |
|---|
| 548 | | okButton = document.createElement("input"); |
|---|
| 549 | | okButton.type = "submit"; |
|---|
| 550 | | okButton.value = this.options.okText; |
|---|
| 551 | | this.form.appendChild(okButton); |
|---|
| 552 | | } |
|---|
| 553 | | |
|---|
| 554 | | if (this.options.cancelLink) { |
|---|
| 555 | | cancelLink = document.createElement("a"); |
|---|
| 556 | | cancelLink.href = "#"; |
|---|
| 557 | | cancelLink.appendChild(document.createTextNode(this.options.cancelText)); |
|---|
| 558 | | cancelLink.onclick = this.onclickCancel.bind(this); |
|---|
| 559 | | this.form.appendChild(cancelLink); |
|---|
| 560 | | } |
|---|
| 561 | | }, |
|---|
| 562 | | hasHTMLLineBreaks: function(string) { |
|---|
| 563 | | if (!this.options.handleLineBreaks) return false; |
|---|
| 564 | | return string.match(/<br/i) || string.match(/<p>/i); |
|---|
| 565 | | }, |
|---|
| 566 | | convertHTMLLineBreaks: function(string) { |
|---|
| 567 | | return string.replace(/<br>/gi, "\n").replace(/<br\/>/gi, "\n").replace(/<\/p>/gi, "\n").replace(/<p>/gi, ""); |
|---|
| 568 | | }, |
|---|
| 569 | | createEditField: function() { |
|---|
| 570 | | var text; |
|---|
| 571 | | if(this.options.loadTextURL) { |
|---|
| 572 | | text = this.options.loadingText; |
|---|
| 573 | | } else { |
|---|
| 574 | | text = this.getText(); |
|---|
| 575 | | } |
|---|
| 576 | | |
|---|
| 577 | | var obj = this; |
|---|
| 578 | | |
|---|
| 579 | | if (this.options.rows == 1 && !this.hasHTMLLineBreaks(text)) { |
|---|
| 580 | | this.options.textarea = false; |
|---|
| 581 | | var textField = document.createElement("input"); |
|---|
| 582 | | textField.obj = this; |
|---|
| 583 | | textField.type = "text"; |
|---|
| 584 | | textField.name = "value"; |
|---|
| 585 | | textField.value = text; |
|---|
| 586 | | textField.style.backgroundColor = this.options.highlightcolor; |
|---|
| 587 | | var size = this.options.size || this.options.cols || 0; |
|---|
| 588 | | if (size != 0) textField.size = size; |
|---|
| 589 | | if (this.options.submitOnBlur) |
|---|
| 590 | | textField.onblur = this.onSubmit.bind(this); |
|---|
| 591 | | this.editField = textField; |
|---|
| 592 | | } else { |
|---|
| 593 | | this.options.textarea = true; |
|---|
| 594 | | var textArea = document.createElement("textarea"); |
|---|
| 595 | | textArea.obj = this; |
|---|
| 596 | | textArea.name = "value"; |
|---|
| 597 | | textArea.value = this.convertHTMLLineBreaks(text); |
|---|
| 598 | | textArea.rows = this.options.rows; |
|---|
| 599 | | textArea.cols = this.options.cols || 40; |
|---|
| 600 | | if (this.options.submitOnBlur) |
|---|
| 601 | | textArea.onblur = this.onSubmit.bind(this); |
|---|
| 602 | | this.editField = textArea; |
|---|
| 603 | | } |
|---|
| 604 | | |
|---|
| 605 | | if(this.options.loadTextURL) { |
|---|
| 606 | | this.loadExternalText(); |
|---|
| 607 | | } |
|---|
| 608 | | this.form.appendChild(this.editField); |
|---|
| | 597 | this.element.parentNode.insertBefore(this._form, this.element); |
|---|
| | 598 | if (!this.options.loadTextURL) |
|---|
| | 599 | this.postProcessEditField(); |
|---|
| | 600 | if (e) Event.stop(e); |
|---|
| | 601 | }, |
|---|
| | 602 | enterHover: function(e) { |
|---|
| | 603 | if (this.options.hoverClassName) |
|---|
| | 604 | this.element.addClassName(this.options.hoverClassName); |
|---|
| | 605 | if (this._saving) return; |
|---|
| | 606 | this.triggerCallback('onEnterHover'); |
|---|
| | 611 | handleAJAXFailure: function(transport) { |
|---|
| | 612 | this.triggerCallback('onFailure', transport); |
|---|
| | 613 | if (this._oldInnerHTML) { |
|---|
| | 614 | this.element.innerHTML = this._oldInnerHTML; |
|---|
| | 615 | this._oldInnerHTML = null; |
|---|
| | 616 | } |
|---|
| | 617 | }, |
|---|
| | 618 | handleFormCancellation: function(e) { |
|---|
| | 619 | this.wrapUp(); |
|---|
| | 620 | if (e) Event.stop(e); |
|---|
| | 621 | }, |
|---|
| | 622 | handleFormSubmission: function(e) { |
|---|
| | 623 | var form = this._form; |
|---|
| | 624 | var value = $F(this._controls.editor); |
|---|
| | 625 | this.prepareSubmission(); |
|---|
| | 626 | var params = this.options.callback(form, value) || ''; |
|---|
| | 627 | if (Object.isString(params)) |
|---|
| | 628 | params = params.toQueryParams(); |
|---|
| | 629 | params.editorId = this.element.id; |
|---|
| | 630 | if (this.options.htmlResponse) { |
|---|
| | 631 | var options = Object.extend({ evalScripts: true }, this.options.ajaxOptions); |
|---|
| | 632 | Object.extend(options, { |
|---|
| | 633 | parameters: params, |
|---|
| | 634 | onComplete: this._boundWrapperHandler, |
|---|
| | 635 | onFailure: this._boundFailureHandler |
|---|
| | 636 | }); |
|---|
| | 637 | new Ajax.Updater({ success: this.element }, this.url, options); |
|---|
| | 638 | } else { |
|---|
| | 639 | var options = Object.extend({ method: 'get' }, this.options.ajaxOptions); |
|---|
| | 640 | Object.extend(options, { |
|---|
| | 641 | parameters: params, |
|---|
| | 642 | onComplete: this._boundWrapperHandler, |
|---|
| | 643 | onFailure: this._boundFailureHandler |
|---|
| | 644 | }); |
|---|
| | 645 | new Ajax.Request(this.url, options); |
|---|
| | 646 | } |
|---|
| | 647 | if (e) Event.stop(e); |
|---|
| | 648 | }, |
|---|
| | 649 | leaveEditMode: function() { |
|---|
| | 650 | this.element.removeClassName(this.options.savingClassName); |
|---|
| | 651 | this.removeForm(); |
|---|
| | 652 | this.leaveHover(); |
|---|
| | 653 | this.element.style.backgroundColor = this._originalBackground; |
|---|
| | 654 | this.element.show(); |
|---|
| | 655 | if (this.options.externalControl) |
|---|
| | 656 | this.options.externalControl.show(); |
|---|
| | 657 | this._saving = false; |
|---|
| | 658 | this._editing = false; |
|---|
| | 659 | this._oldInnerHTML = null; |
|---|
| | 660 | this.triggerCallback('onLeaveEditMode'); |
|---|
| | 661 | }, |
|---|
| | 662 | leaveHover: function(e) { |
|---|
| | 663 | if (this.options.hoverClassName) |
|---|
| | 664 | this.element.removeClassName(this.options.hoverClassName); |
|---|
| | 665 | if (this._saving) return; |
|---|
| | 666 | this.triggerCallback('onLeaveHover'); |
|---|
| | 667 | }, |
|---|
| 614 | | Element.addClassName(this.form, this.options.loadingClassName); |
|---|
| 615 | | this.editField.disabled = true; |
|---|
| 616 | | new Ajax.Request( |
|---|
| 617 | | this.options.loadTextURL, |
|---|
| 618 | | Object.extend({ |
|---|
| 619 | | asynchronous: true, |
|---|
| 620 | | onComplete: this.onLoadedExternalText.bind(this) |
|---|
| 621 | | }, this.options.ajaxOptions) |
|---|
| 622 | | ); |
|---|
| 623 | | }, |
|---|
| 624 | | onLoadedExternalText: function(transport) { |
|---|
| 625 | | Element.removeClassName(this.form, this.options.loadingClassName); |
|---|
| 626 | | this.editField.disabled = false; |
|---|
| 627 | | this.editField.value = transport.responseText.stripTags(); |
|---|
| 628 | | }, |
|---|
| 629 | | onclickCancel: function() { |
|---|
| 630 | | this.onComplete(); |
|---|
| 631 | | this.leaveEditMode(); |
|---|
| 632 | | return false; |
|---|
| 633 | | }, |
|---|
| 634 | | onFailure: function(transport) { |
|---|
| 635 | | this.options.onFailure(transport); |
|---|
| 636 | | if (this.oldInnerHTML) { |
|---|
| 637 | | this.element.innerHTML = this.oldInnerHTML; |
|---|
| 638 | | this.oldInnerHTML = null; |
|---|
| 639 | | } |
|---|
| 640 | | return false; |
|---|
| 641 | | }, |
|---|
| 642 | | onSubmit: function() { |
|---|
| 643 | | // onLoading resets these so we need to save them away for the Ajax call |
|---|
| 644 | | var form = this.form; |
|---|
| 645 | | var value = this.editField.value; |
|---|
| 646 | | |
|---|
| 647 | | // do this first, sometimes the ajax call returns before we get a chance to switch on Saving... |
|---|
| 648 | | // which means this will actually switch on Saving... *after* we've left edit mode causing Saving... |
|---|
| 649 | | // to be displayed indefinitely |
|---|
| 650 | | this.onLoading(); |
|---|
| 651 | | |
|---|
| 652 | | new Ajax.Updater( |
|---|
| 653 | | { |
|---|
| 654 | | success: this.element, |
|---|
| 655 | | // don't update on failure (this could be an option) |
|---|
| 656 | | failure: null |
|---|
| 657 | | }, |
|---|
| 658 | | this.url, |
|---|
| 659 | | Object.extend({ |
|---|
| 660 | | parameters: this.options.callback(form, value), |
|---|
| 661 | | onComplete: this.onComplete.bind(this), |
|---|
| 662 | | onFailure: this.onFailure.bind(this) |
|---|
| 663 | | }, this.options.ajaxOptions) |
|---|
| 664 | | ); |
|---|
| 665 | | // stop the event to avoid a page refresh in Safari |
|---|
| 666 | | if (arguments.length > 1) { |
|---|
| 667 | | Event.stop(arguments[0]); |
|---|
| 668 | | } |
|---|
| 669 | | return false; |
|---|
| 670 | | }, |
|---|
| 671 | | onLoading: function() { |
|---|
| 672 | | this.saving = true; |
|---|
| | 669 | this._form.addClassName(this.options.loadingClassName); |
|---|
| | 670 | this._controls.editor.disabled = true; |
|---|
| | 671 | var options = Object.extend({ method: 'get' }, this.options.ajaxOptions); |
|---|
| | 672 | Object.extend(options, { |
|---|
| | 673 | parameters: 'editorId=' + encodeURIComponent(this.element.id), |
|---|
| | 674 | onComplete: Prototype.emptyFunction, |
|---|
| | 675 | onSuccess: function(transport) { |
|---|
| | 676 | this._form.removeClassName(this.options.loadingClassName); |
|---|
| | 677 | var text = transport.responseText; |
|---|
| | 678 | if (this.options.stripLoadedTextTags) |
|---|
| | 679 | text = text.stripTags(); |
|---|
| | 680 | this._controls.editor.value = text; |
|---|
| | 681 | this._controls.editor.disabled = false; |
|---|
| | 682 | this.postProcessEditField(); |
|---|
| | 683 | }.bind(this), |
|---|
| | 684 | onFailure: this._boundFailureHandler |
|---|
| | 685 | }); |
|---|
| | 686 | new Ajax.Request(this.options.loadTextURL, options); |
|---|
| | 687 | }, |
|---|
| | 688 | postProcessEditField: function() { |
|---|
| | 689 | var fpc = this.options.fieldPostCreation; |
|---|
| | 690 | if (fpc) |
|---|
| | 691 | $(this._controls.editor)['focus' == fpc ? 'focus' : 'activate'](); |
|---|
| | 692 | }, |
|---|
| | 693 | prepareOptions: function() { |
|---|
| | 694 | this.options = Object.clone(Ajax.InPlaceEditor.DefaultOptions); |
|---|
| | 695 | Object.extend(this.options, Ajax.InPlaceEditor.DefaultCallbacks); |
|---|
| | 696 | [this._extraDefaultOptions].flatten().compact().each(function(defs) { |
|---|
| | 697 | Object.extend(this.options, defs); |
|---|
| | 698 | }.bind(this)); |
|---|
| | 699 | }, |
|---|
| | 700 | prepareSubmission: function() { |
|---|
| | 701 | this._saving = true; |
|---|
| 680 | | Element.addClassName(this.element, this.options.savingClassName); |
|---|
| 681 | | this.element.style.backgroundColor = this.originalBackground; |
|---|
| 682 | | Element.show(this.element); |
|---|
| 683 | | }, |
|---|
| 684 | | removeForm: function() { |
|---|
| 685 | | if(this.form) { |
|---|
| 686 | | if (this.form.parentNode) Element.remove(this.form); |
|---|
| 687 | | this.form = null; |
|---|
| 688 | | } |
|---|
| 689 | | }, |
|---|
| 690 | | enterHover: function() { |
|---|
| 691 | | if (this.saving) return; |
|---|
| 692 | | this.element.style.backgroundColor = this.options.highlightcolor; |
|---|
| 693 | | if (this.effect) { |
|---|
| 694 | | this.effect.cancel(); |
|---|
| 695 | | } |
|---|
| 696 | | Element.addClassName(this.element, this.options.hoverClassName) |
|---|
| 697 | | }, |
|---|
| 698 | | leaveHover: function() { |
|---|
| 699 | | if (this.options.backgroundColor) { |
|---|
| 700 | | this.element.style.backgroundColor = this.oldBackground; |
|---|
| 701 | | } |
|---|
| 702 | | Element.removeClassName(this.element, this.options.hoverClassName) |
|---|
| 703 | | if (this.saving) return; |
|---|
| 704 | | this.effect = new Effect.Highlight(this.element, { |
|---|
| 705 | | startcolor: this.options.highlightcolor, |
|---|
| 706 | | endcolor: this.options.highlightendcolor, |
|---|
| 707 | | restorecolor: this.originalBackground |
|---|
| | 727 | this.element.addClassName(this.options.savingClassName); |
|---|
| | 728 | this.element.style.backgroundColor = this._originalBackground; |
|---|
| | 729 | this.element.show(); |
|---|
| | 730 | }, |
|---|
| | 731 | triggerCallback: function(cbName, arg) { |
|---|
| | 732 | if ('function' == typeof this.options[cbName]) { |
|---|
| | 733 | this.options[cbName](this, arg); |
|---|
| | 734 | } |
|---|
| | 735 | }, |
|---|
| | 736 | unregisterListeners: function() { |
|---|
| | 737 | $H(this._listeners).each(function(pair) { |
|---|
| | 738 | if (!this.options.externalControlOnly) |
|---|
| | 739 | this.element.stopObserving(pair.key, pair.value); |
|---|
| | 740 | if (this.options.externalControl) |
|---|
| | 741 | this.options.externalControl.stopObserving(pair.key, pair.value); |
|---|
| | 742 | }.bind(this)); |
|---|
| | 743 | }, |
|---|
| | 744 | wrapUp: function(transport) { |
|---|
| | 745 | this.leaveEditMode(); |
|---|
| | 746 | // Can't use triggerCallback due to backward compatibility: requires |
|---|
| | 747 | // binding + direct element |
|---|
| | 748 | this._boundComplete(transport, this.element); |
|---|
| | 749 | } |
|---|
| | 750 | }); |
|---|
| | 751 | |
|---|