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