bgneal@312: /** bgneal@312: * editor_plugin_src.js bgneal@312: * bgneal@312: * Copyright 2009, Moxiecode Systems AB bgneal@312: * Released under LGPL License. bgneal@312: * bgneal@312: * License: http://tinymce.moxiecode.com/license bgneal@312: * Contributing: http://tinymce.moxiecode.com/contributing bgneal@312: */ bgneal@312: bgneal@312: (function() { bgneal@312: var Event = tinymce.dom.Event, each = tinymce.each, DOM = tinymce.DOM; bgneal@312: bgneal@312: /** bgneal@312: * This plugin a context menu to TinyMCE editor instances. bgneal@312: * bgneal@312: * @class tinymce.plugins.ContextMenu bgneal@312: */ bgneal@312: tinymce.create('tinymce.plugins.ContextMenu', { bgneal@312: /** bgneal@312: * Initializes the plugin, this will be executed after the plugin has been created. bgneal@312: * This call is done before the editor instance has finished it's initialization so use the onInit event bgneal@312: * of the editor instance to intercept that event. bgneal@312: * bgneal@312: * @method init bgneal@312: * @param {tinymce.Editor} ed Editor instance that the plugin is initialized in. bgneal@312: * @param {string} url Absolute URL to where the plugin is located. bgneal@312: */ bgneal@312: init : function(ed) { bgneal@442: var t = this, showMenu, contextmenuNeverUseNative, realCtrlKey; bgneal@312: bgneal@312: t.editor = ed; bgneal@312: bgneal@442: contextmenuNeverUseNative = ed.settings.contextmenu_never_use_native; bgneal@442: bgneal@312: /** bgneal@312: * This event gets fired when the context menu is shown. bgneal@312: * bgneal@312: * @event onContextMenu bgneal@312: * @param {tinymce.plugins.ContextMenu} sender Plugin instance sending the event. bgneal@312: * @param {tinymce.ui.DropMenu} menu Drop down menu to fill with more items if needed. bgneal@312: */ bgneal@312: t.onContextMenu = new tinymce.util.Dispatcher(this); bgneal@312: bgneal@442: showMenu = ed.onContextMenu.add(function(ed, e) { bgneal@442: // Block TinyMCE menu on ctrlKey and work around Safari issue bgneal@442: if ((realCtrlKey !== 0 ? realCtrlKey : e.ctrlKey) && !contextmenuNeverUseNative) bgneal@442: return; bgneal@312: bgneal@442: Event.cancel(e); bgneal@442: bgneal@442: // Select the image if it's clicked. WebKit would other wise expand the selection bgneal@442: if (e.target.nodeName == 'IMG') bgneal@442: ed.selection.select(e.target); bgneal@442: bgneal@442: t._getMenu(ed).showMenu(e.clientX || e.pageX, e.clientY || e.pageX); bgneal@442: Event.add(ed.getDoc(), 'click', function(e) { bgneal@442: hide(ed, e); bgneal@442: }); bgneal@442: bgneal@442: ed.nodeChanged(); bgneal@312: }); bgneal@312: bgneal@312: ed.onRemove.add(function() { bgneal@312: if (t._menu) bgneal@312: t._menu.removeAll(); bgneal@312: }); bgneal@312: bgneal@312: function hide(ed, e) { bgneal@442: realCtrlKey = 0; bgneal@312: bgneal@312: // Since the contextmenu event moves bgneal@312: // the selection we need to store it away bgneal@312: if (e && e.button == 2) { bgneal@442: realCtrlKey = e.ctrlKey; bgneal@312: return; bgneal@312: } bgneal@312: bgneal@312: if (t._menu) { bgneal@312: t._menu.removeAll(); bgneal@312: t._menu.destroy(); bgneal@312: Event.remove(ed.getDoc(), 'click', hide); bgneal@312: } bgneal@312: }; bgneal@312: bgneal@312: ed.onMouseDown.add(hide); bgneal@312: ed.onKeyDown.add(hide); bgneal@442: ed.onKeyDown.add(function(ed, e) { bgneal@442: if (e.shiftKey && !e.ctrlKey && !e.altKey && e.keyCode === 121) { bgneal@442: Event.cancel(e); bgneal@442: showMenu(ed, e); bgneal@442: } bgneal@442: }); bgneal@312: }, bgneal@312: bgneal@312: /** bgneal@312: * Returns information about the plugin as a name/value array. bgneal@312: * The current keys are longname, author, authorurl, infourl and version. bgneal@312: * bgneal@312: * @method getInfo bgneal@312: * @return {Object} Name/value array containing information about the plugin. bgneal@312: */ bgneal@312: getInfo : function() { bgneal@312: return { bgneal@312: longname : 'Contextmenu', bgneal@312: author : 'Moxiecode Systems AB', bgneal@312: authorurl : 'http://tinymce.moxiecode.com', bgneal@312: infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/contextmenu', bgneal@312: version : tinymce.majorVersion + "." + tinymce.minorVersion bgneal@312: }; bgneal@312: }, bgneal@312: bgneal@312: _getMenu : function(ed) { bgneal@312: var t = this, m = t._menu, se = ed.selection, col = se.isCollapsed(), el = se.getNode() || ed.getBody(), am, p1, p2; bgneal@312: bgneal@312: if (m) { bgneal@312: m.removeAll(); bgneal@312: m.destroy(); bgneal@312: } bgneal@312: bgneal@312: p1 = DOM.getPos(ed.getContentAreaContainer()); bgneal@312: p2 = DOM.getPos(ed.getContainer()); bgneal@312: bgneal@312: m = ed.controlManager.createDropMenu('contextmenu', { bgneal@312: offset_x : p1.x + ed.getParam('contextmenu_offset_x', 0), bgneal@312: offset_y : p1.y + ed.getParam('contextmenu_offset_y', 0), bgneal@442: constrain : 1, bgneal@442: keyboard_focus: true bgneal@312: }); bgneal@312: bgneal@312: t._menu = m; bgneal@312: bgneal@312: m.add({title : 'advanced.cut_desc', icon : 'cut', cmd : 'Cut'}).setDisabled(col); bgneal@312: m.add({title : 'advanced.copy_desc', icon : 'copy', cmd : 'Copy'}).setDisabled(col); bgneal@312: m.add({title : 'advanced.paste_desc', icon : 'paste', cmd : 'Paste'}); bgneal@312: bgneal@312: if ((el.nodeName == 'A' && !ed.dom.getAttrib(el, 'name')) || !col) { bgneal@312: m.addSeparator(); bgneal@312: m.add({title : 'advanced.link_desc', icon : 'link', cmd : ed.plugins.advlink ? 'mceAdvLink' : 'mceLink', ui : true}); bgneal@312: m.add({title : 'advanced.unlink_desc', icon : 'unlink', cmd : 'UnLink'}); bgneal@312: } bgneal@312: bgneal@312: m.addSeparator(); bgneal@312: m.add({title : 'advanced.image_desc', icon : 'image', cmd : ed.plugins.advimage ? 'mceAdvImage' : 'mceImage', ui : true}); bgneal@312: bgneal@312: m.addSeparator(); bgneal@312: am = m.addMenu({title : 'contextmenu.align'}); bgneal@312: am.add({title : 'contextmenu.left', icon : 'justifyleft', cmd : 'JustifyLeft'}); bgneal@312: am.add({title : 'contextmenu.center', icon : 'justifycenter', cmd : 'JustifyCenter'}); bgneal@312: am.add({title : 'contextmenu.right', icon : 'justifyright', cmd : 'JustifyRight'}); bgneal@312: am.add({title : 'contextmenu.full', icon : 'justifyfull', cmd : 'JustifyFull'}); bgneal@312: bgneal@312: t.onContextMenu.dispatch(t, m, el, col); bgneal@312: bgneal@312: return m; bgneal@312: } bgneal@312: }); bgneal@312: bgneal@312: // Register plugin bgneal@312: tinymce.PluginManager.add('contextmenu', tinymce.plugins.ContextMenu); bgneal@442: })();