3 * A Backbone Model for the state of Contextual module's edit toolbar tab.
6 (function (Drupal, Backbone) {
10 Drupal.contextualToolbar.StateModel = Backbone.Model.extend(/** @lends Drupal.contextualToolbar.StateModel# */{
15 * @prop {bool} isViewing
16 * @prop {bool} isVisible
17 * @prop {number} contextualCount
18 * @prop {Drupal~TabbingContext} tabbingContext
20 defaults: /** @lends Drupal.contextualToolbar.StateModel# */{
23 * Indicates whether the toggle is currently in "view" or "edit" mode.
30 * Indicates whether the toggle should be visible or hidden. Automatically
31 * calculated, depends on contextualCount.
38 * Tracks how many contextual links exist on the page.
45 * A TabbingContext object as returned by {@link Drupal~TabbingManager}:
46 * the set of tabbable elements when edit mode is enabled.
48 * @type {?Drupal~TabbingContext}
54 * Models the state of the edit mode toggle.
58 * @augments Backbone.Model
60 * @param {object} attrs
61 * Attributes for the backbone model.
62 * @param {object} options
63 * An object with the following option:
64 * @param {Backbone.collection} options.contextualCollection
65 * The collection of {@link Drupal.contextual.StateModel} models that
66 * represent the contextual links on the page.
68 initialize: function (attrs, options) {
69 // Respond to new/removed contextual links.
70 this.listenTo(options.contextualCollection, 'reset remove add', this.countContextualLinks);
71 this.listenTo(options.contextualCollection, 'add', this.lockNewContextualLinks);
73 // Automatically determine visibility.
74 this.listenTo(this, 'change:contextualCount', this.updateVisibility);
76 // Whenever edit mode is toggled, lock all contextual links.
77 this.listenTo(this, 'change:isViewing', function (model, isViewing) {
78 options.contextualCollection.each(function (contextualModel) {
79 contextualModel.set('isLocked', !isViewing);
85 * Tracks the number of contextual link models in the collection.
87 * @param {Drupal.contextual.StateModel} contextualModel
88 * The contextual links model that was added or removed.
89 * @param {Backbone.Collection} contextualCollection
90 * The collection of contextual link models.
92 countContextualLinks: function (contextualModel, contextualCollection) {
93 this.set('contextualCount', contextualCollection.length);
97 * Lock newly added contextual links if edit mode is enabled.
99 * @param {Drupal.contextual.StateModel} contextualModel
100 * The contextual links model that was added.
101 * @param {Backbone.Collection} [contextualCollection]
102 * The collection of contextual link models.
104 lockNewContextualLinks: function (contextualModel, contextualCollection) {
105 if (!this.get('isViewing')) {
106 contextualModel.set('isLocked', true);
111 * Automatically updates visibility of the view/edit mode toggle.
113 updateVisibility: function () {
114 this.set('isVisible', this.get('contextualCount') > 0);
119 })(Drupal, Backbone);