Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / contextual / js / models / StateModel.es6.js
1 /**
2  * @file
3  * A Backbone Model for the state of a contextual link's trigger, list & region.
4  */
5
6 (function (Drupal, Backbone) {
7   /**
8    * Models the state of a contextual link's trigger, list & region.
9    *
10    * @constructor
11    *
12    * @augments Backbone.Model
13    */
14   Drupal.contextual.StateModel = Backbone.Model.extend(/** @lends Drupal.contextual.StateModel# */{
15
16     /**
17      * @type {object}
18      *
19      * @prop {string} title
20      * @prop {bool} regionIsHovered
21      * @prop {bool} hasFocus
22      * @prop {bool} isOpen
23      * @prop {bool} isLocked
24      */
25     defaults: /** @lends Drupal.contextual.StateModel# */{
26
27       /**
28        * The title of the entity to which these contextual links apply.
29        *
30        * @type {string}
31        */
32       title: '',
33
34       /**
35        * Represents if the contextual region is being hovered.
36        *
37        * @type {bool}
38        */
39       regionIsHovered: false,
40
41       /**
42        * Represents if the contextual trigger or options have focus.
43        *
44        * @type {bool}
45        */
46       hasFocus: false,
47
48       /**
49        * Represents if the contextual options for an entity are available to
50        * be selected (i.e. whether the list of options is visible).
51        *
52        * @type {bool}
53        */
54       isOpen: false,
55
56       /**
57        * When the model is locked, the trigger remains active.
58        *
59        * @type {bool}
60        */
61       isLocked: false,
62     },
63
64     /**
65      * Opens or closes the contextual link.
66      *
67      * If it is opened, then also give focus.
68      *
69      * @return {Drupal.contextual.StateModel}
70      *   The current contextual state model.
71      */
72     toggleOpen() {
73       const newIsOpen = !this.get('isOpen');
74       this.set('isOpen', newIsOpen);
75       if (newIsOpen) {
76         this.focus();
77       }
78       return this;
79     },
80
81     /**
82      * Closes this contextual link.
83      *
84      * Does not call blur() because we want to allow a contextual link to have
85      * focus, yet be closed for example when hovering.
86      *
87      * @return {Drupal.contextual.StateModel}
88      *   The current contextual state model.
89      */
90     close() {
91       this.set('isOpen', false);
92       return this;
93     },
94
95     /**
96      * Gives focus to this contextual link.
97      *
98      * Also closes + removes focus from every other contextual link.
99      *
100      * @return {Drupal.contextual.StateModel}
101      *   The current contextual state model.
102      */
103     focus() {
104       this.set('hasFocus', true);
105       const cid = this.cid;
106       this.collection.each((model) => {
107         if (model.cid !== cid) {
108           model.close().blur();
109         }
110       });
111       return this;
112     },
113
114     /**
115      * Removes focus from this contextual link, unless it is open.
116      *
117      * @return {Drupal.contextual.StateModel}
118      *   The current contextual state model.
119      */
120     blur() {
121       if (!this.get('isOpen')) {
122         this.set('hasFocus', false);
123       }
124       return this;
125     },
126
127   });
128 }(Drupal, Backbone));