whizzywig.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. var wysiwygWhizzywig = { currentField: null, fields: {} };
  2. var buttonPath = null;
  3. /**
  4. * Override Whizzywig's document.write() function.
  5. *
  6. * Whizzywig uses document.write() by default, which leads to a blank page when
  7. * invoked in jQuery.ready(). Luckily, Whizzywig developers implemented a
  8. * shorthand w() substitute function that we can override to redirect the output
  9. * into the global wysiwygWhizzywig variable.
  10. *
  11. * @see o()
  12. */
  13. var w = function (string) {
  14. if (string) {
  15. wysiwygWhizzywig.fields[wysiwygWhizzywig.currentField] += string;
  16. }
  17. return wysiwygWhizzywig.fields[wysiwygWhizzywig.currentField];
  18. };
  19. /**
  20. * Override Whizzywig's document.getElementById() function.
  21. *
  22. * Since we redirect the output of w() into a temporary string upon attaching
  23. * an editor, we also have to override the o() shorthand substitute function
  24. * for document.getElementById() to search in the document or our container.
  25. * This override function also inserts the editor instance when Whizzywig
  26. * tries to access its IFRAME, so it has access to the full/regular window
  27. * object.
  28. *
  29. * @see w()
  30. */
  31. var o = function (id) {
  32. // Upon first access to "whizzy" + id, Whizzywig tries to access its IFRAME,
  33. // so we need to insert the editor into the DOM.
  34. if (id == 'whizzy' + wysiwygWhizzywig.currentField && wysiwygWhizzywig.fields[wysiwygWhizzywig.currentField]) {
  35. jQuery('#' + wysiwygWhizzywig.currentField).after('<div id="' + wysiwygWhizzywig.currentField + '-whizzywig"></div>');
  36. // Iframe's .contentWindow becomes null in Webkit if inserted via .after().
  37. jQuery('#' + wysiwygWhizzywig.currentField + '-whizzywig').html(w());
  38. // Prevent subsequent invocations from inserting the editor multiple times.
  39. wysiwygWhizzywig.fields[wysiwygWhizzywig.currentField] = '';
  40. }
  41. // If id exists in the regular window.document, return it.
  42. if (jQuery('#' + id).size()) {
  43. return jQuery('#' + id).get(0);
  44. }
  45. // Otherwise return id from our container.
  46. return jQuery('#' + id, w()).get(0);
  47. };
  48. (function($) {
  49. /**
  50. * Attach this editor to a target element.
  51. */
  52. Drupal.wysiwyg.editor.attach.whizzywig = function(context, params, settings) {
  53. // Assign button images path, if available.
  54. if (settings.buttonPath) {
  55. window.buttonPath = settings.buttonPath;
  56. }
  57. // Create Whizzywig container.
  58. wysiwygWhizzywig.currentField = params.field;
  59. wysiwygWhizzywig.fields[wysiwygWhizzywig.currentField] = '';
  60. // Whizzywig needs to have the width set 'inline'.
  61. var $field = $('#' + params.field);
  62. this.originalStyle = $field.attr('style');
  63. $field.css('width', $field.width() + 'px');
  64. // Attach editor.
  65. makeWhizzyWig(params.field, (settings.buttons ? settings.buttons : 'all'));
  66. // Whizzywig fails to detect and set initial textarea contents.
  67. $('#whizzy' + params.field).contents().find('body').html(tidyD($field.val()));
  68. };
  69. /**
  70. * Detach a single or all editors.
  71. */
  72. Drupal.wysiwyg.editor.detach.whizzywig = function (context, params, trigger) {
  73. for (var index = 0; index < whizzies.length; index++) {
  74. if (whizzies[index] !== this.field) {
  75. continue;
  76. }
  77. var $field = $('#' + this.field);
  78. // Save contents of editor back into textarea.
  79. $field.val(this.getContent());
  80. // If the editor is just being serialized (not detached), our work is done.
  81. if (trigger == 'serialize') {
  82. return;
  83. }
  84. // Remove editor instance.
  85. $('#' + this.field + '-whizzywig').remove();
  86. whizzies.splice(index, 1);
  87. // Restore original textarea styling.
  88. if ('originalStyle' in this) {
  89. $field.removeAttr('style').attr('style', this.originalStyle);
  90. }
  91. break;
  92. }
  93. };
  94. /**
  95. * Instance methods for Whizzywig.
  96. */
  97. Drupal.wysiwyg.editor.instance.whizzywig = {
  98. insert: function (content) {
  99. // Whizzywig executes any string beginning with 'js:'.
  100. insHTML(content.replace(/^js:/, 'js&colon;'));
  101. },
  102. setContent: function (content) {
  103. var $field = $('#' + this.field);
  104. // Whizzywig shows the original textarea in source mode.
  105. if ($field.css('display') == 'block') {
  106. $field.val(content);
  107. }
  108. else {
  109. var doc = $('#whizzy' + this.field).contents()[0];
  110. doc.open();
  111. doc.write(content);
  112. doc.close();
  113. }
  114. },
  115. getContent: function () {
  116. var $field = $('#' + this.field),
  117. // Whizzywig shows the original textarea in source mode.
  118. content = ($field.css('display') == 'block' ?
  119. $field.val() : $('#whizzy' + this.field).contents().find('body').html()
  120. );
  121. content = tidyH(content);
  122. // Whizzywig's get_xhtml() addon, if defined, expects a DOM node.
  123. if ($.isFunction(window.get_xhtml)) {
  124. var pre = document.createElement('pre');
  125. pre.innerHTML = content;
  126. content = get_xhtml(pre);
  127. }
  128. return content.replace(location.href + '#', '#');
  129. }
  130. };
  131. })(jQuery);