openwysiwyg.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Backup $ and reset it to jQuery.
  2. Drupal.wysiwyg._openwysiwyg = $;
  3. $ = jQuery;
  4. // Wrap openWYSIWYG's methods to temporarily use its version of $.
  5. jQuery.each(WYSIWYG, function (key, value) {
  6. if (jQuery.isFunction(value)) {
  7. WYSIWYG[key] = function () {
  8. var old$ = $;
  9. $ = Drupal.wysiwyg._openwysiwyg;
  10. var result = value.apply(this, arguments);
  11. $ = old$;
  12. return result;
  13. };
  14. }
  15. });
  16. // Override editor functions.
  17. WYSIWYG.getEditor = function (n) {
  18. return Drupal.wysiwyg._openwysiwyg("wysiwyg" + n);
  19. };
  20. (function($) {
  21. // Fix Drupal toolbar obscuring editor toolbar in fullscreen mode.
  22. var oldMaximize = WYSIWYG.maximize;
  23. WYSIWYG.maximize = function (n) {
  24. var $drupalToolbar = $('#toolbar', Drupal.overlayChild ? window.parent.document : document);
  25. oldMaximize.apply(this, arguments);
  26. if (this.maximized[n]) {
  27. $drupalToolbar.hide();
  28. }
  29. else {
  30. $drupalToolbar.show();
  31. }
  32. }
  33. /**
  34. * Attach this editor to a target element.
  35. */
  36. Drupal.wysiwyg.editor.attach.openwysiwyg = function(context, params, settings) {
  37. // Initialize settings.
  38. settings.ImagesDir = settings.path + 'images/';
  39. settings.PopupsDir = settings.path + 'popups/';
  40. settings.CSSFile = settings.path + 'styles/wysiwyg.css';
  41. //settings.DropDowns = [];
  42. var config = new WYSIWYG.Settings();
  43. for (var setting in settings) {
  44. config[setting] = settings[setting];
  45. }
  46. // Attach editor.
  47. WYSIWYG.setSettings(params.field, config);
  48. WYSIWYG_Core.includeCSS(WYSIWYG.config[params.field].CSSFile);
  49. WYSIWYG._generate(params.field, config);
  50. };
  51. /**
  52. * Detach a single or all editors.
  53. */
  54. Drupal.wysiwyg.editor.detach.openwysiwyg = function (context, params, trigger) {
  55. if (typeof params != 'undefined') {
  56. var instance = WYSIWYG.config[params.field];
  57. if (typeof instance != 'undefined') {
  58. WYSIWYG.updateTextArea(params.field);
  59. if (trigger != 'serialize') {
  60. jQuery('#wysiwyg_div_' + params.field).remove();
  61. delete instance;
  62. }
  63. }
  64. if (trigger != 'serialize') {
  65. jQuery('#' + params.field).show();
  66. }
  67. }
  68. else {
  69. jQuery.each(WYSIWYG.config, function(field) {
  70. WYSIWYG.updateTextArea(field);
  71. if (trigger != 'serialize') {
  72. jQuery('#wysiwyg_div_' + field).remove();
  73. delete this;
  74. jQuery('#' + field).show();
  75. }
  76. });
  77. }
  78. };
  79. /**
  80. * Instance methods for openWYSIWYG.
  81. */
  82. Drupal.wysiwyg.editor.instance.openwysiwyg = {
  83. insert: function (content) {
  84. // If IE has dropped focus content will be inserted at the top of the page.
  85. $('#wysiwyg' + this.field).contents().find('body').focus();
  86. WYSIWYG.insertHTML(content, this.field);
  87. },
  88. setContent: function (content) {
  89. // Based on openWYSIWYG's _generate() method.
  90. var doc = WYSIWYG.getEditorWindow(this.field).document;
  91. if (WYSIWYG.config[this.field].ReplaceLineBreaks) {
  92. content = content.replace(/\n\r|\n/ig, '<br />');
  93. }
  94. if (WYSIWYG.viewTextMode[this.field]) {
  95. var html = document.createTextNode(content);
  96. doc.body.innerHTML = '';
  97. doc.body.appendChild(html);
  98. }
  99. else {
  100. doc.open();
  101. doc.write(content);
  102. doc.close();
  103. }
  104. },
  105. getContent: function () {
  106. // Based on openWYSIWYG's updateTextarea() method.
  107. var content = '';
  108. var doc = WYSIWYG.getEditorWindow(this.field).document;
  109. if (WYSIWYG.viewTextMode[this.field]) {
  110. if (WYSIWYG_Core.isMSIE) {
  111. content = doc.body.innerText;
  112. }
  113. else {
  114. var range = doc.body.ownerDocument.createRange();
  115. range.selectNodeContents(doc.body);
  116. content = range.toString();
  117. }
  118. }
  119. else {
  120. content = doc.body.innerHTML;
  121. }
  122. content = WYSIWYG.stripURLPath(this.field, content);
  123. content = WYSIWYG_Core.replaceRGBWithHexColor(content);
  124. if (WYSIWYG.config[this.field].ReplaceLineBreaks) {
  125. content = content.replace(/(\r\n)|(\n)/ig, '');
  126. }
  127. return content;
  128. }
  129. };
  130. })(jQuery);