media.popups.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /**
  2. * @file: Popup dialog interfaces for the media project.
  3. *
  4. * Drupal.media.popups.mediaBrowser
  5. * Launches the media browser which allows users to pick a piece of media.
  6. *
  7. * Drupal.media.popups.mediaStyleSelector
  8. * Launches the style selection form where the user can choose
  9. * what format / style they want their media in.
  10. *
  11. */
  12. (function ($) {
  13. namespace('Drupal.media.popups');
  14. /**
  15. * Media browser popup. Creates a media browser dialog.
  16. *
  17. * @param {function}
  18. * onSelect Callback for when dialog is closed, received (Array
  19. * media, Object extra);
  20. * @param {Object}
  21. * globalOptions Global options that will get passed upon initialization of the browser.
  22. * @see Drupal.media.popups.mediaBrowser.getDefaults();
  23. *
  24. * @param {Object}
  25. * pluginOptions Options for specific plugins. These are passed
  26. * to the plugin upon initialization. If a function is passed here as
  27. * a callback, it is obviously not passed, but is accessible to the plugin
  28. * in Drupal.settings.variables.
  29. *
  30. * Example
  31. * pluginOptions = {library: {url_include_patterns:'/foo/bar'}};
  32. *
  33. * @param {Object}
  34. * widgetOptions Options controlling the appearance and behavior of the
  35. * modal dialog.
  36. * @see Drupal.media.popups.mediaBrowser.getDefaults();
  37. */
  38. Drupal.media.popups.mediaBrowser = function (onSelect, globalOptions, pluginOptions, widgetOptions) {
  39. var options = Drupal.media.popups.mediaBrowser.getDefaults();
  40. options.global = $.extend({}, options.global, globalOptions);
  41. options.plugins = pluginOptions;
  42. options.widget = $.extend({}, options.widget, widgetOptions);
  43. // Create it as a modal window.
  44. var browserSrc = options.widget.src;
  45. if ($.isArray(browserSrc) && browserSrc.length) {
  46. browserSrc = browserSrc[browserSrc.length - 1];
  47. }
  48. // Params to send along to the iframe. WIP.
  49. var params = {};
  50. $.extend(params, options.global);
  51. params.plugins = options.plugins;
  52. browserSrc += '&' + $.param(params);
  53. var mediaIframe = Drupal.media.popups.getPopupIframe(browserSrc, 'mediaBrowser');
  54. // Attach the onLoad event
  55. mediaIframe.bind('load', options, options.widget.onLoad);
  56. /**
  57. * Setting up the modal dialog
  58. */
  59. var ok = 'OK';
  60. var cancel = 'Cancel';
  61. var notSelected = 'You have not selected anything!';
  62. if (Drupal && Drupal.t) {
  63. ok = Drupal.t(ok);
  64. cancel = Drupal.t(cancel);
  65. notSelected = Drupal.t(notSelected);
  66. }
  67. // @todo: let some options come through here. Currently can't be changed.
  68. var dialogOptions = options.dialog;
  69. dialogOptions.buttons[ok] = function () {
  70. var selected = this.contentWindow.Drupal.media.browser.selectedMedia;
  71. if (selected.length < 1) {
  72. alert(notSelected);
  73. return;
  74. }
  75. onSelect(selected);
  76. $(this).dialog("destroy");
  77. $(this).remove();
  78. };
  79. dialogOptions.buttons[cancel] = function () {
  80. $(this).dialog("destroy");
  81. $(this).remove();
  82. };
  83. Drupal.media.popups.setDialogPadding(mediaIframe.dialog(dialogOptions));
  84. // Remove the title bar.
  85. mediaIframe.parents(".ui-dialog").find(".ui-dialog-titlebar").remove();
  86. Drupal.media.popups.overlayDisplace(mediaIframe.parents(".ui-dialog"));
  87. return mediaIframe;
  88. };
  89. Drupal.media.popups.mediaBrowser.mediaBrowserOnLoad = function (e) {
  90. var options = e.data;
  91. if (this.contentWindow.Drupal.media.browser.selectedMedia.length > 0) {
  92. var ok = $(this).dialog('option', 'buttons')['OK'];
  93. ok.call(this);
  94. return;
  95. }
  96. };
  97. Drupal.media.popups.mediaBrowser.getDefaults = function () {
  98. return {
  99. global: {
  100. types: [], // Types to allow, defaults to all.
  101. activePlugins: [] // If provided, a list of plugins which should be enabled.
  102. },
  103. widget: { // Settings for the actual iFrame which is launched.
  104. src: Drupal.settings.media.browserUrl, // Src of the media browser (if you want to totally override it)
  105. onLoad: Drupal.media.popups.mediaBrowser.mediaBrowserOnLoad // Onload function when iFrame loads.
  106. },
  107. dialog: Drupal.media.popups.getDialogOptions()
  108. };
  109. };
  110. Drupal.media.popups.mediaBrowser.finalizeSelection = function () {
  111. var selected = this.contentWindow.Drupal.media.browser.selectedMedia;
  112. if (selected.length < 1) {
  113. alert(notSelected);
  114. return;
  115. }
  116. onSelect(selected);
  117. $(this).dialog("destroy");
  118. $(this).remove();
  119. }
  120. /**
  121. * Style chooser Popup. Creates a dialog for a user to choose a media style.
  122. *
  123. * @param mediaFile
  124. * The mediaFile you are requesting this formatting form for.
  125. * @todo: should this be fid? That's actually all we need now.
  126. *
  127. * @param Function
  128. * onSubmit Function to be called when the user chooses a media
  129. * style. Takes one parameter (Object formattedMedia).
  130. *
  131. * @param Object
  132. * options Options for the mediaStyleChooser dialog.
  133. */
  134. Drupal.media.popups.mediaStyleSelector = function (mediaFile, onSelect, options) {
  135. var defaults = Drupal.media.popups.mediaStyleSelector.getDefaults();
  136. // @todo: remove this awful hack :(
  137. defaults.src = defaults.src.replace('-media_id-', mediaFile.fid);
  138. options = $.extend({}, defaults, options);
  139. // Create it as a modal window.
  140. var mediaIframe = Drupal.media.popups.getPopupIframe(options.src, 'mediaStyleSelector');
  141. // Attach the onLoad event
  142. mediaIframe.bind('load', options, options.onLoad);
  143. /**
  144. * Set up the button text
  145. */
  146. var ok = 'OK';
  147. var cancel = 'Cancel';
  148. var notSelected = 'Very sorry, there was an unknown error embedding media.';
  149. if (Drupal && Drupal.t) {
  150. ok = Drupal.t(ok);
  151. cancel = Drupal.t(cancel);
  152. notSelected = Drupal.t(notSelected);
  153. }
  154. // @todo: let some options come through here. Currently can't be changed.
  155. var dialogOptions = Drupal.media.popups.getDialogOptions();
  156. dialogOptions.buttons[ok] = function () {
  157. var formattedMedia = this.contentWindow.Drupal.media.formatForm.getFormattedMedia();
  158. if (!formattedMedia) {
  159. alert(notSelected);
  160. return;
  161. }
  162. onSelect(formattedMedia);
  163. $(this).dialog("destroy");
  164. $(this).remove();
  165. };
  166. dialogOptions.buttons[cancel] = function () {
  167. $(this).dialog("destroy");
  168. $(this).remove();
  169. };
  170. Drupal.media.popups.setDialogPadding(mediaIframe.dialog(dialogOptions));
  171. // Remove the title bar.
  172. mediaIframe.parents(".ui-dialog").find(".ui-dialog-titlebar").remove();
  173. Drupal.media.popups.overlayDisplace(mediaIframe.parents(".ui-dialog"));
  174. return mediaIframe;
  175. };
  176. Drupal.media.popups.mediaStyleSelector.mediaBrowserOnLoad = function (e) {
  177. };
  178. Drupal.media.popups.mediaStyleSelector.getDefaults = function () {
  179. return {
  180. src: Drupal.settings.media.styleSelectorUrl,
  181. onLoad: Drupal.media.popups.mediaStyleSelector.mediaBrowserOnLoad
  182. };
  183. };
  184. /**
  185. * Style chooser Popup. Creates a dialog for a user to choose a media style.
  186. *
  187. * @param mediaFile
  188. * The mediaFile you are requesting this formatting form for.
  189. * @todo: should this be fid? That's actually all we need now.
  190. *
  191. * @param Function
  192. * onSubmit Function to be called when the user chooses a media
  193. * style. Takes one parameter (Object formattedMedia).
  194. *
  195. * @param Object
  196. * options Options for the mediaStyleChooser dialog.
  197. */
  198. Drupal.media.popups.mediaFieldEditor = function (fid, onSelect, options) {
  199. var defaults = Drupal.media.popups.mediaFieldEditor.getDefaults();
  200. // @todo: remove this awful hack :(
  201. defaults.src = defaults.src.replace('-media_id-', fid);
  202. options = $.extend({}, defaults, options);
  203. // Create it as a modal window.
  204. var mediaIframe = Drupal.media.popups.getPopupIframe(options.src, 'mediaFieldEditor');
  205. // Attach the onLoad event
  206. // @TODO - This event is firing too early in IE on Windows 7,
  207. // - so the height being calculated is too short for the content.
  208. mediaIframe.bind('load', options, options.onLoad);
  209. /**
  210. * Set up the button text
  211. */
  212. var ok = 'OK';
  213. var cancel = 'Cancel';
  214. var notSelected = 'Very sorry, there was an unknown error embedding media.';
  215. if (Drupal && Drupal.t) {
  216. ok = Drupal.t(ok);
  217. cancel = Drupal.t(cancel);
  218. notSelected = Drupal.t(notSelected);
  219. }
  220. // @todo: let some options come through here. Currently can't be changed.
  221. var dialogOptions = Drupal.media.popups.getDialogOptions();
  222. dialogOptions.buttons[ok] = function () {
  223. alert('hell yeah');
  224. return "poo";
  225. var formattedMedia = this.contentWindow.Drupal.media.formatForm.getFormattedMedia();
  226. if (!formattedMedia) {
  227. alert(notSelected);
  228. return;
  229. }
  230. onSelect(formattedMedia);
  231. $(this).dialog("destroy");
  232. $(this).remove();
  233. };
  234. dialogOptions.buttons[cancel] = function () {
  235. $(this).dialog("destroy");
  236. $(this).remove();
  237. };
  238. Drupal.media.popups.setDialogPadding(mediaIframe.dialog(dialogOptions));
  239. // Remove the title bar.
  240. mediaIframe.parents(".ui-dialog").find(".ui-dialog-titlebar").remove();
  241. Drupal.media.popups.overlayDisplace(mediaIframe.parents(".ui-dialog"));
  242. return mediaIframe;
  243. };
  244. Drupal.media.popups.mediaFieldEditor.mediaBrowserOnLoad = function (e) {
  245. };
  246. Drupal.media.popups.mediaFieldEditor.getDefaults = function () {
  247. return {
  248. // @todo: do this for real
  249. src: '/media/-media_id-/edit?render=media-popup',
  250. onLoad: Drupal.media.popups.mediaFieldEditor.mediaBrowserOnLoad
  251. };
  252. };
  253. /**
  254. * Generic functions to both the media-browser and style selector
  255. */
  256. /**
  257. * Returns the commonly used options for the dialog.
  258. */
  259. Drupal.media.popups.getDialogOptions = function () {
  260. return {
  261. buttons: {},
  262. dialogClass: 'media-wrapper',
  263. modal: true,
  264. draggable: false,
  265. resizable: false,
  266. minWidth: 600,
  267. width: 800,
  268. height: 550,
  269. position: 'center',
  270. overlay: {
  271. backgroundColor: '#000000',
  272. opacity: 0.4
  273. }
  274. };
  275. };
  276. /**
  277. * Created padding on a dialog
  278. *
  279. * @param jQuery dialogElement
  280. * The element which has .dialog() attached to it.
  281. */
  282. Drupal.media.popups.setDialogPadding = function (dialogElement) {
  283. // @TODO: Perhaps remove this hardcoded reference to height.
  284. // - It's included to make IE on Windows 7 display the dialog without
  285. // collapsing. 550 is the height that displays all of the tab panes
  286. // within the Add Media overlay. This is either a bug in the jQuery
  287. // UI library, a bug in IE on Windows 7 or a bug in the way the
  288. // dialog is instantiated. Or a combo of the three.
  289. // All browsers except IE on Win7 ignore these defaults and adjust
  290. // the height of the iframe correctly to match the content in the panes
  291. dialogElement.height(dialogElement.dialog('option', 'height'));
  292. dialogElement.width(dialogElement.dialog('option', 'width'));
  293. };
  294. /**
  295. * Get an iframe to serve as the dialog's contents. Common to both plugins.
  296. */
  297. Drupal.media.popups.getPopupIframe = function (src, id, options) {
  298. var defaults = {width: '800px', scrolling: 'no'};
  299. var options = $.extend({}, defaults, options);
  300. return $('<iframe class="media-modal-frame"/>')
  301. .attr('src', src)
  302. .attr('width', options.width)
  303. .attr('id', id)
  304. .attr('scrolling', options.scrolling);
  305. };
  306. Drupal.media.popups.overlayDisplace = function (dialog) {
  307. if (parent.window.Drupal.overlay) {
  308. var overlayDisplace = parent.window.Drupal.overlay.getDisplacement('top');
  309. if (dialog.offset().top < overlayDisplace) {
  310. dialog.css('top', overlayDisplace);
  311. }
  312. }
  313. }
  314. })(jQuery);