media_library.widget.es6.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * @file media_library.widget.js
  3. */
  4. (($, Drupal) => {
  5. /**
  6. * Allows users to re-order their selection with drag+drop.
  7. */
  8. Drupal.behaviors.MediaLibraryWidgetSortable = {
  9. attach(context) {
  10. // Allow media items to be re-sorted with drag+drop in the widget.
  11. $('.js-media-library-selection', context)
  12. .once('media-library-sortable')
  13. .sortable({
  14. tolerance: 'pointer',
  15. helper: 'clone',
  16. handle: '.js-media-library-item-preview',
  17. stop: ({ target }) => {
  18. // Update all the hidden "weight" fields.
  19. $(target)
  20. .children()
  21. .each((index, child) => {
  22. $(child)
  23. .find('.js-media-library-item-weight')
  24. .val(index);
  25. });
  26. },
  27. });
  28. },
  29. };
  30. /**
  31. * Allows selection order to be set without drag+drop for accessibility.
  32. */
  33. Drupal.behaviors.MediaLibraryWidgetToggleWeight = {
  34. attach(context) {
  35. const strings = {
  36. show: Drupal.t('Show media item weights'),
  37. hide: Drupal.t('Hide media item weights'),
  38. };
  39. $('.js-media-library-widget-toggle-weight', context)
  40. .once('media-library-toggle')
  41. .on('click', e => {
  42. e.preventDefault();
  43. $(e.currentTarget)
  44. .toggleClass('active')
  45. .text(
  46. $(e.currentTarget).hasClass('active')
  47. ? strings.hide
  48. : strings.show,
  49. )
  50. .parent()
  51. .find('.js-media-library-item-weight')
  52. .parent()
  53. .toggle();
  54. })
  55. .text(strings.show);
  56. $('.js-media-library-item-weight', context)
  57. .once('media-library-toggle')
  58. .parent()
  59. .hide();
  60. },
  61. };
  62. /**
  63. * Warn users when clicking outgoing links from the library or widget.
  64. */
  65. Drupal.behaviors.MediaLibraryWidgetWarn = {
  66. attach(context) {
  67. $('.js-media-library-item a[href]', context)
  68. .once('media-library-warn-link')
  69. .on('click', e => {
  70. const message = Drupal.t(
  71. 'Unsaved changes to the form will be lost. Are you sure you want to leave?',
  72. );
  73. const confirmation = window.confirm(message);
  74. if (!confirmation) {
  75. e.preventDefault();
  76. }
  77. });
  78. },
  79. };
  80. /**
  81. * Prevent users from selecting more items than allowed in the view.
  82. */
  83. Drupal.behaviors.MediaLibraryWidgetRemaining = {
  84. attach(context, settings) {
  85. const $view = $('.js-media-library-view', context).once(
  86. 'media-library-remaining',
  87. );
  88. $view
  89. .find('.js-media-library-item input[type="checkbox"]')
  90. .on('change', () => {
  91. if (
  92. settings.media_library &&
  93. settings.media_library.selection_remaining
  94. ) {
  95. const $checkboxes = $view.find(
  96. '.js-media-library-item input[type="checkbox"]',
  97. );
  98. if (
  99. $checkboxes.filter(':checked').length ===
  100. settings.media_library.selection_remaining
  101. ) {
  102. $checkboxes
  103. .not(':checked')
  104. .prop('disabled', true)
  105. .closest('.js-media-library-item')
  106. .addClass('media-library-item--disabled');
  107. } else {
  108. $checkboxes
  109. .prop('disabled', false)
  110. .closest('.js-media-library-item')
  111. .removeClass('media-library-item--disabled');
  112. }
  113. }
  114. });
  115. },
  116. };
  117. })(jQuery, Drupal);