media.browser.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. (function ($) {
  2. namespace('Drupal.media.browser');
  3. Drupal.media.browser.selectedMedia = [];
  4. Drupal.media.browser.mediaAdded = function () {};
  5. Drupal.media.browser.selectionFinalized = function (selectedMedia) {
  6. // This is intended to be overridden if a callee wants to be triggered
  7. // when the media selection is finalized from inside the browser.
  8. // This is used for the file upload form for instance.
  9. };
  10. Drupal.behaviors.experimentalMediaBrowser = {
  11. attach: function (context) {
  12. if (Drupal.settings.media.selectedMedia) {
  13. Drupal.media.browser.selectMedia(Drupal.settings.media.selectedMedia);
  14. // Fire a confirmation of some sort.
  15. Drupal.media.browser.finalizeSelection();
  16. }
  17. $('#media-browser-tabset').tabs({
  18. show: Drupal.media.browser.resizeIframe
  19. });
  20. $('.media-browser-tab').each( Drupal.media.browser.validateButtons );
  21. }
  22. // Wait for additional params to be passed in.
  23. };
  24. Drupal.media.browser.launch = function () {
  25. };
  26. Drupal.media.browser.validateButtons = function() {
  27. // The media browser runs in an IFRAME. The Drupal.media.popups.mediaBrowser()
  28. // function sets up the IFRAME and "OK" and "Cancel" buttons that are outside
  29. // of the IFRAME, so that their click handlers can destroy the IFRAME while
  30. // retaining information about what media items were selected. However,
  31. // Drupal UI convention is to place all action buttons on the same "line"
  32. // at the bottom of the form, so if the form within the IFRAME contains a
  33. // "Submit" button or other action buttons, then the "OK" and "Cancel"
  34. // buttons below the IFRAME break this convention and confuse the user.
  35. // Therefore, we add "Submit" and "Cancel" buttons inside the IFRAME, and
  36. // have their click action trigger the click action of the corresonding
  37. // "OK" and "Cancel" buttons that are outside the IFRAME. media.css contains
  38. // CSS rules that hide the outside buttons.
  39. //
  40. // We don't add a "Submit" button if the form already has one, since in these
  41. // cases, another round-trip to the server is needed before the user's
  42. // selection is finalized. For these cases, when the form's real Submit
  43. // button is clicked, the server either returns another form for the user to
  44. // fill out, or else a completion page that contains or sets the
  45. // Drupal.media.browser.selectedMedia variable. If the latter, then
  46. // Drupal.media.popups.mediaBrowser.mediaBrowserOnLoad() auto-triggers the
  47. // "OK" button action to finalize the selection and remove the IFRAME.
  48. //
  49. // @todo An alternate, less hacky solution would be most welcome.
  50. if (!($('.form-submit', this).length > 0)) {
  51. $('<a class="button button-yes fake-ok">' + Drupal.t('Submit') + '</a>').appendTo(this).bind('click', Drupal.media.browser.submit);
  52. if (!($('.fake-cancel', this).length > 0)) {
  53. $('<a class="button button-no fake-cancel">' + Drupal.t('Cancel') + '</a>').appendTo(this).bind('click', Drupal.media.browser.submit);
  54. }
  55. } else if (!($('.fake-cancel', this).length > 0)) {
  56. var parent = $('.form-actions', this);
  57. if (!parent.length) {
  58. parent = $('form > div', this);
  59. }
  60. $('<a class="button button-no fake-cancel">' + Drupal.t('Cancel') + '</a>').appendTo(parent).bind('click', Drupal.media.browser.submit);
  61. }
  62. };
  63. Drupal.media.browser.submit = function () {
  64. // @see Drupal.media.browser.validateButtons().
  65. var buttons = $(parent.window.document.body).find('#mediaBrowser').parent('.ui-dialog').find('.ui-dialog-buttonpane button');
  66. if ($(this).hasClass('fake-cancel')) {
  67. buttons[1].click();
  68. } else {
  69. buttons[0].click();
  70. }
  71. }
  72. Drupal.media.browser.selectMedia = function (selectedMedia) {
  73. Drupal.media.browser.selectedMedia = selectedMedia;
  74. };
  75. Drupal.media.browser.finalizeSelection = function () {
  76. if (!Drupal.media.browser.selectedMedia) {
  77. throw new exception(Drupal.t('Cannot continue, nothing selected'));
  78. }
  79. else {
  80. Drupal.media.browser.selectionFinalized(Drupal.media.browser.selectedMedia);
  81. }
  82. };
  83. Drupal.media.browser.resizeIframe = function (event) {
  84. var h = $('body').height();
  85. $(parent.window.document).find('#mediaBrowser').height(h);
  86. };
  87. }(jQuery));