fckeditor-2.6.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. (function($) {
  2. /**
  3. * Attach this editor to a target element.
  4. */
  5. Drupal.wysiwyg.editor.attach.fckeditor = function(context, params, settings) {
  6. var FCKinstance = new FCKeditor(params.field, settings.Width, settings.Height, settings.ToolbarSet);
  7. // Apply editor instance settings.
  8. FCKinstance.BasePath = settings.EditorPath;
  9. FCKinstance.Config.wysiwygFormat = params.format;
  10. FCKinstance.Config.CustomConfigurationsPath = settings.CustomConfigurationsPath;
  11. // Load Drupal plugins and apply format specific settings.
  12. // @see fckeditor.config.js
  13. // @see Drupal.wysiwyg.editor.instance.fckeditor.init()
  14. // Attach editor.
  15. FCKinstance.ReplaceTextarea();
  16. };
  17. /**
  18. * Detach a single or all editors.
  19. */
  20. Drupal.wysiwyg.editor.detach.fckeditor = function (context, params, trigger) {
  21. var instances = [];
  22. if (typeof params != 'undefined' && typeof FCKeditorAPI != 'undefined') {
  23. var instance = FCKeditorAPI.GetInstance(params.field);
  24. if (instance) {
  25. instances[params.field] = instance;
  26. }
  27. }
  28. else {
  29. instances = FCKeditorAPI.__Instances;
  30. }
  31. for (var instanceName in instances) {
  32. var instance = instances[instanceName];
  33. instance.UpdateLinkedField();
  34. if (trigger == 'serialize') {
  35. // The editor is not being removed from the DOM, so updating the linked
  36. // field is the only action necessary.
  37. continue;
  38. }
  39. // Since we already detach the editor and update the textarea, the submit
  40. // event handler needs to be removed to prevent data loss (in IE).
  41. // FCKeditor uses 2 nested iFrames; instance.EditingArea.Window is the
  42. // deepest. Its parent is the iFrame containing the editor.
  43. var instanceScope = instance.EditingArea.Window.parent;
  44. instanceScope.FCKTools.RemoveEventListener(instance.GetParentForm(), 'submit', instance.UpdateLinkedField);
  45. // Run cleanups before forcing an unload of the iFrames or IE crashes.
  46. // This also deletes the instance from the FCKeditorAPI.__Instances array.
  47. instanceScope.FCKTools.RemoveEventListener(instanceScope, 'unload', instanceScope.FCKeditorAPI_Cleanup);
  48. instanceScope.FCKTools.RemoveEventListener(instanceScope, 'beforeunload', instanceScope.FCKeditorAPI_ConfirmCleanup);
  49. if (jQuery.isFunction(instanceScope.FCKIECleanup_Cleanup)) {
  50. instanceScope.FCKIECleanup_Cleanup();
  51. }
  52. instanceScope.FCKeditorAPI_ConfirmCleanup();
  53. instanceScope.FCKeditorAPI_Cleanup();
  54. // Remove the editor elements.
  55. $('#' + instanceName + '___Config').remove();
  56. $('#' + instanceName + '___Frame').remove();
  57. $('#' + instanceName).show();
  58. }
  59. };
  60. Drupal.wysiwyg.editor.instance.fckeditor = {
  61. init: function(instance) {
  62. // Track which editor instance is active.
  63. instance.FCK.Events.AttachEvent('OnFocus', function(editorInstance) {
  64. Drupal.wysiwyg.activeId = editorInstance.Name;
  65. });
  66. // Create a custom data processor to wrap the default one and allow Drupal
  67. // plugins modify the editor contents.
  68. var wysiwygDataProcessor = function() {};
  69. wysiwygDataProcessor.prototype = new instance.FCKDataProcessor();
  70. // Attach: Convert text into HTML.
  71. wysiwygDataProcessor.prototype.ConvertToHtml = function(data) {
  72. // Called from SetData() with stripped comments/scripts, revert those
  73. // manipulations and attach Drupal plugins.
  74. var data = instance.FCKConfig.ProtectedSource.Revert(data);
  75. if (Drupal.settings.wysiwyg.plugins[instance.wysiwygFormat] && Drupal.settings.wysiwyg.plugins[instance.wysiwygFormat].drupal) {
  76. for (var plugin in Drupal.settings.wysiwyg.plugins[instance.wysiwygFormat].drupal) {
  77. if (typeof Drupal.wysiwyg.plugins[plugin].attach == 'function') {
  78. data = Drupal.wysiwyg.plugins[plugin].attach(data, Drupal.settings.wysiwyg.plugins.drupal[plugin], instance.FCK.Name);
  79. data = Drupal.wysiwyg.editor.instance.fckeditor.prepareContent(data);
  80. }
  81. }
  82. }
  83. // Re-protect the source and use the original data processor to convert it
  84. // into XHTML.
  85. data = instance.FCKConfig.ProtectedSource.Protect(data);
  86. return instance.FCKDataProcessor.prototype.ConvertToHtml.call(this, data);
  87. };
  88. // Detach: Convert HTML into text.
  89. wysiwygDataProcessor.prototype.ConvertToDataFormat = function(rootNode, excludeRoot, ignoreIfEmptyParagraph, format) {
  90. // Called from GetData(), convert the content's DOM into a XHTML string
  91. // using the original data processor and detach Drupal plugins.
  92. var data = instance.FCKDataProcessor.prototype.ConvertToDataFormat.call(this, rootNode, excludeRoot, ignoreIfEmptyParagraph, format);
  93. if (Drupal.settings.wysiwyg.plugins[instance.wysiwygFormat] && Drupal.settings.wysiwyg.plugins[instance.wysiwygFormat].drupal) {
  94. for (var plugin in Drupal.settings.wysiwyg.plugins[instance.wysiwygFormat].drupal) {
  95. if (typeof Drupal.wysiwyg.plugins[plugin].detach == 'function') {
  96. data = Drupal.wysiwyg.plugins[plugin].detach(data, Drupal.settings.wysiwyg.plugins.drupal[plugin], instance.FCK.Name);
  97. }
  98. }
  99. }
  100. return data;
  101. };
  102. instance.FCK.DataProcessor = new wysiwygDataProcessor();
  103. },
  104. addPlugin: function(plugin, settings, pluginSettings, instance) {
  105. if (typeof Drupal.wysiwyg.plugins[plugin] != 'object') {
  106. return;
  107. }
  108. if (Drupal.settings.wysiwyg.plugins[instance.wysiwygFormat].drupal[plugin].css) {
  109. instance.FCKConfig.EditorAreaCSS += ',' + Drupal.settings.wysiwyg.plugins[instance.wysiwygFormat].drupal[plugin].css;
  110. }
  111. // @see fckcommands.js, fck_othercommands.js, fckpastewordcommand.js
  112. instance.FCKCommands.RegisterCommand(plugin, {
  113. // Invoke the plugin's button.
  114. Execute: function () {
  115. if (typeof Drupal.wysiwyg.plugins[plugin].invoke == 'function') {
  116. var data = { format: 'html', node: instance.FCKSelection.GetParentElement() };
  117. // @todo This is NOT the same as data.node.
  118. data.content = data.node.innerHTML;
  119. Drupal.wysiwyg.plugins[plugin].invoke(data, pluginSettings, instance.FCK.Name);
  120. }
  121. },
  122. // isNode: Return whether the plugin button should be enabled for the
  123. // current selection.
  124. // @see FCKUnlinkCommand.prototype.GetState()
  125. GetState: function () {
  126. // Always disabled if not in WYSIWYG mode.
  127. if (instance.FCK.EditMode != FCK_EDITMODE_WYSIWYG) {
  128. return FCK_TRISTATE_DISABLED;
  129. }
  130. var state = instance.FCK.GetNamedCommandState(this.Name);
  131. // FCKeditor sets the wrong state in WebKit browsers.
  132. if (!$.support.queryCommandEnabled && state == FCK_TRISTATE_DISABLED) {
  133. state = FCK_TRISTATE_OFF;
  134. }
  135. if (state == FCK_TRISTATE_OFF && instance.FCK.EditMode == FCK_EDITMODE_WYSIWYG) {
  136. if (typeof Drupal.wysiwyg.plugins[plugin].isNode == 'function') {
  137. var node = instance.FCKSelection.GetSelectedElement();
  138. state = Drupal.wysiwyg.plugins[plugin].isNode(node) ? FCK_TRISTATE_ON : FCK_TRISTATE_OFF;
  139. }
  140. }
  141. return state;
  142. },
  143. /**
  144. * Return information about the plugin as a name/value array.
  145. */
  146. Name: plugin
  147. });
  148. // Register the plugin button.
  149. // Arguments: commandName, label, tooltip, style, sourceView, contextSensitive, icon.
  150. instance.FCKToolbarItems.RegisterItem(plugin, new instance.FCKToolbarButton(plugin, settings.iconTitle, settings.iconTitle, null, false, true, settings.icon));
  151. },
  152. openDialog: function(dialog, params) {
  153. // @todo Implement open dialog.
  154. },
  155. closeDialog: function(dialog) {
  156. // @todo Implement close dialog.
  157. },
  158. prepareContent: function(content) {
  159. // @todo Not needed for FCKeditor?
  160. return content;
  161. },
  162. insert: function(content) {
  163. var instance = FCKeditorAPI.GetInstance(this.field);
  164. // @see FCK.InsertHtml(), FCK.InsertElement()
  165. instance.InsertHtml(content);
  166. },
  167. getContent: function () {
  168. var instance = FCKeditorAPI.GetInstance(this.field);
  169. return instance.GetData();
  170. },
  171. setContent: function (content) {
  172. var instance = FCKeditorAPI.GetInstance(this.field);
  173. instance.SetHTML(content);
  174. }
  175. };
  176. })(jQuery);