wysiwyg.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. (function($) {
  2. /**
  3. * Initialize editor libraries.
  4. *
  5. * Some editors need to be initialized before the DOM is fully loaded. The
  6. * init hook gives them a chance to do so.
  7. */
  8. Drupal.wysiwygInit = function() {
  9. // This breaks in Konqueror. Prevent it from running.
  10. if (/KDE/.test(navigator.vendor)) {
  11. return;
  12. }
  13. jQuery.each(Drupal.wysiwyg.editor.init, function(editor) {
  14. // Clone, so original settings are not overwritten.
  15. this(jQuery.extend(true, {}, Drupal.settings.wysiwyg.configs[editor]));
  16. });
  17. };
  18. /**
  19. * Attach editors to input formats and target elements (f.e. textareas).
  20. *
  21. * This behavior searches for input format selectors and formatting guidelines
  22. * that have been preprocessed by Wysiwyg API. All CSS classes of those elements
  23. * with the prefix 'wysiwyg-' are parsed into input format parameters, defining
  24. * the input format, configured editor, target element id, and variable other
  25. * properties, which are passed to the attach/detach hooks of the corresponding
  26. * editor.
  27. *
  28. * Furthermore, an "enable/disable rich-text" toggle link is added after the
  29. * target element to allow users to alter its contents in plain text.
  30. *
  31. * This is executed once, while editor attach/detach hooks can be invoked
  32. * multiple times.
  33. *
  34. * @param context
  35. * A DOM element, supplied by Drupal.attachBehaviors().
  36. */
  37. Drupal.behaviors.attachWysiwyg = {
  38. attach: function(context, settings) {
  39. // This breaks in Konqueror. Prevent it from running.
  40. if (/KDE/.test(navigator.vendor)) {
  41. return;
  42. }
  43. $('.wysiwyg', context).once('wysiwyg', function() {
  44. if (!this.id || typeof Drupal.settings.wysiwyg.triggers[this.id] === 'undefined') {
  45. return;
  46. }
  47. var $this = $(this);
  48. var params = Drupal.settings.wysiwyg.triggers[this.id];
  49. for (var format in params) {
  50. params[format].format = format;
  51. params[format].trigger = this.id;
  52. params[format].field = params.field;
  53. }
  54. var format = 'format' + this.value;
  55. // Directly attach this editor, if the input format is enabled or there is
  56. // only one input format at all.
  57. if ($this.is(':input')) {
  58. Drupal.wysiwygAttach(context, params[format]);
  59. }
  60. // Attach onChange handlers to input format selector elements.
  61. if ($this.is('select')) {
  62. $this.change(function() {
  63. // If not disabled, detach the current and attach a new editor.
  64. Drupal.wysiwygDetach(context, params[format]);
  65. format = 'format' + this.value;
  66. Drupal.wysiwygAttach(context, params[format]);
  67. });
  68. }
  69. // Detach any editor when the containing form is submitted.
  70. $('#' + params.field).parents('form').submit(function (event) {
  71. // Do not detach if the event was cancelled.
  72. if (event.isDefaultPrevented()) {
  73. return;
  74. }
  75. Drupal.wysiwygDetach(context, params[format]);
  76. });
  77. });
  78. }
  79. };
  80. /**
  81. * Attach an editor to a target element.
  82. *
  83. * This tests whether the passed in editor implements the attach hook and
  84. * invokes it if available. Editor profile settings are cloned first, so they
  85. * cannot be overridden. After attaching the editor, the toggle link is shown
  86. * again, except in case we are attaching no editor.
  87. *
  88. * @param context
  89. * A DOM element, supplied by Drupal.attachBehaviors().
  90. * @param params
  91. * An object containing input format parameters.
  92. */
  93. Drupal.wysiwygAttach = function(context, params) {
  94. if (typeof Drupal.wysiwyg.editor.attach[params.editor] == 'function') {
  95. // (Re-)initialize field instance.
  96. Drupal.wysiwyg.instances[params.field] = {};
  97. // Provide all input format parameters to editor instance.
  98. jQuery.extend(Drupal.wysiwyg.instances[params.field], params);
  99. // Provide editor callbacks for plugins, if available.
  100. if (typeof Drupal.wysiwyg.editor.instance[params.editor] == 'object') {
  101. jQuery.extend(Drupal.wysiwyg.instances[params.field], Drupal.wysiwyg.editor.instance[params.editor]);
  102. }
  103. // Store this field id, so (external) plugins can use it.
  104. // @todo Wrong point in time. Probably can only supported by editors which
  105. // support an onFocus() or similar event.
  106. Drupal.wysiwyg.activeId = params.field;
  107. // Attach or update toggle link, if enabled.
  108. if (params.toggle) {
  109. Drupal.wysiwygAttachToggleLink(context, params);
  110. }
  111. // Otherwise, ensure that toggle link is hidden.
  112. else {
  113. $('#wysiwyg-toggle-' + params.field).hide();
  114. }
  115. // Attach editor, if enabled by default or last state was enabled.
  116. if (params.status) {
  117. Drupal.wysiwyg.editor.attach[params.editor](context, params, (Drupal.settings.wysiwyg.configs[params.editor] ? jQuery.extend(true, {}, Drupal.settings.wysiwyg.configs[params.editor][params.format]) : {}));
  118. }
  119. // Otherwise, attach default behaviors.
  120. else {
  121. Drupal.wysiwyg.editor.attach.none(context, params);
  122. Drupal.wysiwyg.instances[params.field].editor = 'none';
  123. }
  124. }
  125. };
  126. /**
  127. * Detach all editors from a target element.
  128. *
  129. * @param context
  130. * A DOM element, supplied by Drupal.attachBehaviors().
  131. * @param params
  132. * An object containing input format parameters.
  133. */
  134. Drupal.wysiwygDetach = function(context, params) {
  135. var editor = Drupal.wysiwyg.instances[params.field].editor;
  136. if (jQuery.isFunction(Drupal.wysiwyg.editor.detach[editor])) {
  137. Drupal.wysiwyg.editor.detach[editor](context, params);
  138. }
  139. };
  140. /**
  141. * Append or update an editor toggle link to a target element.
  142. *
  143. * @param context
  144. * A DOM element, supplied by Drupal.attachBehaviors().
  145. * @param params
  146. * An object containing input format parameters.
  147. */
  148. Drupal.wysiwygAttachToggleLink = function(context, params) {
  149. if (!$('#wysiwyg-toggle-' + params.field).length) {
  150. var text = document.createTextNode(params.status ? Drupal.settings.wysiwyg.disable : Drupal.settings.wysiwyg.enable);
  151. var a = document.createElement('a');
  152. $(a).attr({ id: 'wysiwyg-toggle-' + params.field, href: 'javascript:void(0);' }).append(text);
  153. var div = document.createElement('div');
  154. $(div).addClass('wysiwyg-toggle-wrapper').append(a);
  155. $('#' + params.field).after(div);
  156. }
  157. $('#wysiwyg-toggle-' + params.field)
  158. .html(params.status ? Drupal.settings.wysiwyg.disable : Drupal.settings.wysiwyg.enable).show()
  159. .unbind('click.wysiwyg', Drupal.wysiwyg.toggleWysiwyg)
  160. .bind('click.wysiwyg', { params: params, context: context }, Drupal.wysiwyg.toggleWysiwyg);
  161. // Hide toggle link in case no editor is attached.
  162. if (params.editor == 'none') {
  163. $('#wysiwyg-toggle-' + params.field).hide();
  164. }
  165. };
  166. /**
  167. * Callback for the Enable/Disable rich editor link.
  168. */
  169. Drupal.wysiwyg.toggleWysiwyg = function (event) {
  170. var context = event.data.context;
  171. var params = event.data.params;
  172. if (params.status) {
  173. // Detach current editor.
  174. params.status = false;
  175. Drupal.wysiwygDetach(context, params);
  176. // After disabling the editor, re-attach default behaviors.
  177. // @todo We HAVE TO invoke Drupal.wysiwygAttach() here.
  178. Drupal.wysiwyg.editor.attach.none(context, params);
  179. Drupal.wysiwyg.instances[params.field] = Drupal.wysiwyg.editor.instance.none;
  180. Drupal.wysiwyg.instances[params.field].editor = 'none';
  181. $(this).html(Drupal.settings.wysiwyg.enable).blur();
  182. }
  183. else {
  184. // Before enabling the editor, detach default behaviors.
  185. Drupal.wysiwyg.editor.detach.none(context, params);
  186. // Attach new editor using parameters of the currently selected input format.
  187. params = Drupal.settings.wysiwyg.triggers[params.trigger]['format' + $('#' + params.trigger).val()];
  188. params.status = true;
  189. Drupal.wysiwygAttach(context, params);
  190. $(this).html(Drupal.settings.wysiwyg.disable).blur();
  191. }
  192. }
  193. /**
  194. * Parse the CSS classes of an input format DOM element into parameters.
  195. *
  196. * Syntax for CSS classes is "wysiwyg-name-value".
  197. *
  198. * @param element
  199. * An input format DOM element containing CSS classes to parse.
  200. * @param params
  201. * (optional) An object containing input format parameters to update.
  202. */
  203. Drupal.wysiwyg.getParams = function(element, params) {
  204. var classes = element.className.split(' ');
  205. var params = params || {};
  206. for (var i = 0; i < classes.length; i++) {
  207. if (classes[i].substr(0, 8) == 'wysiwyg-') {
  208. var parts = classes[i].split('-');
  209. var value = parts.slice(2).join('-');
  210. params[parts[1]] = value;
  211. }
  212. }
  213. // Convert format id into string.
  214. params.format = 'format' + params.format;
  215. // Convert numeric values.
  216. params.status = parseInt(params.status, 10);
  217. params.toggle = parseInt(params.toggle, 10);
  218. params.resizable = parseInt(params.resizable, 10);
  219. return params;
  220. };
  221. /**
  222. * Allow certain editor libraries to initialize before the DOM is loaded.
  223. */
  224. Drupal.wysiwygInit();
  225. })(jQuery);