Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
Prado.WebUI.TInPlaceTextBox = Base.extend(
2
{
3
	constructor : function(options)
4
	{
5
 
6
		this.isSaving = false;
7
		this.isEditing = false;
8
		this.editField = null;
9
		this.readOnly = options.ReadOnly;
10
 
11
		this.options = Object.extend(
12
		{
13
			LoadTextFromSource : false,
14
			TextMode : 'SingleLine'
15
 
16
		}, options || {});
17
		this.element = $(this.options.ID);
18
		Prado.WebUI.TInPlaceTextBox.register(this);
19
		this.createEditorInput();
20
		this.initializeListeners();
21
	},
22
 
23
	/**
24
	 * Initialize the listeners.
25
	 */
26
	initializeListeners : function()
27
	{
28
		this.onclickListener = this.enterEditMode.bindAsEventListener(this);
29
	    Event.observe(this.element, 'click', this.onclickListener);
30
	    if (this.options.ExternalControl)
31
			Event.observe($(this.options.ExternalControl), 'click', this.onclickListener);
32
	},
33
 
34
	/**
35
	 * Changes the panel to an editable input.
36
	 * @param {Event} evt event source
37
	 */
38
	enterEditMode :  function(evt)
39
	{
40
	    if (this.isSaving || this.isEditing || this.readOnly) return;
41
	    this.isEditing = true;
42
		this.onEnterEditMode();
43
		this.createEditorInput();
44
		this.showTextBox();
45
		this.editField.disabled = false;
46
		if(this.options.LoadTextOnEdit)
47
			this.loadExternalText();
48
		Prado.Element.focus(this.editField);
49
		if (evt)
50
			Event.stop(evt);
51
    	return false;
52
	},
53
 
54
	exitEditMode : function(evt)
55
	{
56
		this.isEditing = false;
57
		this.isSaving = false;
58
		this.editField.disabled = false;
59
		this.element.innerHTML = this.editField.value;
60
		this.showLabel();
61
	},
62
 
63
	showTextBox : function()
64
	{
65
		Element.hide(this.element);
66
		Element.show(this.editField);
67
	},
68
 
69
	showLabel : function()
70
	{
71
		Element.show(this.element);
72
		Element.hide(this.editField);
73
	},
74
 
75
	/**
76
	 * Create the edit input field.
77
	 */
78
	createEditorInput : function()
79
	{
80
		if(this.editField == null)
81
			this.createTextBox();
82
 
83
		this.editField.value = this.getText();
84
	},
85
 
86
	loadExternalText : function()
87
	{
88
		this.editField.disabled = true;
89
		this.onLoadingText();
90
		options = new Array('__InlineEditor_loadExternalText__', this.getText());
91
		request = new Prado.CallbackRequest(this.options.EventTarget, this.options);
92
		request.setCausesValidation(false);
93
		request.setCallbackParameter(options);
94
		request.ActiveControl.onSuccess = this.onloadExternalTextSuccess.bind(this);
95
		request.ActiveControl.onFailure = this.onloadExternalTextFailure.bind(this);
96
		request.dispatch();
97
	},
98
 
99
	/**
100
	 * Create a new input textbox or textarea
101
	 */
102
	createTextBox : function()
103
	{
104
		cssClass= this.element.className || '';
105
		inputName = this.options.EventTarget;
106
		options = {'className' : cssClass, name : inputName, id : this.options.TextBoxID};
107
		if(this.options.TextMode == 'SingleLine')
108
		{
109
			if(this.options.MaxLength > 0)
110
				options['maxlength'] = this.options.MaxLength;
111
			this.editField = INPUT(options);
112
		}
113
		else
114
		{
115
			if(this.options.Rows > 0)
116
				options['rows'] = this.options.Rows;
117
			if(this.options.Columns > 0)
118
				options['cols'] = this.options.Columns;
119
			if(this.options.Wrap)
120
				options['wrap'] = 'off';
121
			this.editField = TEXTAREA(options);
122
		}
123
 
124
		this.editField.style.display="none";
125
		this.element.parentNode.insertBefore(this.editField,this.element)
126
 
127
		//handle return key within single line textbox
128
		if(this.options.TextMode == 'SingleLine')
129
		{
130
			Event.observe(this.editField, "keydown", function(e)
131
			{
132
				 if(Event.keyCode(e) == Event.KEY_RETURN)
133
		        {
134
					var target = Event.element(e);
135
					if(target)
136
					{
137
						Event.fireEvent(target, "blur");
138
						Event.stop(e);
139
					}
140
				}
141
			});
142
		}
143
 
144
		Event.observe(this.editField, "blur", this.onTextBoxBlur.bind(this));
145
		Event.observe(this.editField, "keypress", this.onKeyPressed.bind(this));
146
	},
147
 
148
	/**
149
	 * @return {String} panel inner html text.
150
	 */
151
	getText: function()
152
	{
153
    	return this.element.innerHTML;
154
  	},
155
 
156
	/**
157
	 * Edit mode entered, calls optional event handlers.
158
	 */
159
	onEnterEditMode : function()
160
	{
161
		if(typeof(this.options.onEnterEditMode) == "function")
162
			this.options.onEnterEditMode(this,null);
163
	},
164
 
165
	onTextBoxBlur : function(e)
166
	{
167
		text = this.element.innerHTML;
168
		if(this.options.AutoPostBack && text != this.editField.value)
169
		{
170
			if(this.isEditing)
171
				this.onTextChanged(text);
172
		}
173
		else
174
		{
175
			this.element.innerHTML = this.editField.value;
176
			this.isEditing = false;
177
			if(this.options.AutoHide)
178
				this.showLabel();
179
		}
180
	},
181
 
182
	onKeyPressed : function(e)
183
	{
184
		if (Event.keyCode(e) == Event.KEY_ESC)
185
		{
186
			this.editField.value = this.getText();
187
			this.isEditing = false;
188
			if(this.options.AutoHide)
189
				this.showLabel();
190
		}
191
		else if (Event.keyCode(e) == Event.KEY_RETURN && this.options.TextMode != 'MultiLine')
192
			Event.stop(e);
193
	},
194
 
195
	/**
196
	 * When the text input value has changed.
197
	 * @param {String} original text
198
	 */
199
	onTextChanged : function(text)
200
	{
201
		request = new Prado.CallbackRequest(this.options.EventTarget, this.options);
202
		request.setCallbackParameter(text);
203
		request.ActiveControl.onSuccess = this.onTextChangedSuccess.bind(this);
204
		request.ActiveControl.onFailure = this.onTextChangedFailure.bind(this);
205
		if(request.dispatch())
206
		{
207
			this.isSaving = true;
208
			this.editField.disabled = true;
209
		}
210
	},
211
 
212
	/**
213
	 * When loading external text.
214
	 */
215
	onLoadingText : function()
216
	{
217
		//Logger.info("on loading text");
218
	},
219
 
220
	onloadExternalTextSuccess : function(request, parameter)
221
	{
222
		this.isEditing = true;
223
		this.editField.disabled = false;
224
		this.editField.value = this.getText();
225
		Prado.Element.focus(this.editField);
226
		if(typeof(this.options.onSuccess)=="function")
227
			this.options.onSuccess(sender,parameter);
228
	},
229
 
230
	onloadExternalTextFailure : function(request, parameter)
231
	{
232
		this.isSaving = false;
233
		this.isEditing = false;
234
		this.showLabel();
235
		if(typeof(this.options.onFailure)=="function")
236
			this.options.onFailure(sender,parameter);
237
	},
238
 
239
	/**
240
	 * Text change successfully.
241
	 * @param {Object} sender
242
	 * @param {Object} parameter
243
	 */
244
	onTextChangedSuccess : function(sender, parameter)
245
	{
246
		this.isSaving = false;
247
		this.isEditing = false;
248
		if(this.options.AutoHide)
249
			this.showLabel();
250
		this.element.innerHTML = parameter == null ? this.editField.value : parameter;
251
		this.editField.disabled = false;
252
		if(typeof(this.options.onSuccess)=="function")
253
			this.options.onSuccess(sender,parameter);
254
	},
255
 
256
	onTextChangedFailure : function(sender, parameter)
257
	{
258
		this.editField.disabled = false;
259
		this.isSaving = false;
260
		this.isEditing = false;
261
		if(typeof(this.options.onFailure)=="function")
262
			this.options.onFailure(sender,parameter);
263
	}
264
},
265
{
266
	textboxes : {},
267
 
268
	register : function(obj)
269
	{
270
		Prado.WebUI.TInPlaceTextBox.textboxes[obj.options.TextBoxID] = obj;
271
	},
272
 
273
	setDisplayTextBox : function(id,value)
274
	{
275
		var textbox = Prado.WebUI.TInPlaceTextBox.textboxes[id];
276
		if(textbox)
277
		{
278
			if(value)
279
				textbox.enterEditMode(null);
280
			else
281
			{
282
				textbox.exitEditMode(null);
283
			}
284
		}
285
	},
286
 
287
	setReadOnly : function(id, value)
288
	{
289
		var textbox = Prado.WebUI.TInPlaceTextBox.textboxes[id];
290
		if (textbox)
291
		{
292
			textbox.readOnly=value;
293
		}
294
	}
295
});