Projet

Général

Profil

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

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

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
   * The selected text string.
18
   */
19
  selectedText: null,
20

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

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

    
62
        // Insert new media.
63
        insert.prompt(settings.global);
64
      }
65
    }
66
  },
67

    
68
  /**
69
   * Attach function, called when a rich text editor loads.
70
   * This finds all [[tags]] and replaces them with the html
71
   * that needs to show in the editor.
72
   *
73
   * This finds all JSON macros and replaces them with the HTML placeholder
74
   * that will show in the editor.
75
   */
76
  attach: function (content, settings, instanceId) {
77
    content = Drupal.media.filter.replaceTokenWithPlaceholder(content);
78
    return content;
79
  },
80

    
81
  /**
82
   * Detach function, called when a rich text editor detaches
83
   */
84
  detach: function (content, settings, instanceId) {
85
    content = Drupal.media.filter.replacePlaceholderWithToken(content);
86
    return content;
87
  }
88
};
89
/**
90
 * Defining InsertMedia object to manage the sequence of actions involved in
91
 * inserting a media element into the WYSIWYG.
92
 * Keeps track of the WYSIWYG instance id.
93
 */
94
var InsertMedia = function (instance_id) {
95
  this.instanceId = instance_id;
96
  return this;
97
};
98

    
99
InsertMedia.prototype = {
100
  /**
101
   * Prompt user to select a media item with the media browser.
102
   *
103
   * @param settings
104
   *    Settings object to pass on to the media browser.
105
   *    TODO: Determine if this is actually necessary.
106
   */
107
  prompt: function (settings) {
108
    Drupal.media.popups.mediaBrowser($.proxy(this, 'onSelect'), settings);
109
  },
110

    
111
  /**
112
   * On selection of a media item, display item's display configuration form.
113
   */
114
  onSelect: function (media_files) {
115
    this.mediaFile = media_files[0];
116
    Drupal.media.popups.mediaStyleSelector(this.mediaFile, $.proxy(this, 'insert'), {});
117
  },
118

    
119
  /**
120
   * When display config has been set, insert the placeholder markup into the
121
   * wysiwyg and generate its corresponding json macro pair to be added to the
122
   * tagmap.
123
   */
124
  insert: function (formatted_media) {
125
    var element = Drupal.media.filter.create_element(formatted_media.html, {
126
          fid: this.mediaFile.fid,
127
          view_mode: formatted_media.type,
128
          attributes: this.mediaFile.attributes,
129
          fields: formatted_media.options,
130
          link_text: Drupal.wysiwyg.plugins.media.selectedText
131
        });
132
    // Get the markup and register it for the macro / placeholder handling.
133
    var markup = Drupal.media.filter.getWysiwygHTML(element);
134

    
135
    // Insert placeholder markup into wysiwyg.
136
    Drupal.wysiwyg.instances[this.instanceId].insert(markup);
137
  }
138
};
139

    
140
/** Helper functions */
141

    
142
/**
143
 * Ensures the tag map has been initialized.
144
 */
145
function ensure_tagmap () {
146
  return Drupal.media.filter.ensure_tagmap();
147
}
148

    
149
/**
150
 * Serializes file information as a url-encoded JSON object and stores it as a
151
 * data attribute on the html element.
152
 *
153
 * @param html (string)
154
 *    A html element to be used to represent the inserted media element.
155
 * @param info (object)
156
 *    A object containing the media file information (fid, view_mode, etc).
157
 *
158
 * @deprecated
159
 */
160
function create_element (html, info) {
161
  return Drupal.media.filter.create_element(html, info);
162
}
163

    
164
/**
165
 * Create a macro representation of the inserted media element.
166
 *
167
 * @param element (jQuery object)
168
 *    A media element with attached serialized file info.
169
 *
170
 * @deprecated
171
 */
172
function create_macro (element) {
173
  return Drupal.media.filter.create_macro(element);
174
}
175

    
176
/**
177
 * Extract the file info from a WYSIWYG placeholder element as JSON.
178
 *
179
 * @param element (jQuery object)
180
 *    A media element with attached serialized file info.
181
 *
182
 * @deprecated
183
 */
184
function extract_file_info (element) {
185
  return Drupal.media.filter.extract_file_info(element);
186
}
187

    
188
/**
189
 * Gets the HTML content of an element.
190
 *
191
 * @param element (jQuery object)
192
 *
193
 * @deprecated
194
 */
195
function outerHTML (element) {
196
  return Drupal.media.filter.outerHTML(element);
197
}
198

    
199
})(jQuery);