yui.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. (function($) {
  2. /**
  3. * Attach this editor to a target element.
  4. *
  5. * Since buttons must be added before the editor is rendered, we add plugins
  6. * buttons on attach event rather than in init.
  7. */
  8. Drupal.wysiwyg.editor.attach.yui = function(context, params, settings) {
  9. // Apply theme.
  10. $('#' + params.field).parent().addClass('yui-skin-' + settings.theme);
  11. // Load plugins stylesheet.
  12. for (var plugin in Drupal.settings.wysiwyg.plugins[params.format].drupal) {
  13. settings.extracss += settings.extracss+' @import "'+Drupal.settings.wysiwyg.plugins[params.format].drupal[plugin].css+'"; ';
  14. }
  15. // Attach editor.
  16. var editor = new YAHOO.widget.Editor(params.field, settings);
  17. editor.on('toolbarLoaded', function() {
  18. // Load Drupal plugins.
  19. for (var plugin in Drupal.settings.wysiwyg.plugins[params.format].drupal) {
  20. Drupal.wysiwyg.instances[params.field].addPlugin(plugin, Drupal.settings.wysiwyg.plugins[params.format].drupal[plugin], Drupal.settings.wysiwyg.plugins.drupal[plugin]);
  21. }
  22. });
  23. // Allow plugins to act on setEditorHTML.
  24. var oldSetEditorHTML = editor.setEditorHTML;
  25. editor.setEditorHTML = function (content) {
  26. for (var plugin in Drupal.settings.wysiwyg.plugins[params.format].drupal) {
  27. var pluginSettings = Drupal.settings.wysiwyg.plugins.drupal[plugin];
  28. if (typeof Drupal.wysiwyg.plugins[plugin].attach == 'function') {
  29. content = Drupal.wysiwyg.plugins[plugin].attach(content, pluginSettings, params.field);
  30. content = Drupal.wysiwyg.instances[params.field].prepareContent(content);
  31. }
  32. }
  33. oldSetEditorHTML.call(this, content);
  34. };
  35. // Allow plugins to act on getEditorHTML.
  36. var oldGetEditorHTML = editor.getEditorHTML;
  37. editor.getEditorHTML = function () {
  38. var content = oldGetEditorHTML.call(this);
  39. for (var plugin in Drupal.settings.wysiwyg.plugins[params.format].drupal) {
  40. var pluginSettings = Drupal.settings.wysiwyg.plugins.drupal[plugin];
  41. if (typeof Drupal.wysiwyg.plugins[plugin].detach == 'function') {
  42. content = Drupal.wysiwyg.plugins[plugin].detach(content, pluginSettings, params.field);
  43. }
  44. }
  45. return content;
  46. }
  47. // Reload the editor contents to give Drupal plugins a chance to act.
  48. editor.on('editorContentLoaded', function (e) {
  49. e.target.setEditorHTML(oldGetEditorHTML.call(e.target));
  50. });
  51. editor.on('afterNodeChange', function (e) {
  52. for (var plugin in Drupal.settings.wysiwyg.plugins[params.format].drupal) {
  53. if (typeof Drupal.wysiwyg.plugins[plugin].isNode == 'function') {
  54. if (Drupal.wysiwyg.plugins[plugin].isNode(e.target._getSelectedElement())) {
  55. this.toolbar.selectButton(plugin);
  56. }
  57. }
  58. }
  59. });
  60. editor.render();
  61. };
  62. /**
  63. * Detach a single or all editors.
  64. *
  65. * See Drupal.wysiwyg.editor.detach.none() for a full desciption of this hook.
  66. */
  67. Drupal.wysiwyg.editor.detach.yui = function (context, params, trigger) {
  68. var method = (trigger && trigger == 'serialize') ? 'saveHTML' : 'destroy';
  69. if (typeof params != 'undefined') {
  70. var instance = YAHOO.widget.EditorInfo._instances[params.field];
  71. if (instance) {
  72. instance[method]();
  73. if (method == 'destroy') {
  74. delete YAHOO.widget.EditorInfo._instances[params.field];
  75. }
  76. }
  77. }
  78. else {
  79. for (var e in YAHOO.widget.EditorInfo._instances) {
  80. // Save contents of all editors back into textareas.
  81. var instance = YAHOO.widget.EditorInfo._instances[e];
  82. instance[method]();
  83. if (method == 'destroy') {
  84. delete YAHOO.widget.EditorInfo._instances[e];
  85. }
  86. }
  87. }
  88. };
  89. /**
  90. * Instance methods for YUI Editor.
  91. */
  92. Drupal.wysiwyg.editor.instance.yui = {
  93. addPlugin: function (plugin, settings, pluginSettings) {
  94. if (typeof Drupal.wysiwyg.plugins[plugin] != 'object') {
  95. return;
  96. }
  97. var editor = YAHOO.widget.EditorInfo.getEditorById(this.field);
  98. var button = editor.toolbar.getButtonByValue(plugin);
  99. $(button._button).parent().css('background', 'transparent url(' + settings.icon + ') no-repeat center');
  100. // 'this' will reference the toolbar while inside the event handler.
  101. var instanceId = this.field;
  102. editor.toolbar.on(plugin + 'Click', function (e) {
  103. var selectedElement = editor._getSelectedElement();
  104. // @todo Using .html() will cause XTHML vs HTML conflicts.
  105. var data = {
  106. format: 'html',
  107. node: selectedElement,
  108. content: $(selectedElement).html()
  109. };
  110. Drupal.wysiwyg.plugins[plugin].invoke(data, pluginSettings, instanceId);
  111. });
  112. },
  113. prepareContent: function (content) {
  114. var editor = YAHOO.widget.EditorInfo.getEditorById(this.field);
  115. content = editor.cleanHTML(content);
  116. return content;
  117. },
  118. insert: function (content) {
  119. YAHOO.widget.EditorInfo.getEditorById(this.field).cmd_inserthtml(content);
  120. },
  121. setContent: function (content) {
  122. YAHOO.widget.EditorInfo.getEditorById(this.field).setEditorHTML(content);
  123. },
  124. getContent: function () {
  125. var instance = YAHOO.widget.EditorInfo.getEditorById(this.field);
  126. return instance.cleanHTML(instance.getEditorHTML(content));
  127. }
  128. };
  129. })(jQuery);