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