whizzywig-60.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. var buttonPath = null;
  2. (function($) {
  3. /**
  4. * Attach this editor to a target element.
  5. */
  6. Drupal.wysiwyg.editor.attach.whizzywig = function(context, params, settings) {
  7. // Previous versions used per-button images found in this location,
  8. // now it is only used for custom buttons.
  9. if (settings.buttonPath) {
  10. window.buttonPath = settings.buttonPath;
  11. }
  12. // Assign the toolbar image path used for native buttons, if available.
  13. if (settings.toolbarImagePath) {
  14. btn._f = settings.toolbarImagePath;
  15. }
  16. // Fall back to text labels for all buttons.
  17. else {
  18. window.buttonPath = 'textbuttons';
  19. }
  20. // Whizzywig needs to have the width set 'inline'.
  21. $field = $('#' + params.field);
  22. var originalValues = Drupal.wysiwyg.instances[params.field];
  23. originalValues.originalStyle = $field.attr('style');
  24. $field.css('width', $field.width() + 'px');
  25. // Attach editor.
  26. makeWhizzyWig(params.field, (settings.buttons ? settings.buttons : 'all'));
  27. // Whizzywig fails to detect and set initial textarea contents.
  28. $('#whizzy' + params.field).contents().find('body').html(tidyD($field.val()));
  29. };
  30. /**
  31. * Detach a single or all editors.
  32. */
  33. Drupal.wysiwyg.editor.detach.whizzywig = function (context, params, trigger) {
  34. var detach = function (index) {
  35. var id = whizzies[index], $field = $('#' + id), instance = Drupal.wysiwyg.instances[id];
  36. // Save contents of editor back into textarea.
  37. $field.val(instance.getContent());
  38. // If the editor is just being serialized (not detached), our work is done.
  39. if (trigger == 'serialize') {
  40. return;
  41. }
  42. // Move original textarea back to its previous location.
  43. var $container = $('#CONTAINER' + id);
  44. $field.insertBefore($container);
  45. // Remove editor instance.
  46. $container.remove();
  47. whizzies.splice(index, 1);
  48. // Restore original textarea styling.
  49. $field.removeAttr('style').attr('style', instance.originalStyle);
  50. }
  51. if (typeof params != 'undefined') {
  52. for (var i = 0; i < whizzies.length; i++) {
  53. if (whizzies[i] == params.field) {
  54. detach(i);
  55. break;
  56. }
  57. }
  58. }
  59. else {
  60. while (whizzies.length > 0) {
  61. detach(0);
  62. }
  63. }
  64. };
  65. /**
  66. * Instance methods for Whizzywig.
  67. */
  68. Drupal.wysiwyg.editor.instance.whizzywig = {
  69. insert: function (content) {
  70. // Whizzywig executes any string beginning with 'js:'.
  71. insHTML(content.replace(/^js:/, 'js&colon;'));
  72. },
  73. setContent: function (content) {
  74. // Whizzywig shows the original textarea in source mode.
  75. if ($field.css('display') == 'block') {
  76. $('#' + this.field).val(content);
  77. }
  78. else {
  79. var doc = $('#whizzy' + this.field).contents()[0];
  80. doc.open();
  81. doc.write(content);
  82. doc.close();
  83. }
  84. },
  85. getContent: function () {
  86. // Whizzywig's tidyH() expects a document node. Clone the editing iframe's
  87. // document so tidyH() won't mess with it if this gets called while editing.
  88. var clone = $($('#whizzy' + this.field).contents()[0].documentElement).clone()[0].ownerDocument;
  89. // Whizzywig shows the original textarea in source mode so update the body.
  90. if ($field.css('display') == 'block') {
  91. clone.body.innerHTML = $('#' + this.field).val();
  92. }
  93. return tidyH(clone);
  94. }
  95. };
  96. })(jQuery);