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