Projet

Général

Profil

Paste
Télécharger (14,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ckeditor / includes / ckeditor.utils.js @ 2e0f6994

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
if (typeof window.CKEDITOR_BASEPATH === 'undefined') {
6
  window.CKEDITOR_BASEPATH = Drupal.settings.ckeditor.editor_path;
7
}
8
(function ($) {
9
  Drupal.ckeditor = (typeof(CKEDITOR) != 'undefined');
10
  Drupal.ckeditor_ver = false;
11

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

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

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

    
39
    var textarea_settings = false;
40
    if (typeof(ckeditor_obj.input_formats[ckeditor_obj.elements[textarea_id]].toolbar) != 'object') {
41
      ckeditor_obj.input_formats[ckeditor_obj.elements[textarea_id]].toolbar = Drupal.ckeditorToolbarToArray(ckeditor_obj.input_formats[ckeditor_obj.elements[textarea_id]].toolbar);
42
    }
43
    textarea_settings = ckeditor_obj.input_formats[ckeditor_obj.elements[textarea_id]];
44

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

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

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

    
89
        if (ev.editor.config.bodyClass)
90
          body.addClass(ev.editor.config.bodyClass);
91
        if (ev.editor.config.bodyId)
92
          body.attr('id', ev.editor.config.bodyId);
93
        if (typeof(Drupal.smileysAttach) != 'undefined' && typeof(ev.editor.dataProcessor.writer) != 'undefined')
94
          ev.editor.dataProcessor.writer.indentationChars = '    ';
95

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

    
122
    if (typeof Drupal.settings.ckeditor.scayt_language != 'undefined'){
123
      textarea_settings['scayt_sLang'] = Drupal.settings.ckeditor.scayt_language;
124
    }
125

    
126
    if (typeof textarea_settings['js_conf'] != 'undefined'){
127
      for (var add_conf in textarea_settings['js_conf']){
128
        var data;
129
        if (add_conf == 'toolbar') {
130
          data = Drupal.ckeditorToolbarToArray(textarea_settings['js_conf'][add_conf]);
131
        } else if (typeof textarea_settings['js_conf'][add_conf] === "boolean" ) {
132
          data = textarea_settings['js_conf'][add_conf];
133
        } else {
134
          data = JSON.parse(textarea_settings['js_conf'][add_conf].replace(/'/g, '"'));
135
        }
136

    
137
        textarea_settings[add_conf] = data;
138
      }
139
    }
140

    
141
    //remove width 100% from settings because this may cause problems with theme css
142
    if (textarea_settings.width == '100%') textarea_settings.width = '';
143

    
144
    if (CKEDITOR.loadFullCore) {
145
      CKEDITOR.on('loaded', function() {
146
        textarea_settings = Drupal.ckeditorLoadPlugins(textarea_settings);
147
        Drupal.ckeditorInstance = CKEDITOR.replace(textarea_id, textarea_settings);
148
      });
149
      CKEDITOR.loadFullCore();
150
    }
151
    else {
152
      textarea_settings = Drupal.ckeditorLoadPlugins(textarea_settings);
153
      Drupal.ckeditorInstance = CKEDITOR.replace(textarea_id, textarea_settings);
154
    }
155
  };
156

    
157
  Drupal.ckeditorOn = function(textarea_id, run_filter) {
158

    
159
    run_filter = typeof(run_filter) != 'undefined' ? run_filter : true;
160

    
161
    if (typeof(textarea_id) == 'undefined' || textarea_id.length == 0 || $("#" + textarea_id).length == 0) {
162
      return;
163
    }
164
    if ((typeof(Drupal.settings.ckeditor.load_timeout) == 'undefined') && (typeof(CKEDITOR.instances[textarea_id]) != 'undefined')) {
165
      return;
166
    }
167
    if (typeof(Drupal.settings.ckeditor.elements[textarea_id]) == 'undefined') {
168
      return;
169
    }
170
    var ckeditor_obj = Drupal.settings.ckeditor;
171

    
172
    if (!CKEDITOR.env.isCompatible) {
173
      return;
174
    }
175

    
176
    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)) {
177
      $.ajax({
178
        type: 'POST',
179
        url: Drupal.settings.ckeditor.xss_url,
180
        async: false,
181
        data: {
182
          text: $('#' + textarea_id).val(),
183
          input_format: ckeditor_obj.textarea_default_format[textarea_id],
184
          token: Drupal.settings.ckeditor.ajaxToken
185
        },
186
        success: function(text){
187
          $("#" + textarea_id).val(text);
188
          Drupal.ckeditorInit(textarea_id);
189
        }
190
      })
191
    }
192
    else {
193
      Drupal.ckeditorInit(textarea_id);
194
    }
195
  };
196

    
197
  /**
198
 * CKEditor destroy function
199
 *
200
 * @param string textarea_id
201
 */
202
  Drupal.ckeditorOff = function(textarea_id) {
203
    if (!CKEDITOR.instances || typeof(CKEDITOR.instances[textarea_id]) == 'undefined') {
204
      return;
205
    }
206
    if (!CKEDITOR.env.isCompatible) {
207
      return;
208
    }
209
    if (Drupal.ckeditorInstance && Drupal.ckeditorInstance.name == textarea_id)
210
      delete Drupal.ckeditorInstance;
211

    
212
    $("#" + textarea_id).val(CKEDITOR.instances[textarea_id].getData());
213
    CKEDITOR.instances[textarea_id].destroy(true);
214

    
215
    $("#" + textarea_id).next(".grippie").css("display", "block");
216
  };
217

    
218
  /**
219
* Loading selected CKEditor plugins
220
*
221
* @param object textarea_settings
222
*/
223
  Drupal.ckeditorLoadPlugins = function(textarea_settings) {
224
    if (typeof(textarea_settings.extraPlugins) == 'undefined') {
225
      textarea_settings.extraPlugins = '';
226
    }
227
    if (typeof CKEDITOR.plugins != 'undefined') {
228
      for (var plugin in textarea_settings['loadPlugins']) {
229
        textarea_settings.extraPlugins += (textarea_settings.extraPlugins) ? ',' + textarea_settings['loadPlugins'][plugin]['name'] : textarea_settings['loadPlugins'][plugin]['name'];
230
        CKEDITOR.plugins.addExternal(textarea_settings['loadPlugins'][plugin]['name'], textarea_settings['loadPlugins'][plugin]['path']);
231
      }
232
    }
233
    return textarea_settings;
234
  };
235

    
236
  /**
237
 * Returns true if CKEDITOR.version >= version
238
 */
239
  Drupal.ckeditorCompareVersion = function (version){
240
    var ckver = CKEDITOR.version;
241
    ckver = ckver.match(/(([\d]\.)+[\d]+)/i);
242
    version = version.match(/((\d+\.)+[\d]+)/i);
243
    ckver = ckver[0].split('.');
244
    version = version[0].split('.');
245
    for (var x in ckver) {
246
      if (ckver[x]<version[x]) {
247
        return false;
248
      }
249
      else if (ckver[x]>version[x]) {
250
        return true;
251
      }
252
    }
253
    return true;
254
  };
255

    
256
  Drupal.ckeditorInsertHtml = function(html) {
257
    if (!Drupal.ckeditorInstance)
258
      return false;
259

    
260
    if (Drupal.ckeditorInstance.mode == 'wysiwyg') {
261
      Drupal.ckeditorInstance.insertHtml(html);
262
      return true;
263
    }
264
    else {
265
      alert(Drupal.t('Content can only be inserted into CKEditor in the WYSIWYG mode.'));
266
      return false;
267
    }
268
  };
269

    
270
  if (typeof(Drupal.ckeditorToolbarToArray) == 'undefined') {
271
    Drupal.ckeditorToolbarToArray = function (toolbar) {
272
      toolbar = toolbar.replace(/\r?\n|\r/gmi, '')
273
          .replace(/\s/gmi, '')
274
          .replace(/([a-zA-Z0-9]+?):/g, '"$1":')
275
          .replace(/'/g, '"');
276

    
277
      return JSON.parse(toolbar);
278
    };
279
  }
280

    
281
  /**
282
 * Ajax support
283
 */
284
  if (typeof(Drupal.Ajax) != 'undefined' && typeof(Drupal.Ajax.plugins) != 'undefined') {
285
    Drupal.Ajax.plugins.CKEditor = function(hook, args) {
286
      if (hook === 'submit' && typeof(CKEDITOR.instances) != 'undefined') {
287
        for (var i in CKEDITOR.instances)
288
          CKEDITOR.instances[i].updateElement();
289
      }
290
      return true;
291
    };
292
  }
293

    
294
  //Support for Panels [#679976]
295
  Drupal.ckeditorSubmitAjaxForm = function () {
296
    if (typeof(CKEDITOR.instances) != 'undefined' && typeof(CKEDITOR.instances['edit-body']) != 'undefined') {
297
      Drupal.ckeditorOff('edit-body');
298
    }
299
  };
300

    
301
  function attachCKEditor(context) {
302
    // make sure the textarea behavior is run first, to get a correctly sized grippie
303
    if (Drupal.behaviors.textarea && Drupal.behaviors.textarea.attach) {
304
      Drupal.behaviors.textarea.attach(context);
305
    }
306

    
307
    // Manually set the cache-busting string to the same value as Drupal.
308
    if (typeof(Drupal.settings.ckeditor.timestamp) != 'undefined') {
309
      CKEDITOR.timestamp = Drupal.settings.ckeditor.timestamp;
310
    }
311

    
312
    $(context).find("textarea.ckeditor-mod:not(.ckeditor-processed)").each(function () {
313
      var ta_id=$(this).attr("id");
314
      if (CKEDITOR.instances && typeof(CKEDITOR.instances[ta_id]) != 'undefined'){
315
        Drupal.ckeditorOff(ta_id);
316
      }
317

    
318
      if ((typeof(Drupal.settings.ckeditor.autostart) != 'undefined') && (typeof(Drupal.settings.ckeditor.autostart[ta_id]) != 'undefined')) {
319
        Drupal.ckeditorOn(ta_id);
320
      }
321

    
322
      if (typeof(Drupal.settings.ckeditor.input_formats[Drupal.settings.ckeditor.elements[ta_id]]) != 'undefined') {
323
        $('.ckeditor_links').show();
324
      }
325

    
326
      var sel_format = $("#" + ta_id.substr(0, ta_id.lastIndexOf("-")) + "-format--2");
327
      if (sel_format && sel_format.not('.ckeditor-processed')) {
328
        sel_format.addClass('ckeditor-processed').change(function() {
329
          Drupal.settings.ckeditor.elements[ta_id] = $(this).val();
330
          if (CKEDITOR.instances && typeof(CKEDITOR.instances[ta_id]) != 'undefined') {
331
            $('#'+ta_id).val(CKEDITOR.instances[ta_id].getData());
332
          }
333
          Drupal.ckeditorOff(ta_id);
334
          if (typeof(Drupal.settings.ckeditor.input_formats[$(this).val()]) != 'undefined'){
335
            if ($('#'+ta_id).hasClass('ckeditor-processed')) {
336
              Drupal.ckeditorOn(ta_id, false);
337
            }
338
            else {
339
              Drupal.ckeditorOn(ta_id);
340
            }
341
            $('#switch_'+ta_id).show();
342
          }
343
          else {
344
            $('#switch_'+ta_id).hide();
345
          }
346
        });
347
      }
348
    });
349
  }
350

    
351
  /**
352
 * Drupal behaviors
353
 */
354
  Drupal.behaviors.ckeditor = {
355
    attach:
356
    function (context) {
357
      // If CKEDITOR is undefined and script is loaded from CDN, wait up to 15 seconds until it loads [#2244817]
358
      if ((typeof(CKEDITOR) == 'undefined') && Drupal.settings.ckeditor.editor_path.match(/^(http(s)?:)?\/\//i)) {
359
        if (typeof(Drupal.settings.ckeditor.loadAttempts) == 'undefined') {
360
          Drupal.settings.ckeditor.loadAttempts = 50;
361
        }
362
        if (Drupal.settings.ckeditor.loadAttempts > 0) {
363
          Drupal.settings.ckeditor.loadAttempts--;
364
          window.setTimeout(function() {
365
            Drupal.behaviors.ckeditor.attach(context);
366
          }, 300);
367
        }
368
        return;
369
      }
370
      if ((typeof(CKEDITOR) == 'undefined') || !CKEDITOR.env.isCompatible) {
371
        return;
372
      }
373
      attachCKEditor(context);
374
    },
375
    detach:
376
    function(context, settings, trigger){
377
      $(context).find("textarea.ckeditor-mod.ckeditor-processed").each(function () {
378
        var ta_id=$(this).attr("id");
379
        if (CKEDITOR.instances[ta_id])
380
          $('#'+ta_id).val(CKEDITOR.instances[ta_id].getData());
381
        if(trigger != 'serialize') {
382
          Drupal.ckeditorOff(ta_id);
383
          $(this).removeClass('ckeditor-processed');
384
        }
385
      });
386
    }
387
  };
388

    
389
  // Support CTools detach event.
390
  $(document).bind('CToolsDetachBehaviors', function(event, context) {
391
    Drupal.behaviors.ckeditor.detach(context, {}, 'unload');
392
  });
393
})(jQuery);
394

    
395
/**
396
 * IMCE support
397
 */
398
var ckeditor_imceSendTo = function (file, win){
399
  var cfunc = win.location.href.split('&');
400

    
401
  for (var x in cfunc) {
402
    if (cfunc[x].match(/^CKEditorFuncNum=\d+$/)) {
403
      cfunc = cfunc[x].split('=');
404
      break;
405
    }
406
  }
407
  CKEDITOR.tools.callFunction(cfunc[1], file.url);
408
  win.close();
409
}
410