Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | 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
 
11
dojo.provide("dojo.widget.Editor2Plugin.ContextMenu");
12
dojo.require("dojo.widget.Menu2");
13
dojo.event.topic.subscribe("dojo.widget.Editor2::onLoad", function (editor) {
14
	dojo.widget.Editor2Plugin.ContextMenuManager.getContextMenu(editor);
15
});
16
dojo.widget.Editor2Plugin.ContextMenuManager = {menuGroups:["Generic", "Link", "Anchor", "Image", "List", "Table"], _contextMenuGroupSets:{}, _registeredGroups:{}, _menus:{}, registerGroup:function (name, handler) {
17
	if (this._registeredGroups[name]) {
18
		alert("dojo.widget.Editor2Plugin.ContextMenuManager.registerGroup: menu group " + name + "is already registered. Ignored.");
19
		return;
20
	}
21
	this._registeredGroups[name] = handler;
22
}, removeGroup:function (name) {
23
	delete this._registeredGroups[name];
24
}, getGroup:function (name, contextmenuplugin) {
25
	if (this._registeredGroups[name]) {
26
		var item = this._registeredGroups[name](name, contextmenuplugin);
27
		if (item) {
28
			return item;
29
		}
30
	}
31
	switch (name) {
32
	  case "Generic":
33
	  case "Link":
34
	  case "Image":
35
		return new dojo.widget.Editor2Plugin[name + "ContextMenuGroup"](contextmenuplugin);
36
	  case "Anchor":
37
	  case "List":
38
	}
39
}, registerGroupSet:function (name, set) {
40
	this._contextMenuGroupSets[name] = set;
41
}, removeGroupSet:function (name) {
42
	var set = this._contextMenuGroupSets[name];
43
	delete this._contextMenuGroupSets[name];
44
	return set;
45
}, getContextMenu:function (editor) {
46
	var set = editor.contextMenuGroupSet || "defaultDojoEditor2MenuGroupSet";
47
	if (this._menus[set]) {
48
		this._menus[set].bindEditor(editor);
49
		return this._menus[set];
50
	}
51
	var gs = (editor.contextMenuGroupSet && this._contextMenuGroupSets[editor.contextMenuGroupSet]) || this.menuGroups;
52
	var menu = new dojo.widget.Editor2Plugin.ContextMenu(editor, gs);
53
	this._menus[set] = menu;
54
	return menu;
55
}};
56
dojo.declare("dojo.widget.Editor2Plugin.ContextMenu", null, function (editor, gs) {
57
	this.groups = [];
58
	this.separators = [];
59
	this.editor = editor;
60
	this.editor.registerLoadedPlugin(this);
61
	this.contextMenu = dojo.widget.createWidget("PopupMenu2", {});
62
	dojo.body().appendChild(this.contextMenu.domNode);
63
	this.bindEditor(this.editor);
64
	dojo.event.connect(this.contextMenu, "aboutToShow", this, "aboutToShow");
65
	dojo.event.connect(this.editor, "destroy", this, "destroy");
66
	this.setup(gs);
67
}, {bindEditor:function (editor) {
68
	this.contextMenu.bindDomNode(editor.document.body);
69
}, setup:function (gs) {
70
	for (var i in gs) {
71
		var g = dojo.widget.Editor2Plugin.ContextMenuManager.getGroup(gs[i], this);
72
		if (g) {
73
			this.groups.push(g);
74
		}
75
	}
76
}, aboutToShow:function () {
77
	var first = true;
78
	for (var i in this.groups) {
79
		if (i > 0 && this.separators.length != this.groups.length - 1) {
80
			this.separators.push(dojo.widget.createWidget("MenuSeparator2", {}));
81
			this.contextMenu.addChild(this.separators[this.separators.length - 1]);
82
		}
83
		if (this.groups[i].refresh()) {
84
			if (i > 0) {
85
				if (first) {
86
					this.separators[i - 1].hide();
87
				} else {
88
					this.separators[i - 1].show();
89
				}
90
			}
91
			if (first) {
92
				first = false;
93
			}
94
		} else {
95
			if (i > 0) {
96
				this.separators[i - 1].hide();
97
			}
98
		}
99
	}
100
}, destroy:function () {
101
	this.editor.unregisterLoadedPlugin(this);
102
	delete this.groups;
103
	delete this.separators;
104
	this.contextMenu.destroy();
105
	delete this.contextMenu;
106
}});
107
dojo.widget.defineWidget("dojo.widget.Editor2ContextMenuItem", dojo.widget.MenuItem2, {command:"", buildRendering:function () {
108
	var curInst = dojo.widget.Editor2Manager.getCurrentInstance();
109
	this.caption = curInst.getCommand(this.command).getText();
110
	dojo.widget.Editor2ContextMenuItem.superclass.buildRendering.apply(this, arguments);
111
}, onClick:function () {
112
	var curInst = dojo.widget.Editor2Manager.getCurrentInstance();
113
	if (curInst) {
114
		var _command = curInst.getCommand(this.command);
115
		if (_command) {
116
			_command.execute();
117
		}
118
	}
119
}, refresh:function () {
120
	var curInst = dojo.widget.Editor2Manager.getCurrentInstance();
121
	if (curInst) {
122
		var _command = curInst.getCommand(this.command);
123
		if (_command) {
124
			if (_command.getState() == dojo.widget.Editor2Manager.commandState.Disabled) {
125
				this.disable();
126
				return false;
127
			} else {
128
				this.enable();
129
				return true;
130
			}
131
		}
132
	}
133
}, hide:function () {
134
	this.domNode.style.display = "none";
135
}, show:function () {
136
	this.domNode.style.display = "";
137
}});
138
dojo.declare("dojo.widget.Editor2Plugin.SimpleContextMenuGroup", null, function (contextmenuplugin) {
139
	this.contextMenu = contextmenuplugin.contextMenu;
140
	this.items = [];
141
	dojo.event.connect(contextmenuplugin, "destroy", this, "destroy");
142
}, {refresh:function () {
143
	if (!this.items.length) {
144
		this.createItems();
145
		for (var i in this.items) {
146
			this.contextMenu.addChild(this.items[i]);
147
		}
148
	}
149
	return this.checkVisibility();
150
}, destroy:function () {
151
	this.contextmenu = null;
152
	delete this.items;
153
	delete this.contextMenu;
154
}, createItems:function () {
155
}, checkVisibility:function () {
156
	var show = false;
157
	for (var i in this.items) {
158
		show = show || this.items[i].refresh();
159
	}
160
	var action = show ? "show" : "hide";
161
	for (var i in this.items) {
162
		this.items[i][action]();
163
	}
164
	return show;
165
}});
166
dojo.declare("dojo.widget.Editor2Plugin.GenericContextMenuGroup", dojo.widget.Editor2Plugin.SimpleContextMenuGroup, {createItems:function () {
167
	this.items.push(dojo.widget.createWidget("Editor2ContextMenuItem", {command:"cut", iconClass:"dojoE2TBIcon dojoE2TBIcon_Cut"}));
168
	this.items.push(dojo.widget.createWidget("Editor2ContextMenuItem", {command:"copy", iconClass:"dojoE2TBIcon dojoE2TBIcon_Copy"}));
169
	this.items.push(dojo.widget.createWidget("Editor2ContextMenuItem", {command:"paste", iconClass:"dojoE2TBIcon dojoE2TBIcon_Paste"}));
170
}});
171
dojo.declare("dojo.widget.Editor2Plugin.LinkContextMenuGroup", dojo.widget.Editor2Plugin.SimpleContextMenuGroup, {createItems:function () {
172
	this.items.push(dojo.widget.createWidget("Editor2ContextMenuItem", {command:"createlink", iconClass:"dojoE2TBIcon dojoE2TBIcon_Link"}));
173
	this.items.push(dojo.widget.createWidget("Editor2ContextMenuItem", {command:"unlink", iconClass:"dojoE2TBIcon dojoE2TBIcon_UnLink"}));
174
}, checkVisibility:function () {
175
	var show = this.items[1].refresh();
176
	if (show) {
177
		this.items[0].refresh();
178
		for (var i in this.items) {
179
			this.items[i].show();
180
		}
181
	} else {
182
		for (var i in this.items) {
183
			this.items[i].hide();
184
		}
185
	}
186
	return show;
187
}});
188
dojo.declare("dojo.widget.Editor2Plugin.ImageContextMenuGroup", dojo.widget.Editor2Plugin.SimpleContextMenuGroup, {createItems:function () {
189
	this.items.push(dojo.widget.createWidget("Editor2ContextMenuItem", {command:"insertimage", iconClass:"dojoE2TBIcon dojoE2TBIcon_Image"}));
190
}, checkVisibility:function () {
191
	var curInst = dojo.widget.Editor2Manager.getCurrentInstance();
192
	var img = dojo.withGlobal(curInst.window, "getSelectedElement", dojo.html.selection);
193
	if (img && img.tagName.toLowerCase() == "img") {
194
		this.items[0].show();
195
		return true;
196
	} else {
197
		this.items[0].hide();
198
		return false;
199
	}
200
}});
201