Chris@0: /** Chris@0: * @file Chris@0: * A Backbone Model for the state of Contextual module's edit toolbar tab. Chris@0: */ Chris@0: Chris@17: (function(Drupal, Backbone) { Chris@17: Drupal.contextualToolbar.StateModel = Backbone.Model.extend( Chris@17: /** @lends Drupal.contextualToolbar.StateModel# */ { Chris@17: /** Chris@17: * @type {object} Chris@17: * Chris@17: * @prop {bool} isViewing Chris@17: * @prop {bool} isVisible Chris@17: * @prop {number} contextualCount Chris@17: * @prop {Drupal~TabbingContext} tabbingContext Chris@17: */ Chris@17: defaults: /** @lends Drupal.contextualToolbar.StateModel# */ { Chris@17: /** Chris@17: * Indicates whether the toggle is currently in "view" or "edit" mode. Chris@17: * Chris@17: * @type {bool} Chris@17: */ Chris@17: isViewing: true, Chris@0: Chris@17: /** Chris@17: * Indicates whether the toggle should be visible or hidden. Automatically Chris@17: * calculated, depends on contextualCount. Chris@17: * Chris@17: * @type {bool} Chris@17: */ Chris@17: isVisible: false, Chris@17: Chris@17: /** Chris@17: * Tracks how many contextual links exist on the page. Chris@17: * Chris@17: * @type {number} Chris@17: */ Chris@17: contextualCount: 0, Chris@17: Chris@17: /** Chris@17: * A TabbingContext object as returned by {@link Drupal~TabbingManager}: Chris@17: * the set of tabbable elements when edit mode is enabled. Chris@17: * Chris@17: * @type {?Drupal~TabbingContext} Chris@17: */ Chris@17: tabbingContext: null, Chris@17: }, Chris@0: Chris@0: /** Chris@17: * Models the state of the edit mode toggle. Chris@0: * Chris@17: * @constructs Chris@17: * Chris@17: * @augments Backbone.Model Chris@17: * Chris@17: * @param {object} attrs Chris@17: * Attributes for the backbone model. Chris@17: * @param {object} options Chris@17: * An object with the following option: Chris@17: * @param {Backbone.collection} options.contextualCollection Chris@17: * The collection of {@link Drupal.contextual.StateModel} models that Chris@17: * represent the contextual links on the page. Chris@0: */ Chris@17: initialize(attrs, options) { Chris@17: // Respond to new/removed contextual links. Chris@17: this.listenTo( Chris@17: options.contextualCollection, Chris@17: 'reset remove add', Chris@17: this.countContextualLinks, Chris@17: ); Chris@17: this.listenTo( Chris@17: options.contextualCollection, Chris@17: 'add', Chris@17: this.lockNewContextualLinks, Chris@17: ); Chris@17: Chris@17: // Automatically determine visibility. Chris@17: this.listenTo(this, 'change:contextualCount', this.updateVisibility); Chris@17: Chris@17: // Whenever edit mode is toggled, lock all contextual links. Chris@17: this.listenTo(this, 'change:isViewing', (model, isViewing) => { Chris@17: options.contextualCollection.each(contextualModel => { Chris@17: contextualModel.set('isLocked', !isViewing); Chris@17: }); Chris@17: }); Chris@17: }, Chris@0: Chris@0: /** Chris@17: * Tracks the number of contextual link models in the collection. Chris@0: * Chris@17: * @param {Drupal.contextual.StateModel} contextualModel Chris@17: * The contextual links model that was added or removed. Chris@17: * @param {Backbone.Collection} contextualCollection Chris@17: * The collection of contextual link models. Chris@0: */ Chris@17: countContextualLinks(contextualModel, contextualCollection) { Chris@17: this.set('contextualCount', contextualCollection.length); Chris@17: }, Chris@0: Chris@0: /** Chris@17: * Lock newly added contextual links if edit mode is enabled. Chris@0: * Chris@17: * @param {Drupal.contextual.StateModel} contextualModel Chris@17: * The contextual links model that was added. Chris@17: * @param {Backbone.Collection} [contextualCollection] Chris@17: * The collection of contextual link models. Chris@0: */ Chris@17: lockNewContextualLinks(contextualModel, contextualCollection) { Chris@17: if (!this.get('isViewing')) { Chris@17: contextualModel.set('isLocked', true); Chris@17: } Chris@17: }, Chris@0: Chris@0: /** Chris@17: * Automatically updates visibility of the view/edit mode toggle. Chris@0: */ Chris@17: updateVisibility() { Chris@17: this.set('isVisible', this.get('contextualCount') > 0); Chris@17: }, Chris@0: }, Chris@17: ); Chris@17: })(Drupal, Backbone);