Projet

Général

Profil

Paste
Télécharger (5,11 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / media / modules / media_wysiwyg / js / wysiwyg-media.js @ da542b7b

1

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

    
7
(function ($) {
8

    
9
Drupal.media = Drupal.media || {};
10

    
11
/**
12
 * Register the plugin with WYSIWYG.
13
 */
14
Drupal.wysiwyg.plugins.media = {
15

    
16
  /**
17
   * Determine whether a DOM element belongs to this plugin.
18
   *
19
   * @param node
20
   *   A DOM element
21
   */
22
  isNode: function(node) {
23
    return $(node).is('img[data-media-element]');
24
  },
25

    
26
  /**
27
   * Execute the button.
28
   *
29
   * @param data
30
   *   An object containing data about the current selection:
31
   *   - format: 'html' when the passed data is HTML content, 'text' when the
32
   *     passed data is plain-text content.
33
   *   - node: When 'format' is 'html', the focused DOM element in the editor.
34
   *   - content: The textual representation of the focused/selected editor
35
   *     content.
36
   * @param settings
37
   *   The plugin settings, as provided in the plugin's PHP include file.
38
   * @param instanceId
39
   *   The ID of the current editor instance.
40
   */
41
  invoke: function (data, settings, instanceId) {
42
    if (data.format == 'html') {
43
      var insert = new InsertMedia(instanceId);
44
      // CKEDITOR module support doesn't set this setting
45
      if (typeof settings['global'] === 'undefined') {
46
        settings['global'] = {id: 'media_wysiwyg'};
47
      }
48
      if (this.isNode(data.node)) {
49
        // Change the view mode for already-inserted media.
50
        var media_file = Drupal.media.filter.extract_file_info($(data.node));
51
        insert.onSelect([media_file]);
52
      }
53
      else {
54
        // Insert new media.
55
        insert.prompt(settings.global);
56
      }
57
    }
58
  },
59

    
60
  /**
61
   * Attach function, called when a rich text editor loads.
62
   * This finds all [[tags]] and replaces them with the html
63
   * that needs to show in the editor.
64
   *
65
   * This finds all JSON macros and replaces them with the HTML placeholder
66
   * that will show in the editor.
67
   */
68
  attach: function (content, settings, instanceId) {
69
    content = Drupal.media.filter.replaceTokenWithPlaceholder(content);
70
    return content;
71
  },
72

    
73
  /**
74
   * Detach function, called when a rich text editor detaches
75
   */
76
  detach: function (content, settings, instanceId) {
77
    content = Drupal.media.filter.replacePlaceholderWithToken(content);
78
    return content;
79
  }
80
};
81
/**
82
 * Defining InsertMedia object to manage the sequence of actions involved in
83
 * inserting a media element into the WYSIWYG.
84
 * Keeps track of the WYSIWYG instance id.
85
 */
86
var InsertMedia = function (instance_id) {
87
  this.instanceId = instance_id;
88
  return this;
89
};
90

    
91
InsertMedia.prototype = {
92
  /**
93
   * Prompt user to select a media item with the media browser.
94
   *
95
   * @param settings
96
   *    Settings object to pass on to the media browser.
97
   *    TODO: Determine if this is actually necessary.
98
   */
99
  prompt: function (settings) {
100
    Drupal.media.popups.mediaBrowser($.proxy(this, 'onSelect'), settings);
101
  },
102

    
103
  /**
104
   * On selection of a media item, display item's display configuration form.
105
   */
106
  onSelect: function (media_files) {
107
    this.mediaFile = media_files[0];
108
    Drupal.media.popups.mediaStyleSelector(this.mediaFile, $.proxy(this, 'insert'), {});
109
  },
110

    
111
  /**
112
   * When display config has been set, insert the placeholder markup into the
113
   * wysiwyg and generate its corresponding json macro pair to be added to the
114
   * tagmap.
115
   */
116
  insert: function (formatted_media) {
117
    var element = Drupal.media.filter.create_element(formatted_media.html, {
118
          fid: this.mediaFile.fid,
119
          view_mode: formatted_media.type,
120
          attributes: this.mediaFile.attributes,
121
          fields: formatted_media.options
122
        });
123
    // Get the markup and register it for the macro / placeholder handling.
124
    var markup = Drupal.media.filter.getWysiwygHTML(element);
125

    
126
    // Insert placeholder markup into wysiwyg.
127
    Drupal.wysiwyg.instances[this.instanceId].insert(markup);
128
  }
129
};
130

    
131
/** Helper functions */
132

    
133
/**
134
 * Ensures the tag map has been initialized.
135
 */
136
function ensure_tagmap () {
137
  return Drupal.media.filter.ensure_tagmap();
138
}
139

    
140
/**
141
 * Serializes file information as a url-encoded JSON object and stores it as a
142
 * data attribute on the html element.
143
 *
144
 * @param html (string)
145
 *    A html element to be used to represent the inserted media element.
146
 * @param info (object)
147
 *    A object containing the media file information (fid, view_mode, etc).
148
 *
149
 * @deprecated
150
 */
151
function create_element (html, info) {
152
  return Drupal.media.filter.create_element(html, info);
153
}
154

    
155
/**
156
 * Create a macro representation of the inserted media element.
157
 *
158
 * @param element (jQuery object)
159
 *    A media element with attached serialized file info.
160
 *
161
 * @deprecated
162
 */
163
function create_macro (element) {
164
  return Drupal.media.filter.create_macro(element);
165
}
166

    
167
/**
168
 * Extract the file info from a WYSIWYG placeholder element as JSON.
169
 *
170
 * @param element (jQuery object)
171
 *    A media element with attached serialized file info.
172
 *
173
 * @deprecated
174
 */
175
function extract_file_info (element) {
176
  return Drupal.media.filter.extract_file_info(element);
177
}
178

    
179
/**
180
 * Gets the HTML content of an element.
181
 *
182
 * @param element (jQuery object)
183
 *
184
 * @deprecated
185
 */
186
function outerHTML (element) {
187
  return Drupal.media.filter.outerHTML(element);
188
}
189

    
190
})(jQuery);