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