media_library.view.es6.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * @file media_library.view.es6.js
  3. */
  4. (($, Drupal) => {
  5. /**
  6. * Adds hover effect to media items.
  7. */
  8. Drupal.behaviors.MediaLibraryHover = {
  9. attach(context) {
  10. $(
  11. '.media-library-item .js-click-to-select-trigger,.media-library-item .js-click-to-select-checkbox',
  12. context,
  13. )
  14. .once('media-library-item-hover')
  15. .on('mouseover mouseout', ({ currentTarget, type }) => {
  16. $(currentTarget)
  17. .closest('.media-library-item')
  18. .toggleClass('is-hover', type === 'mouseover');
  19. });
  20. },
  21. };
  22. /**
  23. * Adds focus effect to media items.
  24. */
  25. Drupal.behaviors.MediaLibraryFocus = {
  26. attach(context) {
  27. $('.media-library-item .js-click-to-select-checkbox input', context)
  28. .once('media-library-item-focus')
  29. .on('focus blur', ({ currentTarget, type }) => {
  30. $(currentTarget)
  31. .closest('.media-library-item')
  32. .toggleClass('is-focus', type === 'focus');
  33. });
  34. },
  35. };
  36. /**
  37. * Adds checkbox to select all items in the library.
  38. */
  39. Drupal.behaviors.MediaLibrarySelectAll = {
  40. attach(context) {
  41. const $view = $('.media-library-view', context).once(
  42. 'media-library-select-all',
  43. );
  44. if ($view.length && $view.find('.media-library-item').length) {
  45. const $checkbox = $(
  46. '<input type="checkbox" class="form-checkbox" />',
  47. ).on('click', ({ currentTarget }) => {
  48. // Toggle all checkboxes.
  49. const $checkboxes = $(currentTarget)
  50. .closest('.media-library-view')
  51. .find('.media-library-item input[type="checkbox"]');
  52. $checkboxes
  53. .prop('checked', $(currentTarget).prop('checked'))
  54. .trigger('change');
  55. // Announce the selection.
  56. const announcement = $(currentTarget).prop('checked')
  57. ? Drupal.t('Zero items selected')
  58. : Drupal.t('All @count items selected', {
  59. '@count': $checkboxes.length,
  60. });
  61. Drupal.announce(announcement);
  62. });
  63. const $label = $(
  64. '<label class="media-library-select-all"></label>',
  65. ).text(Drupal.t('Select all media'));
  66. $label.prepend($checkbox);
  67. $view
  68. .find('.media-library-item')
  69. .first()
  70. .before($label);
  71. }
  72. },
  73. };
  74. })(jQuery, Drupal);