import mtweb.0.4.1
[mtweb] / web / libs / 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, lastRng;\r
31 \r
32                         t.editor = ed;\r
33 \r
34                         /**\r
35                          * This event gets fired when the context menu is shown.\r
36                          *\r
37                          * @event onContextMenu\r
38                          * @param {tinymce.plugins.ContextMenu} sender Plugin instance sending the event.\r
39                          * @param {tinymce.ui.DropMenu} menu Drop down menu to fill with more items if needed.\r
40                          */\r
41                         t.onContextMenu = new tinymce.util.Dispatcher(this);\r
42 \r
43                         ed.onContextMenu.add(function(ed, e) {\r
44                                 if (!e.ctrlKey) {\r
45                                         // Restore the last selection since it was removed\r
46                                         if (lastRng)\r
47                                                 ed.selection.setRng(lastRng);\r
48 \r
49                                         t._getMenu(ed).showMenu(e.clientX, e.clientY);\r
50                                         Event.add(ed.getDoc(), 'click', function(e) {\r
51                                                 hide(ed, e);\r
52                                         });\r
53                                         Event.cancel(e);\r
54                                 }\r
55                         });\r
56 \r
57                         ed.onRemove.add(function() {\r
58                                 if (t._menu)\r
59                                         t._menu.removeAll();\r
60                         });\r
61 \r
62                         function hide(ed, e) {\r
63                                 lastRng = null;\r
64 \r
65                                 // Since the contextmenu event moves\r
66                                 // the selection we need to store it away\r
67                                 if (e && e.button == 2) {\r
68                                         lastRng = ed.selection.getRng();\r
69                                         return;\r
70                                 }\r
71 \r
72                                 if (t._menu) {\r
73                                         t._menu.removeAll();\r
74                                         t._menu.destroy();\r
75                                         Event.remove(ed.getDoc(), 'click', hide);\r
76                                 }\r
77                         };\r
78 \r
79                         ed.onMouseDown.add(hide);\r
80                         ed.onKeyDown.add(hide);\r
81                 },\r
82 \r
83                 /**\r
84                  * Returns information about the plugin as a name/value array.\r
85                  * The current keys are longname, author, authorurl, infourl and version.\r
86                  *\r
87                  * @method getInfo\r
88                  * @return {Object} Name/value array containing information about the plugin.\r
89                  */\r
90                 getInfo : function() {\r
91                         return {\r
92                                 longname : 'Contextmenu',\r
93                                 author : 'Moxiecode Systems AB',\r
94                                 authorurl : 'http://tinymce.moxiecode.com',\r
95                                 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/contextmenu',\r
96                                 version : tinymce.majorVersion + "." + tinymce.minorVersion\r
97                         };\r
98                 },\r
99 \r
100                 _getMenu : function(ed) {\r
101                         var t = this, m = t._menu, se = ed.selection, col = se.isCollapsed(), el = se.getNode() || ed.getBody(), am, p1, p2;\r
102 \r
103                         if (m) {\r
104                                 m.removeAll();\r
105                                 m.destroy();\r
106                         }\r
107 \r
108                         p1 = DOM.getPos(ed.getContentAreaContainer());\r
109                         p2 = DOM.getPos(ed.getContainer());\r
110 \r
111                         m = ed.controlManager.createDropMenu('contextmenu', {\r
112                                 offset_x : p1.x + ed.getParam('contextmenu_offset_x', 0),\r
113                                 offset_y : p1.y + ed.getParam('contextmenu_offset_y', 0),\r
114                                 constrain : 1\r
115                         });\r
116 \r
117                         t._menu = m;\r
118 \r
119                         m.add({title : 'advanced.cut_desc', icon : 'cut', cmd : 'Cut'}).setDisabled(col);\r
120                         m.add({title : 'advanced.copy_desc', icon : 'copy', cmd : 'Copy'}).setDisabled(col);\r
121                         m.add({title : 'advanced.paste_desc', icon : 'paste', cmd : 'Paste'});\r
122 \r
123                         if ((el.nodeName == 'A' && !ed.dom.getAttrib(el, 'name')) || !col) {\r
124                                 m.addSeparator();\r
125                                 m.add({title : 'advanced.link_desc', icon : 'link', cmd : ed.plugins.advlink ? 'mceAdvLink' : 'mceLink', ui : true});\r
126                                 m.add({title : 'advanced.unlink_desc', icon : 'unlink', cmd : 'UnLink'});\r
127                         }\r
128 \r
129                         m.addSeparator();\r
130                         m.add({title : 'advanced.image_desc', icon : 'image', cmd : ed.plugins.advimage ? 'mceAdvImage' : 'mceImage', ui : true});\r
131 \r
132                         m.addSeparator();\r
133                         am = m.addMenu({title : 'contextmenu.align'});\r
134                         am.add({title : 'contextmenu.left', icon : 'justifyleft', cmd : 'JustifyLeft'});\r
135                         am.add({title : 'contextmenu.center', icon : 'justifycenter', cmd : 'JustifyCenter'});\r
136                         am.add({title : 'contextmenu.right', icon : 'justifyright', cmd : 'JustifyRight'});\r
137                         am.add({title : 'contextmenu.full', icon : 'justifyfull', cmd : 'JustifyFull'});\r
138 \r
139                         t.onContextMenu.dispatch(t, m, el, col);\r
140 \r
141                         return m;\r
142                 }\r
143         });\r
144 \r
145         // Register plugin\r
146         tinymce.PluginManager.add('contextmenu', tinymce.plugins.ContextMenu);\r
147 })();