A tumblelog CMS built on AJAX, PHP and MySQL.

editor_plugin_src.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /**
  2. * $Id: editor_plugin_src.js 201 2007-02-12 15:56:56Z spocke $
  3. *
  4. * @author Moxiecode
  5. * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved.
  6. */
  7. /* Import plugin specific language pack */
  8. if (!tinyMCE.settings['contextmenu_skip_plugin_css']) {
  9. tinyMCE.loadCSS(tinyMCE.baseURL + "/plugins/contextmenu/css/contextmenu.css");
  10. }
  11. var TinyMCE_ContextMenuPlugin = {
  12. // Private fields
  13. _contextMenu : null,
  14. getInfo : function() {
  15. return {
  16. longname : 'Context menus',
  17. author : 'Moxiecode Systems AB',
  18. authorurl : 'http://tinymce.moxiecode.com',
  19. infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/contextmenu',
  20. version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion
  21. };
  22. },
  23. initInstance : function(inst) {
  24. // Is not working on MSIE 5.0 or Opera no contextmenu event
  25. if (tinyMCE.isMSIE5_0 && tinyMCE.isOpera)
  26. return;
  27. TinyMCE_ContextMenuPlugin._contextMenu = new TinyMCE_ContextMenu({
  28. commandhandler : "TinyMCE_ContextMenuPlugin._commandHandler",
  29. spacer_image : tinyMCE.baseURL + "/plugins/contextmenu/images/spacer.gif"
  30. });
  31. // Add hide event handles
  32. tinyMCE.addEvent(inst.getDoc(), "click", TinyMCE_ContextMenuPlugin._hideContextMenu);
  33. tinyMCE.addEvent(inst.getDoc(), "keypress", TinyMCE_ContextMenuPlugin._hideContextMenu);
  34. tinyMCE.addEvent(inst.getDoc(), "keydown", TinyMCE_ContextMenuPlugin._hideContextMenu);
  35. tinyMCE.addEvent(document, "click", TinyMCE_ContextMenuPlugin._hideContextMenu);
  36. tinyMCE.addEvent(document, "keypress", TinyMCE_ContextMenuPlugin._hideContextMenu);
  37. tinyMCE.addEvent(document, "keydown", TinyMCE_ContextMenuPlugin._hideContextMenu);
  38. // Attach contextmenu event
  39. if (tinyMCE.isGecko) {
  40. tinyMCE.addEvent(inst.getDoc(), "contextmenu", function(e) {TinyMCE_ContextMenuPlugin._showContextMenu(tinyMCE.isMSIE ? inst.contentWindow.event : e, inst);});
  41. } else
  42. tinyMCE.addEvent(inst.getDoc(), "contextmenu", TinyMCE_ContextMenuPlugin._onContextMenu);
  43. },
  44. // Private plugin internal methods
  45. _onContextMenu : function(e) {
  46. var elm = tinyMCE.isMSIE ? e.srcElement : e.target;
  47. var targetInst, body;
  48. // Find instance
  49. if ((body = tinyMCE.getParentElement(elm, "body")) != null) {
  50. for (var n in tinyMCE.instances) {
  51. var inst = tinyMCE.instances[n];
  52. if (!tinyMCE.isInstance(inst))
  53. continue;
  54. if (body == inst.getBody()) {
  55. targetInst = inst;
  56. break;
  57. }
  58. }
  59. return TinyMCE_ContextMenuPlugin._showContextMenu(tinyMCE.isMSIE ? targetInst.contentWindow.event : e, targetInst);
  60. }
  61. },
  62. _showContextMenu : function(e, inst) {
  63. function getAttrib(elm, name) {
  64. return elm.getAttribute(name) ? elm.getAttribute(name) : "";
  65. }
  66. var x, y, elm, contextMenu;
  67. var pos = tinyMCE.getAbsPosition(inst.iframeElement);
  68. x = tinyMCE.isMSIE ? e.screenX : pos.absLeft + (e.pageX - inst.getBody().scrollLeft);
  69. y = tinyMCE.isMSIE ? e.screenY : pos.absTop + (e.pageY - inst.getBody().scrollTop);
  70. elm = tinyMCE.isMSIE ? e.srcElement : e.target;
  71. contextMenu = this._contextMenu;
  72. contextMenu.inst = inst;
  73. // Mozilla needs some time
  74. window.setTimeout(function () {
  75. var theme = tinyMCE.getParam("theme");
  76. contextMenu.clearAll();
  77. var sel = inst.selection.getSelectedText().length != 0 || elm.nodeName == "IMG";
  78. // Default items
  79. contextMenu.addItem(tinyMCE.baseURL + "/themes/" + theme + "/images/cut.gif", "$lang_cut_desc", "Cut", "", !sel);
  80. contextMenu.addItem(tinyMCE.baseURL + "/themes/" + theme + "/images/copy.gif", "$lang_copy_desc", "Copy", "", !sel);
  81. contextMenu.addItem(tinyMCE.baseURL + "/themes/" + theme + "/images/paste.gif", "$lang_paste_desc", "Paste", "", false);
  82. if (sel || (elm ? (elm.nodeName == 'A' && tinyMCE.getAttrib(elm, 'name') == '') || (elm.nodeName == 'IMG') : false)) {
  83. contextMenu.addSeparator();
  84. contextMenu.addItem(tinyMCE.baseURL + "/themes/advanced/images/link.gif", "$lang_link_desc", inst.hasPlugin("advlink") ? "mceAdvLink" : "mceLink");
  85. contextMenu.addItem(tinyMCE.baseURL + "/themes/advanced/images/unlink.gif", "$lang_unlink_desc", "unlink", "", (elm ? (elm.nodeName != 'A') && (elm.nodeName != 'IMG') : true));
  86. }
  87. // Get element
  88. elm = tinyMCE.getParentElement(elm, "img,table,td" + (inst.hasPlugin("advhr") ? ',hr' : ''));
  89. if (elm) {
  90. switch (elm.nodeName) {
  91. case "IMG":
  92. contextMenu.addSeparator();
  93. // If flash
  94. if (tinyMCE.hasPlugin('flash') && tinyMCE.getAttrib(elm, 'class').indexOf('mceItemFlash') != -1)
  95. contextMenu.addItem(tinyMCE.baseURL + "/plugins/flash/images/flash.gif", "$lang_flash_props", "mceFlash");
  96. else if (tinyMCE.hasPlugin('media') && /mceItem(Flash|ShockWave|WindowsMedia|QuickTime|RealMedia)/.test(tinyMCE.getAttrib(elm, 'class')))
  97. contextMenu.addItem(tinyMCE.baseURL + "/plugins/flash/images/flash.gif", "$lang_media_title", "mceMedia");
  98. else
  99. contextMenu.addItem(tinyMCE.baseURL + "/themes/" + theme + "/images/image.gif", "$lang_image_props_desc", inst.hasPlugin("advimage") ? "mceAdvImage" : "mceImage");
  100. break;
  101. case "HR":
  102. contextMenu.addSeparator();
  103. contextMenu.addItem(tinyMCE.baseURL + "/plugins/advhr/images/advhr.gif", "$lang_insert_advhr_desc", "mceAdvancedHr");
  104. break;
  105. case "TABLE":
  106. case "TD":
  107. // Is table plugin loaded
  108. if (inst.hasPlugin("table")) {
  109. var colspan = (elm.nodeName == "TABLE") ? "" : getAttrib(elm, "colspan");
  110. var rowspan = (elm.nodeName == "TABLE") ? "" : getAttrib(elm, "rowspan");
  111. colspan = colspan == "" ? "1" : colspan;
  112. rowspan = rowspan == "" ? "1" : rowspan;
  113. contextMenu.addSeparator();
  114. contextMenu.addItem(tinyMCE.baseURL + "/themes/" + theme + "/images/cut.gif", "$lang_table_cut_row_desc", "mceTableCutRow");
  115. contextMenu.addItem(tinyMCE.baseURL + "/themes/" + theme + "/images/copy.gif", "$lang_table_copy_row_desc", "mceTableCopyRow");
  116. contextMenu.addItem(tinyMCE.baseURL + "/themes/" + theme + "/images/paste.gif", "$lang_table_paste_row_before_desc", "mceTablePasteRowBefore", "", inst.tableRowClipboard == null);
  117. contextMenu.addItem(tinyMCE.baseURL + "/themes/" + theme + "/images/paste.gif", "$lang_table_paste_row_after_desc", "mceTablePasteRowAfter", "", inst.tableRowClipboard == null);
  118. /* contextMenu.addItem(tinyMCE.baseURL + "/themes/" + theme + "/images/justifyleft.gif", "$lang_justifyleft_desc", "JustifyLeft", "", false);
  119. contextMenu.addItem(tinyMCE.baseURL + "/themes/" + theme + "/images/justifycenter.gif", "$lang_justifycenter_desc", "JustifyCenter", "", false);
  120. contextMenu.addItem(tinyMCE.baseURL + "/themes/" + theme + "/images/justifyright.gif", "$lang_justifyright_desc", "JustifyRight", "", false);
  121. contextMenu.addItem(tinyMCE.baseURL + "/themes/" + theme + "/images/justifyfull.gif", "$lang_justifyfull_desc", "JustifyFull", "", false);*/
  122. contextMenu.addSeparator();
  123. contextMenu.addItem(tinyMCE.baseURL + "/plugins/table/images/table.gif", "$lang_table_desc", "mceInsertTable", "insert");
  124. contextMenu.addItem(tinyMCE.baseURL + "/plugins/table/images/table.gif", "$lang_table_props_desc", "mceInsertTable");
  125. contextMenu.addItem(tinyMCE.baseURL + "/plugins/table/images/table_cell_props.gif", "$lang_table_cell_desc", "mceTableCellProps");
  126. contextMenu.addItem(tinyMCE.baseURL + "/plugins/table/images/table_delete.gif", "$lang_table_del", "mceTableDelete");
  127. contextMenu.addSeparator();
  128. contextMenu.addItem(tinyMCE.baseURL + "/plugins/table/images/table_row_props.gif", "$lang_table_row_desc", "mceTableRowProps");
  129. contextMenu.addItem(tinyMCE.baseURL + "/plugins/table/images/table_insert_row_before.gif", "$lang_table_row_before_desc", "mceTableInsertRowBefore");
  130. contextMenu.addItem(tinyMCE.baseURL + "/plugins/table/images/table_insert_row_after.gif", "$lang_table_row_after_desc", "mceTableInsertRowAfter");
  131. contextMenu.addItem(tinyMCE.baseURL + "/plugins/table/images/table_delete_row.gif", "$lang_table_delete_row_desc", "mceTableDeleteRow");
  132. contextMenu.addSeparator();
  133. contextMenu.addItem(tinyMCE.baseURL + "/plugins/table/images/table_insert_col_before.gif", "$lang_table_col_before_desc", "mceTableInsertColBefore");
  134. contextMenu.addItem(tinyMCE.baseURL + "/plugins/table/images/table_insert_col_after.gif", "$lang_table_col_after_desc", "mceTableInsertColAfter");
  135. contextMenu.addItem(tinyMCE.baseURL + "/plugins/table/images/table_delete_col.gif", "$lang_table_delete_col_desc", "mceTableDeleteCol");
  136. contextMenu.addSeparator();
  137. contextMenu.addItem(tinyMCE.baseURL + "/plugins/table/images/table_split_cells.gif", "$lang_table_split_cells_desc", "mceTableSplitCells", "", (colspan == "1" && rowspan == "1"));
  138. contextMenu.addItem(tinyMCE.baseURL + "/plugins/table/images/table_merge_cells.gif", "$lang_table_merge_cells_desc", "mceTableMergeCells", "", false);
  139. }
  140. break;
  141. }
  142. } else {
  143. // Add table specific
  144. if (inst.hasPlugin("table")) {
  145. contextMenu.addSeparator();
  146. contextMenu.addItem(tinyMCE.baseURL + "/plugins/table/images/table.gif", "$lang_table_desc", "mceInsertTable", "insert");
  147. }
  148. }
  149. contextMenu.show(x, y);
  150. }, 10);
  151. // Cancel default handeling
  152. tinyMCE.cancelEvent(e);
  153. return false;
  154. },
  155. _hideContextMenu : function() {
  156. if (TinyMCE_ContextMenuPlugin._contextMenu)
  157. TinyMCE_ContextMenuPlugin._contextMenu.hide();
  158. },
  159. _commandHandler : function(command, value) {
  160. var cm = TinyMCE_ContextMenuPlugin._contextMenu;
  161. cm.hide();
  162. // UI must be true on these
  163. var ui = false;
  164. if (command == "mceInsertTable" || command == "mceTableCellProps" || command == "mceTableRowProps" || command == "mceTableMergeCells")
  165. ui = true;
  166. if (command == "Paste")
  167. value = null;
  168. if (tinyMCE.getParam("dialog_type") == "modal" && tinyMCE.isMSIE) {
  169. // Cell properties will generate access denied error is this isn't done?!
  170. window.setTimeout(function() {
  171. cm.inst.execCommand(command, ui, value);
  172. }, 100);
  173. } else
  174. cm.inst.execCommand(command, ui, value);
  175. }
  176. };
  177. tinyMCE.addPlugin("contextmenu", TinyMCE_ContextMenuPlugin);
  178. // Context menu class
  179. function TinyMCE_ContextMenu(settings) {
  180. var doc, self = this;
  181. // Default value function
  182. function defParam(key, def_val) {
  183. settings[key] = typeof(settings[key]) != "undefined" ? settings[key] : def_val;
  184. }
  185. this.isMSIE = (navigator.appName == "Microsoft Internet Explorer");
  186. // Setup contextmenu div
  187. this.contextMenuDiv = document.createElement("div");
  188. this.contextMenuDiv.className = "contextMenu";
  189. this.contextMenuDiv.setAttribute("class", "contextMenu");
  190. this.contextMenuDiv.style.display = "none";
  191. this.contextMenuDiv.style.position = 'absolute';
  192. this.contextMenuDiv.style.zindex = 1000;
  193. this.contextMenuDiv.style.left = '0';
  194. this.contextMenuDiv.style.top = '0';
  195. this.contextMenuDiv.unselectable = "on";
  196. document.body.appendChild(this.contextMenuDiv);
  197. // Setup default values
  198. defParam("commandhandler", "");
  199. defParam("spacer_image", "images/spacer.gif");
  200. this.items = new Array();
  201. this.settings = settings;
  202. this.html = "";
  203. // IE Popup
  204. if (tinyMCE.isMSIE && !tinyMCE.isMSIE5_0 && !tinyMCE.isOpera) {
  205. this.pop = window.createPopup();
  206. doc = this.pop.document;
  207. doc.open();
  208. 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>');
  209. doc.close();
  210. }
  211. };
  212. TinyMCE_ContextMenu.prototype = {
  213. clearAll : function() {
  214. this.html = "";
  215. this.contextMenuDiv.innerHTML = "";
  216. },
  217. addSeparator : function() {
  218. 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>';
  219. },
  220. addItem : function(icon, title, command, value, disabled) {
  221. if (title.charAt(0) == '$')
  222. title = tinyMCE.getLang(title.substring(1));
  223. var onMouseDown = '';
  224. var html = '';
  225. if (tinyMCE.isMSIE && !tinyMCE.isMSIE5_0)
  226. onMouseDown = 'contextMenu.execCommand(\'' + command + '\', \'' + value + '\');return false;';
  227. else
  228. onMouseDown = this.settings['commandhandler'] + '(\'' + command + '\', \'' + value + '\');return false;';
  229. if (icon == "")
  230. icon = this.settings['spacer_image'];
  231. if (!disabled)
  232. html += '<tr class="contextMenuItem">';
  233. else
  234. html += '<tr class="contextMenuItemDisabled">';
  235. html += '<td class="contextMenuIcon"><img src="' + icon + '" width="20" height="20" class="contextMenuImage" /></td>';
  236. html += '<td><div class="contextMenuText">';
  237. html += '<a href="javascript:void(0);" onclick="' + onMouseDown + '" onmousedown="return false;">&#160;';
  238. // Add text
  239. html += title;
  240. html += '&#160;</a>';
  241. html += '</div></td>';
  242. html += '</tr>';
  243. // Add to main
  244. this.html += html;
  245. },
  246. show : function(x, y) {
  247. var vp, width, height, yo;
  248. if (this.html == "")
  249. return;
  250. var html = '';
  251. html += '<a href="#"></a><table border="0" cellpadding="0" cellspacing="0">';
  252. html += this.html;
  253. html += '</table>';
  254. this.contextMenuDiv.innerHTML = html;
  255. // Get dimensions
  256. this.contextMenuDiv.style.display = "block";
  257. width = this.contextMenuDiv.offsetWidth;
  258. height = this.contextMenuDiv.offsetHeight;
  259. this.contextMenuDiv.style.display = "none";
  260. if (tinyMCE.isMSIE && !tinyMCE.isMSIE5_0 && !tinyMCE.isOpera) {
  261. // Setup popup and show
  262. this.pop.document.body.innerHTML = '<div class="contextMenu">' + html + "</div>";
  263. this.pop.document.tinyMCE = tinyMCE;
  264. this.pop.document.contextMenu = this;
  265. this.pop.show(x, y, width, height);
  266. } else {
  267. vp = this.getViewPort();
  268. yo = tinyMCE.isMSIE5_0 ? document.body.scrollTop : self.pageYOffset;
  269. this.contextMenuDiv.style.left = (x > vp.left + vp.width - width ? vp.left + vp.width - width : x) + 'px';
  270. this.contextMenuDiv.style.top = (y > vp.top + vp.height - height ? vp.top + vp.height - height : y) + 'px';
  271. this.contextMenuDiv.style.display = "block";
  272. }
  273. },
  274. getViewPort : function() {
  275. return {
  276. left : self.pageXOffset || self.document.documentElement.scrollLeft || self.document.body.scrollLeft,
  277. top: self.pageYOffset || self.document.documentElement.scrollTop || self.document.body.scrollTop,
  278. width : document.documentElement.offsetWidth || document.body.offsetWidth,
  279. height : self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
  280. };
  281. },
  282. hide : function() {
  283. if (tinyMCE.isMSIE && !tinyMCE.isMSIE5_0 && !tinyMCE.isOpera)
  284. this.pop.hide();
  285. else
  286. this.contextMenuDiv.style.display = "none";
  287. },
  288. execCommand : function(command, value) {
  289. eval(this.settings['commandhandler'] + "(command, value);");
  290. }
  291. };