PageRenderTime 63ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/js/mage/adminhtml/wysiwyg/tiny_mce/setup.js

https://bitbucket.org/dnejedly/eaparts
JavaScript | 358 lines | 279 code | 47 blank | 32 comment | 35 complexity | c6b9e0de4619712bb03cb5103afd45d5 MD5 | raw file
  1. /**
  2. * Magento
  3. *
  4. * NOTICE OF LICENSE
  5. *
  6. * This source file is subject to the Academic Free License (AFL 3.0)
  7. * that is bundled with this package in the file LICENSE_AFL.txt.
  8. * It is also available through the world-wide-web at this URL:
  9. * http://opensource.org/licenses/afl-3.0.php
  10. * If you did not receive a copy of the license and are unable to
  11. * obtain it through the world-wide-web, please send an email
  12. * to license@magentocommerce.com so we can send you a copy immediately.
  13. *
  14. * DISCLAIMER
  15. *
  16. * Do not edit or add to this file if you wish to upgrade Magento to newer
  17. * versions in the future. If you wish to customize Magento for your
  18. * needs please refer to http://www.magentocommerce.com for more information.
  19. *
  20. * @category Mage
  21. * @package Mage_Adminhtml
  22. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  23. * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
  24. */
  25. var tinyMceWysiwygSetup = Class.create();
  26. tinyMceWysiwygSetup.prototype =
  27. {
  28. mediaBrowserOpener: null,
  29. mediaBrowserTargetElementId: null,
  30. initialize: function(htmlId, config)
  31. {
  32. this.id = htmlId;
  33. this.config = config;
  34. varienGlobalEvents.attachEventHandler('tinymceChange', this.onChangeContent.bind(this));
  35. this.notifyFirebug();
  36. if(typeof tinyMceEditors == 'undefined') {
  37. tinyMceEditors = $H({});
  38. }
  39. tinyMceEditors.set(this.id, this);
  40. },
  41. notifyFirebug: function() {
  42. if (firebugEnabled() && $('fb' + this.id) == undefined) {
  43. var noticeHtml = '<ul class="messages message-firebug" id="fb' + this.id + '"><li class="notice-msg">';
  44. noticeHtml+= '<ul><li>';
  45. noticeHtml+= '<b>' + this.config.firebug_warning_title + ':</b> ';
  46. noticeHtml+= this.config.firebug_warning_text;
  47. noticeHtml+= ' <a id="hidefb' + this.id + '" href="">' + this.config.firebug_warning_anchor + '</a>';
  48. noticeHtml+= '</li></ul>';
  49. noticeHtml+= '</li></ul>';
  50. $('buttons' + this.id).insert({before: noticeHtml});
  51. Event.observe($('hidefb' + this.id), "click", function(e) {
  52. $('fb' + this.id).remove();
  53. Event.stop(e);
  54. }.bind(this));
  55. }
  56. },
  57. setup: function(mode)
  58. {
  59. if (this.config.widget_plugin_src) {
  60. tinymce.PluginManager.load('magentowidget', this.config.widget_plugin_src);
  61. }
  62. if (this.config.plugins) {
  63. (this.config.plugins).each(function(plugin){
  64. tinymce.PluginManager.load(plugin.name, plugin.src);
  65. });
  66. }
  67. tinyMCE.init(this.getSettings(mode));
  68. },
  69. getSettings: function(mode)
  70. {
  71. var plugins = 'safari,pagebreak,style,layer,table,advhr,advimage,emotions,iespell,media,searchreplace,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras';
  72. if (this.config.widget_plugin_src) {
  73. plugins = 'magentowidget,' + plugins;
  74. }
  75. var magentoPluginsOptions = $H({});
  76. var magentoPlugins = '';
  77. if (this.config.plugins) {
  78. (this.config.plugins).each(function(plugin){
  79. magentoPlugins = plugin.name + ',' + magentoPlugins;
  80. magentoPluginsOptions.set(plugin.name, plugin.options);
  81. });
  82. if (magentoPlugins) {
  83. plugins = '-' + magentoPlugins + plugins;
  84. }
  85. }
  86. var settings = {
  87. mode : (mode != undefined ? mode : 'none'),
  88. elements : this.id,
  89. theme : 'advanced',
  90. plugins : plugins,
  91. theme_advanced_buttons1 : magentoPlugins + 'magentowidget,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect',
  92. theme_advanced_buttons2 : 'cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,forecolor,backcolor',
  93. theme_advanced_buttons3 : 'tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,iespell,media,advhr,|,ltr,rtl,|,fullscreen',
  94. theme_advanced_buttons4 : 'insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,pagebreak',
  95. theme_advanced_toolbar_location : 'top',
  96. theme_advanced_toolbar_align : 'left',
  97. theme_advanced_statusbar_location : 'bottom',
  98. theme_advanced_resizing : true,
  99. convert_urls : false,
  100. relative_urls : false,
  101. content_css: this.config.content_css,
  102. custom_popup_css: this.config.popup_css,
  103. magentowidget_url: this.config.widget_window_url,
  104. magentoPluginsOptions: magentoPluginsOptions,
  105. doctype : '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
  106. setup : function(ed) {
  107. ed.onSubmit.add(function(ed, e) {
  108. varienGlobalEvents.fireEvent('tinymceSubmit', e);
  109. });
  110. ed.onPaste.add(function(ed, e, o) {
  111. varienGlobalEvents.fireEvent('tinymcePaste', o);
  112. });
  113. ed.onBeforeSetContent.add(function(ed, o) {
  114. varienGlobalEvents.fireEvent('tinymceBeforeSetContent', o);
  115. });
  116. ed.onSetContent.add(function(ed, o) {
  117. varienGlobalEvents.fireEvent('tinymceSetContent', o);
  118. });
  119. ed.onSaveContent.add(function(ed, o) {
  120. varienGlobalEvents.fireEvent('tinymceSaveContent', o);
  121. });
  122. ed.onChange.add(function(ed, l) {
  123. varienGlobalEvents.fireEvent('tinymceChange', l);
  124. });
  125. ed.onExecCommand.add(function(ed, cmd, ui, val) {
  126. varienGlobalEvents.fireEvent('tinymceExecCommand', cmd);
  127. });
  128. }
  129. };
  130. // Set the document base URL
  131. if (this.config.document_base_url) {
  132. settings.document_base_url = this.config.document_base_url;
  133. }
  134. if (this.config.files_browser_window_url) {
  135. settings.file_browser_callback = function(fieldName, url, objectType, w) {
  136. varienGlobalEvents.fireEvent("open_browser_callback", {win:w, type:objectType, field:fieldName});
  137. };
  138. }
  139. if (this.config.width) {
  140. settings.width = this.config.width;
  141. }
  142. if (this.config.height) {
  143. settings.height = this.config.height;
  144. }
  145. return settings;
  146. },
  147. openFileBrowser: function(o) {
  148. var typeTitle;
  149. var storeId = this.config.store_id !== null ? this.config.store_id : 0;
  150. var wUrl = this.config.files_browser_window_url +
  151. 'target_element_id/' + this.id + '/' +
  152. 'store/' + storeId + '/';
  153. this.mediaBrowserOpener = o.win;
  154. this.mediaBrowserOpener.blur();
  155. this.mediaBrowserTargetElementId = o.field;
  156. if (typeof(o.type) != 'undefined' && o.type != "") {
  157. typeTitle = 'image' == o.type ? this.translate('Insert Image...') : this.translate('Insert Media...');
  158. wUrl = wUrl + "type/" + o.type + "/";
  159. } else {
  160. typeTitle = this.translate('Insert File...');
  161. }
  162. MediabrowserUtility.openDialog(wUrl, false, false, typeTitle);
  163. },
  164. translate: function(string) {
  165. return 'undefined' != typeof(Translator) ? Translator.translate(string) : string;
  166. },
  167. getMediaBrowserOpener: function() {
  168. return this.mediaBrowserOpener;
  169. },
  170. getMediaBrowserTargetElementId: function() {
  171. return this.mediaBrowserTargetElementId;
  172. },
  173. getToggleButton: function() {
  174. return $('toggle' + this.id);
  175. },
  176. getPluginButtons: function() {
  177. return $$('#buttons' + this.id + ' > button.plugin');
  178. },
  179. turnOn: function() {
  180. this.closePopups();
  181. this.setup();
  182. tinyMCE.execCommand('mceAddControl', false, this.id);
  183. this.getPluginButtons().each(function(e) {
  184. e.hide();
  185. });
  186. },
  187. turnOff: function() {
  188. this.closePopups();
  189. tinyMCE.execCommand('mceRemoveControl', false, this.id);
  190. this.getPluginButtons().each(function(e) {
  191. e.show();
  192. });
  193. if (Prototype.Browser.IE) {
  194. // workaround for ie textarea redraw bug
  195. window.setTimeout(function(){
  196. $(this.id).value = $(this.id).value;
  197. }.bind(this), 0);
  198. }
  199. },
  200. closePopups: function() {
  201. // close all popups to avoid problems with updating parent content area
  202. closeEditorPopup('widget_window' + this.id);
  203. closeEditorPopup('browser_window' + this.id);
  204. },
  205. toggle: function() {
  206. if (!tinyMCE.get(this.id)) {
  207. this.turnOn();
  208. return true;
  209. } else {
  210. this.turnOff();
  211. return false;
  212. }
  213. },
  214. onFormValidation: function() {
  215. if (tinyMCE.get(this.id)) {
  216. $(this.id).value = tinyMCE.get(this.id).getContent();
  217. }
  218. },
  219. onChangeContent: function() {
  220. // Add "changed" to tab class if it exists
  221. if(this.config.tab_id) {
  222. var tab = $$('a[id$=' + this.config.tab_id + ']')[0];
  223. if ($(tab) != undefined && $(tab).hasClassName('tab-item-link')) {
  224. $(tab).addClassName('changed');
  225. }
  226. }
  227. },
  228. // retrieve directives URL with substituted directive value
  229. makeDirectiveUrl: function(directive) {
  230. return this.config.directives_url.replace('directive', 'directive/___directive/' + directive);
  231. },
  232. encodeDirectives: function(content) {
  233. // collect all HTML tags with attributes that contain directives
  234. return content.gsub(/<([a-z0-9\-\_]+.+?)([a-z0-9\-\_]+=".*?\{\{.+?\}\}.*?".+?)>/i, function(match) {
  235. var attributesString = match[2];
  236. // process tag attributes string
  237. attributesString = attributesString.gsub(/([a-z0-9\-\_]+)="(.*?)(\{\{.+?\}\})(.*?)"/i, function(m) {
  238. return m[1] + '="' + m[2] + this.makeDirectiveUrl(Base64.mageEncode(m[3])) + m[4] + '"';
  239. }.bind(this));
  240. return '<' + match[1] + attributesString + '>';
  241. }.bind(this));
  242. },
  243. encodeWidgets: function(content) {
  244. return content.gsub(/\{\{widget(.*?)\}\}/i, function(match){
  245. var attributes = this.parseAttributesString(match[1]);
  246. if (attributes.type) {
  247. var placeholderFilename = attributes.type.replace(/\//g, "__") + ".gif";
  248. if (!this.widgetPlaceholderExist(placeholderFilename)) {
  249. placeholderFilename = 'default.gif';
  250. }
  251. var imageSrc = this.config.widget_images_url + placeholderFilename;
  252. var imageHtml = '<img';
  253. imageHtml+= ' id="' + Base64.idEncode(match[0]) + '"';
  254. imageHtml+= ' src="' + imageSrc + '"';
  255. imageHtml+= ' title="' + match[0].replace(/\{\{/g, '{').replace(/\}\}/g, '}').replace(/\"/g, '&quot;') + '"';
  256. imageHtml+= '>';
  257. return imageHtml;
  258. }
  259. }.bind(this));
  260. },
  261. decodeDirectives: function(content) {
  262. // escape special chars in directives url to use it in regular expression
  263. var url = this.makeDirectiveUrl('%directive%').replace(/([$^.?*!+:=()\[\]{}|\\])/g, '\\$1');
  264. var reg = new RegExp(url.replace('%directive%', '([a-zA-Z0-9,_-]+)'));
  265. return content.gsub(reg, function(match) {
  266. return Base64.mageDecode(match[1]);
  267. }.bind(this));
  268. },
  269. decodeWidgets: function(content) {
  270. return content.gsub(/<img([^>]+id=\"[^>]+)>/i, function(match) {
  271. var attributes = this.parseAttributesString(match[1]);
  272. if(attributes.id) {
  273. var widgetCode = Base64.idDecode(attributes.id);
  274. if (widgetCode.indexOf('{{widget') != -1) {
  275. return widgetCode;
  276. }
  277. return match[0];
  278. }
  279. return match[0];
  280. }.bind(this));
  281. },
  282. parseAttributesString: function(attributes) {
  283. var result = {};
  284. attributes.gsub(/(\w+)(?:\s*=\s*(?:(?:"((?:\\.|[^"])*)")|(?:'((?:\\.|[^'])*)')|([^>\s]+)))?/, function(match){
  285. result[match[1]] = match[2];
  286. });
  287. return result;
  288. },
  289. beforeSetContent: function(o) {
  290. if (this.config.add_widgets) {
  291. o.content = this.encodeWidgets(o.content);
  292. o.content = this.encodeDirectives(o.content);
  293. } else if (this.config.add_directives) {
  294. o.content = this.encodeDirectives(o.content);
  295. }
  296. },
  297. saveContent: function(o) {
  298. if (this.config.add_widgets) {
  299. o.content = this.decodeWidgets(o.content);
  300. o.content = this.decodeDirectives(o.content);
  301. } else if (this.config.add_directives) {
  302. o.content = this.decodeDirectives(o.content);
  303. }
  304. },
  305. widgetPlaceholderExist: function(filename) {
  306. return this.config.widget_placeholders.indexOf(filename) != -1;
  307. }
  308. }