Projet

Général

Profil

Paste
Télécharger (12,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ckeditor / includes / ckeditor.utils.js @ 5a7e6170

1
/*
2
Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3
For licensing, see LICENSE.html or http://ckeditor.com/license
4
*/
5
window.CKEDITOR_BASEPATH = Drupal.settings.ckeditor.editor_path;
6
(function ($) {
7
  Drupal.ckeditor = (typeof(CKEDITOR) != 'undefined');
8
  Drupal.ckeditor_ver = false;
9

    
10
  Drupal.ckeditorToggle = function(textarea_ids, TextTextarea, TextRTE){
11
    if (!CKEDITOR.env.isCompatible) {
12
      return;
13
    }
14

    
15
    for (i=0; i<textarea_ids.length; i++){
16
      if (typeof(CKEDITOR.instances) != 'undefined' && typeof(CKEDITOR.instances[textarea_ids[i]]) != 'undefined'){
17
        Drupal.ckeditorOff(textarea_ids[i]);
18
        $('#switch_' + textarea_ids[0]).text(TextRTE);
19
      }
20
      else {
21
        Drupal.ckeditorOn(textarea_ids[i]);
22
        $('#switch_' + textarea_ids[0]).text(TextTextarea);
23
      }
24
    }
25
  };
26

    
27
  /**
28
 * CKEditor starting function
29
 *
30
 * @param string textarea_id
31
 */
32
  Drupal.ckeditorInit = function(textarea_id) {
33
    var ckeditor_obj = Drupal.settings.ckeditor;
34
    $("#" + textarea_id).next(".grippie").css("display", "none");
35
    $("#" + textarea_id).addClass("ckeditor-processed");
36

    
37
    var textarea_settings = false;
38
    ckeditor_obj.input_formats[ckeditor_obj.elements[textarea_id]].toolbar = eval(ckeditor_obj.input_formats[ckeditor_obj.elements[textarea_id]].toolbar);
39
    textarea_settings = ckeditor_obj.input_formats[ckeditor_obj.elements[textarea_id]];
40

    
41
    var drupalTopToolbar = $('#toolbar, #admin-menu', Drupal.overlayChild ? window.parent.document : document);
42

    
43
    textarea_settings['on'] =
44
    {
45
      configLoaded  : function(ev)
46
      {
47
        Drupal.ckeditor_ver = CKEDITOR.version.split('.')[0];
48
        if (Drupal.ckeditor_ver == 3) {
49
          ev.editor.addCss(ev.editor.config.extraCss);
50
        }
51
        else {
52
          CKEDITOR.addCss(ev.editor.config.extraCss);
53
        }
54
        // Let Drupal trigger formUpdated event [#1895278]
55
        ev.editor.on('change', function(ev) {
56
          $(ev.editor.element.$).trigger('change');
57
        });
58
        ev.editor.on('blur', function(ev) {
59
          $(ev.editor.element.$).trigger('blur');
60
        });
61
        ev.editor.on('focus', function(ev) {
62
          $(ev.editor.element.$).trigger('click');
63
        });
64
      },
65
      instanceReady : function(ev)
66
      {
67
        var body = $(ev.editor.document.$.body);
68
        if (typeof(ev.editor.dataProcessor.writer.setRules) != 'undefined') {
69
          ev.editor.dataProcessor.writer.setRules('p', {
70
            breakAfterOpen: false
71
          });
72

    
73
          if (typeof(ckeditor_obj.input_formats[ckeditor_obj.elements[textarea_id]].custom_formatting) != 'undefined') {
74
            var dtd = CKEDITOR.dtd;
75
            for ( var e in CKEDITOR.tools.extend( {}, dtd.$block, dtd.$listItem, dtd.$tableContent ) ) {
76
              ev.editor.dataProcessor.writer.setRules( e, ckeditor_obj.input_formats[ckeditor_obj.elements[textarea_id]].custom_formatting);
77
            }
78
            ev.editor.dataProcessor.writer.setRules( 'pre',
79
            {
80
              indent: ckeditor_obj.input_formats[ckeditor_obj.elements[textarea_id]].output_pre_indent
81
            });
82
          }
83
        }
84

    
85
        if (ev.editor.config.bodyClass)
86
          body.addClass(ev.editor.config.bodyClass);
87
        if (ev.editor.config.bodyId)
88
          body.attr('id', ev.editor.config.bodyId);
89
        if (typeof(Drupal.smileysAttach) != 'undefined' && typeof(ev.editor.dataProcessor.writer) != 'undefined')
90
          ev.editor.dataProcessor.writer.indentationChars = '    ';
91

    
92
        // Let Drupal trigger formUpdated event [#1895278]
93
        ((ev.editor.editable && ev.editor.editable()) || ev.editor.document.getBody()).on( 'keyup', function() {
94
          $(ev.editor.element.$).trigger('keyup');
95
        });
96
        ((ev.editor.editable && ev.editor.editable()) || ev.editor.document.getBody()).on( 'keydown', function() {
97
          $(ev.editor.element.$).trigger('keydown');
98
        });
99
      },
100
      focus : function(ev)
101
      {
102
        Drupal.ckeditorInstance = ev.editor;
103
        Drupal.ckeditorActiveId = ev.editor.name;
104
      },
105
      afterCommandExec: function(ev)
106
      {
107
        if (ev.data.name != 'maximize') {
108
          return;
109
        }
110
        if (ev.data.command.state == CKEDITOR.TRISTATE_ON) {
111
          drupalTopToolbar.hide();
112
        } else {
113
          drupalTopToolbar.show();
114
        }
115
      }
116
    };
117

    
118
    if (typeof Drupal.settings.ckeditor.scayt_language != 'undefined'){
119
      textarea_settings['scayt_sLang'] = Drupal.settings.ckeditor.scayt_language;
120
    }
121

    
122
    if (typeof textarea_settings['js_conf'] != 'undefined'){
123
      for (var add_conf in textarea_settings['js_conf']){
124
        textarea_settings[add_conf] = eval(textarea_settings['js_conf'][add_conf]);
125
      }
126
    }
127

    
128
    //remove width 100% from settings because this may cause problems with theme css
129
    if (textarea_settings.width == '100%') textarea_settings.width = '';
130

    
131
    if (CKEDITOR.loadFullCore) {
132
      CKEDITOR.on('loaded', function() {
133
        textarea_settings = Drupal.ckeditorLoadPlugins(textarea_settings);
134
        Drupal.ckeditorInstance = CKEDITOR.replace(textarea_id, textarea_settings);
135
      });
136
      CKEDITOR.loadFullCore();
137
    }
138
    else {
139
      textarea_settings = Drupal.ckeditorLoadPlugins(textarea_settings);
140
      Drupal.ckeditorInstance = CKEDITOR.replace(textarea_id, textarea_settings);
141
    }
142
  }
143

    
144
  Drupal.ckeditorOn = function(textarea_id, run_filter) {
145

    
146
    run_filter = typeof(run_filter) != 'undefined' ? run_filter : true;
147

    
148
    if (typeof(textarea_id) == 'undefined' || textarea_id.length == 0 || $("#" + textarea_id).length == 0) {
149
      return;
150
    }
151
    if ((typeof(Drupal.settings.ckeditor.load_timeout) == 'undefined') && (typeof(CKEDITOR.instances[textarea_id]) != 'undefined')) {
152
      return;
153
    }
154
    if (typeof(Drupal.settings.ckeditor.elements[textarea_id]) == 'undefined') {
155
      return;
156
    }
157
    var ckeditor_obj = Drupal.settings.ckeditor;
158

    
159
    if (!CKEDITOR.env.isCompatible) {
160
      return;
161
    }
162

    
163
    if (run_filter && ($("#" + textarea_id).val().length > 0) && typeof(ckeditor_obj.input_formats[ckeditor_obj.elements[textarea_id]]) != 'undefined' && ((ckeditor_obj.input_formats[ckeditor_obj.elements[textarea_id]]['ss'] == 1 && typeof(Drupal.settings.ckeditor.autostart) != 'undefined' && typeof(Drupal.settings.ckeditor.autostart[textarea_id]) != 'undefined') || ckeditor_obj.input_formats[ckeditor_obj.elements[textarea_id]]['ss'] == 2)) {
164
      $.ajax({
165
        type: 'POST',
166
        url: Drupal.settings.ckeditor.xss_url,
167
        async: false,
168
        data: {
169
          text: $('#' + textarea_id).val(),
170
          input_format: ckeditor_obj.textarea_default_format[textarea_id],
171
          token: Drupal.settings.ckeditor.ajaxToken
172
        },
173
        success: function(text){
174
          $("#" + textarea_id).val(text);
175
          Drupal.ckeditorInit(textarea_id);
176
        }
177
      })
178
    }
179
    else {
180
      Drupal.ckeditorInit(textarea_id);
181
    }
182
  };
183

    
184
  /**
185
 * CKEditor destroy function
186
 *
187
 * @param string textarea_id
188
 */
189
  Drupal.ckeditorOff = function(textarea_id) {
190
    if (!CKEDITOR.instances || typeof(CKEDITOR.instances[textarea_id]) == 'undefined') {
191
      return;
192
    }
193
    if (!CKEDITOR.env.isCompatible) {
194
      return;
195
    }
196
    if (Drupal.ckeditorInstance && Drupal.ckeditorInstance.name == textarea_id)
197
      delete Drupal.ckeditorInstance;
198

    
199
    $("#" + textarea_id).val(CKEDITOR.instances[textarea_id].getData());
200
    CKEDITOR.instances[textarea_id].destroy(true);
201

    
202
    $("#" + textarea_id).next(".grippie").css("display", "block");
203
  };
204

    
205
  /**
206
* Loading selected CKEditor plugins
207
*
208
* @param object textarea_settings
209
*/
210
  Drupal.ckeditorLoadPlugins = function(textarea_settings) {
211
    if (typeof(textarea_settings.extraPlugins) == 'undefined') {
212
      textarea_settings.extraPlugins = '';
213
    }
214
    if (typeof CKEDITOR.plugins != 'undefined') {
215
      for (var plugin in textarea_settings['loadPlugins']) {
216
        if (typeof(textarea_settings['loadPlugins'][plugin]['active']) == 'undefined' || textarea_settings['loadPlugins'][plugin]['active'] == 1) {
217
          textarea_settings.extraPlugins += (textarea_settings.extraPlugins) ? ',' + textarea_settings['loadPlugins'][plugin]['name'] : textarea_settings['loadPlugins'][plugin]['name'];
218
          CKEDITOR.plugins.addExternal(textarea_settings['loadPlugins'][plugin]['name'], textarea_settings['loadPlugins'][plugin]['path']);
219
        }
220
      }
221
    }
222
    return textarea_settings;
223
  }
224

    
225
  /**
226
 * Returns true if CKEDITOR.version >= version
227
 */
228
  Drupal.ckeditorCompareVersion = function (version){
229
    var ckver = CKEDITOR.version;
230
    ckver = ckver.match(/(([\d]\.)+[\d]+)/i);
231
    version = version.match(/((\d+\.)+[\d]+)/i);
232
    ckver = ckver[0].split('.');
233
    version = version[0].split('.');
234
    for (var x in ckver) {
235
      if (ckver[x]<version[x]) {
236
        return false;
237
      }
238
      else if (ckver[x]>version[x]) {
239
        return true;
240
      }
241
    }
242
    return true;
243
  };
244

    
245
  Drupal.ckeditorInsertHtml = function(html) {
246
    if (!Drupal.ckeditorInstance)
247
      return false;
248

    
249
    if (Drupal.ckeditorInstance.mode == 'wysiwyg') {
250
      Drupal.ckeditorInstance.insertHtml(html);
251
      return true;
252
    }
253
    else {
254
      alert(Drupal.t('Content can only be inserted into CKEditor in the WYSIWYG mode.'));
255
      return false;
256
    }
257
  };
258

    
259
  /**
260
 * Ajax support
261
 */
262
  if (typeof(Drupal.Ajax) != 'undefined' && typeof(Drupal.Ajax.plugins) != 'undefined') {
263
    Drupal.Ajax.plugins.CKEditor = function(hook, args) {
264
      if (hook === 'submit' && typeof(CKEDITOR.instances) != 'undefined') {
265
        for (var i in CKEDITOR.instances)
266
          CKEDITOR.instances[i].updateElement();
267
      }
268
      return true;
269
    };
270
  }
271

    
272
  //Support for Panels [#679976]
273
  Drupal.ckeditorSubmitAjaxForm = function () {
274
    if (typeof(CKEDITOR.instances) != 'undefined' && typeof(CKEDITOR.instances['edit-body']) != 'undefined') {
275
      Drupal.ckeditorOff('edit-body');
276
    }
277
  };
278

    
279
  /**
280
 * Drupal behaviors
281
 */
282
  Drupal.behaviors.ckeditor = {
283
    attach:
284
    function (context) {
285
      if ((typeof(CKEDITOR) == 'undefined') || !CKEDITOR.env.isCompatible) {
286
        return;
287
      }
288

    
289
      // make sure the textarea behavior is run first, to get a correctly sized grippie
290
      if (Drupal.behaviors.textarea && Drupal.behaviors.textarea.attach) {
291
        Drupal.behaviors.textarea.attach(context);
292
      }
293

    
294
      $(context).find("textarea.ckeditor-mod:not(.ckeditor-processed)").each(function () {
295
        var ta_id=$(this).attr("id");
296
        if (CKEDITOR.instances && typeof(CKEDITOR.instances[ta_id]) != 'undefined'){
297
          Drupal.ckeditorOff(ta_id);
298
        }
299

    
300
        if ((typeof(Drupal.settings.ckeditor.autostart) != 'undefined') && (typeof(Drupal.settings.ckeditor.autostart[ta_id]) != 'undefined')) {
301
          Drupal.ckeditorOn(ta_id);
302
        }
303

    
304
        if (typeof(Drupal.settings.ckeditor.input_formats[Drupal.settings.ckeditor.elements[ta_id]]) != 'undefined') {
305
          $('.ckeditor_links').show();
306
        }
307

    
308
        var sel_format = $("#" + ta_id.substr(0, ta_id.lastIndexOf("-")) + "-format--2");
309
        if (sel_format && sel_format.not('.ckeditor-processed')) {
310
          sel_format.addClass('ckeditor-processed').change(function() {
311
            Drupal.settings.ckeditor.elements[ta_id] = $(this).val();
312
            if (CKEDITOR.instances && typeof(CKEDITOR.instances[ta_id]) != 'undefined') {
313
              $('#'+ta_id).val(CKEDITOR.instances[ta_id].getData());
314
            }
315
            Drupal.ckeditorOff(ta_id);
316
            if (typeof(Drupal.settings.ckeditor.input_formats[$(this).val()]) != 'undefined'){
317
              if ($('#'+ta_id).hasClass('ckeditor-processed')) {
318
                Drupal.ckeditorOn(ta_id, false);
319
              }
320
              else {
321
                Drupal.ckeditorOn(ta_id);
322
              }
323
              $('#switch_'+ta_id).show();
324
            }
325
            else {
326
              $('#switch_'+ta_id).hide();
327
            }
328
          });
329
        }
330
      });
331
    },
332
    detach:
333
    function(context, settings, trigger){
334
      $(context).find("textarea.ckeditor-mod.ckeditor-processed").each(function () {
335
        var ta_id=$(this).attr("id");
336
        if (CKEDITOR.instances[ta_id])
337
          $('#'+ta_id).val(CKEDITOR.instances[ta_id].getData());
338
        if(trigger != 'serialize') {
339
          Drupal.ckeditorOff(ta_id);
340
          $(this).removeClass('ckeditor-processed');
341
        }
342
      });
343
    }
344
  };
345
})(jQuery);
346

    
347
/**
348
 * IMCE support
349
 */
350
var ckeditor_imceSendTo = function (file, win){
351
  var cfunc = win.location.href.split('&');
352

    
353
  for (var x in cfunc) {
354
    if (cfunc[x].match(/^CKEditorFuncNum=\d+$/)) {
355
      cfunc = cfunc[x].split('=');
356
      break;
357
    }
358
  }
359
  CKEDITOR.tools.callFunction(cfunc[1], file.url);
360
  win.close();
361
}