annotate core/modules/contextual/js/toolbar/models/StateModel.es6.js @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 /**
Chris@0 2 * @file
Chris@0 3 * A Backbone Model for the state of Contextual module's edit toolbar tab.
Chris@0 4 */
Chris@0 5
Chris@17 6 (function(Drupal, Backbone) {
Chris@17 7 Drupal.contextualToolbar.StateModel = Backbone.Model.extend(
Chris@17 8 /** @lends Drupal.contextualToolbar.StateModel# */ {
Chris@17 9 /**
Chris@17 10 * @type {object}
Chris@17 11 *
Chris@17 12 * @prop {bool} isViewing
Chris@17 13 * @prop {bool} isVisible
Chris@17 14 * @prop {number} contextualCount
Chris@17 15 * @prop {Drupal~TabbingContext} tabbingContext
Chris@17 16 */
Chris@17 17 defaults: /** @lends Drupal.contextualToolbar.StateModel# */ {
Chris@17 18 /**
Chris@17 19 * Indicates whether the toggle is currently in "view" or "edit" mode.
Chris@17 20 *
Chris@17 21 * @type {bool}
Chris@17 22 */
Chris@17 23 isViewing: true,
Chris@0 24
Chris@17 25 /**
Chris@17 26 * Indicates whether the toggle should be visible or hidden. Automatically
Chris@17 27 * calculated, depends on contextualCount.
Chris@17 28 *
Chris@17 29 * @type {bool}
Chris@17 30 */
Chris@17 31 isVisible: false,
Chris@17 32
Chris@17 33 /**
Chris@17 34 * Tracks how many contextual links exist on the page.
Chris@17 35 *
Chris@17 36 * @type {number}
Chris@17 37 */
Chris@17 38 contextualCount: 0,
Chris@17 39
Chris@17 40 /**
Chris@17 41 * A TabbingContext object as returned by {@link Drupal~TabbingManager}:
Chris@17 42 * the set of tabbable elements when edit mode is enabled.
Chris@17 43 *
Chris@17 44 * @type {?Drupal~TabbingContext}
Chris@17 45 */
Chris@17 46 tabbingContext: null,
Chris@17 47 },
Chris@0 48
Chris@0 49 /**
Chris@17 50 * Models the state of the edit mode toggle.
Chris@0 51 *
Chris@17 52 * @constructs
Chris@17 53 *
Chris@17 54 * @augments Backbone.Model
Chris@17 55 *
Chris@17 56 * @param {object} attrs
Chris@17 57 * Attributes for the backbone model.
Chris@17 58 * @param {object} options
Chris@17 59 * An object with the following option:
Chris@17 60 * @param {Backbone.collection} options.contextualCollection
Chris@17 61 * The collection of {@link Drupal.contextual.StateModel} models that
Chris@17 62 * represent the contextual links on the page.
Chris@0 63 */
Chris@17 64 initialize(attrs, options) {
Chris@17 65 // Respond to new/removed contextual links.
Chris@17 66 this.listenTo(
Chris@17 67 options.contextualCollection,
Chris@17 68 'reset remove add',
Chris@17 69 this.countContextualLinks,
Chris@17 70 );
Chris@17 71 this.listenTo(
Chris@17 72 options.contextualCollection,
Chris@17 73 'add',
Chris@17 74 this.lockNewContextualLinks,
Chris@17 75 );
Chris@17 76
Chris@17 77 // Automatically determine visibility.
Chris@17 78 this.listenTo(this, 'change:contextualCount', this.updateVisibility);
Chris@17 79
Chris@17 80 // Whenever edit mode is toggled, lock all contextual links.
Chris@17 81 this.listenTo(this, 'change:isViewing', (model, isViewing) => {
Chris@17 82 options.contextualCollection.each(contextualModel => {
Chris@17 83 contextualModel.set('isLocked', !isViewing);
Chris@17 84 });
Chris@17 85 });
Chris@17 86 },
Chris@0 87
Chris@0 88 /**
Chris@17 89 * Tracks the number of contextual link models in the collection.
Chris@0 90 *
Chris@17 91 * @param {Drupal.contextual.StateModel} contextualModel
Chris@17 92 * The contextual links model that was added or removed.
Chris@17 93 * @param {Backbone.Collection} contextualCollection
Chris@17 94 * The collection of contextual link models.
Chris@0 95 */
Chris@17 96 countContextualLinks(contextualModel, contextualCollection) {
Chris@17 97 this.set('contextualCount', contextualCollection.length);
Chris@17 98 },
Chris@0 99
Chris@0 100 /**
Chris@17 101 * Lock newly added contextual links if edit mode is enabled.
Chris@0 102 *
Chris@17 103 * @param {Drupal.contextual.StateModel} contextualModel
Chris@17 104 * The contextual links model that was added.
Chris@17 105 * @param {Backbone.Collection} [contextualCollection]
Chris@17 106 * The collection of contextual link models.
Chris@0 107 */
Chris@17 108 lockNewContextualLinks(contextualModel, contextualCollection) {
Chris@17 109 if (!this.get('isViewing')) {
Chris@17 110 contextualModel.set('isLocked', true);
Chris@17 111 }
Chris@17 112 },
Chris@0 113
Chris@0 114 /**
Chris@17 115 * Automatically updates visibility of the view/edit mode toggle.
Chris@0 116 */
Chris@17 117 updateVisibility() {
Chris@17 118 this.set('isVisible', this.get('contextualCount') > 0);
Chris@17 119 },
Chris@0 120 },
Chris@17 121 );
Chris@17 122 })(Drupal, Backbone);