media_library.widget.es6.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * @file media_library.widget.es6.js
  3. */
  4. (($, Drupal, Sortable) => {
  5. /**
  6. * Allows users to re-order their selection with drag+drop.
  7. *
  8. * @type {Drupal~behavior}
  9. *
  10. * @prop {Drupal~behaviorAttach} attach
  11. * Attaches behavior to re-order selected media items.
  12. */
  13. Drupal.behaviors.MediaLibraryWidgetSortable = {
  14. attach(context) {
  15. // Allow media items to be re-sorted with drag+drop in the widget.
  16. const selection = context.querySelectorAll('.js-media-library-selection');
  17. selection.forEach(widget => {
  18. Sortable.create(widget, {
  19. draggable: '.js-media-library-item',
  20. handle: '.js-media-library-item-preview',
  21. onEnd: () => {
  22. $(widget)
  23. .children()
  24. .each((index, child) => {
  25. $(child)
  26. .find('.js-media-library-item-weight')
  27. .val(index);
  28. });
  29. },
  30. });
  31. });
  32. },
  33. };
  34. /**
  35. * Allows selection order to be set without drag+drop for accessibility.
  36. *
  37. * @type {Drupal~behavior}
  38. *
  39. * @prop {Drupal~behaviorAttach} attach
  40. * Attaches behavior to toggle the weight field for media items.
  41. */
  42. Drupal.behaviors.MediaLibraryWidgetToggleWeight = {
  43. attach(context) {
  44. const strings = {
  45. show: Drupal.t('Show media item weights'),
  46. hide: Drupal.t('Hide media item weights'),
  47. };
  48. $('.js-media-library-widget-toggle-weight', context)
  49. .once('media-library-toggle')
  50. .on('click', e => {
  51. e.preventDefault();
  52. $(e.currentTarget)
  53. .toggleClass('active')
  54. .text(
  55. $(e.currentTarget).hasClass('active')
  56. ? strings.hide
  57. : strings.show,
  58. )
  59. .closest('.js-media-library-widget')
  60. .find('.js-media-library-item-weight')
  61. .parent()
  62. .toggle();
  63. })
  64. .text(strings.show);
  65. $('.js-media-library-item-weight', context)
  66. .once('media-library-toggle')
  67. .parent()
  68. .hide();
  69. },
  70. };
  71. /**
  72. * Disable the open button when the user is not allowed to add more items.
  73. *
  74. * @type {Drupal~behavior}
  75. *
  76. * @prop {Drupal~behaviorAttach} attach
  77. * Attaches behavior to disable the media library open button.
  78. */
  79. Drupal.behaviors.MediaLibraryWidgetDisableButton = {
  80. attach(context) {
  81. // When the user returns from the modal to the widget, we want to shift
  82. // the focus back to the open button. If the user is not allowed to add
  83. // more items, the button needs to be disabled. Since we can't shift the
  84. // focus to disabled elements, the focus is set back to the open button
  85. // via JavaScript by adding the 'data-disabled-focus' attribute.
  86. $('.js-media-library-open-button[data-disabled-focus="true"]', context)
  87. .once('media-library-disable')
  88. .each(function() {
  89. $(this).focus();
  90. // There is a small delay between the focus set by the browser and the
  91. // focus of screen readers. We need to give screen readers time to
  92. // shift the focus as well before the button is disabled.
  93. setTimeout(() => {
  94. $(this).attr('disabled', 'disabled');
  95. }, 50);
  96. });
  97. },
  98. };
  99. })(jQuery, Drupal, Sortable);