summaryrefslogtreecommitdiff
path: root/sites/all/modules/wysiwyg/editors/js/yui.js
blob: 3f4e7c63a31d9ba95040f9927e09f88a2a8b2cce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
(function($) {

/**
 * Attach this editor to a target element.
 *
 * Since buttons must be added before the editor is rendered, we add plugins
 * buttons on attach event rather than in init.
 */
Drupal.wysiwyg.editor.attach.yui = function(context, params, settings) {
  // Apply theme.
  $('#' + params.field).parent().addClass('yui-skin-' + settings.theme);

  // Load plugins stylesheet.
  for (var plugin in Drupal.settings.wysiwyg.plugins[params.format].drupal) {
    settings.extracss += settings.extracss+' @import "'+Drupal.settings.wysiwyg.plugins[params.format].drupal[plugin].css+'"; ';
  }

  // Attach editor.
  var editor = new YAHOO.widget.Editor(params.field, settings);

  editor.on('toolbarLoaded', function() {
    // Load Drupal plugins.
    for (var plugin in Drupal.settings.wysiwyg.plugins[params.format].drupal) {
      Drupal.wysiwyg.instances[params.field].addPlugin(plugin, Drupal.settings.wysiwyg.plugins[params.format].drupal[plugin], Drupal.settings.wysiwyg.plugins.drupal[plugin]);
    }
  });

  // Allow plugins to act on setEditorHTML.
  var oldSetEditorHTML = editor.setEditorHTML;
  editor.setEditorHTML = function (content) {
    for (var plugin in Drupal.settings.wysiwyg.plugins[params.format].drupal) {
      var pluginSettings = Drupal.settings.wysiwyg.plugins.drupal[plugin];
      if (typeof Drupal.wysiwyg.plugins[plugin].attach == 'function') {
        content = Drupal.wysiwyg.plugins[plugin].attach(content, pluginSettings, params.field);
        content = Drupal.wysiwyg.instances[params.field].prepareContent(content);
      }
    }
    oldSetEditorHTML.call(this, content);
  };

  // Allow plugins to act on getEditorHTML.
  var oldGetEditorHTML = editor.getEditorHTML;
  editor.getEditorHTML = function () {
    var content = oldGetEditorHTML.call(this);
    for (var plugin in Drupal.settings.wysiwyg.plugins[params.format].drupal) {
      var pluginSettings = Drupal.settings.wysiwyg.plugins.drupal[plugin];
      if (typeof Drupal.wysiwyg.plugins[plugin].detach == 'function') {
        content = Drupal.wysiwyg.plugins[plugin].detach(content, pluginSettings, params.field);
      }
    }
    return content;
  }

  // Reload the editor contents to give Drupal plugins a chance to act.
  editor.on('editorContentLoaded', function (e) {
    e.target.setEditorHTML(oldGetEditorHTML.call(e.target));
  });

  editor.on('afterNodeChange', function (e) {
    for (var plugin in Drupal.settings.wysiwyg.plugins[params.format].drupal) {
      if (typeof Drupal.wysiwyg.plugins[plugin].isNode == 'function') {
        if (Drupal.wysiwyg.plugins[plugin].isNode(e.target._getSelectedElement())) {
          this.toolbar.selectButton(plugin);
        }
      }
    }
  });

  editor.render();
};

/**
 * Detach a single or all editors.
 *
 * See Drupal.wysiwyg.editor.detach.none() for a full desciption of this hook.
 */
Drupal.wysiwyg.editor.detach.yui = function (context, params, trigger) {
  var method = (trigger && trigger == 'serialize') ? 'saveHTML' : 'destroy';
  if (typeof params != 'undefined') {
    var instance = YAHOO.widget.EditorInfo._instances[params.field];
    if (instance) {
      instance[method]();
      if (method == 'destroy') {
        delete YAHOO.widget.EditorInfo._instances[params.field];
      }
    }
  }
  else {
    for (var e in YAHOO.widget.EditorInfo._instances) {
      // Save contents of all editors back into textareas.
      var instance = YAHOO.widget.EditorInfo._instances[e];
      instance[method]();
      if (method == 'destroy') {
        delete YAHOO.widget.EditorInfo._instances[e];
      }
    }
  }
};

/**
 * Instance methods for YUI Editor.
 */
Drupal.wysiwyg.editor.instance.yui = {
  addPlugin: function (plugin, settings, pluginSettings) {
    if (typeof Drupal.wysiwyg.plugins[plugin] != 'object') {
      return;
    }
    var editor = YAHOO.widget.EditorInfo.getEditorById(this.field);
    var button = editor.toolbar.getButtonByValue(plugin);
    $(button._button).parent().css('background', 'transparent url(' + settings.icon + ') no-repeat center');
    // 'this' will reference the toolbar while inside the event handler.
    var instanceId = this.field;
    editor.toolbar.on(plugin + 'Click', function (e) {
      var selectedElement = editor._getSelectedElement();
      // @todo Using .html() will cause XTHML vs HTML conflicts.
      var data = {
        format: 'html',
        node: selectedElement,
        content: $(selectedElement).html()
      };
      Drupal.wysiwyg.plugins[plugin].invoke(data, pluginSettings, instanceId);
    });
  },

  prepareContent: function (content) {
    var editor = YAHOO.widget.EditorInfo.getEditorById(this.field);
    content = editor.cleanHTML(content);
    return content;
  },

  insert: function (content) {
    YAHOO.widget.EditorInfo.getEditorById(this.field).cmd_inserthtml(content);
  },

  setContent: function (content) {
    YAHOO.widget.EditorInfo.getEditorById(this.field).setEditorHTML(content);
  },

  getContent: function () {
    var instance = YAHOO.widget.EditorInfo.getEditorById(this.field);
    return instance.cleanHTML(instance.getEditorHTML(content));
  }
};

})(jQuery);