Version 1
[yaffs-website] / web / core / modules / contextual / js / contextual.toolbar.js
1 /**
2  * @file
3  * Attaches behaviors for the Contextual module's edit toolbar tab.
4  */
5
6 (function ($, Drupal, Backbone) {
7
8   'use strict';
9
10   var strings = {
11     tabbingReleased: Drupal.t('Tabbing is no longer constrained by the Contextual module.'),
12     tabbingConstrained: Drupal.t('Tabbing is constrained to a set of @contextualsCount and the edit mode toggle.'),
13     pressEsc: Drupal.t('Press the esc key to exit.')
14   };
15
16   /**
17    * Initializes a contextual link: updates its DOM, sets up model and views.
18    *
19    * @param {HTMLElement} context
20    *   A contextual links DOM element as rendered by the server.
21    */
22   function initContextualToolbar(context) {
23     if (!Drupal.contextual || !Drupal.contextual.collection) {
24       return;
25     }
26
27     var contextualToolbar = Drupal.contextualToolbar;
28     var model = contextualToolbar.model = new contextualToolbar.StateModel({
29       // Checks whether localStorage indicates we should start in edit mode
30       // rather than view mode.
31       // @see Drupal.contextualToolbar.VisualView.persist
32       isViewing: localStorage.getItem('Drupal.contextualToolbar.isViewing') !== 'false'
33     }, {
34       contextualCollection: Drupal.contextual.collection
35     });
36
37     var viewOptions = {
38       el: $('.toolbar .toolbar-bar .contextual-toolbar-tab'),
39       model: model,
40       strings: strings
41     };
42     new contextualToolbar.VisualView(viewOptions);
43     new contextualToolbar.AuralView(viewOptions);
44   }
45
46   /**
47    * Attaches contextual's edit toolbar tab behavior.
48    *
49    * @type {Drupal~behavior}
50    *
51    * @prop {Drupal~behaviorAttach} attach
52    *   Attaches contextual toolbar behavior on a contextualToolbar-init event.
53    */
54   Drupal.behaviors.contextualToolbar = {
55     attach: function (context) {
56       if ($('body').once('contextualToolbar-init').length) {
57         initContextualToolbar(context);
58       }
59     }
60   };
61
62   /**
63    * Namespace for the contextual toolbar.
64    *
65    * @namespace
66    */
67   Drupal.contextualToolbar = {
68
69     /**
70      * The {@link Drupal.contextualToolbar.StateModel} instance.
71      *
72      * @type {?Drupal.contextualToolbar.StateModel}
73      */
74     model: null
75   };
76
77 })(jQuery, Drupal, Backbone);