Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / contextual / js / toolbar / models / StateModel.es6.js
1 /**
2  * @file
3  * A Backbone Model for the state of Contextual module's edit toolbar tab.
4  */
5
6 (function (Drupal, Backbone) {
7   Drupal.contextualToolbar.StateModel = Backbone.Model.extend(/** @lends Drupal.contextualToolbar.StateModel# */{
8
9     /**
10      * @type {object}
11      *
12      * @prop {bool} isViewing
13      * @prop {bool} isVisible
14      * @prop {number} contextualCount
15      * @prop {Drupal~TabbingContext} tabbingContext
16      */
17     defaults: /** @lends Drupal.contextualToolbar.StateModel# */{
18
19       /**
20        * Indicates whether the toggle is currently in "view" or "edit" mode.
21        *
22        * @type {bool}
23        */
24       isViewing: true,
25
26       /**
27        * Indicates whether the toggle should be visible or hidden. Automatically
28        * calculated, depends on contextualCount.
29        *
30        * @type {bool}
31        */
32       isVisible: false,
33
34       /**
35        * Tracks how many contextual links exist on the page.
36        *
37        * @type {number}
38        */
39       contextualCount: 0,
40
41       /**
42        * A TabbingContext object as returned by {@link Drupal~TabbingManager}:
43        * the set of tabbable elements when edit mode is enabled.
44        *
45        * @type {?Drupal~TabbingContext}
46        */
47       tabbingContext: null,
48     },
49
50     /**
51      * Models the state of the edit mode toggle.
52      *
53      * @constructs
54      *
55      * @augments Backbone.Model
56      *
57      * @param {object} attrs
58      *   Attributes for the backbone model.
59      * @param {object} options
60      *   An object with the following option:
61      * @param {Backbone.collection} options.contextualCollection
62      *   The collection of {@link Drupal.contextual.StateModel} models that
63      *   represent the contextual links on the page.
64      */
65     initialize(attrs, options) {
66       // Respond to new/removed contextual links.
67       this.listenTo(options.contextualCollection, 'reset remove add', this.countContextualLinks);
68       this.listenTo(options.contextualCollection, 'add', this.lockNewContextualLinks);
69
70       // Automatically determine visibility.
71       this.listenTo(this, 'change:contextualCount', this.updateVisibility);
72
73       // Whenever edit mode is toggled, lock all contextual links.
74       this.listenTo(this, 'change:isViewing', (model, isViewing) => {
75         options.contextualCollection.each((contextualModel) => {
76           contextualModel.set('isLocked', !isViewing);
77         });
78       });
79     },
80
81     /**
82      * Tracks the number of contextual link models in the collection.
83      *
84      * @param {Drupal.contextual.StateModel} contextualModel
85      *   The contextual links model that was added or removed.
86      * @param {Backbone.Collection} contextualCollection
87      *    The collection of contextual link models.
88      */
89     countContextualLinks(contextualModel, contextualCollection) {
90       this.set('contextualCount', contextualCollection.length);
91     },
92
93     /**
94      * Lock newly added contextual links if edit mode is enabled.
95      *
96      * @param {Drupal.contextual.StateModel} contextualModel
97      *   The contextual links model that was added.
98      * @param {Backbone.Collection} [contextualCollection]
99      *    The collection of contextual link models.
100      */
101     lockNewContextualLinks(contextualModel, contextualCollection) {
102       if (!this.get('isViewing')) {
103         contextualModel.set('isLocked', true);
104       }
105     },
106
107     /**
108      * Automatically updates visibility of the view/edit mode toggle.
109      */
110     updateVisibility() {
111       this.set('isVisible', this.get('contextualCount') > 0);
112     },
113
114   });
115 }(Drupal, Backbone));