whizzywig-56.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. // Previous versions used per-button images found in this location,
  54. // now it is only used for custom buttons.
  55. if (settings.buttonPath) {
  56. window.buttonPath = settings.buttonPath;
  57. }
  58. // Assign the toolbar image path used for native buttons, if available.
  59. if (settings.toolbarImagePath) {
  60. btn._f = settings.toolbarImagePath;
  61. }
  62. // Fall back to text labels for all buttons.
  63. else {
  64. window.buttonPath = 'textbuttons';
  65. }
  66. // Create Whizzywig container.
  67. wysiwygWhizzywig.currentField = params.field;
  68. wysiwygWhizzywig.fields[wysiwygWhizzywig.currentField] = '';
  69. // Whizzywig needs to have the width set 'inline'.
  70. var $field = $('#' + params.field);
  71. this.originalStyle = $field.attr('style');
  72. $field.css('width', $field.width() + 'px');
  73. // Attach editor.
  74. makeWhizzyWig(params.field, (settings.buttons ? settings.buttons : 'all'));
  75. // Whizzywig fails to detect and set initial textarea contents.
  76. $('#whizzy' + params.field).contents().find('body').html(tidyD($field.val()));
  77. };
  78. /**
  79. * Detach a single or all editors.
  80. */
  81. Drupal.wysiwyg.editor.detach.whizzywig = function (context, params, trigger) {
  82. for (var index = 0; index < whizzies.length; index++) {
  83. if (whizzies[index] !== this.field) {
  84. continue;
  85. }
  86. var $field = $('#' + this.field);
  87. // Save contents of editor back into textarea.
  88. $field.val(this.getContent());
  89. // If the editor is just being serialized (not detached), our work is done.
  90. if (trigger == 'serialize') {
  91. return;
  92. }
  93. // Remove editor instance.
  94. $('#' + this.field + '-whizzywig').remove();
  95. whizzies.splice(index, 1);
  96. // Restore original textarea styling.
  97. if ('originalStyle' in this) {
  98. $field.removeAttr('style').attr('style', this.originalStyle);
  99. }
  100. break;
  101. }
  102. };
  103. /**
  104. * Instance methods for Whizzywig.
  105. */
  106. Drupal.wysiwyg.editor.instance.whizzywig = {
  107. insert: function (content) {
  108. // Whizzywig executes any string beginning with 'js:'.
  109. insHTML(content.replace(/^js:/, 'js&colon;'));
  110. },
  111. setContent: function (content) {
  112. // Whizzywig shows the original textarea in source mode.
  113. if ($field.css('display') == 'block') {
  114. $('#' + this.field).val(content);
  115. }
  116. else {
  117. var doc = $('#whizzy' + this.field).contents()[0];
  118. doc.open();
  119. doc.write(content);
  120. doc.close();
  121. }
  122. },
  123. getContent: function () {
  124. var $field = $('#' + this.field);
  125. // Whizzywig's tidyH() expects a document node. Clone the editing iframe's
  126. // document so tidyH() won't mess with it if this gets called while editing.
  127. var clone = $($('#whizzy' + this.field).contents()[0].documentElement).clone()[0].ownerDocument;
  128. // Whizzywig shows the original textarea in source mode so update the body.
  129. if ($field.css('display') == 'block') {
  130. clone.body.innerHTML = $field.val();
  131. }
  132. return tidyH(clone);
  133. }
  134. };
  135. })(jQuery);