block.admin.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * @file
  3. * Block admin behaviors.
  4. */
  5. (function ($, Drupal) {
  6. 'use strict';
  7. /**
  8. * Filters the block list by a text input search string.
  9. *
  10. * The text input will have the selector `input.block-filter-text`.
  11. *
  12. * The target element to do searching in will be in the selector
  13. * `input.block-filter-text[data-element]`
  14. *
  15. * The text source where the text should be found will have the selector
  16. * `.block-filter-text-source`
  17. *
  18. * @type {Drupal~behavior}
  19. *
  20. * @prop {Drupal~behaviorAttach} attach
  21. * Attaches the behavior for the block filtering.
  22. */
  23. Drupal.behaviors.blockFilterByText = {
  24. attach: function (context, settings) {
  25. var $input = $('input.block-filter-text').once('block-filter-text');
  26. var $table = $($input.attr('data-element'));
  27. var $filter_rows;
  28. /**
  29. * Filters the block list.
  30. *
  31. * @param {jQuery.Event} e
  32. * The jQuery event for the keyup event that triggered the filter.
  33. */
  34. function filterBlockList(e) {
  35. var query = $(e.target).val().toLowerCase();
  36. /**
  37. * Shows or hides the block entry based on the query.
  38. *
  39. * @param {number} index
  40. * The index in the loop, as provided by `jQuery.each`
  41. * @param {HTMLElement} label
  42. * The label of the block.
  43. */
  44. function toggleBlockEntry(index, label) {
  45. var $label = $(label);
  46. var $row = $label.parent().parent();
  47. var textMatch = $label.text().toLowerCase().indexOf(query) !== -1;
  48. $row.toggle(textMatch);
  49. }
  50. // Filter if the length of the query is at least 2 characters.
  51. if (query.length >= 2) {
  52. $filter_rows.each(toggleBlockEntry);
  53. }
  54. else {
  55. $filter_rows.each(function (index) {
  56. $(this).parent().parent().show();
  57. });
  58. }
  59. }
  60. if ($table.length) {
  61. $filter_rows = $table.find('div.block-filter-text-source');
  62. $input.on('keyup', filterBlockList);
  63. }
  64. }
  65. };
  66. /**
  67. * Highlights the block that was just placed into the block listing.
  68. *
  69. * @type {Drupal~behavior}
  70. *
  71. * @prop {Drupal~behaviorAttach} attach
  72. * Attaches the behavior for the block placement highlighting.
  73. */
  74. Drupal.behaviors.blockHighlightPlacement = {
  75. attach: function (context, settings) {
  76. if (settings.blockPlacement) {
  77. $(context).find('[data-drupal-selector="edit-blocks"]').once('block-highlight').each(function () {
  78. var $container = $(this);
  79. // Just scrolling the document.body will not work in Firefox. The html
  80. // element is needed as well.
  81. $('html, body').animate({
  82. scrollTop: $('.js-block-placed').offset().top - $container.offset().top + $container.scrollTop()
  83. }, 500);
  84. });
  85. }
  86. }
  87. };
  88. }(jQuery, Drupal));