* Upgraded TinyMCE to version 2.0RC3. This fixes a conflict with
[citadel.git] / webcit / tiny_mce / plugins / contextmenu / editor_plugin_src.js
1 /* Import plugin specific language pack */
2 //tinyMCE.importPluginLanguagePack('contextmenu', 'en,zh_cn,cs,fa,fr_ca,fr,de,no');
3 if (!tinyMCE.settings['contextmenu_skip_plugin_css'])
4         tinyMCE.loadCSS(tinyMCE.baseURL + "/plugins/contextmenu/css/contextmenu.css");
5
6 // Global contextmenu class instance
7 var TinyMCE_contextmenu_contextMenu = null;
8
9 function TinyMCE_contextmenu_getInfo() {
10         return {
11                 longname : 'Context menus',
12                 author : 'Moxiecode Systems',
13                 authorurl : 'http://tinymce.moxiecode.com',
14                 infourl : 'http://tinymce.moxiecode.com/tinymce/docs/plugin_contextmenu.html',
15                 version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion
16         };
17 };
18
19 function TinyMCE_contextmenu_initInstance(inst) {
20         // Is not working on MSIE 5.0
21         if (tinyMCE.isMSIE5_0)
22                 return;
23
24         // Add hide event handles
25         tinyMCE.addEvent(inst.getDoc(), "click", TinyMCE_contextmenu_hideContextMenu);
26         tinyMCE.addEvent(inst.getDoc(), "keypress", TinyMCE_contextmenu_hideContextMenu);
27         tinyMCE.addEvent(inst.getDoc(), "keydown", TinyMCE_contextmenu_hideContextMenu);
28         tinyMCE.addEvent(document, "click", TinyMCE_contextmenu_hideContextMenu);
29         tinyMCE.addEvent(document, "keypress", TinyMCE_contextmenu_hideContextMenu);
30         tinyMCE.addEvent(document, "keydown", TinyMCE_contextmenu_hideContextMenu);
31
32         var contextMenu = new ContextMenu({
33                 commandhandler : "TinyMCE_contextmenu_commandHandler",
34                 spacer_image : tinyMCE.baseURL + "/plugins/contextmenu/images/spacer.gif"
35         });
36
37         // Register global reference
38         TinyMCE_contextmenu_contextMenu = contextMenu;
39
40         // Attach contextmenu event
41         if (tinyMCE.isGecko) {
42                 tinyMCE.addEvent(inst.getDoc(), "contextmenu", function(e) {TinyMCE_contextmenu_showContextMenu(tinyMCE.isMSIE ? inst.contentWindow.event : e, inst);});
43         } else
44                 tinyMCE.addEvent(inst.getDoc(), "contextmenu", TinyMCE_contextmenu_onContextMenu);
45 }
46
47 function TinyMCE_contextmenu_onContextMenu(e) {
48         var elm = tinyMCE.isMSIE ? e.srcElement : e.target;
49         var targetInst, body;
50
51         // Find instance
52         if ((body = tinyMCE.getParentElement(elm, "body")) != null) {
53                 for (var n in tinyMCE.instances) {
54                         var inst = tinyMCE.instances[n];
55
56                         if (body == inst.getBody()) {
57                                 targetInst = inst;
58                                 break;
59                         }
60                 }
61
62                 return TinyMCE_contextmenu_showContextMenu(tinyMCE.isMSIE ? targetInst.contentWindow.event : e, targetInst);
63         }
64 }
65
66 function TinyMCE_contextmenu_showContextMenu(e, inst) {
67         function getAttrib(elm, name) {
68                 return elm.getAttribute(name) ? elm.getAttribute(name) : "";
69         }
70
71         var x, y, elm, contextMenu;
72         var pos = tinyMCE.getAbsPosition(inst.iframeElement);
73
74         x = tinyMCE.isMSIE ? e.screenX : pos.absLeft + (e.pageX - inst.getBody().scrollLeft);
75         y = tinyMCE.isMSIE ? e.screenY : pos.absTop + (e.pageY - inst.getBody().scrollTop);
76         elm = tinyMCE.isMSIE ? e.srcElement : e.target;
77         contextMenu = TinyMCE_contextmenu_contextMenu;
78         contextMenu.inst = inst;
79
80         // Mozilla needs some time
81         window.setTimeout(function () {
82                 var theme = tinyMCE.getParam("theme");
83
84                 contextMenu.clearAll();
85                 var sel = inst.getSelectedText().length != 0 || elm.nodeName == "IMG";
86
87                 // Default items
88                 contextMenu.addItem(tinyMCE.baseURL + "/themes/" + theme + "/images/cut.gif", "$lang_cut_desc", "Cut", "", !sel);
89                 contextMenu.addItem(tinyMCE.baseURL + "/themes/" + theme + "/images/copy.gif", "$lang_copy_desc", "Copy", "", !sel);
90                 contextMenu.addItem(tinyMCE.baseURL + "/themes/" + theme + "/images/paste.gif", "$lang_paste_desc", "Paste", "", false);
91
92                 // Get element
93                 elm = tinyMCE.getParentElement(elm, "img,table,td");
94                 if (elm) {
95                         switch (elm.nodeName) {
96                                 case "IMG":
97                                         contextMenu.addSeparator();
98
99                                         // If flash
100                                         if (tinyMCE.getAttrib(elm, 'class').indexOf('mceItemFlash') == 0)
101                                                 contextMenu.addItem(tinyMCE.baseURL + "/plugins/flash/images/flash.gif", "$lang_flash_props", "mceFlash");
102                                         else
103                                                 contextMenu.addItem(tinyMCE.baseURL + "/themes/" + theme + "/images/image.gif", "$lang_image_props_desc", typeof(TinyMCE_advimage_getControlHTML) != "undefined" ? "mceAdvImage" : "mceImage");
104                                         break;
105
106                                 case "TABLE":
107                                 case "TD":
108                                         // Is table plugin loaded
109                                         if (typeof(TinyMCE_table_getControlHTML) != "undefined") {
110                                                 var colspan = (elm.nodeName == "TABLE") ? "" : getAttrib(elm, "colspan");
111                                                 var rowspan = (elm.nodeName == "TABLE") ? "" : getAttrib(elm, "rowspan");
112
113                                                 colspan = colspan == "" ? "1" : colspan;
114                                                 rowspan = rowspan == "" ? "1" : rowspan;
115
116                                                 contextMenu.addSeparator();
117                                                 contextMenu.addItem(tinyMCE.baseURL + "/themes/" + theme + "/images/cut.gif", "$lang_table_cut_row_desc", "mceTableCutRow");
118                                                 contextMenu.addItem(tinyMCE.baseURL + "/themes/" + theme + "/images/copy.gif", "$lang_table_copy_row_desc", "mceTableCopyRow");
119                                                 contextMenu.addItem(tinyMCE.baseURL + "/themes/" + theme + "/images/paste.gif", "$lang_table_paste_row_before_desc", "mceTablePasteRowBefore", "", inst.tableRowClipboard == null);
120                                                 contextMenu.addItem(tinyMCE.baseURL + "/themes/" + theme + "/images/paste.gif", "$lang_table_paste_row_after_desc", "mceTablePasteRowAfter", "", inst.tableRowClipboard == null);
121
122 /*                                              contextMenu.addItem(tinyMCE.baseURL + "/themes/" + theme + "/images/left.gif", "$lang_justifyleft_desc", "JustifyLeft", "", false);
123                                                 contextMenu.addItem(tinyMCE.baseURL + "/themes/" + theme + "/images/center.gif", "$lang_justifycenter_desc", "JustifyCenter", "", false);
124                                                 contextMenu.addItem(tinyMCE.baseURL + "/themes/" + theme + "/images/right.gif", "$lang_justifyright_desc", "JustifyRight", "", false);
125                                                 contextMenu.addItem(tinyMCE.baseURL + "/themes/" + theme + "/images/full.gif", "$lang_justifyfull_desc", "JustifyFull", "", false);*/
126                                                 contextMenu.addSeparator();
127                                                 contextMenu.addItem(tinyMCE.baseURL + "/plugins/table/images/table.gif", "$lang_table_desc", "mceInsertTable", "insert");
128                                                 contextMenu.addItem(tinyMCE.baseURL + "/plugins/table/images/table.gif", "$lang_table_props_desc", "mceInsertTable");
129                                                 contextMenu.addItem(tinyMCE.baseURL + "/plugins/table/images/table_cell_props.gif", "$lang_table_cell_desc", "mceTableCellProps");
130                                                 contextMenu.addItem(tinyMCE.baseURL + "/plugins/table/images/table_delete.gif", "$lang_table_del", "mceTableDelete");
131                                                 contextMenu.addSeparator();
132                                                 contextMenu.addItem(tinyMCE.baseURL + "/plugins/table/images/table_row_props.gif", "$lang_table_row_desc", "mceTableRowProps");
133                                                 contextMenu.addItem(tinyMCE.baseURL + "/plugins/table/images/table_insert_row_before.gif", "$lang_table_row_before_desc", "mceTableInsertRowBefore");
134                                                 contextMenu.addItem(tinyMCE.baseURL + "/plugins/table/images/table_insert_row_after.gif", "$lang_table_row_after_desc", "mceTableInsertRowAfter");
135                                                 contextMenu.addItem(tinyMCE.baseURL + "/plugins/table/images/table_delete_row.gif", "$lang_table_delete_row_desc", "mceTableDeleteRow");
136                                                 contextMenu.addSeparator();
137                                                 contextMenu.addItem(tinyMCE.baseURL + "/plugins/table/images/table_insert_col_before.gif", "$lang_table_col_before_desc", "mceTableInsertColBefore");
138                                                 contextMenu.addItem(tinyMCE.baseURL + "/plugins/table/images/table_insert_col_after.gif", "$lang_table_col_after_desc", "mceTableInsertColAfter");
139                                                 contextMenu.addItem(tinyMCE.baseURL + "/plugins/table/images/table_delete_col.gif", "$lang_table_delete_col_desc", "mceTableDeleteCol");
140                                                 contextMenu.addSeparator();
141                                                 contextMenu.addItem(tinyMCE.baseURL + "/plugins/table/images/table_split_cells.gif", "$lang_table_split_cells_desc", "mceTableSplitCells", "", (colspan == "1" && rowspan == "1"));
142                                                 contextMenu.addItem(tinyMCE.baseURL + "/plugins/table/images/table_merge_cells.gif", "$lang_table_merge_cells_desc", "mceTableMergeCells", "", false);
143                                         }
144                                         break;
145                         }
146                 }  else {
147                         // Add table specific
148                         if (typeof(TinyMCE_table_getControlHTML) != "undefined") {
149                                 contextMenu.addSeparator();
150                                 contextMenu.addItem(tinyMCE.baseURL + "/plugins/table/images/table.gif", "$lang_table_desc", "mceInsertTable", "insert");
151                         }
152                 }
153
154                 contextMenu.show(x, y);
155         }, 10);
156
157         // Cancel default handeling
158         tinyMCE.cancelEvent(e);
159         return false;
160 }
161
162 function TinyMCE_contextmenu_hideContextMenu() {
163         TinyMCE_contextmenu_contextMenu.hide();
164
165         return true;
166 }
167
168 function TinyMCE_contextmenu_commandHandler(command, value) {
169         TinyMCE_contextmenu_contextMenu.hide();
170
171         // UI must be true on these
172         var ui = false;
173         if (command == "mceInsertTable" || command == "mceTableCellProps" || command == "mceTableRowProps" || command == "mceTableMergeCells")
174                 ui = true;
175
176         if (command == "Paste")
177                 value = null;
178
179         TinyMCE_contextmenu_contextMenu.inst.execCommand(command, ui, value);
180 }
181
182 // Context menu class
183
184 function ContextMenu(settings) {
185         // Default value function
186         function defParam(key, def_val) {
187                 settings[key] = typeof(settings[key]) != "undefined" ? settings[key] : def_val;
188         }
189
190         var self = this;
191
192         this.isMSIE = (navigator.appName == "Microsoft Internet Explorer");
193
194         // Setup contextmenu div
195         this.contextMenuDiv = document.createElement("div");
196         this.contextMenuDiv.className = "contextMenu";
197         this.contextMenuDiv.setAttribute("class", "contextMenu");
198         this.contextMenuDiv.style.display = "none";
199         this.contextMenuDiv.style.position = 'absolute';
200         this.contextMenuDiv.style.zindex = 1000;
201         this.contextMenuDiv.style.left = '0px';
202         this.contextMenuDiv.style.top = '0px';
203         this.contextMenuDiv.unselectable = "on";
204
205         document.body.appendChild(this.contextMenuDiv);
206
207         // Setup default values
208         defParam("commandhandler", "");
209         defParam("spacer_image", "images/spacer.gif");
210
211         this.items = new Array();
212         this.settings = settings;
213         this.html = "";
214
215         // IE Popup
216         if (tinyMCE.isMSIE && !tinyMCE.isMSIE5_0) {
217                 this.pop = window.createPopup();
218                 doc = this.pop.document;
219                 doc.open();
220                 doc.write('<html><head><link href="' + tinyMCE.baseURL + '/plugins/contextmenu/css/contextmenu.css" rel="stylesheet" type="text/css" /></head><body unselectable="yes" class="contextMenuIEPopup"></body></html>');
221                 doc.close();
222         }
223 };
224
225 ContextMenu.prototype.clearAll = function() {
226         this.html = "";
227         this.contextMenuDiv.innerHTML = "";
228 };
229
230 ContextMenu.prototype.addSeparator = function() {
231         this.html += '<tr class="contextMenuItem"><td class="contextMenuIcon"><img src="' + this.settings['spacer_image'] + '" width="20" height="1" class="contextMenuImage" /></td><td><img class="contextMenuSeparator" width="1" height="1" src="' + this.settings['spacer_image'] + '" /></td></tr>';
232 };
233
234 ContextMenu.prototype.addItem = function(icon, title, command, value, disabled) {
235         if (title.charAt(0) == '$')
236                 title = tinyMCE.getLang(title.substring(1));
237
238         var onMouseDown = '';
239         var html = '';
240
241         if (tinyMCE.isMSIE && !tinyMCE.isMSIE5_0)
242                 onMouseDown = 'contextMenu.execCommand(\'' + command + '\', \'' + value + '\');return false;';
243         else
244                 onMouseDown = this.settings['commandhandler'] + '(\'' + command + '\', \'' + value + '\');return false;';
245
246         if (icon == "")
247                 icon = this.settings['spacer_image'];
248
249         if (!disabled)
250                 html += '<tr class="contextMenuItem" onmousedown="' + onMouseDown + '" onmouseover="tinyMCE.switchClass(this,\'contextMenuItemOver\');" onmouseout="tinyMCE.switchClass(this,\'contextMenuItem\');">';
251         else
252                 html += '<tr class="contextMenuItemDisabled">';
253
254         html += '<td class="contextMenuIcon"><img src="' + icon + '" width="20" height="20" class="contextMenuImage" /></td>';
255         html += '<td><div class="contextMenuText">';
256
257         // Add text
258         html += title;
259
260         html += '</div></td>';
261         html += '</tr>';
262
263         // Add to main
264         this.html += html;
265 };
266
267 ContextMenu.prototype.show = function(x, y) {
268         if (this.html == "")
269                 return;
270
271         var html = '';
272
273         html += '<table border="0" cellpadding="0" cellspacing="0">';
274         html += this.html;
275         html += '</table>';
276
277         this.contextMenuDiv.innerHTML = html;
278
279         if (tinyMCE.isMSIE && !tinyMCE.isMSIE5_0) {
280                 var width, height;
281
282                 // Get dimensions
283                 this.contextMenuDiv.style.display = "block";
284                 width = this.contextMenuDiv.offsetWidth;
285                 height = this.contextMenuDiv.offsetHeight;
286                 this.contextMenuDiv.style.display = "none";
287
288                 // Setup popup and show
289                 this.pop.document.body.innerHTML = '<div class="contextMenu">' + html + "</div>";
290                 this.pop.document.tinyMCE = tinyMCE;
291                 this.pop.document.contextMenu = this;
292                 this.pop.show(x, y, width, height);
293         } else {
294                 this.contextMenuDiv.style.left = x + 'px';
295                 this.contextMenuDiv.style.top = y + 'px';
296                 this.contextMenuDiv.style.display = "block";
297         }
298 };
299
300 ContextMenu.prototype.hide = function() {
301         if (tinyMCE.isMSIE && !tinyMCE.isMSIE5_0)
302                 this.pop.hide();
303         else
304                 this.contextMenuDiv.style.display = "none";
305 };
306
307 ContextMenu.prototype.execCommand = function(command, value) {
308         eval(this.settings['commandhandler'] + "(command, value);");
309 };