277dec0b010e84f1b44927e7b32469490b037c95
[yaffs-website] / web / core / modules / contextual / js / toolbar / models / StateModel.js
1 /**
2 * DO NOT EDIT THIS FILE.
3 * See the following change record for more information,
4 * https://www.drupal.org/node/2815083
5 * @preserve
6 **/
7
8 (function (Drupal, Backbone) {
9   Drupal.contextualToolbar.StateModel = Backbone.Model.extend({
10     defaults: {
11       isViewing: true,
12
13       isVisible: false,
14
15       contextualCount: 0,
16
17       tabbingContext: null
18     },
19
20     initialize: function initialize(attrs, options) {
21       this.listenTo(options.contextualCollection, 'reset remove add', this.countContextualLinks);
22       this.listenTo(options.contextualCollection, 'add', this.lockNewContextualLinks);
23
24       this.listenTo(this, 'change:contextualCount', this.updateVisibility);
25
26       this.listenTo(this, 'change:isViewing', function (model, isViewing) {
27         options.contextualCollection.each(function (contextualModel) {
28           contextualModel.set('isLocked', !isViewing);
29         });
30       });
31     },
32     countContextualLinks: function countContextualLinks(contextualModel, contextualCollection) {
33       this.set('contextualCount', contextualCollection.length);
34     },
35     lockNewContextualLinks: function lockNewContextualLinks(contextualModel, contextualCollection) {
36       if (!this.get('isViewing')) {
37         contextualModel.set('isLocked', true);
38       }
39     },
40     updateVisibility: function updateVisibility() {
41       this.set('isVisible', this.get('contextualCount') > 0);
42     }
43   });
44 })(Drupal, Backbone);