Projet

Général

Profil

Paste
Télécharger (6,86 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / media_ckeditor / js / plugins / media / library.js @ da542b7b

1

    
2
/**
3
 *  @file
4
 *  Attach Media ckeditor behaviors.
5
 */
6

    
7
(function ($) {
8
  Drupal.media = Drupal.media || {};
9

    
10
  Drupal.settings.ckeditor.plugins['media'] = {
11
    /**
12
     * Execute the button.
13
     */
14
    invoke: function (data, settings, instanceId) {
15
      if (data.format == 'html') {
16
        // CKEDITOR module support doesn't set this setting
17
        if (typeof settings['global'] === 'undefined') {
18
          settings['global'] = {id: 'media_wysiwyg'};
19
        }
20
        // If the selection is (or contains) an element with the attribute of
21
        // "data-media-element", assume the user wants to edit that thing.
22
        var $alreadyInsertedMedia;
23
        if (jQuery(data.node).is('[data-media-element]')) {
24
          $alreadyInsertedMedia = jQuery(data.node);
25
        }
26
        else {
27
          $alreadyInsertedMedia = jQuery(data.node).find('[data-media-element]');
28
        }
29
        if ($alreadyInsertedMedia.length) {
30
          // Change the view mode for already-inserted media.
31
          var mediaFile = Drupal.media.filter.extract_file_info($alreadyInsertedMedia);
32
          Drupal.media.popups.mediaStyleSelector(mediaFile, function (mediaFiles) {
33
            Drupal.settings.ckeditor.plugins['media'].insertMediaFile(mediaFile, mediaFiles, CKEDITOR.instances[instanceId]);
34
          }, settings['global']);
35
        }
36
        else {
37
          Drupal.media.popups.mediaBrowser(function (mediaFiles) {
38
            Drupal.settings.ckeditor.plugins['media'].mediaBrowserOnSelect(mediaFiles, instanceId);
39
          }, settings['global']);
40
        }
41
      }
42
    },
43

    
44
    /**
45
     * Respond to the mediaBrowser's onSelect event.
46
     */
47
    mediaBrowserOnSelect: function (mediaFiles, instanceId) {
48
      var mediaFile = mediaFiles[0];
49
      var options = {};
50
      Drupal.media.popups.mediaStyleSelector(mediaFile, function (formattedMedia) {
51
        Drupal.settings.ckeditor.plugins['media'].insertMediaFile(mediaFile, formattedMedia, CKEDITOR.instances[instanceId]);
52
      }, options);
53

    
54
      return;
55
    },
56

    
57
    insertMediaFile: function (mediaFile, formattedMedia, ckeditorInstance) {
58
      // Customization of Drupal.media.filter.registerNewElement().
59
      var element = Drupal.media.filter.create_element(formattedMedia.html, {
60
        fid: mediaFile.fid,
61
        view_mode: formattedMedia.type,
62
        attributes: mediaFile.attributes,
63
        fields: formattedMedia.options
64
      });
65

    
66
      var hasWidgetSupport = typeof(CKEDITOR.plugins.registered.widget) != 'undefined';
67

    
68
      // Use own wrapper element to be able to properly deal with selections.
69
      // Check prepareDataForWysiwygMode() in plugin.js for details.
70
      var wysiwygHTML = Drupal.media.filter.getWysiwygHTML(element);
71

    
72
      if (wysiwygHTML.indexOf("<!--MEDIA-WRAPPER-START-") !== -1) {
73
        ckeditorInstance.plugins.media.mediaLegacyWrappers = true;
74
        wysiwygHTML = wysiwygHTML.replace(/<!--MEDIA-WRAPPER-START-(\d+)-->(.*?)<!--MEDIA-WRAPPER-END-\d+-->/gi, '');
75
      }
76

    
77
      // Insert element. Use CKEDITOR.dom.element.createFromHtml to ensure our
78
      // custom wrapper element is preserved.
79
      var editorElement = CKEDITOR.dom.element.createFromHtml(wysiwygHTML);
80
      ckeditorInstance.insertElement(editorElement);
81

    
82
      // Initialize widget on our html if possible.
83
      if (parseFloat(CKEDITOR.version) >= 4.3 && hasWidgetSupport) {
84
        ckeditorInstance.widgets.initOn( editorElement, 'mediabox' );
85

    
86
        // Also support the image2 plugin.
87
        ckeditorInstance.widgets.initOn( editorElement, 'image' );
88
      }
89
    },
90

    
91
    /**
92
     * Forces custom attributes into the class field of the specified image.
93
     *
94
     * Due to a bug in some versions of Firefox
95
     * (http://forums.mozillazine.org/viewtopic.php?f=9&t=1991855), the
96
     * custom attributes used to share information about the image are
97
     * being stripped as the image markup is set into the rich text
98
     * editor.  Here we encode these attributes into the class field so
99
     * the data survives.
100
     *
101
     * @param imgElement
102
     *   The image
103
     * @fid
104
     *   The file id.
105
     * @param view_mode
106
     *   The view mode.
107
     * @param additional
108
     *   Additional attributes to add to the image.
109
     */
110
    forceAttributesIntoClass: function (imgElement, fid, view_mode, additional) {
111
      var wysiwyg = imgElement.attr('wysiwyg');
112
      if (wysiwyg) {
113
        imgElement.addClass('attr__wysiwyg__' + wysiwyg);
114
      }
115
      var format = imgElement.attr('format');
116
      if (format) {
117
        imgElement.addClass('attr__format__' + format);
118
      }
119
      var typeOf = imgElement.attr('typeof');
120
      if (typeOf) {
121
        imgElement.addClass('attr__typeof__' + typeOf);
122
      }
123
      if (fid) {
124
        imgElement.addClass('img__fid__' + fid);
125
      }
126
      if (view_mode) {
127
        imgElement.addClass('img__view_mode__' + view_mode);
128
      }
129
      if (additional) {
130
        for (var name in additional) {
131
          if (additional.hasOwnProperty(name)) {
132
            switch (name) {
133
              case 'field_file_image_alt_text[und][0][value]':
134
                imgElement.attr('alt', additional[name]);
135
                break;
136
              case 'field_file_image_title_text[und][0][value]':
137
                imgElement.attr('title', additional[name]);
138
                break;
139
              default:
140
                imgElement.addClass('attr__' + name + '__' + additional[name]);
141
                break;
142
            }
143
          }
144
        }
145
      }
146
    },
147

    
148
    /**
149
     * Retrieves encoded attributes from the specified class string.
150
     *
151
     * @param classString
152
     *   A string containing the value of the class attribute.
153
     * @return
154
     *   An array containing the attribute names as keys, and an object
155
     *   with the name, value, and attribute type (either 'attr' or
156
     *   'img', depending on whether it is an image attribute or should
157
     *   be it the attributes section)
158
     */
159
    getAttributesFromClass: function (classString) {
160
      var actualClasses = [];
161
      var otherAttributes = [];
162
      var classes = classString.split(' ');
163
      var regexp = new RegExp('^(attr|img)__([^\S]*)__([^\S]*)$');
164
      for (var index = 0; index < classes.length; index++) {
165
        var matches = classes[index].match(regexp);
166
        if (matches && matches.length === 4) {
167
          otherAttributes[matches[2]] = {
168
            name: matches[2],
169
            value: matches[3],
170
            type: matches[1]
171
          };
172
        }
173
        else {
174
          actualClasses.push(classes[index]);
175
        }
176
      }
177
      if (actualClasses.length > 0) {
178
        otherAttributes['class'] = {
179
          name: 'class',
180
          value: actualClasses.join(' '),
181
          type: 'attr'
182
        };
183
      }
184
      return otherAttributes;
185
    },
186

    
187
    sortAttributes: function (a, b) {
188
      var nameA = a.name.toLowerCase();
189
      var nameB = b.name.toLowerCase();
190
      if (nameA < nameB) {
191
        return -1;
192
      }
193
      if (nameA > nameB) {
194
        return 1;
195
      }
196
      return 0;
197
    }
198
  };
199

    
200
})(jQuery);