summaryrefslogtreecommitdiff
path: root/sites/all/modules/wysiwyg/editors/js/openwysiwyg.js
blob: a01e8a0b1ea5cb0a82dd0949bc49ee46302bf305 (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

// Backup $ and reset it to jQuery.
Drupal.wysiwyg._openwysiwyg = $;
$ = jQuery;

// Wrap openWYSIWYG's methods to temporarily use its version of $.
jQuery.each(WYSIWYG, function (key, value) {
  if (jQuery.isFunction(value)) {
    WYSIWYG[key] = function () {
      var old$ = $;
      $ = Drupal.wysiwyg._openwysiwyg;
      var result = value.apply(this, arguments);
      $ = old$;
      return result;
    };
  }
});

// Override editor functions.
WYSIWYG.getEditor = function (n) {
  return Drupal.wysiwyg._openwysiwyg("wysiwyg" + n);
};

(function($) {

// Fix Drupal toolbar obscuring editor toolbar in fullscreen mode.
var oldMaximize = WYSIWYG.maximize;
WYSIWYG.maximize = function (n) {
var $drupalToolbar = $('#toolbar', Drupal.overlayChild ? window.parent.document : document);
  oldMaximize.apply(this, arguments);
  if (this.maximized[n]) {
    $drupalToolbar.hide();
  }
  else {
    $drupalToolbar.show();
  }
}

/**
 * Attach this editor to a target element.
 */
Drupal.wysiwyg.editor.attach.openwysiwyg = function(context, params, settings) {
  // Initialize settings.
  settings.ImagesDir = settings.path + 'images/';
  settings.PopupsDir = settings.path + 'popups/';
  settings.CSSFile = settings.path + 'styles/wysiwyg.css';
  //settings.DropDowns = [];
  var config = new WYSIWYG.Settings();
  for (var setting in settings) {
    config[setting] = settings[setting];
  }
  // Attach editor.
  WYSIWYG.setSettings(params.field, config);
  WYSIWYG_Core.includeCSS(WYSIWYG.config[params.field].CSSFile);
  WYSIWYG._generate(params.field, config);
};

/**
 * Detach a single or all editors.
 */
Drupal.wysiwyg.editor.detach.openwysiwyg = function (context, params, trigger) {
  if (typeof params != 'undefined') {
    var instance = WYSIWYG.config[params.field];
    if (typeof instance != 'undefined') {
      WYSIWYG.updateTextArea(params.field);
      if (trigger != 'serialize') {
        jQuery('#wysiwyg_div_' + params.field).remove();
        delete instance;
      }
    }
    if (trigger != 'serialize') {
      jQuery('#' + params.field).show();
    }
  }
  else {
    jQuery.each(WYSIWYG.config, function(field) {
      WYSIWYG.updateTextArea(field);
      if (trigger != 'serialize') {
        jQuery('#wysiwyg_div_' + field).remove();
        delete this;
        jQuery('#' + field).show();
      }
    });
  }
};

/**
 * Instance methods for openWYSIWYG.
 */
Drupal.wysiwyg.editor.instance.openwysiwyg = {
  insert: function (content) {
    // If IE has dropped focus content will be inserted at the top of the page.
    $('#wysiwyg' + this.field).contents().find('body').focus();
    WYSIWYG.insertHTML(content, this.field);
  },

  setContent: function (content) {
    // Based on openWYSIWYG's _generate() method.
    var doc = WYSIWYG.getEditorWindow(this.field).document;
    if (WYSIWYG.config[this.field].ReplaceLineBreaks) {
      content = content.replace(/\n\r|\n/ig, '<br />');
    }
    if (WYSIWYG.viewTextMode[this.field]) {
      var html = document.createTextNode(content);
      doc.body.innerHTML = '';
      doc.body.appendChild(html);
    }
    else {
      doc.open();
      doc.write(content);
      doc.close();
    }
  },

  getContent: function () {
    // Based on openWYSIWYG's updateTextarea() method.
    var content = '';
    var doc = WYSIWYG.getEditorWindow(this.field).document;
    if (WYSIWYG.viewTextMode[this.field]) {
      if (WYSIWYG_Core.isMSIE) {
        content = doc.body.innerText;
      }
      else {
        var range = doc.body.ownerDocument.createRange();
        range.selectNodeContents(doc.body);
        content = range.toString();
      }
    }
    else {
      content = doc.body.innerHTML;
    }
    content = WYSIWYG.stripURLPath(this.field, content);
    content = WYSIWYG_Core.replaceRGBWithHexColor(content);
    if (WYSIWYG.config[this.field].ReplaceLineBreaks) {
      content = content.replace(/(\r\n)|(\n)/ig, '');
    }
    return content;
  }
};

})(jQuery);