d9159e4e97f69762629120d56e6b1ce5855fdeba
[yaffs-website] / web / core / modules / contextual / js / toolbar / models / StateModel.js
1 /**
2  * @file
3  * A Backbone Model for the state of Contextual module's edit toolbar tab.
4  */
5
6 (function (Drupal, Backbone) {
7
8   'use strict';
9
10   Drupal.contextualToolbar.StateModel = Backbone.Model.extend(/** @lends Drupal.contextualToolbar.StateModel# */{
11
12     /**
13      * @type {object}
14      *
15      * @prop {bool} isViewing
16      * @prop {bool} isVisible
17      * @prop {number} contextualCount
18      * @prop {Drupal~TabbingContext} tabbingContext
19      */
20     defaults: /** @lends Drupal.contextualToolbar.StateModel# */{
21
22       /**
23        * Indicates whether the toggle is currently in "view" or "edit" mode.
24        *
25        * @type {bool}
26        */
27       isViewing: true,
28
29       /**
30        * Indicates whether the toggle should be visible or hidden. Automatically
31        * calculated, depends on contextualCount.
32        *
33        * @type {bool}
34        */
35       isVisible: false,
36
37       /**
38        * Tracks how many contextual links exist on the page.
39        *
40        * @type {number}
41        */
42       contextualCount: 0,
43
44       /**
45        * A TabbingContext object as returned by {@link Drupal~TabbingManager}:
46        * the set of tabbable elements when edit mode is enabled.
47        *
48        * @type {?Drupal~TabbingContext}
49        */
50       tabbingContext: null
51     },
52
53     /**
54      * Models the state of the edit mode toggle.
55      *
56      * @constructs
57      *
58      * @augments Backbone.Model
59      *
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.
67      */
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);
72
73       // Automatically determine visibility.
74       this.listenTo(this, 'change:contextualCount', this.updateVisibility);
75
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);
80         });
81       });
82     },
83
84     /**
85      * Tracks the number of contextual link models in the collection.
86      *
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.
91      */
92     countContextualLinks: function (contextualModel, contextualCollection) {
93       this.set('contextualCount', contextualCollection.length);
94     },
95
96     /**
97      * Lock newly added contextual links if edit mode is enabled.
98      *
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.
103      */
104     lockNewContextualLinks: function (contextualModel, contextualCollection) {
105       if (!this.get('isViewing')) {
106         contextualModel.set('isLocked', true);
107       }
108     },
109
110     /**
111      * Automatically updates visibility of the view/edit mode toggle.
112      */
113     updateVisibility: function () {
114       this.set('isVisible', this.get('contextualCount') > 0);
115     }
116
117   });
118
119 })(Drupal, Backbone);