3 * A Backbone Model for the state of Contextual module's edit toolbar tab.
6 (function (Drupal, Backbone) {
7 Drupal.contextualToolbar.StateModel = Backbone.Model.extend(/** @lends Drupal.contextualToolbar.StateModel# */{
12 * @prop {bool} isViewing
13 * @prop {bool} isVisible
14 * @prop {number} contextualCount
15 * @prop {Drupal~TabbingContext} tabbingContext
17 defaults: /** @lends Drupal.contextualToolbar.StateModel# */{
20 * Indicates whether the toggle is currently in "view" or "edit" mode.
27 * Indicates whether the toggle should be visible or hidden. Automatically
28 * calculated, depends on contextualCount.
35 * Tracks how many contextual links exist on the page.
42 * A TabbingContext object as returned by {@link Drupal~TabbingManager}:
43 * the set of tabbable elements when edit mode is enabled.
45 * @type {?Drupal~TabbingContext}
51 * Models the state of the edit mode toggle.
55 * @augments Backbone.Model
57 * @param {object} attrs
58 * Attributes for the backbone model.
59 * @param {object} options
60 * An object with the following option:
61 * @param {Backbone.collection} options.contextualCollection
62 * The collection of {@link Drupal.contextual.StateModel} models that
63 * represent the contextual links on the page.
65 initialize(attrs, options) {
66 // Respond to new/removed contextual links.
67 this.listenTo(options.contextualCollection, 'reset remove add', this.countContextualLinks);
68 this.listenTo(options.contextualCollection, 'add', this.lockNewContextualLinks);
70 // Automatically determine visibility.
71 this.listenTo(this, 'change:contextualCount', this.updateVisibility);
73 // Whenever edit mode is toggled, lock all contextual links.
74 this.listenTo(this, 'change:isViewing', (model, isViewing) => {
75 options.contextualCollection.each((contextualModel) => {
76 contextualModel.set('isLocked', !isViewing);
82 * Tracks the number of contextual link models in the collection.
84 * @param {Drupal.contextual.StateModel} contextualModel
85 * The contextual links model that was added or removed.
86 * @param {Backbone.Collection} contextualCollection
87 * The collection of contextual link models.
89 countContextualLinks(contextualModel, contextualCollection) {
90 this.set('contextualCount', contextualCollection.length);
94 * Lock newly added contextual links if edit mode is enabled.
96 * @param {Drupal.contextual.StateModel} contextualModel
97 * The contextual links model that was added.
98 * @param {Backbone.Collection} [contextualCollection]
99 * The collection of contextual link models.
101 lockNewContextualLinks(contextualModel, contextualCollection) {
102 if (!this.get('isViewing')) {
103 contextualModel.set('isLocked', true);
108 * Automatically updates visibility of the view/edit mode toggle.
111 this.set('isVisible', this.get('contextualCount') > 0);
115 }(Drupal, Backbone));