A tumblelog CMS built on AJAX, PHP and MySQL.

editor_plugin_src.js 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. tinyMCE.importPluginLanguagePack('flash');
  9. var TinyMCE_FlashPlugin = {
  10. getInfo : function() {
  11. return {
  12. longname : 'Flash',
  13. author : 'Moxiecode Systems AB',
  14. authorurl : 'http://tinymce.moxiecode.com',
  15. infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/flash',
  16. version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion
  17. };
  18. },
  19. initInstance : function(inst) {
  20. if (!tinyMCE.settings['flash_skip_plugin_css'])
  21. tinyMCE.importCSS(inst.getDoc(), tinyMCE.baseURL + "/plugins/flash/css/content.css");
  22. },
  23. getControlHTML : function(cn) {
  24. switch (cn) {
  25. case "flash":
  26. return tinyMCE.getButtonHTML(cn, 'lang_flash_desc', '{$pluginurl}/images/flash.gif', 'mceFlash');
  27. }
  28. return "";
  29. },
  30. execCommand : function(editor_id, element, command, user_interface, value) {
  31. // Handle commands
  32. switch (command) {
  33. case "mceFlash":
  34. var name = "", swffile = "", swfwidth = "", swfheight = "", action = "insert";
  35. var template = new Array();
  36. var inst = tinyMCE.getInstanceById(editor_id);
  37. var focusElm = inst.getFocusElement();
  38. template['file'] = '../../plugins/flash/flash.htm'; // Relative to theme
  39. template['width'] = 430;
  40. template['height'] = 175;
  41. template['width'] += tinyMCE.getLang('lang_flash_delta_width', 0);
  42. template['height'] += tinyMCE.getLang('lang_flash_delta_height', 0);
  43. // Is selection a image
  44. if (focusElm != null && focusElm.nodeName.toLowerCase() == "img") {
  45. name = tinyMCE.getAttrib(focusElm, 'class');
  46. if (name.indexOf('mceItemFlash') == -1) // Not a Flash
  47. return true;
  48. // Get rest of Flash items
  49. swffile = tinyMCE.getAttrib(focusElm, 'alt');
  50. if (tinyMCE.getParam('convert_urls'))
  51. swffile = eval(tinyMCE.settings['urlconverter_callback'] + "(swffile, null, true);");
  52. swfwidth = tinyMCE.getAttrib(focusElm, 'width');
  53. swfheight = tinyMCE.getAttrib(focusElm, 'height');
  54. action = "update";
  55. }
  56. tinyMCE.openWindow(template, {editor_id : editor_id, inline : "yes", swffile : swffile, swfwidth : swfwidth, swfheight : swfheight, action : action});
  57. return true;
  58. }
  59. // Pass to next handler in chain
  60. return false;
  61. },
  62. cleanup : function(type, content) {
  63. switch (type) {
  64. case "insert_to_editor_dom":
  65. // Force relative/absolute
  66. if (tinyMCE.getParam('convert_urls')) {
  67. var imgs = content.getElementsByTagName("img");
  68. for (var i=0; i<imgs.length; i++) {
  69. if (tinyMCE.getAttrib(imgs[i], "class") == "mceItemFlash") {
  70. var src = tinyMCE.getAttrib(imgs[i], "alt");
  71. if (tinyMCE.getParam('convert_urls'))
  72. src = eval(tinyMCE.settings['urlconverter_callback'] + "(src, null, true);");
  73. imgs[i].setAttribute('alt', src);
  74. imgs[i].setAttribute('title', src);
  75. }
  76. }
  77. }
  78. break;
  79. case "get_from_editor_dom":
  80. var imgs = content.getElementsByTagName("img");
  81. for (var i=0; i<imgs.length; i++) {
  82. if (tinyMCE.getAttrib(imgs[i], "class") == "mceItemFlash") {
  83. var src = tinyMCE.getAttrib(imgs[i], "alt");
  84. if (tinyMCE.getParam('convert_urls'))
  85. src = eval(tinyMCE.settings['urlconverter_callback'] + "(src, null, true);");
  86. imgs[i].setAttribute('alt', src);
  87. imgs[i].setAttribute('title', src);
  88. }
  89. }
  90. break;
  91. case "insert_to_editor":
  92. var startPos = 0;
  93. var embedList = new Array();
  94. // Fix the embed and object elements
  95. content = content.replace(new RegExp('<[ ]*embed','gi'),'<embed');
  96. content = content.replace(new RegExp('<[ ]*/embed[ ]*>','gi'),'</embed>');
  97. content = content.replace(new RegExp('<[ ]*object','gi'),'<object');
  98. content = content.replace(new RegExp('<[ ]*/object[ ]*>','gi'),'</object>');
  99. // Parse all embed tags
  100. while ((startPos = content.indexOf('<embed', startPos+1)) != -1) {
  101. var endPos = content.indexOf('>', startPos);
  102. var attribs = TinyMCE_FlashPlugin._parseAttributes(content.substring(startPos + 6, endPos));
  103. embedList[embedList.length] = attribs;
  104. }
  105. // Parse all object tags and replace them with images from the embed data
  106. var index = 0;
  107. while ((startPos = content.indexOf('<object', startPos)) != -1) {
  108. if (index >= embedList.length)
  109. break;
  110. var attribs = embedList[index];
  111. // Find end of object
  112. endPos = content.indexOf('</object>', startPos);
  113. endPos += 9;
  114. // Insert image
  115. var contentAfter = content.substring(endPos);
  116. content = content.substring(0, startPos);
  117. content += '<img width="' + attribs["width"] + '" height="' + attribs["height"] + '"';
  118. content += ' src="' + (tinyMCE.getParam("theme_href") + '/images/spacer.gif') + '" title="' + attribs["src"] + '"';
  119. content += ' alt="' + attribs["src"] + '" class="mceItemFlash" />' + content.substring(endPos);
  120. content += contentAfter;
  121. index++;
  122. startPos++;
  123. }
  124. // Parse all embed tags and replace them with images from the embed data
  125. var index = 0;
  126. while ((startPos = content.indexOf('<embed', startPos)) != -1) {
  127. if (index >= embedList.length)
  128. break;
  129. var attribs = embedList[index];
  130. // Find end of embed
  131. endPos = content.indexOf('>', startPos);
  132. endPos += 9;
  133. // Insert image
  134. var contentAfter = content.substring(endPos);
  135. content = content.substring(0, startPos);
  136. content += '<img width="' + attribs["width"] + '" height="' + attribs["height"] + '"';
  137. content += ' src="' + (tinyMCE.getParam("theme_href") + '/images/spacer.gif') + '" title="' + attribs["src"] + '"';
  138. content += ' alt="' + attribs["src"] + '" class="mceItemFlash" />' + content.substring(endPos);
  139. content += contentAfter;
  140. index++;
  141. startPos++;
  142. }
  143. break;
  144. case "get_from_editor":
  145. // Parse all img tags and replace them with object+embed
  146. var startPos = -1;
  147. while ((startPos = content.indexOf('<img', startPos+1)) != -1) {
  148. var endPos = content.indexOf('/>', startPos);
  149. var attribs = TinyMCE_FlashPlugin._parseAttributes(content.substring(startPos + 4, endPos));
  150. // Is not flash, skip it
  151. if (attribs['class'] != "mceItemFlash")
  152. continue;
  153. endPos += 2;
  154. var embedHTML = '';
  155. var wmode = tinyMCE.getParam("flash_wmode", "");
  156. var quality = tinyMCE.getParam("flash_quality", "high");
  157. var menu = tinyMCE.getParam("flash_menu", "false");
  158. // Insert object + embed
  159. embedHTML += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"';
  160. embedHTML += ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0"';
  161. embedHTML += ' width="' + attribs["width"] + '" height="' + attribs["height"] + '">';
  162. embedHTML += '<param name="movie" value="' + attribs["title"] + '" />';
  163. embedHTML += '<param name="quality" value="' + quality + '" />';
  164. embedHTML += '<param name="menu" value="' + menu + '" />';
  165. embedHTML += '<param name="wmode" value="' + wmode + '" />';
  166. embedHTML += '<embed src="' + attribs["title"] + '" wmode="' + wmode + '" quality="' + quality + '" menu="' + menu + '" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + attribs["width"] + '" height="' + attribs["height"] + '"></embed></object>';
  167. // Insert embed/object chunk
  168. chunkBefore = content.substring(0, startPos);
  169. chunkAfter = content.substring(endPos);
  170. content = chunkBefore + embedHTML + chunkAfter;
  171. }
  172. break;
  173. }
  174. // Pass through to next handler in chain
  175. return content;
  176. },
  177. handleNodeChange : function(editor_id, node, undo_index, undo_levels, visual_aid, any_selection) {
  178. if (node == null)
  179. return;
  180. do {
  181. if (node.nodeName == "IMG" && tinyMCE.getAttrib(node, 'class').indexOf('mceItemFlash') == 0) {
  182. tinyMCE.switchClass(editor_id + '_flash', 'mceButtonSelected');
  183. return true;
  184. }
  185. } while ((node = node.parentNode));
  186. tinyMCE.switchClass(editor_id + '_flash', 'mceButtonNormal');
  187. return true;
  188. },
  189. // Private plugin internal functions
  190. _parseAttributes : function(attribute_string) {
  191. var attributeName = "";
  192. var attributeValue = "";
  193. var withInName;
  194. var withInValue;
  195. var attributes = new Array();
  196. var whiteSpaceRegExp = new RegExp('^[ \n\r\t]+', 'g');
  197. if (attribute_string == null || attribute_string.length < 2)
  198. return null;
  199. withInName = withInValue = false;
  200. for (var i=0; i<attribute_string.length; i++) {
  201. var chr = attribute_string.charAt(i);
  202. if ((chr == '"' || chr == "'") && !withInValue)
  203. withInValue = true;
  204. else if ((chr == '"' || chr == "'") && withInValue) {
  205. withInValue = false;
  206. var pos = attributeName.lastIndexOf(' ');
  207. if (pos != -1)
  208. attributeName = attributeName.substring(pos+1);
  209. attributes[attributeName.toLowerCase()] = attributeValue.substring(1);
  210. attributeName = "";
  211. attributeValue = "";
  212. } else if (!whiteSpaceRegExp.test(chr) && !withInName && !withInValue)
  213. withInName = true;
  214. if (chr == '=' && withInName)
  215. withInName = false;
  216. if (withInName)
  217. attributeName += chr;
  218. if (withInValue)
  219. attributeValue += chr;
  220. }
  221. return attributes;
  222. }
  223. };
  224. tinyMCE.addPlugin("flash", TinyMCE_FlashPlugin);