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