root / trunk / src / widget / Editor2Plugin / ContextMenu.js

Revision 6608, 7.9 kB (checked in by liucougar, 4 years ago)

make use of deprecated to notify user of the moving of some functionalities to plugins
get rid of unnecessary case change calls

  • Property svn:eol-style set to native
Line 
1dojo.provide("dojo.widget.Editor2Plugin.ContextMenu");
2
3// summary: dojo.widget.Editor2Plugin.ContextMenu provides context menu for Editor2 widget
4// description:
5//              This plugin should be dojo.required-ed before all other plugins which
6//              support contextmenu, otherwise the menu for that plugin won't be shown
7//              For each Editor2, what will appear in its context menu can be set by changing
8//              Editor2.contextMenuGroupSet property (by default it is empty, which means use the
9//              default contextMenu). A contextMenuGroupSet has to be registered by calling
10//              dojo.widget.Editor2Plugin.ContextMenuManager.registerGroupSet()
11//              All Editor2 with the same contextMenuGroupSet will share the same ContextMenu
12
13dojo.require("dojo.widget.Menu2");
14
15dojo.event.topic.subscribe("dojo.widget.Editor2::onLoad", function(editor){
16        dojo.widget.Editor2Plugin.ContextMenuManager.getContextMenu(editor);
17//      var p = new dojo.widget.Editor2Plugin.ContextMenu();
18});
19
20dojo.widget.Editor2Plugin.ContextMenuManager = {
21        menuGroups: ['Generic', 'Link', 'Anchor', 'Image', 'List', 'Table'],
22        _contextMenuGroupSets: {},
23        _registeredGroups: {},
24        _menus: {},
25        registerGroup: function(name, handler){
26                if(this._registeredGroups[name]){
27                        alert("dojo.widget.Editor2Plugin.ContextMenuManager.registerGroup: menu group "+name+"is already registered. Ignored.");
28                        return;
29                }
30                this._registeredGroups[name] = handler;
31        },
32        removeGroup: function(name){
33                delete this._registeredGroups[name];
34        },
35        getGroup: function(name, contextmenuplugin){
36                if(this._registeredGroups[name]){
37                        var item = this._registeredGroups[name](name, contextmenuplugin);
38                        if(item){
39                                return item;
40                        }
41                }
42                switch(name){
43                        case 'Generic':
44                        case 'Link':
45                        case 'Image':
46                                return new dojo.widget.Editor2Plugin[name+"ContextMenuGroup"](contextmenuplugin);
47                        //TODO
48                        case 'Anchor':
49                        case 'List':
50                }
51        },
52        registerGroupSet: function(/*String*/name, /*Array*/set){
53                // summary: register a group set
54                // name: name of the group set
55                // set: an array of groups, such as ['Generic','Link']
56                this._contextMenuGroupSets[name] = set;
57        },
58        removeGroupSet: function(name){
59                var set = this._contextMenuGroupSets[name];
60                delete this._contextMenuGroupSets[name];
61                return set;
62        },
63        getContextMenu: function(editor){
64                var set = editor.contextMenuGroupSet || 'defaultDojoEditor2MenuGroupSet';
65                if(this._menus[set]){
66                        this._menus[set].bindEditor(editor);
67                        return this._menus[set];
68                }
69
70                var gs = (editor.contextMenuGroupSet && this._contextMenuGroupSets[editor.contextMenuGroupSet]) || this.menuGroups;
71                var menu = new dojo.widget.Editor2Plugin.ContextMenu(editor, gs);
72                this._menus[set] = menu;
73                return menu;
74        }
75};
76
77dojo.declare("dojo.widget.Editor2Plugin.ContextMenu", null,
78        function(editor, gs){
79                this.groups = [];
80                this.separators = [];
81                this.editor = editor;
82                this.editor.registerLoadedPlugin(this);
83                this.contextMenu = dojo.widget.createWidget("PopupMenu2", {});
84                dojo.body().appendChild(this.contextMenu.domNode);
85                this.bindEditor(this.editor);
86
87                dojo.event.connect(this.contextMenu, "aboutToShow", this, "aboutToShow");
88                dojo.event.connect(this.editor, "destroy", this, "destroy");
89
90                this.setup(gs);
91        },
92        {
93        bindEditor: function(editor){
94                this.contextMenu.bindDomNode(editor.document.body);
95        },
96        setup: function(gs){
97                for(var i in gs){
98                        var g = dojo.widget.Editor2Plugin.ContextMenuManager.getGroup(gs[i], this);
99                        if(g){
100                                this.groups.push(g);
101                        }
102                }
103        },
104        aboutToShow: function(){
105                var first = true;
106                for(var i in this.groups){
107                        if(i>0 && this.separators.length != this.groups.length-1){
108                                this.separators.push(dojo.widget.createWidget("MenuSeparator2", {}));
109                                this.contextMenu.addChild(this.separators[this.separators.length-1]);
110                        }
111                        if(this.groups[i].refresh()){
112                                if(i>0){
113                                        if(first){
114                                                this.separators[i-1].hide();
115                                        }else{
116                                                this.separators[i-1].show();
117                                        }
118                                }
119                                if(first){ first = false; }
120                        }else{
121                                if(i>0){
122                                        this.separators[i-1].hide();
123                                }
124                        }
125                }
126        },
127        destroy: function(){
128                this.editor.unregisterLoadedPlugin(this);
129                delete this.groups;
130                delete this.separators;
131                this.contextMenu.destroy();
132                delete this.contextMenu;
133        }
134});
135
136dojo.widget.defineWidget(
137        "dojo.widget.Editor2ContextMenuItem",
138        dojo.widget.MenuItem2,
139{
140        command: '',
141        buildRendering: function(){
142                var curInst = dojo.widget.Editor2Manager.getCurrentInstance();
143                this.caption = curInst.getCommand(this.command).getText();
144
145                dojo.widget.Editor2ContextMenuItem.superclass.buildRendering.apply(this, arguments);
146        },
147        onClick: function(){
148                var curInst = dojo.widget.Editor2Manager.getCurrentInstance();
149                if(curInst){
150                        var _command = curInst.getCommand(this.command);
151                        if(_command){
152                                _command.execute();
153                        }
154                }
155        },
156        refresh: function(){
157                var curInst = dojo.widget.Editor2Manager.getCurrentInstance();
158                if(curInst){
159                        var _command = curInst.getCommand(this.command);
160                        if(_command){
161                                if(_command.getState() == dojo.widget.Editor2Manager.commandState.Disabled){
162                                        this.disable();
163                                        return false;
164                                }else{
165                                        this.enable();
166                                        return true;
167                                }
168                        }
169                }
170        },
171        //improve performance by skipping animation
172        hide: function(){
173                this.domNode.style.display = "none";
174        },
175        show: function(){
176                this.domNode.style.display = "";
177        }
178});
179dojo.declare("dojo.widget.Editor2Plugin.SimpleContextMenuGroup", null,
180        function(contextmenuplugin){
181                this.contextMenu = contextmenuplugin.contextMenu;
182                this.items = [];
183
184                dojo.event.connect(contextmenuplugin, "destroy", this, "destroy");
185        },
186        {
187        refresh: function(){
188                if(!this.items.length){
189                        this.createItems();
190                        for(var i in this.items){
191                                this.contextMenu.addChild(this.items[i]);
192                        }
193                }
194
195                return this.checkVisibility();
196        },
197        destroy: function(){
198                this.contextmenu = null;
199                delete this.items;
200                delete this.contextMenu;
201        },
202        //implement this to fill in the menu items
203        createItems: function(){        },
204
205        //overload this to show/hide items
206        checkVisibility: function(){
207                var show = false;
208                for(var i in this.items){
209                        show = show || this.items[i].refresh();
210                }
211                var action = show ? "show" : "hide";
212                for(var i in this.items){
213                        this.items[i][action]();
214                }
215                return show;
216        }
217});
218dojo.declare("dojo.widget.Editor2Plugin.GenericContextMenuGroup",
219        dojo.widget.Editor2Plugin.SimpleContextMenuGroup,
220{
221        createItems: function(){
222                this.items.push(dojo.widget.createWidget("Editor2ContextMenuItem", {command: "cut", iconClass: "dojoE2TBIcon dojoE2TBIcon_Cut"}));
223                this.items.push(dojo.widget.createWidget("Editor2ContextMenuItem", {command: "copy", iconClass: "dojoE2TBIcon dojoE2TBIcon_Copy"}));
224                this.items.push(dojo.widget.createWidget("Editor2ContextMenuItem", {command: "paste", iconClass: "dojoE2TBIcon dojoE2TBIcon_Paste"}));
225        }
226});
227dojo.declare("dojo.widget.Editor2Plugin.LinkContextMenuGroup",
228        dojo.widget.Editor2Plugin.SimpleContextMenuGroup,
229{
230        createItems: function(){
231                this.items.push(dojo.widget.createWidget("Editor2ContextMenuItem", {command: 'createlink', iconClass: "dojoE2TBIcon dojoE2TBIcon_Link"}));
232                this.items.push(dojo.widget.createWidget("Editor2ContextMenuItem", {command: 'unlink', iconClass: "dojoE2TBIcon dojoE2TBIcon_UnLink"}));
233        },
234        checkVisibility: function(){
235                var show = this.items[1].refresh();
236                if(show){
237                        this.items[0].refresh();
238                        for(var i in this.items){
239                                this.items[i].show();
240                        }
241                }else{
242                        for(var i in this.items){
243                                this.items[i].hide();
244                        }
245                }
246
247                return show;
248        }
249});
250dojo.declare("dojo.widget.Editor2Plugin.ImageContextMenuGroup",
251        dojo.widget.Editor2Plugin.SimpleContextMenuGroup,
252{
253        createItems: function(){
254                this.items.push(dojo.widget.createWidget("Editor2ContextMenuItem", {command: 'insertimage', iconClass: "dojoE2TBIcon dojoE2TBIcon_Image"}));
255        },
256        checkVisibility: function(){
257                var curInst = dojo.widget.Editor2Manager.getCurrentInstance();
258                var img = dojo.withGlobal(curInst.window, "getSelectedElement", dojo.html.selection);
259
260                if(img && img.tagName.toUpperCase() == 'IMG'){
261                        this.items[0].show();
262                        return true;
263                }else{
264                        this.items[0].hide();
265                        return false;
266                }
267        }
268});
Note: See TracBrowser for help on using the browser.