whizzywig.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. $field = $('#' + params.field);
  62. var originalValues = Drupal.wysiwyg.instances[params.field];
  63. originalValues.originalStyle = $field.attr('style');
  64. $field.css('width', $field.width() + 'px');
  65. // Attach editor.
  66. makeWhizzyWig(params.field, (settings.buttons ? settings.buttons : 'all'));
  67. // Whizzywig fails to detect and set initial textarea contents.
  68. var instance = $('#whizzy' + params.field).get(0);
  69. if (instance) {
  70. instance.contentWindow.document.body.innerHTML = tidyD($field.val());
  71. }
  72. };
  73. /**
  74. * Detach a single or all editors.
  75. */
  76. Drupal.wysiwyg.editor.detach.whizzywig = function(context, params) {
  77. var detach = function (index) {
  78. var id = whizzies[index];
  79. var instance = $('#whizzy' + id).get(0);
  80. if (!instance) {
  81. return;
  82. }
  83. var body = instance.contentWindow.document.body;
  84. var $field = $('#' + id);
  85. // Whizzywig shows the original textarea in source mode.
  86. if ($field.css('display') == 'block') {
  87. body.innerHTML = $field.val();
  88. }
  89. body.innerHTML = tidyH(body.innerHTML);
  90. // Save contents of editor back into textarea.
  91. $field.val(window.get_xhtml ? get_xhtml(body) : body.innerHTML);
  92. $field.val($field.val().replace(location.href + '#', '#'));
  93. // Remove editor instance.
  94. $('#' + id + '-whizzywig').remove();
  95. whizzies.splice(index, 1);
  96. // Restore original textarea styling.
  97. var originalValues = Drupal.wysiwyg.instances[id];
  98. $field.removeAttr('style');
  99. $field.attr('style', originalValues.originalStyle);
  100. };
  101. if (typeof params != 'undefined') {
  102. for (var i = 0; i < whizzies.length; i++) {
  103. if (whizzies[i] == params.field) {
  104. detach(i);
  105. break;
  106. }
  107. }
  108. }
  109. else {
  110. while (whizzies.length > 0) {
  111. detach(0);
  112. }
  113. }
  114. };
  115. })(jQuery);