summaryrefslogtreecommitdiff
path: root/sites/all/modules/wysiwyg/editors/js/nicedit.js
blob: 10b7809c5d5997cecadf111530ee1453ae6616c5 (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
(function($) {

/**
 * Attach this editor to a target element.
 */
Drupal.wysiwyg.editor.attach.nicedit = function(context, params, settings) {
  // Intercept and ignore submit handlers or they will revert changes made
  // since the instance was removed. The handlers are anonymous and hidden out
  // of scope in a closure so we can't unbind them. The same operations are
  // performed when the instance is detached anyway.
  var oldAddEvent = bkLib.addEvent;
  bkLib.addEvent = function(obj, type, fn) {
    if (type != 'submit') {
      oldAddEvent(obj, type, fn);
    }
  }
  // Attach editor.
  var editor = new nicEditor(settings);
  editor.panelInstance(params.field);
  // The old addEvent() must be restored after creating a new instance, as
  // plugins with dialogs use it to bind submit handlers to their forms.
  bkLib.addEvent = oldAddEvent;
  editor.addEvent('focus', function () {
    Drupal.wysiwyg.activeId = params.field;
  });
};

/**
 * Detach a single or all editors.
 *
 * See Drupal.wysiwyg.editor.detach.none() for a full description of this hook.
 */
Drupal.wysiwyg.editor.detach.nicedit = function (context, params, trigger) {
  if (typeof params != 'undefined') {
    var instance = nicEditors.findEditor(params.field);
    if (instance) {
      if (trigger == 'serialize') {
        instance.saveContent();
      }
      else {
        instance.ne.removeInstance(params.field);
        instance.ne.removePanel();
      }
    }
  }
  else {
    for (var e in nicEditors.editors) {
      // Save contents of all editors back into textareas.
      var instances = nicEditors.editors[e].nicInstances;
      for (var i = 0; i < instances.length; i++) {
        if (trigger == 'serialize') {
          instances[i].saveContent();
        }
        else {
          instances[i].remove();
        }
      }
      // Remove all editor instances.
      if (trigger != 'serialize') {
        nicEditors.editors[e].nicInstances = [];
      }
    }
  }
};

/**
 * Instance methods for nicEdit.
 */
Drupal.wysiwyg.editor.instance.nicedit = {
  insert: function (content) {
    var instance = nicEditors.findEditor(this.field);
    var editingArea = instance.getElm();
    var sel = instance.getSel();
    // IE.
    if (document.selection) {
      editingArea.focus();
      sel.createRange().pasteHTML(content);
    }
    else {
      // Convert selection to a range.
      var range;
      // W3C compatible.
      if (sel.getRangeAt) {
        range = sel.getRangeAt(0);
      }
      // Safari.
      else {
        range = editingArea.ownerDocument.createRange();
        range.setStart(sel.anchorNode, sel.anchorOffset);
        range.setEnd(sel.focusNode, userSeletion.focusOffset);
      }
      // The code below doesn't work in IE, but it never gets here.
      var fragment = editingArea.ownerDocument.createDocumentFragment();
      // Fragments don't support innerHTML.
      var wrapper = editingArea.ownerDocument.createElement('div');
      wrapper.innerHTML = content;
      while (wrapper.firstChild) {
        fragment.appendChild(wrapper.firstChild);
      }
      range.deleteContents();
      // Only fragment children are inserted.
      range.insertNode(fragment);
    }
  },

  setContent: function (content) {
    nicEditors.findEditor(this.field).setContent(content);
  },

  getContent: function () {
    return nicEditors.findEditor(this.field).getContent();
  }
};

})(jQuery);