Subversion Repositories Applications.papyrus

Rev

Rev 1371 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
11
dojo.provide("dojo.widget.Editor");
12
dojo.deprecated("dojo.widget.Editor", "is replaced by dojo.widget.Editor2", "0.5");
13
dojo.require("dojo.io.*");
14
dojo.require("dojo.widget.*");
15
dojo.require("dojo.widget.Toolbar");
16
dojo.require("dojo.widget.RichText");
17
dojo.require("dojo.widget.ColorPalette");
18
dojo.require("dojo.string.extras");
19
dojo.widget.tags.addParseTreeHandler("dojo:Editor");
20
dojo.widget.Editor = function () {
21
	dojo.widget.HtmlWidget.call(this);
22
	this.contentFilters = [];
23
	this._toolbars = [];
24
};
25
dojo.inherits(dojo.widget.Editor, dojo.widget.HtmlWidget);
26
dojo.widget.Editor.itemGroups = {textGroup:["bold", "italic", "underline", "strikethrough"], blockGroup:["formatBlock", "fontName", "fontSize"], justifyGroup:["justifyleft", "justifycenter", "justifyright"], commandGroup:["save", "cancel"], colorGroup:["forecolor", "hilitecolor"], listGroup:["insertorderedlist", "insertunorderedlist"], indentGroup:["outdent", "indent"], linkGroup:["createlink", "insertimage", "inserthorizontalrule"]};
27
dojo.widget.Editor.formatBlockValues = {"Normal":"p", "Main heading":"h2", "Sub heading":"h3", "Sub sub heading":"h4", "Preformatted":"pre"};
28
dojo.widget.Editor.fontNameValues = {"Arial":"Arial, Helvetica, sans-serif", "Verdana":"Verdana, sans-serif", "Times New Roman":"Times New Roman, serif", "Courier":"Courier New, monospace"};
29
dojo.widget.Editor.fontSizeValues = {"1 (8 pt)":"1", "2 (10 pt)":"2", "3 (12 pt)":"3", "4 (14 pt)":"4", "5 (18 pt)":"5", "6 (24 pt)":"6", "7 (36 pt)":"7"};
30
dojo.widget.Editor.defaultItems = ["commandGroup", "|", "blockGroup", "|", "textGroup", "|", "colorGroup", "|", "justifyGroup", "|", "listGroup", "indentGroup", "|", "linkGroup"];
31
dojo.widget.Editor.supportedCommands = ["save", "cancel", "|", "-", "/", " "];
32
dojo.lang.extend(dojo.widget.Editor, {widgetType:"Editor", saveUrl:"", saveMethod:"post", saveArgName:"editorContent", closeOnSave:false, items:dojo.widget.Editor.defaultItems, formatBlockItems:dojo.lang.shallowCopy(dojo.widget.Editor.formatBlockValues), fontNameItems:dojo.lang.shallowCopy(dojo.widget.Editor.fontNameValues), fontSizeItems:dojo.lang.shallowCopy(dojo.widget.Editor.fontSizeValues), getItemProperties:function (name) {
33
	var props = {};
34
	switch (name.toLowerCase()) {
35
	  case "bold":
36
	  case "italic":
37
	  case "underline":
38
	  case "strikethrough":
39
		props.toggleItem = true;
40
		break;
41
	  case "justifygroup":
42
		props.defaultButton = "justifyleft";
43
		props.preventDeselect = true;
44
		props.buttonGroup = true;
45
		break;
46
	  case "listgroup":
47
		props.buttonGroup = true;
48
		break;
49
	  case "save":
50
	  case "cancel":
51
		props.label = dojo.string.capitalize(name);
52
		break;
53
	  case "forecolor":
54
	  case "hilitecolor":
55
		props.name = name;
56
		props.toggleItem = true;
57
		props.icon = this.getCommandImage(name);
58
		break;
59
	  case "formatblock":
60
		props.name = "formatBlock";
61
		props.values = this.formatBlockItems;
62
		break;
63
	  case "fontname":
64
		props.name = "fontName";
65
		props.values = this.fontNameItems;
66
	  case "fontsize":
67
		props.name = "fontSize";
68
		props.values = this.fontSizeItems;
69
	}
70
	return props;
71
}, validateItems:true, focusOnLoad:true, minHeight:"1em", _richText:null, _richTextType:"RichText", _toolbarContainer:null, _toolbarContainerType:"ToolbarContainer", _toolbars:[], _toolbarType:"Toolbar", _toolbarItemType:"ToolbarItem", buildRendering:function (args, frag) {
72
	var node = frag["dojo:" + this.widgetType.toLowerCase()]["nodeRef"];
73
	var trt = dojo.widget.createWidget(this._richTextType, {focusOnLoad:this.focusOnLoad, minHeight:this.minHeight}, node);
74
	var _this = this;
75
	setTimeout(function () {
76
		_this.setRichText(trt);
77
		_this.initToolbar();
78
		_this.fillInTemplate(args, frag);
79
	}, 0);
80
}, setRichText:function (richText) {
81
	if (this._richText && this._richText == richText) {
82
		dojo.debug("Already set the richText to this richText!");
83
		return;
84
	}
85
	if (this._richText && !this._richText.isClosed) {
86
		dojo.debug("You are switching richTexts yet you haven't closed the current one. Losing reference!");
87
	}
88
	this._richText = richText;
89
	dojo.event.connect(this._richText, "close", this, "onClose");
90
	dojo.event.connect(this._richText, "onLoad", this, "onLoad");
91
	dojo.event.connect(this._richText, "onDisplayChanged", this, "updateToolbar");
92
	if (this._toolbarContainer) {
93
		this._toolbarContainer.enable();
94
		this.updateToolbar(true);
95
	}
96
}, initToolbar:function () {
97
	if (this._toolbarContainer) {
98
		return;
99
	}
100
	this._toolbarContainer = dojo.widget.createWidget(this._toolbarContainerType);
101
	var tb = this.addToolbar();
102
	var last = true;
103
	for (var i = 0; i < this.items.length; i++) {
104
		if (this.items[i] == "\n") {
105
			tb = this.addToolbar();
106
		} else {
107
			if ((this.items[i] == "|") && (!last)) {
108
				last = true;
109
			} else {
110
				last = this.addItem(this.items[i], tb);
111
			}
112
		}
113
	}
114
	this.insertToolbar(this._toolbarContainer.domNode, this._richText.domNode);
115
}, insertToolbar:function (tbNode, richTextNode) {
116
	dojo.html.insertBefore(tbNode, richTextNode);
117
}, addToolbar:function (toolbar) {
118
	this.initToolbar();
119
	if (!(toolbar instanceof dojo.widget.Toolbar)) {
120
		toolbar = dojo.widget.createWidget(this._toolbarType);
121
	}
122
	this._toolbarContainer.addChild(toolbar);
123
	this._toolbars.push(toolbar);
124
	return toolbar;
125
}, addItem:function (item, tb, dontValidate) {
126
	if (!tb) {
127
		tb = this._toolbars[0];
128
	}
129
	var cmd = ((item) && (!dojo.lang.isUndefined(item["getValue"]))) ? cmd = item["getValue"]() : item;
130
	var groups = dojo.widget.Editor.itemGroups;
131
	if (item instanceof dojo.widget.ToolbarItem) {
132
		tb.addChild(item);
133
	} else {
134
		if (groups[cmd]) {
135
			var group = groups[cmd];
136
			var worked = true;
137
			if (cmd == "justifyGroup" || cmd == "listGroup") {
138
				var btnGroup = [cmd];
139
				for (var i = 0; i < group.length; i++) {
140
					if (dontValidate || this.isSupportedCommand(group[i])) {
141
						btnGroup.push(this.getCommandImage(group[i]));
142
					} else {
143
						worked = false;
144
					}
145
				}
146
				if (btnGroup.length) {
147
					var btn = tb.addChild(btnGroup, null, this.getItemProperties(cmd));
148
					dojo.event.connect(btn, "onClick", this, "_action");
149
					dojo.event.connect(btn, "onChangeSelect", this, "_action");
150
				}
151
				return worked;
152
			} else {
153
				for (var i = 0; i < group.length; i++) {
154
					if (!this.addItem(group[i], tb)) {
155
						worked = false;
156
					}
157
				}
158
				return worked;
159
			}
160
		} else {
161
			if ((!dontValidate) && (!this.isSupportedCommand(cmd))) {
162
				return false;
163
			}
164
			if (dontValidate || this.isSupportedCommand(cmd)) {
165
				cmd = cmd.toLowerCase();
166
				if (cmd == "formatblock") {
167
					var select = dojo.widget.createWidget("ToolbarSelect", {name:"formatBlock", values:this.formatBlockItems});
168
					tb.addChild(select);
169
					var _this = this;
170
					dojo.event.connect(select, "onSetValue", function (item, value) {
171
						_this.onAction("formatBlock", value);
172
					});
173
				} else {
174
					if (cmd == "fontname") {
175
						var select = dojo.widget.createWidget("ToolbarSelect", {name:"fontName", values:this.fontNameItems});
176
						tb.addChild(select);
177
						dojo.event.connect(select, "onSetValue", dojo.lang.hitch(this, function (item, value) {
178
							this.onAction("fontName", value);
179
						}));
180
					} else {
181
						if (cmd == "fontsize") {
182
							var select = dojo.widget.createWidget("ToolbarSelect", {name:"fontSize", values:this.fontSizeItems});
183
							tb.addChild(select);
184
							dojo.event.connect(select, "onSetValue", dojo.lang.hitch(this, function (item, value) {
185
								this.onAction("fontSize", value);
186
							}));
187
						} else {
188
							if (dojo.lang.inArray(cmd, ["forecolor", "hilitecolor"])) {
189
								var btn = tb.addChild(dojo.widget.createWidget("ToolbarColorDialog", this.getItemProperties(cmd)));
190
								dojo.event.connect(btn, "onSetValue", this, "_setValue");
191
							} else {
192
								var btn = tb.addChild(this.getCommandImage(cmd), null, this.getItemProperties(cmd));
193
								if (cmd == "save") {
194
									dojo.event.connect(btn, "onClick", this, "_save");
195
								} else {
196
									if (cmd == "cancel") {
197
										dojo.event.connect(btn, "onClick", this, "_close");
198
									} else {
199
										dojo.event.connect(btn, "onClick", this, "_action");
200
										dojo.event.connect(btn, "onChangeSelect", this, "_action");
201
									}
202
								}
203
							}
204
						}
205
					}
206
				}
207
			}
208
		}
209
	}
210
	return true;
211
}, enableToolbar:function () {
212
	if (this._toolbarContainer) {
213
		this._toolbarContainer.domNode.style.display = "";
214
		this._toolbarContainer.enable();
215
	}
216
}, disableToolbar:function (hide) {
217
	if (hide) {
218
		if (this._toolbarContainer) {
219
			this._toolbarContainer.domNode.style.display = "none";
220
		}
221
	} else {
222
		if (this._toolbarContainer) {
223
			this._toolbarContainer.disable();
224
		}
225
	}
226
}, _updateToolbarLastRan:null, _updateToolbarTimer:null, _updateToolbarFrequency:500, updateToolbar:function (force) {
227
	if (!this._toolbarContainer) {
228
		return;
229
	}
230
	var diff = new Date() - this._updateToolbarLastRan;
231
	if (!force && this._updateToolbarLastRan && (diff < this._updateToolbarFrequency)) {
232
		clearTimeout(this._updateToolbarTimer);
233
		var _this = this;
234
		this._updateToolbarTimer = setTimeout(function () {
235
			_this.updateToolbar();
236
		}, this._updateToolbarFrequency / 2);
237
		return;
238
	} else {
239
		this._updateToolbarLastRan = new Date();
240
	}
241
	var items = this._toolbarContainer.getItems();
242
	for (var i = 0; i < items.length; i++) {
243
		var item = items[i];
244
		if (item instanceof dojo.widget.ToolbarSeparator) {
245
			continue;
246
		}
247
		var cmd = item._name;
248
		if (cmd == "save" || cmd == "cancel") {
249
			continue;
250
		} else {
251
			if (cmd == "justifyGroup") {
252
				try {
253
					if (!this._richText.queryCommandEnabled("justifyleft")) {
254
						item.disable(false, true);
255
					} else {
256
						item.enable(false, true);
257
						var jitems = item.getItems();
258
						for (var j = 0; j < jitems.length; j++) {
259
							var name = jitems[j]._name;
260
							var value = this._richText.queryCommandValue(name);
261
							if (typeof value == "boolean" && value) {
262
								value = name;
263
								break;
264
							} else {
265
								if (typeof value == "string") {
266
									value = "justify" + value;
267
								} else {
268
									value = null;
269
								}
270
							}
271
						}
272
						if (!value) {
273
							value = "justifyleft";
274
						}
275
						item.setValue(value, false, true);
276
					}
277
				}
278
				catch (err) {
279
				}
280
			} else {
281
				if (cmd == "listGroup") {
282
					var litems = item.getItems();
283
					for (var j = 0; j < litems.length; j++) {
284
						this.updateItem(litems[j]);
285
					}
286
				} else {
287
					this.updateItem(item);
288
				}
289
			}
290
		}
291
	}
292
}, updateItem:function (item) {
293
	try {
294
		var cmd = item._name;
295
		var enabled = this._richText.queryCommandEnabled(cmd);
296
		item.setEnabled(enabled, false, true);
297
		var active = this._richText.queryCommandState(cmd);
298
		if (active && cmd == "underline") {
299
			active = !this._richText.queryCommandEnabled("unlink");
300
		}
301
		item.setSelected(active, false, true);
302
		return true;
303
	}
304
	catch (err) {
305
		return false;
306
	}
307
}, supportedCommands:dojo.widget.Editor.supportedCommands.concat(), isSupportedCommand:function (cmd) {
308
	var yes = dojo.lang.inArray(cmd, this.supportedCommands);
309
	if (!yes) {
310
		try {
311
			var richText = this._richText || dojo.widget.HtmlRichText.prototype;
312
			yes = richText.queryCommandAvailable(cmd);
313
		}
314
		catch (E) {
315
		}
316
	}
317
	return yes;
318
}, getCommandImage:function (cmd) {
319
	if (cmd == "|") {
320
		return cmd;
321
	} else {
322
		return dojo.uri.moduleUri("dojo.widget", "templates/buttons/" + cmd + ".gif");
323
	}
324
}, _action:function (e) {
325
	this._fire("onAction", e.getValue());
326
}, _setValue:function (a, b) {
327
	this._fire("onAction", a.getValue(), b);
328
}, _save:function (e) {
329
	if (!this._richText.isClosed) {
330
		if (this.saveUrl.length) {
331
			var content = {};
332
			content[this.saveArgName] = this.getHtml();
333
			dojo.io.bind({method:this.saveMethod, url:this.saveUrl, content:content});
334
		} else {
335
			dojo.debug("please set a saveUrl for the editor");
336
		}
337
		if (this.closeOnSave) {
338
			this._richText.close(e.getName().toLowerCase() == "save");
339
		}
340
	}
341
}, _close:function (e) {
342
	if (!this._richText.isClosed) {
343
		this._richText.close(e.getName().toLowerCase() == "save");
344
	}
345
}, onAction:function (cmd, value) {
346
	switch (cmd) {
347
	  case "createlink":
348
		if (!(value = prompt("Please enter the URL of the link:", "http://"))) {
349
			return;
350
		}
351
		break;
352
	  case "insertimage":
353
		if (!(value = prompt("Please enter the URL of the image:", "http://"))) {
354
			return;
355
		}
356
		break;
357
	}
358
	this._richText.execCommand(cmd, value);
359
}, fillInTemplate:function (args, frag) {
360
}, _fire:function (eventName) {
361
	if (dojo.lang.isFunction(this[eventName])) {
362
		var args = [];
363
		if (arguments.length == 1) {
364
			args.push(this);
365
		} else {
366
			for (var i = 1; i < arguments.length; i++) {
367
				args.push(arguments[i]);
368
			}
369
		}
370
		this[eventName].apply(this, args);
371
	}
372
}, getHtml:function () {
373
	this._richText.contentFilters = this._richText.contentFilters.concat(this.contentFilters);
374
	return this._richText.getEditorContent();
375
}, getEditorContent:function () {
376
	return this.getHtml();
377
}, onClose:function (save, hide) {
378
	this.disableToolbar(hide);
379
	if (save) {
380
		this._fire("onSave");
381
	} else {
382
		this._fire("onCancel");
383
	}
384
}, onLoad:function () {
385
}, onSave:function () {
386
}, onCancel:function () {
387
}});
388