PageRenderTime 45ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/all/modules/media/js/media.popups.js

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