Projet

Général

Profil

Paste
Télécharger (13,3 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ckeditor / includes / ckeditor.utils.js @ 6fd71452

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
    ckeditor_obj.input_formats[ckeditor_obj.elements[textarea_id]].toolbar = eval(ckeditor_obj.input_formats[ckeditor_obj.elements[textarea_id]].toolbar);
41
    textarea_settings = ckeditor_obj.input_formats[ckeditor_obj.elements[textarea_id]];
42

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

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

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

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

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

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

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

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

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

    
146
  Drupal.ckeditorOn = function(textarea_id, run_filter) {
147

    
148
    run_filter = typeof(run_filter) != 'undefined' ? run_filter : true;
149

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

    
161
    if (!CKEDITOR.env.isCompatible) {
162
      return;
163
    }
164

    
165
    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)) {
166
      $.ajax({
167
        type: 'POST',
168
        url: Drupal.settings.ckeditor.xss_url,
169
        async: false,
170
        data: {
171
          text: $('#' + textarea_id).val(),
172
          input_format: ckeditor_obj.textarea_default_format[textarea_id],
173
          token: Drupal.settings.ckeditor.ajaxToken
174
        },
175
        success: function(text){
176
          $("#" + textarea_id).val(text);
177
          Drupal.ckeditorInit(textarea_id);
178
        }
179
      })
180
    }
181
    else {
182
      Drupal.ckeditorInit(textarea_id);
183
    }
184
  };
185

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

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

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

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

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

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

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

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

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

    
281
  function attachCKEditor(context) {
282
    // make sure the textarea behavior is run first, to get a correctly sized grippie
283
    if (Drupal.behaviors.textarea && Drupal.behaviors.textarea.attach) {
284
      Drupal.behaviors.textarea.attach(context);
285
    }
286

    
287
    $(context).find("textarea.ckeditor-mod:not(.ckeditor-processed)").each(function () {
288
      var ta_id=$(this).attr("id");
289
      if (CKEDITOR.instances && typeof(CKEDITOR.instances[ta_id]) != 'undefined'){
290
        Drupal.ckeditorOff(ta_id);
291
      }
292

    
293
      if ((typeof(Drupal.settings.ckeditor.autostart) != 'undefined') && (typeof(Drupal.settings.ckeditor.autostart[ta_id]) != 'undefined')) {
294
        Drupal.ckeditorOn(ta_id);
295
      }
296

    
297
      if (typeof(Drupal.settings.ckeditor.input_formats[Drupal.settings.ckeditor.elements[ta_id]]) != 'undefined') {
298
        $('.ckeditor_links').show();
299
      }
300

    
301
      var sel_format = $("#" + ta_id.substr(0, ta_id.lastIndexOf("-")) + "-format--2");
302
      if (sel_format && sel_format.not('.ckeditor-processed')) {
303
        sel_format.addClass('ckeditor-processed').change(function() {
304
          Drupal.settings.ckeditor.elements[ta_id] = $(this).val();
305
          if (CKEDITOR.instances && typeof(CKEDITOR.instances[ta_id]) != 'undefined') {
306
            $('#'+ta_id).val(CKEDITOR.instances[ta_id].getData());
307
          }
308
          Drupal.ckeditorOff(ta_id);
309
          if (typeof(Drupal.settings.ckeditor.input_formats[$(this).val()]) != 'undefined'){
310
            if ($('#'+ta_id).hasClass('ckeditor-processed')) {
311
              Drupal.ckeditorOn(ta_id, false);
312
            }
313
            else {
314
              Drupal.ckeditorOn(ta_id);
315
            }
316
            $('#switch_'+ta_id).show();
317
          }
318
          else {
319
            $('#switch_'+ta_id).hide();
320
          }
321
        });
322
      }
323
    });
324
  }
325

    
326
  /**
327
 * Drupal behaviors
328
 */
329
  Drupal.behaviors.ckeditor = {
330
    attach:
331
    function (context) {
332
      // If CKEDITOR is undefined and script is loaded from CDN, wait up to 15 seconds until it loads [#2244817]
333
      if ((typeof(CKEDITOR) == 'undefined') && Drupal.settings.ckeditor.editor_path.match(/^(http(s)?:)?\/\//i)) {
334
        if (typeof(Drupal.settings.ckeditor.loadAttempts) == 'undefined') {
335
          Drupal.settings.ckeditor.loadAttempts = 50;
336
        }
337
        if (Drupal.settings.ckeditor.loadAttempts > 0) {
338
          Drupal.settings.ckeditor.loadAttempts--;
339
          window.setTimeout(function() {
340
            Drupal.behaviors.ckeditor.attach(context);
341
          }, 300);
342
        }
343
        return;
344
      }
345
      if ((typeof(CKEDITOR) == 'undefined') || !CKEDITOR.env.isCompatible) {
346
        return;
347
      }
348
      attachCKEditor(context);
349
    },
350
    detach:
351
    function(context, settings, trigger){
352
      $(context).find("textarea.ckeditor-mod.ckeditor-processed").each(function () {
353
        var ta_id=$(this).attr("id");
354
        if (CKEDITOR.instances[ta_id])
355
          $('#'+ta_id).val(CKEDITOR.instances[ta_id].getData());
356
        if(trigger != 'serialize') {
357
          Drupal.ckeditorOff(ta_id);
358
          $(this).removeClass('ckeditor-processed');
359
        }
360
      });
361
    }
362
  };
363

    
364
  // Support CTools detach event.
365
  $(document).bind('CToolsDetachBehaviors', function(event, context) {
366
    Drupal.behaviors.ckeditor.detach(context, {}, 'unload');
367
  });
368
})(jQuery);
369

    
370
/**
371
 * IMCE support
372
 */
373
var ckeditor_imceSendTo = function (file, win){
374
  var cfunc = win.location.href.split('&');
375

    
376
  for (var x in cfunc) {
377
    if (cfunc[x].match(/^CKEditorFuncNum=\d+$/)) {
378
      cfunc = cfunc[x].split('=');
379
      break;
380
    }
381
  }
382
  CKEDITOR.tools.callFunction(cfunc[1], file.url);
383
  win.close();
384
}
385