Projet

Général

Profil

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

root / drupal7 / sites / all / modules / media_ckeditor / js / plugins / media / plugin.js @ 59ae487e

1
/*
2
Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved.
3
For licensing, see LICENSE.html or http://ckeditor.com/license
4
*/
5

    
6
/**
7
 * @file Plugin for inserting images from Drupal media module
8
 *
9
 * @TODO Remove all the legecy media wrapper once it's sure nobody uses that
10
 * anymore.
11
 */
12
( function() {
13
  var mediaPluginDefinition = {
14
    icons: 'media',
15
    requires: ['button'],
16
    // All the default distributions of the editor have the widget plugin
17
    // disabled by default.
18
    hasWidgetSupport: false,
19
    mediaLegacyWrappers: false,
20
    hidpi: true,
21
    onLoad: function() {
22
      // Check if this instance has widget support.
23
      mediaPluginDefinition.hasWidgetSupport = typeof(CKEDITOR.plugins.registered.widget) != 'undefined';
24
      // Add dependency to widget plugin if possible.
25
      if (parseFloat(CKEDITOR.version) >= 4.3 && mediaPluginDefinition.hasWidgetSupport) {
26
        mediaPluginDefinition.requires.push('widget');
27
      }
28
    },
29

    
30
    // Wrap Drupal plugin in a proxy plugin.
31
    init: function(editor){
32
      editor.addCommand( 'media',
33
      {
34
        exec: function (editor) {
35
          var data = {
36
            format: 'html',
37
            node: null,
38
            content: ''
39
          };
40
          var selection = editor.getSelection();
41
          if (selection) {
42
            data.node = selection.getSelectedElement();
43

    
44
            if (data.node) {
45
              data.node = data.node.$;
46
            }
47
            if (selection.getType() == CKEDITOR.SELECTION_TEXT) {
48
              if (CKEDITOR.env.ie && CKEDITOR.env.version < 10) {
49
                data.content = selection.getNative().createRange().text;
50
              }
51
              else {
52
                data.content = selection.getNative().toString();
53
              }
54
            }
55
            else if (data.node) {
56
              // content is supposed to contain the "outerHTML".
57
              data.content = data.node.parentNode.innerHTML;
58
            }
59
          }
60

    
61
          // If we are inside another media element, we need to move the cursor to the end of the element
62
          var selected_element = selection.getStartElement();
63
          var parent = selected_element.getParent();
64
          //if we are inside "a" tag and inside "span" (with class media-element)
65
          if (selected_element.is('a') && parent.is('span') && parent.hasClass('media-element')) {
66
            selection.selectElement(parent);
67
            selected_ranges = selection.getRanges();
68
            selected_ranges[0].collapse(false);
69
            selection.selectRanges(selected_ranges);
70
          }
71
          //if we are outside "a" tag but inside "span" (with class media-element)
72
          else if (selected_element.is('span') && selected_element.hasClass('media-element')) {
73
              selection.selectElement(selected_element);
74
              selected_ranges = selection.getRanges();
75
              selected_ranges[0].collapse(false);
76
              selection.selectRanges(selected_ranges);
77
          }
78
          Drupal.settings.ckeditor.plugins['media'].invoke(data, Drupal.settings.ckeditor.plugins['media'], editor.name);
79
        }
80
      });
81

    
82
      // Add a Ckeditor context menu item for editing already-inserted media.
83
      if (editor.contextMenu) {
84
        editor.addCommand('mediaConfigure', {
85
          exec: function (editor) {
86
            editor.execCommand('media');
87
          },
88
        });
89

    
90
        editor.addMenuGroup('mediaGroup');
91
        editor.addMenuItem('mediaConfigureItem', {
92
          label: Drupal.t('Media settings'),
93
          icon: this.path + 'images/icon.gif',
94
          command: 'mediaConfigure',
95
          group: 'mediaGroup'
96
        });
97

    
98
        editor.contextMenu.addListener(function(element) {
99
          if (element.getAttribute('data-media-element') ||
100
              element.find('[data-media-element]').count()) {
101
            return { mediaConfigureItem: CKEDITOR.TRISTATE_OFF };
102
          };
103
        });
104
      }
105

    
106
      // Add the toolbar button.
107
      editor.ui.addButton( 'Media',
108
      {
109
        label: 'Add media',
110
        command: 'media'
111
      });
112

    
113
      var ckeditorversion = parseFloat(CKEDITOR.version);
114

    
115
      // Because the media comment wrapper don't work well for CKEditor we
116
      // replace them by using a custom mediawrapper element.
117
      // Instead having
118
      // <!--MEDIA-WRAPPER-START-1--><img /><!--MEDIA-WRAPPER-END-1-->
119
      // We wrap the placeholder with
120
      // <mediawrapper data="1"><img /></mediawrapper>
121
      // That way we can deal better with selections - see selectionChange.
122
      CKEDITOR.dtd['mediawrapper'] = CKEDITOR.dtd;
123
      CKEDITOR.dtd.$blockLimit['mediawrapper'] = 1;
124
      CKEDITOR.dtd.$inline['mediawrapper'] = 1;
125
      CKEDITOR.dtd.$nonEditable['mediawrapper'] = 1;
126
      if (ckeditorversion >= 4.1) {
127
        // Register allowed tag for advanced filtering.
128
        editor.filter.allow( 'mediawrapper[!data]', 'mediawrapper', true);
129
        // Don't remove the data-file_info attribute added by media!
130
        editor.filter.allow( '*[!data-file_info]', 'mediawrapper', true);
131
        // Ensure image tags accept all kinds of attributes.
132
        editor.filter.allow( 'img[*]{*}(*)', 'mediawrapper', true);
133
        // Objects should be selected as a whole in the editor.
134
        CKEDITOR.dtd.$object['mediawrapper'] = 1;
135
      }
136
      function prepareDataForWysiwygMode(data) {
137
        if (typeof Drupal.media !== 'undefined') {
138
          data = Drupal.media.filter.replaceTokenWithPlaceholder(data);
139
        }
140
        // Legacy media wrapper.
141
        mediaPluginDefinition.mediaLegacyWrappers = (data.indexOf("<!--MEDIA-WRAPPER-START-") !== -1);
142
        if (mediaPluginDefinition.mediaLegacyWrappers) {
143
          data = data.replace(/<!--MEDIA-WRAPPER-START-(\d+)-->(.*?)<!--MEDIA-WRAPPER-END-\d+-->/gi, '<mediawrapper data="$1">$2</mediawrapper>');
144
        }
145
        return data;
146
      }
147
      function prepareDataForSourceMode(data) {
148
        var replacement = '$2';
149
        // Legacy wrapper
150
        if (mediaPluginDefinition.mediaLegacyWrappers) {
151
          replacement = '<!--MEDIA-WRAPPER-START-$1-->$2<!--MEDIA-WRAPPER-END-$1-->';
152
        }
153
        data = data.replace(/<mediawrapper data="(.*?)">(.*?)<\/mediawrapper>/gi, replacement);
154
        if (typeof Drupal.media !== 'undefined') {
155
          data = Drupal.media.filter.replacePlaceholderWithToken(data);
156
        }
157
        return data;
158
      }
159

    
160
      // Ensure the tokens are replaced by placeholders while editing.
161
      // Check for widget support.
162
      if (mediaPluginDefinition.hasWidgetSupport) {
163
        editor.widgets.add( 'mediabox',
164
        {
165
          button: 'Create a mediabox',
166
          editables: {},
167
          allowedContent: '*',
168
          upcast: function( element ) {
169
            // Ensure media tokens are converted to media placeholders.
170
            html = Drupal.media.filter.replaceTokenWithPlaceholder(element.getHtml());
171
            // Only replace html if it's different
172
            if (html != element.getHtml()) {
173
              element.setHtml(html);
174
              // CKEditor's setHtml() method automatically fixes the HTML that
175
              // it receives, which means that if it gets an <li> without a
176
              // <ul> or <ol> parent, it adds a <ul>. These extra <ul> tags just
177
              // keep piling up every time this function runs. So check here to
178
              // see if we may need to fix this.
179
              if (element.children && element.children[0]) {
180
                // We identify this by looking for a <ul> inside a <ul> or <ol>.
181
                if (('ul' === element.name && 'ul' === element.children[0].name) ||
182
                    ('ol' === element.name && 'ul' === element.children[0].name)) {
183
                  // If this did happen, fix it by promoting the grandchildren
184
                  // (ie, actual list items) to children.
185
                  element.children = element.children[0].children;
186
                }
187
              }
188
            }
189
            return element.name == 'mediawrapper' || 'data-media-element' in element.attributes;
190
          },
191

    
192
          downcast: function( widgetElement ) {
193
            var token = Drupal.media.filter.replacePlaceholderWithToken(widgetElement.getOuterHtml());
194
            if (token) {
195
              return new CKEDITOR.htmlParser.text(token);
196
            }
197
            return false;
198
          },
199

    
200
          init: function() {
201
            // Add double-click functionality to the widget.
202
            this.on('doubleclick', function(evt) {
203
              editor.execCommand('media');
204
            }, null, null, 5 );
205
          }
206
        });
207
      }
208
      else if (ckeditorversion >= 4) {
209
        // CKEditor >=4.0
210
        editor.on('setData', function( event ) {
211
          event.data.dataValue = prepareDataForWysiwygMode(event.data.dataValue);
212
        });
213
      }
214
      else {
215
        // CKEditor >=3.6 behaviour.
216
        editor.on( 'beforeSetMode', function( event, data ) {
217
          event.removeListener();
218
          var wysiwyg = editor._.modes[ 'wysiwyg' ];
219
          var source = editor._.modes[ 'source' ];
220
          wysiwyg.loadData = CKEDITOR.tools.override( wysiwyg.loadData, function( org )
221
          {
222
            return function( data ) {
223
              return ( org.call( this, prepareDataForWysiwygMode(data)) );
224
            };
225
          } );
226
          source.loadData = CKEDITOR.tools.override( source.loadData, function( org )
227
          {
228
            return function( data ) {
229
              return ( org.call( this, prepareDataForSourceMode(data) ) );
230
            };
231
          } );
232
        });
233
      }
234

    
235
      // Provide alternative to the widget functionality introduced in 4.3.
236
      if (!mediaPluginDefinition.hasWidgetSupport) {
237
        // Ensure tokens instead the html element is saved.
238
        editor.on('getData', function( event ) {
239
          event.data.dataValue = prepareDataForSourceMode(event.data.dataValue);
240
        });
241

    
242
        // Ensure our enclosing wrappers are always included in the selection.
243
        editor.on('selectionChange', function( event ) {
244
          var ranges = editor.getSelection().getRanges().createIterator();
245
          var newRanges = [];
246
          var currRange;
247
          while(currRange = ranges.getNextRange()) {
248
            var commonAncestor = currRange.getCommonAncestor(false);
249
            if (commonAncestor && typeof(commonAncestor.getName) != 'undefined' && commonAncestor.getName() == 'mediawrapper') {
250
              var range = new CKEDITOR.dom.range( editor.document );
251
              if (currRange.collapsed === true) {
252
                // Don't allow selection within the wrapper element.
253
                if (currRange.startOffset == 0) {
254
                  // While v3 plays nice with setting start and end to avoid
255
                  // editing within the media wrapper element, v4 ignores that.
256
                  // Thus we try to move the cursor further away.
257
                  if (parseInt(CKEDITOR.version) > 3) {
258
                    range.setStart(commonAncestor.getPrevious());
259
                    range.setEnd(commonAncestor.getPrevious());
260
                  }
261
                  else {
262
                    range.setStartBefore(commonAncestor);
263
                  }
264
                }
265
                else {
266
                  // While v3 plays nice with setting start and end to avoid
267
                  // editing within the media wrapper element, v4 ignores that.
268
                  // Thus we try to move the cursor further away.
269
                  if (parseInt(CKEDITOR.version) > 3) {
270
                    range.setStart(commonAncestor.getNext(), 1);
271
                    range.setEnd(commonAncestor.getNext(), 1);
272
                  }
273
                  else {
274
                    range.setStartAfter(commonAncestor);
275
                  }
276
                }
277
              }
278
              else {
279
                // Always select the whole wrapper element.
280
                range.setStartBefore(commonAncestor);
281
                range.setEndAfter(commonAncestor);
282
              }
283
              newRanges.push(range);
284
            }
285
          }
286
          if (newRanges.length) {
287
            editor.getSelection().selectRanges(newRanges);
288
          }
289
        });
290
      }
291
    }
292
  };
293

    
294
  CKEDITOR.plugins.add( 'media', mediaPluginDefinition);
295
} )();