1 // Copyright (c) 2006 spinelz.org (http://script.spinelz.org/)
3 // This code is substantially based on code from script.aculo.us which has the
4 // following copyright and permission notice
6 // Copyright (c) 2005 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 InPlaceEditorEx = Class.create();
28 Object.extend(Object.extend(InPlaceEditorEx.prototype, Ajax.InPlaceEditor.prototype), {
29 /*********************************************
31 * The URL option is unnecessary because
32 * of updating without Ajax.
33 *********************************************
34 initialize: function(element, url, options) {
36 *********************************************/
37 initialize: function(element, options) {
38 /*********************************************/
39 this.element = $(element);
41 this.options = Object.extend({
46 savingText: "Saving...",
47 clickToEditText: "Click to edit",
50 onComplete: function(transport, element) {
51 new Effect.Highlight(element, {startcolor: this.options.highlightcolor});
53 onFailure: function(transport) {
54 alert("Error communicating with the server: " + transport.responseText.stripTags());
56 callback: function(form) {
57 return Form.serialize(form);
59 handleLineBreaks: true,
60 loadingText: 'Loading...',
61 savingClassName: 'inplaceeditor-saving',
62 loadingClassName: 'inplaceeditor-loading',
63 formClassName: 'inplaceeditor-form',
64 highlightcolor: Ajax.InPlaceEditor.defaultHighlightColor,
65 highlightendcolor: "#FFFFFF",
66 externalControl: null,
72 if(!this.options.formId && this.element.id) {
73 this.options.formId = this.element.id + "-inplaceeditor";
74 if ($(this.options.formId)) {
75 // there's already a form with that name, don't specify an id
76 this.options.formId = null;
80 if (this.options.externalControl) {
81 this.options.externalControl = $(this.options.externalControl);
84 this.originalBackground = Element.getStyle(this.element, 'background-color');
85 if (!this.originalBackground) {
86 this.originalBackground = "transparent";
89 this.element.title = this.options.clickToEditText;
91 /*********************************************
93 * The event to enter edit mode is changed
94 * from single-click to double-click.
95 *********************************************
96 this.onclickListener = this.enterEditMode.bindAsEventListener(this);
97 this.mouseoverListener = this.enterHover.bindAsEventListener(this);
98 this.mouseoutListener = this.leaveHover.bindAsEventListener(this);
99 Event.observe(this.element, 'click', this.onclickListener);
100 Event.observe(this.element, 'mouseover', this.mouseoverListener);
101 Event.observe(this.element, 'mouseout', this.mouseoutListener);
102 if (this.options.externalControl) {
103 Event.observe(this.options.externalControl, 'click', this.onclickListener);
104 Event.observe(this.options.externalControl, 'mouseover', this.mouseoverListener);
105 Event.observe(this.options.externalControl, 'mouseout', this.mouseoutListener);
107 *********************************************/
108 this.ondblclickListener = this.enterEditMode.bindAsEventListener(this);
109 this.mouseoverListener = this.enterHover.bindAsEventListener(this);
110 this.mouseoutListener = this.leaveHover.bindAsEventListener(this);
111 Event.observe(this.element, 'dblclick', this.ondblclickListener);
112 Event.observe(this.element, 'mouseover', this.mouseoverListener);
113 Event.observe(this.element, 'mouseout', this.mouseoutListener);
114 if (this.options.externalControl) {
115 Event.observe(this.options.externalControl, 'dblclick', this.ondblclickListener);
116 Event.observe(this.options.externalControl, 'mouseover', this.mouseoverListener);
117 Event.observe(this.options.externalControl, 'mouseout', this.mouseoutListener);
119 /*********************************************/
121 getText: function() {
122 return this.element.innerHTML.unescapeHTML();
124 onSubmit: function() {
125 var value = this.editField.value.escapeHTML();
127 this.onclickCancel();
131 this.element.innerHTML = value;
132 new InPlaceEditorEx(this.element.id, this.options);
136 removeForm: function() {
138 if(this.form.parentNode) {
139 var childNodes = this.form.parentNode.childNodes;
140 for(var i = 0; i < childNodes.length; i++) {
141 childNodes[i].data = '';
143 Element.remove(this.form);