Projet

Général

Profil

Révision fc3d89c3

Ajouté par Assos Assos il y a plus de 7 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/media/modules/media_wysiwyg/js/media_wysiwyg.filter.js
61 61
      return content;
62 62
    },
63 63

  
64
    /**
65
     * Returns alt and title field attribute data from the corresponding fields.
66
     *
67
     * Specifically looks for file_entity module's file_image_alt_text and
68
     * file_image_title_text fields as those are by default used to store
69
     * override values for image alt and title attributes.
70
     *
71
     * @param options (array)
72
     *   Options passed through a popup form submission.
73
     * @param includeFieldID (bool)
74
     *   If set, the returned object will have extra keys with the IDs of the
75
     *   found fields.
76
     *
77
     * If the alt or title fields were not found, their keys will be excluded
78
     * from the returned array.
79
     *
80
     * @return
81
     *   An object with the following keys:
82
     *   - alt: The value of the alt field.
83
     *   - altField: The id of the alt field.
84
     *   - title: The value of the title field.
85
     *   - titleField: The id of the title field.
86
     */
87
    parseAttributeFields: function(options, includeFieldID) {
88
      var attributes = {};
89

  
90
      for (var field in options) {
91
        // If the field is set to false, use an empty string for output.
92
        options[field] = options[field] === false ? '' : options[field];
93
        //if (field.match(/^field_file_image_alt_text/)) {
94
        if (field.match(new RegExp('^' + Drupal.settings.media.img_alt_field))) {
95
          attributes.alt = options[field];
96
          if (includeFieldID) {
97
            attributes.altField = field;
98
          }
99
        }
100

  
101
        //if (field.match(/^field_file_image_title_text/)) {
102
        if (field.match(new RegExp('^' + Drupal.settings.media.img_title_field))) {
103
          attributes.title = options[field];
104
          if (includeFieldID) {
105
            attributes.titleField = field;
106
          }
107
        }
108
      }
109

  
110
      return attributes;
111
    },
112

  
113
    /**
114
     * Ensures changes made to fielded attributes are done on the fields too.
115
     *
116
     * This should be called when creating a macro tag from a placeholder.
117
     *
118
     * Changed made to attributes represented by fields are synced back to the
119
     * corresponding fields, if they exist. The alt/title attribute
120
     * values encoded in the macro will override the alt/title field values (set
121
     * in the Media dialog) during rendering of both WYSIWYG placeholders and
122
     * the final file entity on the server. Syncing makes changes applied to a
123
     * placeholder's alt/title attribute using native WYSIWYG tools visible in
124
     * the fields shown in the Media dialog.
125
     *
126
     * The reverse should be done when creating a placeholder from a macro tag
127
     * so changes made in the Media dialog are reflected in the placeholder's
128
     * alt and title attributes or the values there become stale and the change
129
     * appears uneffective.
130
     *
131
     * @param file_info (object)
132
     *   A JSON decoded object of the file being inserted/updated.
133
     */
134
    syncAttributesToFields: function(file_info) {
135
      if (!file_info) {
136
        file_info = {};
137
      }
138
      if (!file_info.attributes) {
139
        file_info.attributes = {};
140
      }
141
      if (!file_info.fields) {
142
        file_info.fields = {};
143
      }
144
      var fields = Drupal.media.filter.parseAttributeFields(file_info.fields, true);
145

  
146
      // If the title attribute has changed, ensure the title field is updated.
147
      var titleAttr = file_info.attributes.title || false;
148
      if (fields.titleField && (titleAttr !== fields.title)) {
149
        file_info.fields[fields.titleField] = titleAttr;
150
      }
151

  
152
      // If the alt attribute has changed, ensure the alt field is updated.
153
      var altAttr = file_info.attributes.alt || false;
154
      if (fields.altField && (altAttr !== fields.alt)) {
155
        file_info.fields[fields.altField] = altAttr;
156
      }
157

  
158
      return file_info;
159
    },
160

  
64 161
    /**
65 162
     * Replaces media elements with tokens.
66 163
     *
......
70 167
    replacePlaceholderWithToken: function(content) {
71 168
      Drupal.media.filter.ensure_tagmap();
72 169

  
73
      // Rewrite the tagmap in case any of the macros have changed.
74
      Drupal.settings.tagmap = {};
75

  
76 170
      // Replace all media placeholders with their JSON macro representations.
77 171
      //
78 172
      // There are issues with using jQuery to parse the WYSIWYG content (see
......
88 182
      // media_get_file_without_label().
89 183
      //
90 184
      // Finds the media-element class.
91
      var classRegex = 'class=[\'"][^\'"]*?media-element';
185
      var classRegex = 'class=([\'"])[^\\1]*?media-element';
92 186
      // Image tag with the media-element class.
93 187
      var regex = '<img[^>]+' + classRegex + '[^>]*?>';
94 188
      // Or a span with the media-element class (used for documents).
......
98 192
      var matches = content.match(RegExp(regex, 'gi'));
99 193
      if (matches) {
100 194
        for (i = 0; i < matches.length; i++) {
101
          markup = matches[i];
102
          macro = Drupal.media.filter.create_macro($(markup));
103
          Drupal.settings.tagmap[macro] = markup;
104
          content = content.replace(markup, macro);
195
          var markup = matches[i];
196
          var macro = Drupal.media.filter.create_macro($(markup));
197
          // If we have a truthy response, store the macro and perform the
198
          // replacement.
199
          if (macro) {
200
            Drupal.settings.tagmap[macro] = markup;
201
            content = content.replace(markup, macro);
202
          }
105 203
        }
106 204
      }
107 205

  
......
131 229
        element = element.children();
132 230
      }
133 231

  
232
      // Extract attributes represented by fields and use those values to keep
233
      // them in sync, usually alt and title.
234
      info.fields = info.attributes;
235
      var attributes = Drupal.media.filter.parseAttributeFields(info.attributes);
236
      info.attributes = $.extend(info.attributes, attributes);
237

  
134 238
      // Move attributes from the file info array to the placeholder element.
135 239
      if (info.attributes) {
136 240
        $.each(Drupal.settings.media.wysiwyg_allowed_attributes, function(i, a) {
137 241
          if (info.attributes[a]) {
138
            element.attr(a, info.attributes[a]);
242
            element.attr(a, $('<textarea />').html(info.attributes[a]).text());
139 243
          }
140 244
        });
141 245
        delete(info.attributes);
......
163 267

  
164 268
      var classes = ['media-element'];
165 269
      if (info.view_mode) {
270
        // Remove any existing view mode classes.
271
        element.removeClass (function (index, css) {
272
          return (css.match (/\bfile-\S+/g) || []).join(' ');
273
        });
166 274
        classes.push('file-' + info.view_mode.replace(/_/g, '-'));
167 275
      }
168 276
      element.addClass(classes.join(' '));
......
223 331
        }
224 332
      }
225 333

  
226
      return file_info;
334
      return Drupal.media.filter.syncAttributesToFields(file_info);
227 335
    },
228 336

  
229 337
    /**

Formats disponibles : Unified diff