bgneal@312
|
1 /**
|
bgneal@312
|
2 * editor_plugin_src.js
|
bgneal@312
|
3 *
|
bgneal@312
|
4 * Copyright 2009, Moxiecode Systems AB
|
bgneal@312
|
5 * Released under LGPL License.
|
bgneal@312
|
6 *
|
bgneal@312
|
7 * License: http://tinymce.moxiecode.com/license
|
bgneal@312
|
8 * Contributing: http://tinymce.moxiecode.com/contributing
|
bgneal@312
|
9 */
|
bgneal@312
|
10
|
bgneal@312
|
11 (function() {
|
bgneal@312
|
12 var Event = tinymce.dom.Event, each = tinymce.each, DOM = tinymce.DOM;
|
bgneal@312
|
13
|
bgneal@312
|
14 /**
|
bgneal@312
|
15 * This plugin a context menu to TinyMCE editor instances.
|
bgneal@312
|
16 *
|
bgneal@312
|
17 * @class tinymce.plugins.ContextMenu
|
bgneal@312
|
18 */
|
bgneal@312
|
19 tinymce.create('tinymce.plugins.ContextMenu', {
|
bgneal@312
|
20 /**
|
bgneal@312
|
21 * Initializes the plugin, this will be executed after the plugin has been created.
|
bgneal@312
|
22 * This call is done before the editor instance has finished it's initialization so use the onInit event
|
bgneal@312
|
23 * of the editor instance to intercept that event.
|
bgneal@312
|
24 *
|
bgneal@312
|
25 * @method init
|
bgneal@312
|
26 * @param {tinymce.Editor} ed Editor instance that the plugin is initialized in.
|
bgneal@312
|
27 * @param {string} url Absolute URL to where the plugin is located.
|
bgneal@312
|
28 */
|
bgneal@312
|
29 init : function(ed) {
|
bgneal@312
|
30 var t = this, lastRng;
|
bgneal@312
|
31
|
bgneal@312
|
32 t.editor = ed;
|
bgneal@312
|
33
|
bgneal@312
|
34 /**
|
bgneal@312
|
35 * This event gets fired when the context menu is shown.
|
bgneal@312
|
36 *
|
bgneal@312
|
37 * @event onContextMenu
|
bgneal@312
|
38 * @param {tinymce.plugins.ContextMenu} sender Plugin instance sending the event.
|
bgneal@312
|
39 * @param {tinymce.ui.DropMenu} menu Drop down menu to fill with more items if needed.
|
bgneal@312
|
40 */
|
bgneal@312
|
41 t.onContextMenu = new tinymce.util.Dispatcher(this);
|
bgneal@312
|
42
|
bgneal@312
|
43 ed.onContextMenu.add(function(ed, e) {
|
bgneal@312
|
44 if (!e.ctrlKey) {
|
bgneal@312
|
45 // Restore the last selection since it was removed
|
bgneal@312
|
46 if (lastRng)
|
bgneal@312
|
47 ed.selection.setRng(lastRng);
|
bgneal@312
|
48
|
bgneal@312
|
49 t._getMenu(ed).showMenu(e.clientX, e.clientY);
|
bgneal@312
|
50 Event.add(ed.getDoc(), 'click', function(e) {
|
bgneal@312
|
51 hide(ed, e);
|
bgneal@312
|
52 });
|
bgneal@312
|
53 Event.cancel(e);
|
bgneal@312
|
54 }
|
bgneal@312
|
55 });
|
bgneal@312
|
56
|
bgneal@312
|
57 ed.onRemove.add(function() {
|
bgneal@312
|
58 if (t._menu)
|
bgneal@312
|
59 t._menu.removeAll();
|
bgneal@312
|
60 });
|
bgneal@312
|
61
|
bgneal@312
|
62 function hide(ed, e) {
|
bgneal@312
|
63 lastRng = null;
|
bgneal@312
|
64
|
bgneal@312
|
65 // Since the contextmenu event moves
|
bgneal@312
|
66 // the selection we need to store it away
|
bgneal@312
|
67 if (e && e.button == 2) {
|
bgneal@312
|
68 lastRng = ed.selection.getRng();
|
bgneal@312
|
69 return;
|
bgneal@312
|
70 }
|
bgneal@312
|
71
|
bgneal@312
|
72 if (t._menu) {
|
bgneal@312
|
73 t._menu.removeAll();
|
bgneal@312
|
74 t._menu.destroy();
|
bgneal@312
|
75 Event.remove(ed.getDoc(), 'click', hide);
|
bgneal@312
|
76 }
|
bgneal@312
|
77 };
|
bgneal@312
|
78
|
bgneal@312
|
79 ed.onMouseDown.add(hide);
|
bgneal@312
|
80 ed.onKeyDown.add(hide);
|
bgneal@312
|
81 },
|
bgneal@312
|
82
|
bgneal@312
|
83 /**
|
bgneal@312
|
84 * Returns information about the plugin as a name/value array.
|
bgneal@312
|
85 * The current keys are longname, author, authorurl, infourl and version.
|
bgneal@312
|
86 *
|
bgneal@312
|
87 * @method getInfo
|
bgneal@312
|
88 * @return {Object} Name/value array containing information about the plugin.
|
bgneal@312
|
89 */
|
bgneal@312
|
90 getInfo : function() {
|
bgneal@312
|
91 return {
|
bgneal@312
|
92 longname : 'Contextmenu',
|
bgneal@312
|
93 author : 'Moxiecode Systems AB',
|
bgneal@312
|
94 authorurl : 'http://tinymce.moxiecode.com',
|
bgneal@312
|
95 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/contextmenu',
|
bgneal@312
|
96 version : tinymce.majorVersion + "." + tinymce.minorVersion
|
bgneal@312
|
97 };
|
bgneal@312
|
98 },
|
bgneal@312
|
99
|
bgneal@312
|
100 _getMenu : function(ed) {
|
bgneal@312
|
101 var t = this, m = t._menu, se = ed.selection, col = se.isCollapsed(), el = se.getNode() || ed.getBody(), am, p1, p2;
|
bgneal@312
|
102
|
bgneal@312
|
103 if (m) {
|
bgneal@312
|
104 m.removeAll();
|
bgneal@312
|
105 m.destroy();
|
bgneal@312
|
106 }
|
bgneal@312
|
107
|
bgneal@312
|
108 p1 = DOM.getPos(ed.getContentAreaContainer());
|
bgneal@312
|
109 p2 = DOM.getPos(ed.getContainer());
|
bgneal@312
|
110
|
bgneal@312
|
111 m = ed.controlManager.createDropMenu('contextmenu', {
|
bgneal@312
|
112 offset_x : p1.x + ed.getParam('contextmenu_offset_x', 0),
|
bgneal@312
|
113 offset_y : p1.y + ed.getParam('contextmenu_offset_y', 0),
|
bgneal@312
|
114 constrain : 1
|
bgneal@312
|
115 });
|
bgneal@312
|
116
|
bgneal@312
|
117 t._menu = m;
|
bgneal@312
|
118
|
bgneal@312
|
119 m.add({title : 'advanced.cut_desc', icon : 'cut', cmd : 'Cut'}).setDisabled(col);
|
bgneal@312
|
120 m.add({title : 'advanced.copy_desc', icon : 'copy', cmd : 'Copy'}).setDisabled(col);
|
bgneal@312
|
121 m.add({title : 'advanced.paste_desc', icon : 'paste', cmd : 'Paste'});
|
bgneal@312
|
122
|
bgneal@312
|
123 if ((el.nodeName == 'A' && !ed.dom.getAttrib(el, 'name')) || !col) {
|
bgneal@312
|
124 m.addSeparator();
|
bgneal@312
|
125 m.add({title : 'advanced.link_desc', icon : 'link', cmd : ed.plugins.advlink ? 'mceAdvLink' : 'mceLink', ui : true});
|
bgneal@312
|
126 m.add({title : 'advanced.unlink_desc', icon : 'unlink', cmd : 'UnLink'});
|
bgneal@312
|
127 }
|
bgneal@312
|
128
|
bgneal@312
|
129 m.addSeparator();
|
bgneal@312
|
130 m.add({title : 'advanced.image_desc', icon : 'image', cmd : ed.plugins.advimage ? 'mceAdvImage' : 'mceImage', ui : true});
|
bgneal@312
|
131
|
bgneal@312
|
132 m.addSeparator();
|
bgneal@312
|
133 am = m.addMenu({title : 'contextmenu.align'});
|
bgneal@312
|
134 am.add({title : 'contextmenu.left', icon : 'justifyleft', cmd : 'JustifyLeft'});
|
bgneal@312
|
135 am.add({title : 'contextmenu.center', icon : 'justifycenter', cmd : 'JustifyCenter'});
|
bgneal@312
|
136 am.add({title : 'contextmenu.right', icon : 'justifyright', cmd : 'JustifyRight'});
|
bgneal@312
|
137 am.add({title : 'contextmenu.full', icon : 'justifyfull', cmd : 'JustifyFull'});
|
bgneal@312
|
138
|
bgneal@312
|
139 t.onContextMenu.dispatch(t, m, el, col);
|
bgneal@312
|
140
|
bgneal@312
|
141 return m;
|
bgneal@312
|
142 }
|
bgneal@312
|
143 });
|
bgneal@312
|
144
|
bgneal@312
|
145 // Register plugin
|
bgneal@312
|
146 tinymce.PluginManager.add('contextmenu', tinymce.plugins.ContextMenu);
|
bgneal@312
|
147 })(); |