2014-01-08 07:03:02 +00:00
|
|
|
/**
|
2022-09-09 06:26:42 +00:00
|
|
|
* @file
|
|
|
|
* A Backbone Model for the state of a contextual link's trigger, list & region.
|
|
|
|
*/
|
2014-01-08 07:03:02 +00:00
|
|
|
|
|
|
|
(function (Drupal, Backbone) {
|
2022-09-09 06:26:42 +00:00
|
|
|
/**
|
|
|
|
* Models the state of a contextual link's trigger, list & region.
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
*
|
|
|
|
* @augments Backbone.Model
|
|
|
|
*
|
|
|
|
* @deprecated in drupal:9.4.0 and is removed from drupal:10.0.0. There is no
|
|
|
|
* replacement.
|
|
|
|
*/
|
|
|
|
Drupal.contextual.StateModel = Backbone.Model.extend(
|
|
|
|
/** @lends Drupal.contextual.StateModel# */ {
|
|
|
|
/**
|
|
|
|
* @type {object}
|
|
|
|
*
|
|
|
|
* @prop {string} title
|
2022-11-28 11:30:17 +00:00
|
|
|
* @prop {boolean} regionIsHovered
|
|
|
|
* @prop {boolean} hasFocus
|
|
|
|
* @prop {boolean} isOpen
|
|
|
|
* @prop {boolean} isLocked
|
2022-09-09 06:26:42 +00:00
|
|
|
*/
|
|
|
|
defaults: /** @lends Drupal.contextual.StateModel# */ {
|
|
|
|
/**
|
|
|
|
* The title of the entity to which these contextual links apply.
|
|
|
|
*
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
title: '',
|
2021-12-18 06:12:16 +00:00
|
|
|
|
2022-09-09 06:26:42 +00:00
|
|
|
/**
|
|
|
|
* Represents if the contextual region is being hovered.
|
|
|
|
*
|
2022-11-28 11:30:17 +00:00
|
|
|
* @type {boolean}
|
2022-09-09 06:26:42 +00:00
|
|
|
*/
|
|
|
|
regionIsHovered: false,
|
2020-01-30 09:08:38 +00:00
|
|
|
|
2022-09-09 06:26:42 +00:00
|
|
|
/**
|
|
|
|
* Represents if the contextual trigger or options have focus.
|
|
|
|
*
|
2022-11-28 11:30:17 +00:00
|
|
|
* @type {boolean}
|
2022-09-09 06:26:42 +00:00
|
|
|
*/
|
|
|
|
hasFocus: false,
|
2020-01-30 09:08:38 +00:00
|
|
|
|
2022-09-09 06:26:42 +00:00
|
|
|
/**
|
|
|
|
* Represents if the contextual options for an entity are available to
|
|
|
|
* be selected (i.e. whether the list of options is visible).
|
|
|
|
*
|
2022-11-28 11:30:17 +00:00
|
|
|
* @type {boolean}
|
2022-09-09 06:26:42 +00:00
|
|
|
*/
|
|
|
|
isOpen: false,
|
2021-12-18 06:12:16 +00:00
|
|
|
|
2022-09-09 06:26:42 +00:00
|
|
|
/**
|
|
|
|
* When the model is locked, the trigger remains active.
|
|
|
|
*
|
2022-11-28 11:30:17 +00:00
|
|
|
* @type {boolean}
|
2022-09-09 06:26:42 +00:00
|
|
|
*/
|
|
|
|
isLocked: false,
|
|
|
|
},
|
2021-12-18 06:12:16 +00:00
|
|
|
|
2022-09-09 06:26:42 +00:00
|
|
|
/**
|
|
|
|
* Opens or closes the contextual link.
|
|
|
|
*
|
|
|
|
* If it is opened, then also give focus.
|
|
|
|
*
|
|
|
|
* @return {Drupal.contextual.StateModel}
|
|
|
|
* The current contextual state model.
|
|
|
|
*/
|
|
|
|
toggleOpen() {
|
|
|
|
const newIsOpen = !this.get('isOpen');
|
|
|
|
this.set('isOpen', newIsOpen);
|
|
|
|
if (newIsOpen) {
|
|
|
|
this.focus();
|
2014-01-27 21:41:32 +00:00
|
|
|
}
|
2022-09-09 06:26:42 +00:00
|
|
|
return this;
|
|
|
|
},
|
2021-12-18 06:12:16 +00:00
|
|
|
|
2022-09-09 06:26:42 +00:00
|
|
|
/**
|
|
|
|
* Closes this contextual link.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
close() {
|
|
|
|
this.set('isOpen', false);
|
|
|
|
return this;
|
|
|
|
},
|
2020-01-30 09:08:38 +00:00
|
|
|
|
2022-09-09 06:26:42 +00:00
|
|
|
/**
|
|
|
|
* Gives focus to this contextual link.
|
|
|
|
*
|
|
|
|
* Also closes + removes focus from every other contextual link.
|
|
|
|
*
|
|
|
|
* @return {Drupal.contextual.StateModel}
|
|
|
|
* The current contextual state model.
|
|
|
|
*/
|
|
|
|
focus() {
|
|
|
|
this.set('hasFocus', true);
|
|
|
|
const cid = this.cid;
|
|
|
|
this.collection.each((model) => {
|
|
|
|
if (model.cid !== cid) {
|
|
|
|
model.close().blur();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return this;
|
|
|
|
},
|
2021-12-18 06:12:16 +00:00
|
|
|
|
2022-09-09 06:26:42 +00:00
|
|
|
/**
|
|
|
|
* Removes focus from this contextual link, unless it is open.
|
|
|
|
*
|
|
|
|
* @return {Drupal.contextual.StateModel}
|
|
|
|
* The current contextual state model.
|
|
|
|
*/
|
|
|
|
blur() {
|
|
|
|
if (!this.get('isOpen')) {
|
|
|
|
this.set('hasFocus', false);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
})(Drupal, Backbone);
|