Projet

Général

Profil

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

root / drupal7 / sites / all / modules / media / js / media.popups.js @ 74f6bef0

1

    
2
/**
3
 * @file: Popup dialog interfaces for the media project.
4
 *
5
 * Drupal.media.popups.mediaBrowser
6
 *   Launches the media browser which allows users to pick a piece of media.
7
 *
8
 * Drupal.media.popups.mediaStyleSelector
9
 *  Launches the style selection form where the user can choose
10
 *  what format / style they want their media in.
11
 *
12
 */
13

    
14
(function ($) {
15
namespace('Drupal.media.popups');
16

    
17
/**
18
 * Media browser popup. Creates a media browser dialog.
19
 *
20
 * @param {function}
21
 *          onSelect Callback for when dialog is closed, received (Array
22
 *          media, Object extra);
23
 * @param {Object}
24
 *          globalOptions Global options that will get passed upon initialization of the browser.
25
 *          @see Drupal.media.popups.mediaBrowser.getDefaults();
26
 *
27
 * @param {Object}
28
 *          pluginOptions Options for specific plugins. These are passed
29
 *          to the plugin upon initialization.  If a function is passed here as
30
 *          a callback, it is obviously not passed, but is accessible to the plugin
31
 *          in Drupal.settings.variables.
32
 *
33
 *          Example
34
 *          pluginOptions = {library: {url_include_patterns:'/foo/bar'}};
35
 *
36
 * @param {Object}
37
 *          widgetOptions Options controlling the appearance and behavior of the
38
 *          modal dialog.
39
 *          @see Drupal.media.popups.mediaBrowser.getDefaults();
40
 */
41
Drupal.media.popups.mediaBrowser = function (onSelect, globalOptions, pluginOptions, widgetOptions) {
42
  var options = Drupal.media.popups.mediaBrowser.getDefaults();
43
  options.global = $.extend({}, options.global, globalOptions);
44
  options.plugins = pluginOptions;
45
  options.widget = $.extend({}, options.widget, widgetOptions);
46

    
47
  // Create it as a modal window.
48
  var browserSrc = options.widget.src;
49
  if ($.isArray(browserSrc) && browserSrc.length) {
50
    browserSrc = browserSrc[browserSrc.length - 1];
51
  }
52
  // Params to send along to the iframe.  WIP.
53
  var params = {};
54
  $.extend(params, options.global);
55
  params.plugins = options.plugins;
56

    
57
  browserSrc += '&' + $.param(params);
58
  var mediaIframe = Drupal.media.popups.getPopupIframe(browserSrc, 'mediaBrowser');
59
  // Attach the onLoad event
60
  mediaIframe.bind('load', options, options.widget.onLoad);
61
  /**
62
   * Setting up the modal dialog
63
   */
64

    
65
  var ok = 'OK';
66
  var cancel = 'Cancel';
67
  var notSelected = 'You have not selected anything!';
68

    
69
  if (Drupal && Drupal.t) {
70
    ok = Drupal.t(ok);
71
    cancel = Drupal.t(cancel);
72
    notSelected = Drupal.t(notSelected);
73
  }
74

    
75
  // @todo: let some options come through here. Currently can't be changed.
76
  var dialogOptions = options.dialog;
77

    
78
  dialogOptions.buttons[ok] = function () {
79
    var selected = this.contentWindow.Drupal.media.browser.selectedMedia;
80
    if (selected.length < 1) {
81
      alert(notSelected);
82
      return;
83
    }
84
    onSelect(selected);
85
    $(this).dialog("close");
86
  };
87

    
88
  dialogOptions.buttons[cancel] = function () {
89
    $(this).dialog("close");
90
  };
91

    
92
  Drupal.media.popups.setDialogPadding(mediaIframe.dialog(dialogOptions));
93
  // Remove the title bar.
94
  mediaIframe.parents(".ui-dialog").find(".ui-dialog-titlebar").remove();
95
  Drupal.media.popups.overlayDisplace(mediaIframe.parents(".ui-dialog"));
96
  return mediaIframe;
97
};
98

    
99
Drupal.media.popups.mediaBrowser.mediaBrowserOnLoad = function (e) {
100
  var options = e.data;
101
  if (this.contentWindow.Drupal.media == undefined) return;
102

    
103
  if (this.contentWindow.Drupal.media.browser.selectedMedia.length > 0) {
104
    var ok = (Drupal && Drupal.t) ? Drupal.t('OK') : 'OK';
105
    var ok_func = $(this).dialog('option', 'buttons')[ok];
106
    ok_func.call(this);
107
    return;
108
  }
109
};
110

    
111
Drupal.media.popups.mediaBrowser.getDefaults = function () {
112
  return {
113
    global: {
114
      types: [], // Types to allow, defaults to all.
115
      activePlugins: [] // If provided, a list of plugins which should be enabled.
116
    },
117
    widget: { // Settings for the actual iFrame which is launched.
118
      src: Drupal.settings.media.browserUrl, // Src of the media browser (if you want to totally override it)
119
      onLoad: Drupal.media.popups.mediaBrowser.mediaBrowserOnLoad // Onload function when iFrame loads.
120
    },
121
    dialog: Drupal.media.popups.getDialogOptions()
122
  };
123
};
124

    
125
Drupal.media.popups.mediaBrowser.finalizeSelection = function () {
126
  var selected = this.contentWindow.Drupal.media.browser.selectedMedia;
127
  if (selected.length < 1) {
128
    alert(notSelected);
129
    return;
130
  }
131
  onSelect(selected);
132
  $(this).dialog("close");
133
}
134

    
135
/**
136
 * Style chooser Popup. Creates a dialog for a user to choose a media style.
137
 *
138
 * @param mediaFile
139
 *          The mediaFile you are requesting this formatting form for.
140
 *          @todo: should this be fid?  That's actually all we need now.
141
 *
142
 * @param Function
143
 *          onSubmit Function to be called when the user chooses a media
144
 *          style. Takes one parameter (Object formattedMedia).
145
 *
146
 * @param Object
147
 *          options Options for the mediaStyleChooser dialog.
148
 */
149
Drupal.media.popups.mediaStyleSelector = function (mediaFile, onSelect, options) {
150
  var defaults = Drupal.media.popups.mediaStyleSelector.getDefaults();
151
  // @todo: remove this awful hack :(
152
  defaults.src = defaults.src.replace('-media_id-', mediaFile.fid) + '&fields=' + JSON.stringify(mediaFile.fields);
153
  options = $.extend({}, defaults, options);
154
  // Create it as a modal window.
155
  var mediaIframe = Drupal.media.popups.getPopupIframe(options.src, 'mediaStyleSelector');
156
  // Attach the onLoad event
157
  mediaIframe.bind('load', options, options.onLoad);
158

    
159
  /**
160
   * Set up the button text
161
   */
162
  var ok = 'OK';
163
  var cancel = 'Cancel';
164
  var notSelected = 'Very sorry, there was an unknown error embedding media.';
165

    
166
  if (Drupal && Drupal.t) {
167
    ok = Drupal.t(ok);
168
    cancel = Drupal.t(cancel);
169
    notSelected = Drupal.t(notSelected);
170
  }
171

    
172
  // @todo: let some options come through here. Currently can't be changed.
173
  var dialogOptions = Drupal.media.popups.getDialogOptions();
174

    
175
  dialogOptions.buttons[ok] = function () {
176

    
177
    var formattedMedia = this.contentWindow.Drupal.media.formatForm.getFormattedMedia();
178
    if (!formattedMedia) {
179
      alert(notSelected);
180
      return;
181
    }
182
    onSelect(formattedMedia);
183
    $(this).dialog("close");
184
  };
185

    
186
  dialogOptions.buttons[cancel] = function () {
187
    $(this).dialog("close");
188
  };
189

    
190
  Drupal.media.popups.setDialogPadding(mediaIframe.dialog(dialogOptions));
191
  // Remove the title bar.
192
  mediaIframe.parents(".ui-dialog").find(".ui-dialog-titlebar").remove();
193
  Drupal.media.popups.overlayDisplace(mediaIframe.parents(".ui-dialog"));
194
  return mediaIframe;
195
};
196

    
197
Drupal.media.popups.mediaStyleSelector.mediaBrowserOnLoad = function (e) {
198
};
199

    
200
Drupal.media.popups.mediaStyleSelector.getDefaults = function () {
201
  return {
202
    src: Drupal.settings.media.styleSelectorUrl,
203
    onLoad: Drupal.media.popups.mediaStyleSelector.mediaBrowserOnLoad
204
  };
205
};
206

    
207

    
208
/**
209
 * Style chooser Popup. Creates a dialog for a user to choose a media style.
210
 *
211
 * @param mediaFile
212
 *          The mediaFile you are requesting this formatting form for.
213
 *          @todo: should this be fid?  That's actually all we need now.
214
 *
215
 * @param Function
216
 *          onSubmit Function to be called when the user chooses a media
217
 *          style. Takes one parameter (Object formattedMedia).
218
 *
219
 * @param Object
220
 *          options Options for the mediaStyleChooser dialog.
221
 */
222
Drupal.media.popups.mediaFieldEditor = function (fid, onSelect, options) {
223
  var defaults = Drupal.media.popups.mediaFieldEditor.getDefaults();
224
  // @todo: remove this awful hack :(
225
  defaults.src = defaults.src.replace('-media_id-', fid);
226
  options = $.extend({}, defaults, options);
227
  // Create it as a modal window.
228
  var mediaIframe = Drupal.media.popups.getPopupIframe(options.src, 'mediaFieldEditor');
229
  // Attach the onLoad event
230
  // @TODO - This event is firing too early in IE on Windows 7,
231
  // - so the height being calculated is too short for the content.
232
  mediaIframe.bind('load', options, options.onLoad);
233

    
234
  /**
235
   * Set up the button text
236
   */
237
  var ok = 'OK';
238
  var cancel = 'Cancel';
239
  var notSelected = 'Very sorry, there was an unknown error embedding media.';
240

    
241
  if (Drupal && Drupal.t) {
242
    ok = Drupal.t(ok);
243
    cancel = Drupal.t(cancel);
244
    notSelected = Drupal.t(notSelected);
245
  }
246

    
247
  // @todo: let some options come through here. Currently can't be changed.
248
  var dialogOptions = Drupal.media.popups.getDialogOptions();
249

    
250
  dialogOptions.buttons[ok] = function () {
251
    var formattedMedia = this.contentWindow.Drupal.media.formatForm.getFormattedMedia();
252
    if (!formattedMedia) {
253
      alert(notSelected);
254
      return;
255
    }
256
    onSelect(formattedMedia);
257
    $(this).dialog("close");
258
  };
259

    
260
  dialogOptions.buttons[cancel] = function () {
261
    $(this).dialog("close");
262
  };
263

    
264
  Drupal.media.popups.setDialogPadding(mediaIframe.dialog(dialogOptions));
265
  // Remove the title bar.
266
  mediaIframe.parents(".ui-dialog").find(".ui-dialog-titlebar").remove();
267
  Drupal.media.popups.overlayDisplace(mediaIframe.parents(".ui-dialog"));
268
  return mediaIframe;
269
};
270

    
271
Drupal.media.popups.mediaFieldEditor.mediaBrowserOnLoad = function (e) {
272

    
273
};
274

    
275
Drupal.media.popups.mediaFieldEditor.getDefaults = function () {
276
  return {
277
    // @todo: do this for real
278
    src: '/media/-media_id-/edit?render=media-popup',
279
    onLoad: Drupal.media.popups.mediaFieldEditor.mediaBrowserOnLoad
280
  };
281
};
282

    
283

    
284
/**
285
 * Generic functions to both the media-browser and style selector
286
 */
287

    
288
/**
289
 * Returns the commonly used options for the dialog.
290
 */
291
Drupal.media.popups.getDialogOptions = function () {
292
  return {
293
    buttons: {},
294
    dialogClass: 'media-wrapper',
295
    modal: true,
296
    draggable: false,
297
    resizable: false,
298
    minWidth: 500,
299
    width: 670,
300
    height: 280,
301
    position: 'center',
302
    overlay: {
303
      backgroundColor: '#000000',
304
      opacity: 0.4
305
    },
306
    zIndex: 10000,
307
    close: function (event, ui) {
308
      $(event.target).remove();
309
    }
310
  };
311
};
312

    
313
/**
314
 * Created padding on a dialog
315
 *
316
 * @param jQuery dialogElement
317
 *  The element which has .dialog() attached to it.
318
 */
319
Drupal.media.popups.setDialogPadding = function (dialogElement) {
320
  // @TODO: Perhaps remove this hardcoded reference to height.
321
  // - It's included to make IE on Windows 7 display the dialog without
322
  //   collapsing. 550 is the height that displays all of the tab panes
323
  //   within the Add Media overlay. This is either a bug in the jQuery
324
  //   UI library, a bug in IE on Windows 7 or a bug in the way the
325
  //   dialog is instantiated. Or a combo of the three.
326
  //   All browsers except IE on Win7 ignore these defaults and adjust
327
  //   the height of the iframe correctly to match the content in the panes
328
  dialogElement.height(dialogElement.dialog('option', 'height'));
329
  dialogElement.width(dialogElement.dialog('option', 'width'));
330
};
331

    
332
/**
333
 * Get an iframe to serve as the dialog's contents. Common to both plugins.
334
 */
335
Drupal.media.popups.getPopupIframe = function (src, id, options) {
336
  var defaults = {width: '800px', scrolling: 'auto'};
337
  var options = $.extend({}, defaults, options);
338

    
339
  return $('<iframe class="media-modal-frame"/>')
340
  .attr('src', src)
341
  .attr('width', options.width)
342
  .attr('id', id)
343
  .attr('scrolling', options.scrolling);
344
};
345

    
346
Drupal.media.popups.overlayDisplace = function (dialog) {
347
  if (parent.window.Drupal.overlay && jQuery.isFunction(parent.window.Drupal.overlay.getDisplacement)) {
348
    var overlayDisplace = parent.window.Drupal.overlay.getDisplacement('top');
349
    if (dialog.offset().top < overlayDisplace) {
350
      dialog.css('top', overlayDisplace);
351
    }
352
  }
353
}
354

    
355
})(jQuery);