summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/toolbar/js/views/MenuVisualView.js
blob: d230d4dfa7cf5b75a7359326e6e517f2ee366085 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
 * @file
 * A Backbone view for the collapsible menus.
 */

(function ($, Backbone, Drupal) {
  Drupal.toolbar.MenuVisualView = Backbone.View.extend(
    /** @lends Drupal.toolbar.MenuVisualView# */ {
      /**
       * Backbone View for collapsible menus.
       *
       * @constructs
       *
       * @augments Backbone.View
       */
      initialize() {
        this.listenTo(this.model, 'change:subtrees', this.render);

        // Render the view immediately on initialization.
        this.render();
      },

      /**
       * {@inheritdoc}
       */
      render() {
        this.renderVertical();
        this.renderHorizontal();
      },

      /**
       * Renders the toolbar menu in horizontal mode.
       */
      renderHorizontal() {
        // Render horizontal.
        if ('drupalToolbarMenu' in $.fn) {
          this.$el.children('.toolbar-menu').drupalToolbarMenuHorizontal();
        }
      },

      /**
       * Renders the toolbar menu in vertical mode.
       */
      renderVertical() {
        const subtrees = this.model.get('subtrees');

        // Rendering the vertical menu depends on the subtrees.
        if (!this.model.get('subtrees')) {
          return;
        }

        // Add subtrees.
        Object.keys(subtrees || {}).forEach((id) => {
          $(
            once('toolbar-subtrees', this.$el.find(`#toolbar-link-${id}`)),
          ).after(subtrees[id]);
        });
        // Render the main menu as a nested, collapsible accordion.
        if ('drupalToolbarMenu' in $.fn) {
          this.$el.children('.toolbar-menu').drupalToolbarMenu();
        }
      },
    },
  );
})(jQuery, Backbone, Drupal);