Newer
Older
Alex Pott
committed
/**
* @file
* A Backbone Model for the state of a contextual link's trigger, list & region.
*/
Lauri Eskola
committed
(function(Drupal, Backbone) {
Alex Pott
committed
/**
* Models the state of a contextual link's trigger, list & region.
*
* @constructor
*
* @augments Backbone.Model
*/
Lauri Eskola
committed
Drupal.contextual.StateModel = Backbone.Model.extend(
/** @lends Drupal.contextual.StateModel# */ {
Alex Pott
committed
/**
Lauri Eskola
committed
* @type {object}
Alex Pott
committed
*
Lauri Eskola
committed
* @prop {string} title
* @prop {bool} regionIsHovered
* @prop {bool} hasFocus
* @prop {bool} isOpen
* @prop {bool} isLocked
Alex Pott
committed
*/
Lauri Eskola
committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
defaults: /** @lends Drupal.contextual.StateModel# */ {
/**
* The title of the entity to which these contextual links apply.
*
* @type {string}
*/
title: '',
/**
* Represents if the contextual region is being hovered.
*
* @type {bool}
*/
regionIsHovered: false,
/**
* Represents if the contextual trigger or options have focus.
*
* @type {bool}
*/
hasFocus: false,
/**
* Represents if the contextual options for an entity are available to
* be selected (i.e. whether the list of options is visible).
*
* @type {bool}
*/
isOpen: false,
/**
* When the model is locked, the trigger remains active.
*
* @type {bool}
*/
isLocked: false,
},
Alex Pott
committed
/**
Lauri Eskola
committed
* Opens or closes the contextual link.
*
* If it is opened, then also give focus.
Alex Pott
committed
*
Lauri Eskola
committed
* @return {Drupal.contextual.StateModel}
* The current contextual state model.
Alex Pott
committed
*/
Lauri Eskola
committed
toggleOpen() {
const newIsOpen = !this.get('isOpen');
this.set('isOpen', newIsOpen);
if (newIsOpen) {
this.focus();
}
return this;
},
Alex Pott
committed
/**
Lauri Eskola
committed
* Closes this contextual link.
Alex Pott
committed
*
Lauri Eskola
committed
* Does not call blur() because we want to allow a contextual link to have
* focus, yet be closed for example when hovering.
*
* @return {Drupal.contextual.StateModel}
* The current contextual state model.
Alex Pott
committed
*/
Lauri Eskola
committed
close() {
this.set('isOpen', false);
return this;
},
Alex Pott
committed
/**
Lauri Eskola
committed
* Gives focus to this contextual link.
*
* Also closes + removes focus from every other contextual link.
Alex Pott
committed
*
Lauri Eskola
committed
* @return {Drupal.contextual.StateModel}
* The current contextual state model.
Alex Pott
committed
*/
Lauri Eskola
committed
focus() {
this.set('hasFocus', true);
const cid = this.cid;
this.collection.each(model => {
if (model.cid !== cid) {
model.close().blur();
}
});
return this;
},
Alex Pott
committed
/**
Lauri Eskola
committed
* Removes focus from this contextual link, unless it is open.
Alex Pott
committed
*
Lauri Eskola
committed
* @return {Drupal.contextual.StateModel}
* The current contextual state model.
Alex Pott
committed
*/
Lauri Eskola
committed
blur() {
if (!this.get('isOpen')) {
this.set('hasFocus', false);
Alex Pott
committed
}
Lauri Eskola
committed
return this;
},
},
Lauri Eskola
committed
);
})(Drupal, Backbone);