Projet

Général

Profil

Paste
Télécharger (13,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / media / js / media.popups.js @ 31a5a6d6

1 85ad3d82 Assos Assos
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 0ccfec7f Assos Assos
 *  Launches the style selection form where the user can choose what
10
 *  format/style they want their media in.
11 85ad3d82 Assos Assos
 */
12
13
(function ($) {
14
namespace('Drupal.media.popups');
15
16
/**
17
 * Media browser popup. Creates a media browser dialog.
18
 *
19
 * @param {function}
20 0ccfec7f Assos Assos
 *   onSelect Callback for when dialog is closed, received (Array media, Object
21
 *   extra);
22 85ad3d82 Assos Assos
 * @param {Object}
23 0ccfec7f Assos Assos
 *   globalOptions Global options that will get passed upon initialization of
24
 *   the browser. @see Drupal.media.popups.mediaBrowser.getDefaults();
25 85ad3d82 Assos Assos
 * @param {Object}
26 0ccfec7f Assos Assos
 *   pluginOptions Options for specific plugins. These are passed to the plugin
27
 *   upon initialization.  If a function is passed here as a callback, it is
28
 *   obviously not passed, but is accessible to the plugin in
29
 *   Drupal.settings.variables. Example:
30
 *   pluginOptions = {library: {url_include_patterns:'/foo/bar'}};
31 85ad3d82 Assos Assos
 * @param {Object}
32 0ccfec7f Assos Assos
 *   widgetOptions Options controlling the appearance and behavior of the modal
33
 *   dialog. @see Drupal.media.popups.mediaBrowser.getDefaults();
34 85ad3d82 Assos Assos
 */
35
Drupal.media.popups.mediaBrowser = function (onSelect, globalOptions, pluginOptions, widgetOptions) {
36 0ccfec7f Assos Assos
  // Get default dialog options.
37 85ad3d82 Assos Assos
  var options = Drupal.media.popups.mediaBrowser.getDefaults();
38 0ccfec7f Assos Assos
39
  // Add global, plugin and widget options.
40 85ad3d82 Assos Assos
  options.global = $.extend({}, options.global, globalOptions);
41
  options.plugins = pluginOptions;
42
  options.widget = $.extend({}, options.widget, widgetOptions);
43
44 0ccfec7f Assos Assos
  // Find the URL of the modal iFrame.
45 85ad3d82 Assos Assos
  var browserSrc = options.widget.src;
46 0ccfec7f Assos Assos
47 85ad3d82 Assos Assos
  if ($.isArray(browserSrc) && browserSrc.length) {
48
    browserSrc = browserSrc[browserSrc.length - 1];
49
  }
50 0ccfec7f Assos Assos
51
  // Create an array of parameters to send along to the iFrame.
52 85ad3d82 Assos Assos
  var params = {};
53 0ccfec7f Assos Assos
54
  // Add global field widget settings and plugin information.
55 85ad3d82 Assos Assos
  $.extend(params, options.global);
56
  params.plugins = options.plugins;
57
58 0ccfec7f Assos Assos
  // Append the list of parameters to the iFrame URL as query parameters.
59 85ad3d82 Assos Assos
  browserSrc += '&' + $.param(params);
60 ca0757b9 Assos Assos
61 0ccfec7f Assos Assos
  // Create an iFrame with the iFrame URL.
62
  var mediaIframe = Drupal.media.popups.getPopupIframe(browserSrc, 'mediaBrowser');
63 85ad3d82 Assos Assos
64 0ccfec7f Assos Assos
  // Attach an onLoad event.
65
  mediaIframe.bind('load', options, options.widget.onLoad);
66 85ad3d82 Assos Assos
67 0ccfec7f Assos Assos
  // Create an array of Dialog options.
68 85ad3d82 Assos Assos
  var dialogOptions = options.dialog;
69
70 0ccfec7f Assos Assos
  // Setup the dialog buttons.
71
  var ok = Drupal.t('OK');
72
  var notSelected = Drupal.t('You have not selected anything!');
73
74 85ad3d82 Assos Assos
  dialogOptions.buttons[ok] = function () {
75 0ccfec7f Assos Assos
    // Find the current file selection.
76 85ad3d82 Assos Assos
    var selected = this.contentWindow.Drupal.media.browser.selectedMedia;
77 0ccfec7f Assos Assos
78
    // Alert the user if a selection has yet to be made.
79 85ad3d82 Assos Assos
    if (selected.length < 1) {
80
      alert(notSelected);
81 0ccfec7f Assos Assos
82 85ad3d82 Assos Assos
      return;
83
    }
84 0ccfec7f Assos Assos
85
    // Select the file.
86 85ad3d82 Assos Assos
    onSelect(selected);
87 0ccfec7f Assos Assos
88
    // Close the dialog.
89
    $(this).dialog('close');
90 85ad3d82 Assos Assos
  };
91
92 0ccfec7f Assos Assos
  // Create a jQuery UI dialog with the given options.
93 ca0757b9 Assos Assos
  var dialog = mediaIframe.dialog(dialogOptions);
94
95 0ccfec7f Assos Assos
  // Allow the dialog to react to re-sizing, scrolling, etc.
96 ca0757b9 Assos Assos
  Drupal.media.popups.sizeDialog(dialog);
97
  Drupal.media.popups.resizeDialog(dialog);
98
  Drupal.media.popups.scrollDialog(dialog);
99
  Drupal.media.popups.overlayDisplace(dialog.parents(".ui-dialog"));
100 85ad3d82 Assos Assos
101
  return mediaIframe;
102
};
103
104 0ccfec7f Assos Assos
/**
105
 * Retrieves a list of default settings for the media browser.
106
 *
107
 * @return
108
 *   An array of default settings.
109
 */
110 85ad3d82 Assos Assos
Drupal.media.popups.mediaBrowser.getDefaults = function () {
111
  return {
112
    global: {
113
      types: [], // Types to allow, defaults to all.
114
      activePlugins: [] // If provided, a list of plugins which should be enabled.
115
    },
116
    widget: { // Settings for the actual iFrame which is launched.
117
      src: Drupal.settings.media.browserUrl, // Src of the media browser (if you want to totally override it)
118
      onLoad: Drupal.media.popups.mediaBrowser.mediaBrowserOnLoad // Onload function when iFrame loads.
119
    },
120
    dialog: Drupal.media.popups.getDialogOptions()
121
  };
122
};
123
124 0ccfec7f Assos Assos
/**
125
 * Sets up the iFrame buttons.
126
 */
127
Drupal.media.popups.mediaBrowser.mediaBrowserOnLoad = function (e) {
128
  var options = e.data;
129
130
  // Ensure that the iFrame is defined.
131 e013fa40 Assos Assos
  if (this.contentWindow.Drupal.media.browser == undefined) {
132 0ccfec7f Assos Assos
    return;
133
  }
134
135
  // Check if a selection has been made and press the 'ok' button.
136
  if (this.contentWindow.Drupal.media.browser.selectedMedia.length > 0) {
137
    var ok = Drupal.t('OK');
138
    var ok_func = $(this).dialog('option', 'buttons')[ok];
139
140
    ok_func.call(this);
141
142
    return;
143
  }
144
};
145
146
/**
147
 * Finalizes the selection of a file.
148
 *
149
 * Alerts the user if a selection has yet to be made, triggers the file
150
 * selection and closes the modal dialog.
151
 */
152 85ad3d82 Assos Assos
Drupal.media.popups.mediaBrowser.finalizeSelection = function () {
153 0ccfec7f Assos Assos
  // Find the current file selection.
154 85ad3d82 Assos Assos
  var selected = this.contentWindow.Drupal.media.browser.selectedMedia;
155 0ccfec7f Assos Assos
156
  // Alert the user if a selection has yet to be made.
157 85ad3d82 Assos Assos
  if (selected.length < 1) {
158
    alert(notSelected);
159 0ccfec7f Assos Assos
160 85ad3d82 Assos Assos
    return;
161
  }
162 0ccfec7f Assos Assos
163
  // Select the file.
164 85ad3d82 Assos Assos
  onSelect(selected);
165 0ccfec7f Assos Assos
166
  // Close the dialog.
167
  $(this).dialog('close');
168
};
169 85ad3d82 Assos Assos
170
/**
171
 * Style chooser Popup. Creates a dialog for a user to choose a media style.
172
 *
173
 * @param mediaFile
174 0ccfec7f Assos Assos
 *   The mediaFile you are requesting this formatting form for.
175
 *   @todo: should this be fid? That's actually all we need now.
176 85ad3d82 Assos Assos
 *
177
 * @param Function
178 0ccfec7f Assos Assos
 *   onSubmit Function to be called when the user chooses a media style. Takes
179
 *   one parameter (Object formattedMedia).
180 85ad3d82 Assos Assos
 *
181
 * @param Object
182 0ccfec7f Assos Assos
 *   options Options for the mediaStyleChooser dialog.
183 85ad3d82 Assos Assos
 */
184
Drupal.media.popups.mediaStyleSelector = function (mediaFile, onSelect, options) {
185
  var defaults = Drupal.media.popups.mediaStyleSelector.getDefaults();
186 0ccfec7f Assos Assos
187 85ad3d82 Assos Assos
  // @todo: remove this awful hack :(
188 ca0757b9 Assos Assos
  if (typeof defaults.src === 'string' ) {
189
    defaults.src = defaults.src.replace('-media_id-', mediaFile.fid) + '&fields=' + encodeURIComponent(JSON.stringify(mediaFile.fields));
190
  }
191
  else {
192
    var src = defaults.src.shift();
193 0ccfec7f Assos Assos
194 ca0757b9 Assos Assos
    defaults.src.unshift(src);
195
    defaults.src = src.replace('-media_id-', mediaFile.fid) + '&fields=' + encodeURIComponent(JSON.stringify(mediaFile.fields));
196
  }
197 0ccfec7f Assos Assos
198 85ad3d82 Assos Assos
  options = $.extend({}, defaults, options);
199
200 0ccfec7f Assos Assos
  // Create an iFrame with the iFrame URL.
201
  var mediaIframe = Drupal.media.popups.getPopupIframe(options.src, 'mediaStyleSelector');
202 85ad3d82 Assos Assos
203 0ccfec7f Assos Assos
  // Attach an onLoad event.
204
  mediaIframe.bind('load', options, options.onLoad);
205 85ad3d82 Assos Assos
206 0ccfec7f Assos Assos
  // Create an array of Dialog options.
207 85ad3d82 Assos Assos
  var dialogOptions = Drupal.media.popups.getDialogOptions();
208
209 0ccfec7f Assos Assos
  // Setup the dialog buttons.
210
  var ok = Drupal.t('OK');
211
  var notSelected = Drupal.t('Very sorry, there was an unknown error embedding media.');
212 85ad3d82 Assos Assos
213 0ccfec7f Assos Assos
  dialogOptions.buttons[ok] = function () {
214
    // Find the current file selection.
215 85ad3d82 Assos Assos
    var formattedMedia = this.contentWindow.Drupal.media.formatForm.getFormattedMedia();
216 d3a59f55 Assos Assos
    formattedMedia.options = $.extend({}, mediaFile.attributes, formattedMedia.options);
217 0ccfec7f Assos Assos
218
    // Alert the user if a selection has yet to be made.
219 85ad3d82 Assos Assos
    if (!formattedMedia) {
220
      alert(notSelected);
221 0ccfec7f Assos Assos
222 85ad3d82 Assos Assos
      return;
223
    }
224 0ccfec7f Assos Assos
225
    // Select the file.
226 85ad3d82 Assos Assos
    onSelect(formattedMedia);
227 0ccfec7f Assos Assos
228
    // Close the dialog.
229
    $(this).dialog('close');
230 85ad3d82 Assos Assos
  };
231
232 0ccfec7f Assos Assos
  // Create a jQuery UI dialog with the given options.
233 ca0757b9 Assos Assos
  var dialog = mediaIframe.dialog(dialogOptions);
234
235 0ccfec7f Assos Assos
  // Allow the dialog to react to re-sizing, scrolling, etc.
236 ca0757b9 Assos Assos
  Drupal.media.popups.sizeDialog(dialog);
237
  Drupal.media.popups.resizeDialog(dialog);
238
  Drupal.media.popups.scrollDialog(dialog);
239
  Drupal.media.popups.overlayDisplace(dialog.parents(".ui-dialog"));
240 85ad3d82 Assos Assos
241
  return mediaIframe;
242
};
243
244
Drupal.media.popups.mediaStyleSelector.mediaBrowserOnLoad = function (e) {
245
};
246
247
Drupal.media.popups.mediaStyleSelector.getDefaults = function () {
248
  return {
249
    src: Drupal.settings.media.styleSelectorUrl,
250
    onLoad: Drupal.media.popups.mediaStyleSelector.mediaBrowserOnLoad
251
  };
252
};
253
254
/**
255
 * Style chooser Popup. Creates a dialog for a user to choose a media style.
256
 *
257
 * @param mediaFile
258 0ccfec7f Assos Assos
 *   The mediaFile you are requesting this formatting form for.
259
 *   @todo: should this be fid? That's actually all we need now.
260 85ad3d82 Assos Assos
 *
261
 * @param Function
262 0ccfec7f Assos Assos
 *   onSubmit Function to be called when the user chooses a media style. Takes
263
 *   one parameter (Object formattedMedia).
264 85ad3d82 Assos Assos
 *
265
 * @param Object
266 0ccfec7f Assos Assos
 *   options Options for the mediaStyleChooser dialog.
267 85ad3d82 Assos Assos
 */
268
Drupal.media.popups.mediaFieldEditor = function (fid, onSelect, options) {
269
  var defaults = Drupal.media.popups.mediaFieldEditor.getDefaults();
270 0ccfec7f Assos Assos
271 85ad3d82 Assos Assos
  // @todo: remove this awful hack :(
272
  defaults.src = defaults.src.replace('-media_id-', fid);
273
  options = $.extend({}, defaults, options);
274
275 0ccfec7f Assos Assos
  // Create an iFrame with the iFrame URL.
276
  var mediaIframe = Drupal.media.popups.getPopupIframe(options.src, 'mediaFieldEditor');
277 85ad3d82 Assos Assos
278 0ccfec7f Assos Assos
  // Attach an onLoad event.
279
  mediaIframe.bind('load', options, options.onLoad);
280 85ad3d82 Assos Assos
281 0ccfec7f Assos Assos
  // Create an array of Dialog options.
282 85ad3d82 Assos Assos
  var dialogOptions = Drupal.media.popups.getDialogOptions();
283
284 0ccfec7f Assos Assos
  // Setup the dialog buttons.
285
  var ok = Drupal.t('OK');
286
  var notSelected = Drupal.t('Very sorry, there was an unknown error embedding media.');
287
288 85ad3d82 Assos Assos
  dialogOptions.buttons[ok] = function () {
289 0ccfec7f Assos Assos
    // Find the current file selection.
290 85ad3d82 Assos Assos
    var formattedMedia = this.contentWindow.Drupal.media.formatForm.getFormattedMedia();
291 0ccfec7f Assos Assos
292
    // Alert the user if a selection has yet to be made.
293 85ad3d82 Assos Assos
    if (!formattedMedia) {
294
      alert(notSelected);
295 0ccfec7f Assos Assos
296 85ad3d82 Assos Assos
      return;
297
    }
298 0ccfec7f Assos Assos
299
    // Select the file.
300 85ad3d82 Assos Assos
    onSelect(formattedMedia);
301 0ccfec7f Assos Assos
302
    // Close the dialog.
303
    $(this).dialog('close');
304 85ad3d82 Assos Assos
  };
305
306 0ccfec7f Assos Assos
  // Create a jQuery UI dialog with the given options.
307 ca0757b9 Assos Assos
  var dialog = mediaIframe.dialog(dialogOptions);
308
309 0ccfec7f Assos Assos
  // Allow the dialog to react to re-sizing, scrolling, etc.
310 ca0757b9 Assos Assos
  Drupal.media.popups.sizeDialog(dialog);
311
  Drupal.media.popups.resizeDialog(dialog);
312
  Drupal.media.popups.scrollDialog(dialog);
313
  Drupal.media.popups.overlayDisplace(dialog);
314 85ad3d82 Assos Assos
315
  return mediaIframe;
316
};
317
318
Drupal.media.popups.mediaFieldEditor.mediaBrowserOnLoad = function (e) {
319
320
};
321
322
Drupal.media.popups.mediaFieldEditor.getDefaults = function () {
323
  return {
324
    // @todo: do this for real
325
    src: '/media/-media_id-/edit?render=media-popup',
326
    onLoad: Drupal.media.popups.mediaFieldEditor.mediaBrowserOnLoad
327
  };
328
};
329
330
/**
331 0ccfec7f Assos Assos
 * Generic functions to both the media-browser and style selector.
332 85ad3d82 Assos Assos
 */
333
334
/**
335
 * Returns the commonly used options for the dialog.
336
 */
337
Drupal.media.popups.getDialogOptions = function () {
338
  return {
339 0ccfec7f Assos Assos
    title: Drupal.t('Media browser'),
340 85ad3d82 Assos Assos
    buttons: {},
341 ca0757b9 Assos Assos
    dialogClass: Drupal.settings.media.dialogOptions.dialogclass,
342
    modal: Drupal.settings.media.dialogOptions.modal,
343
    draggable: Drupal.settings.media.dialogOptions.draggable,
344
    resizable: Drupal.settings.media.dialogOptions.resizable,
345
    minWidth: Drupal.settings.media.dialogOptions.minwidth,
346
    width: Drupal.settings.media.dialogOptions.width,
347
    height: Drupal.settings.media.dialogOptions.height,
348
    position: Drupal.settings.media.dialogOptions.position,
349 85ad3d82 Assos Assos
    overlay: {
350 ca0757b9 Assos Assos
      backgroundColor: Drupal.settings.media.dialogOptions.overlay.backgroundcolor,
351
      opacity: Drupal.settings.media.dialogOptions.overlay.opacity
352 85ad3d82 Assos Assos
    },
353 ca0757b9 Assos Assos
    zIndex: Drupal.settings.media.dialogOptions.zindex,
354 85ad3d82 Assos Assos
    close: function (event, ui) {
355 da542b7b Assos Assos
      var elem = $(event.target);
356
      var id = elem.attr('id');
357
      if(id == 'mediaStyleSelector') {
358
        $(this).dialog("destroy");
359
        $('#mediaStyleSelector').remove();
360
      }
361
      else {
362
        $(this).dialog("destroy");
363
        $('#mediaBrowser').remove();
364
      }
365 85ad3d82 Assos Assos
    }
366
  };
367
};
368
369
/**
370
 * Get an iframe to serve as the dialog's contents. Common to both plugins.
371
 */
372
Drupal.media.popups.getPopupIframe = function (src, id, options) {
373 ca0757b9 Assos Assos
  var defaults = {width: '100%', scrolling: 'auto'};
374 85ad3d82 Assos Assos
  var options = $.extend({}, defaults, options);
375
376
  return $('<iframe class="media-modal-frame"/>')
377
  .attr('src', src)
378
  .attr('width', options.width)
379
  .attr('id', id)
380
  .attr('scrolling', options.scrolling);
381
};
382
383
Drupal.media.popups.overlayDisplace = function (dialog) {
384
  if (parent.window.Drupal.overlay && jQuery.isFunction(parent.window.Drupal.overlay.getDisplacement)) {
385
    var overlayDisplace = parent.window.Drupal.overlay.getDisplacement('top');
386 0ccfec7f Assos Assos
387 85ad3d82 Assos Assos
    if (dialog.offset().top < overlayDisplace) {
388
      dialog.css('top', overlayDisplace);
389
    }
390
  }
391
}
392
393 ca0757b9 Assos Assos
/**
394
 * Size the dialog when it is first loaded and keep it centered when scrolling.
395
 *
396
 * @param jQuery dialogElement
397
 *  The element which has .dialog() attached to it.
398
 */
399
Drupal.media.popups.sizeDialog = function (dialogElement) {
400
  if (!dialogElement.is(':visible')) {
401
    return;
402
  }
403 0ccfec7f Assos Assos
404 ca0757b9 Assos Assos
  var windowWidth = $(window).width();
405
  var dialogWidth = windowWidth * 0.8;
406
  var windowHeight = $(window).height();
407
  var dialogHeight = windowHeight * 0.8;
408
409
  dialogElement.dialog("option", "width", dialogWidth);
410
  dialogElement.dialog("option", "height", dialogHeight);
411
  dialogElement.dialog("option", "position", 'center');
412
413
  $('.media-modal-frame').width('100%');
414
}
415
416
/**
417
 * Resize the dialog when the window changes.
418
 *
419
 * @param jQuery dialogElement
420
 *  The element which has .dialog() attached to it.
421
 */
422
Drupal.media.popups.resizeDialog = function (dialogElement) {
423
  $(window).resize(function() {
424
    Drupal.media.popups.sizeDialog(dialogElement);
425
  });
426
}
427
428
/**
429
 * Keeps the dialog centered when the window is scrolled.
430
 *
431
 * @param jQuery dialogElement
432
 *  The element which has .dialog() attached to it.
433
 */
434
Drupal.media.popups.scrollDialog = function (dialogElement) {
435
  // Keep the dialog window centered when scrolling.
436
  $(window).scroll(function() {
437
    if (!dialogElement.is(':visible')) {
438
      return;
439
    }
440 0ccfec7f Assos Assos
441 ca0757b9 Assos Assos
    dialogElement.dialog("option", "position", 'center');
442
  });
443
}
444
445 85ad3d82 Assos Assos
})(jQuery);