Subversion Repositories Applications.papyrus

Rev

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