media.admin.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * @file
  3. * Javascript for the interface at admin/content/media and also for interfaces
  4. * related to setting up media fields and for media type administration.
  5. *
  6. * Basically, if it's on the /admin path, it's probably here.
  7. */
  8. (function ($) {
  9. /**
  10. * Functionality for the thumbnail display
  11. */
  12. Drupal.behaviors.mediaAdmin = {
  13. attach: function (context) {
  14. // Show a javascript confirmation dialog if a user has files selected and
  15. // they try to switch between the "Thumbnail" and "List" local tasks.
  16. $('.media-display-switch a').bind('click', function () {
  17. if ($(':checkbox:checked', $('form#media-admin')).length != 0) {
  18. return confirm(Drupal.t('If you switch views, you will lose your selection.'));
  19. }
  20. });
  21. // Configure the "Add file" link to fire the media browser popup.
  22. var $launcherLink = $('<a class="media-launcher" href="#"></a>').html(Drupal.t('Add file'));
  23. $launcherLink.bind('click', function () {
  24. // This option format needs *serious* work.
  25. // Not even bothering documenting it because it needs to be thrown.
  26. // See media.browser.js and media.browser.inc - media_browser()
  27. // For how it gets passed.
  28. var options = {
  29. disabledPlugins: ['library'],
  30. multiselect: true
  31. };
  32. Drupal.media.popups.mediaBrowser(function (mediaFiles) {
  33. // When the media browser succeeds, we refresh
  34. // @TODO: Should jump to the new media file and perhaps highlight it.
  35. parent.window.location.reload();
  36. return false;
  37. }, options);
  38. });
  39. $('ul.action-links', context).prepend($('<li></li>').append($launcherLink));
  40. if ($('.media-display-thumbnails').length) {
  41. // Implements 'select all/none' for thumbnail view.
  42. // @TODO: Support grabbing more than one page of thumbnails.
  43. var allLink = $('<a href="#">' + Drupal.t('all') + '</a>')
  44. .click(function () {
  45. $('.media-display-thumbnails', $(this).parents('form')).find(':checkbox').attr('checked', true).change();
  46. return false;
  47. });
  48. var noneLink = $('<a href="#">' + Drupal.t('none') + '</a>')
  49. .click(function () {
  50. $('.media-display-thumbnails', $(this).parents('form')).find(':checkbox').attr('checked', false).change();
  51. return false;
  52. });
  53. $('<div class="media-thumbnails-select" />')
  54. .append('<strong>' + Drupal.t('Select') + ':</strong> ')
  55. .append(allLink)
  56. .append(', ')
  57. .append(noneLink)
  58. .prependTo('#media-admin > div')
  59. // If the media item is clicked anywhere other than on the image itself
  60. // check the checkbox. For the record, JS thinks this is wonky.
  61. $('.media-item').bind('click', function (e) {
  62. if ($(e.target).is('img, a')) {
  63. return;
  64. }
  65. var checkbox = $(this).parent().find(':checkbox');
  66. if (checkbox.is(':checked')) {
  67. checkbox.attr('checked', false).change();
  68. } else {
  69. checkbox.attr('checked', true).change();
  70. }
  71. });
  72. // Add an extra class to selected thumbnails.
  73. $('.media-display-thumbnails :checkbox').each(function () {
  74. var checkbox = $(this);
  75. if (checkbox.is(':checked')) {
  76. $(checkbox.parents('li').find('.media-item')).addClass('selected');
  77. }
  78. checkbox.bind('change.media', function () {
  79. if (checkbox.is(':checked')) {
  80. $(checkbox.parents('li').find('.media-item')).addClass('selected');
  81. }
  82. else {
  83. $(checkbox.parents('li').find('.media-item')).removeClass('selected');
  84. }
  85. });
  86. });
  87. }
  88. // When any checkboxes are clicked on this form check to see if any are checked.
  89. // If any checkboxes are checked, show the edit options (@todo rename to edit-actions).
  90. $('#media-admin :checkbox').bind('change', function () {
  91. Drupal.behaviors.mediaAdmin.showOrHideEditOptions();
  92. });
  93. Drupal.behaviors.mediaAdmin.showOrHideEditOptions();
  94. },
  95. // Checks if any checkboxes on the form are checked, if so it will show the
  96. // edit-options panel.
  97. showOrHideEditOptions: function() {
  98. var fieldset = $('#edit-options');
  99. if (!$('#media-admin input[type=checkbox]:checked').size()) {
  100. fieldset.slideUp('fast');
  101. }
  102. else {
  103. fieldset.slideDown('fast');
  104. }
  105. }
  106. };
  107. /**
  108. * JavaScript for the Media types administrative form.
  109. */
  110. Drupal.behaviors.mediaTypesAdmin = {
  111. attach: function (context) {
  112. if ($('.form-item-match-type', context).length == 0) {
  113. return;
  114. }
  115. // Toggle the 'other' text field on Match type.
  116. if ($('.form-item-match-type input:checked').val() != 'other') {
  117. $('.form-item-match-type-other').hide();
  118. }
  119. $('.form-item-match-type input').change(function () {
  120. if ($(this).val() == 'other') {
  121. $('.form-item-match-type-other').slideDown('fast');
  122. }
  123. else {
  124. $('.form-item-match-type-other').slideUp('fast');
  125. }
  126. });
  127. }
  128. };
  129. })(jQuery);