9c247730e312ea3f8fce213b0dda62d9d1a6418d
[yaffs-website] / web / core / modules / contextual / js / views / KeyboardView.js
1 /**
2  * @file
3  * A Backbone View that provides keyboard interaction for a contextual link.
4  */
5
6 (function (Drupal, Backbone) {
7
8   'use strict';
9
10   Drupal.contextual.KeyboardView = Backbone.View.extend(/** @lends Drupal.contextual.KeyboardView# */{
11
12     /**
13      * @type {object}
14      */
15     events: {
16       'focus .trigger': 'focus',
17       'focus .contextual-links a': 'focus',
18       'blur .trigger': function () { this.model.blur(); },
19       'blur .contextual-links a': function () {
20         // Set up a timeout to allow a user to tab between the trigger and the
21         // contextual links without the menu dismissing.
22         var that = this;
23         this.timer = window.setTimeout(function () {
24           that.model.close().blur();
25         }, 150);
26       }
27     },
28
29     /**
30      * Provides keyboard interaction for a contextual link.
31      *
32      * @constructs
33      *
34      * @augments Backbone.View
35      */
36     initialize: function () {
37
38       /**
39        * The timer is used to create a delay before dismissing the contextual
40        * links on blur. This is only necessary when keyboard users tab into
41        * contextual links without edit mode (i.e. without TabbingManager).
42        * That means that if we decide to disable tabbing of contextual links
43        * without edit mode, all this timer logic can go away.
44        *
45        * @type {NaN|number}
46        */
47       this.timer = NaN;
48     },
49
50     /**
51      * Sets focus on the model; Clears the timer that dismisses the links.
52      */
53     focus: function () {
54       // Clear the timeout that might have been set by blurring a link.
55       window.clearTimeout(this.timer);
56       this.model.focus();
57     }
58
59   });
60
61 })(Drupal, Backbone);