7d0ae120733094b0e1f67be887e939aa706cc576
[yaffs-website] / web / core / modules / contextual / js / contextual.toolbar.es6.js
1 /**
2  * @file
3  * Attaches behaviors for the Contextual module's edit toolbar tab.
4  */
5
6 (function ($, Drupal, Backbone) {
7   const strings = {
8     tabbingReleased: Drupal.t('Tabbing is no longer constrained by the Contextual module.'),
9     tabbingConstrained: Drupal.t('Tabbing is constrained to a set of @contextualsCount and the edit mode toggle.'),
10     pressEsc: Drupal.t('Press the esc key to exit.'),
11   };
12
13   /**
14    * Initializes a contextual link: updates its DOM, sets up model and views.
15    *
16    * @param {HTMLElement} context
17    *   A contextual links DOM element as rendered by the server.
18    */
19   function initContextualToolbar(context) {
20     if (!Drupal.contextual || !Drupal.contextual.collection) {
21       return;
22     }
23
24     const contextualToolbar = Drupal.contextualToolbar;
25     const model = contextualToolbar.model = new contextualToolbar.StateModel({
26       // Checks whether localStorage indicates we should start in edit mode
27       // rather than view mode.
28       // @see Drupal.contextualToolbar.VisualView.persist
29       isViewing: localStorage.getItem('Drupal.contextualToolbar.isViewing') !== 'false',
30     }, {
31       contextualCollection: Drupal.contextual.collection,
32     });
33
34     const viewOptions = {
35       el: $('.toolbar .toolbar-bar .contextual-toolbar-tab'),
36       model,
37       strings,
38     };
39     new contextualToolbar.VisualView(viewOptions);
40     new contextualToolbar.AuralView(viewOptions);
41   }
42
43   /**
44    * Attaches contextual's edit toolbar tab behavior.
45    *
46    * @type {Drupal~behavior}
47    *
48    * @prop {Drupal~behaviorAttach} attach
49    *   Attaches contextual toolbar behavior on a contextualToolbar-init event.
50    */
51   Drupal.behaviors.contextualToolbar = {
52     attach(context) {
53       if ($('body').once('contextualToolbar-init').length) {
54         initContextualToolbar(context);
55       }
56     },
57   };
58
59   /**
60    * Namespace for the contextual toolbar.
61    *
62    * @namespace
63    */
64   Drupal.contextualToolbar = {
65
66     /**
67      * The {@link Drupal.contextualToolbar.StateModel} instance.
68      *
69      * @type {?Drupal.contextualToolbar.StateModel}
70      */
71     model: null,
72   };
73 }(jQuery, Drupal, Backbone));