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