| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 cr.define('cr.ui', function() { | 5 cr.define('cr.ui', function() { |
| 6 /** | 6 /** |
| 7 * The class name to set on the document element. | 7 * The class name to set on the document element. |
| 8 * @const | 8 * @const |
| 9 */ | 9 */ |
| 10 var CLASS_NAME = 'focus-outline-visible'; | 10 var CLASS_NAME = 'focus-outline-visible'; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 * it. | 24 * it. |
| 25 * | 25 * |
| 26 * @param {Document} doc The document to attach the focus outline manager to. | 26 * @param {Document} doc The document to attach the focus outline manager to. |
| 27 * @constructor | 27 * @constructor |
| 28 */ | 28 */ |
| 29 function FocusOutlineManager(doc) { | 29 function FocusOutlineManager(doc) { |
| 30 this.classList_ = doc.documentElement.classList; | 30 this.classList_ = doc.documentElement.classList; |
| 31 | 31 |
| 32 var self = this; | 32 var self = this; |
| 33 | 33 |
| 34 doc.addEventListener( | 34 doc.addEventListener('keydown', function(e) { |
| 35 'keydown', function(e) { self.focusByKeyboard_ = true; }, true); | 35 self.focusByKeyboard_ = true; |
| 36 }, true); |
| 36 | 37 |
| 37 doc.addEventListener( | 38 doc.addEventListener('mousedown', function(e) { |
| 38 'mousedown', function(e) { self.focusByKeyboard_ = false; }, true); | 39 self.focusByKeyboard_ = false; |
| 40 }, true); |
| 39 | 41 |
| 40 doc.addEventListener('focus', function(event) { | 42 doc.addEventListener('focus', function(event) { |
| 41 // Update visibility only when focus is actually changed. | 43 // Update visibility only when focus is actually changed. |
| 42 self.updateVisibility(); | 44 self.updateVisibility(); |
| 43 }, true); | 45 }, true); |
| 44 | 46 |
| 45 doc.addEventListener('focusout', function(event) { | 47 doc.addEventListener('focusout', function(event) { |
| 46 window.setTimeout(function() { | 48 window.setTimeout(function() { |
| 47 if (!doc.hasFocus()) { | 49 if (!doc.hasFocus()) { |
| 48 self.focusByKeyboard_ = true; | 50 self.focusByKeyboard_ = true; |
| 49 self.updateVisibility(); | 51 self.updateVisibility(); |
| 50 } | 52 } |
| 51 }, 0); | 53 }, 0); |
| 52 }); | 54 }); |
| 53 | 55 |
| 54 this.updateVisibility(); | 56 this.updateVisibility(); |
| 55 } | 57 } |
| 56 | 58 |
| 57 FocusOutlineManager.prototype = { | 59 FocusOutlineManager.prototype = { |
| 58 /** | 60 /** |
| 59 * Whether focus change is triggered by TAB key. | 61 * Whether focus change is triggered by TAB key. |
| 60 * @type {boolean} | 62 * @type {boolean} |
| 61 * @private | 63 * @private |
| 62 */ | 64 */ |
| 63 focusByKeyboard_: true, | 65 focusByKeyboard_: true, |
| 64 | 66 |
| 65 updateVisibility: function() { this.visible = this.focusByKeyboard_; }, | 67 updateVisibility: function() { |
| 68 this.visible = this.focusByKeyboard_; |
| 69 }, |
| 66 | 70 |
| 67 /** | 71 /** |
| 68 * Whether the focus outline should be visible. | 72 * Whether the focus outline should be visible. |
| 69 * @type {boolean} | 73 * @type {boolean} |
| 70 */ | 74 */ |
| 71 set visible(visible) { this.classList_.toggle(CLASS_NAME, visible); }, | 75 set visible(visible) { |
| 72 get visible() { return this.classList_.contains(CLASS_NAME); } | 76 this.classList_.toggle(CLASS_NAME, visible); |
| 77 }, |
| 78 get visible() { |
| 79 return this.classList_.contains(CLASS_NAME); |
| 80 } |
| 73 }; | 81 }; |
| 74 | 82 |
| 75 /** | 83 /** |
| 76 * Array of Document and FocusOutlineManager pairs. | 84 * Array of Document and FocusOutlineManager pairs. |
| 77 * @type {Array} | 85 * @type {Array} |
| 78 */ | 86 */ |
| 79 var docsToManager = []; | 87 var docsToManager = []; |
| 80 | 88 |
| 81 /** | 89 /** |
| 82 * Gets a per document singleton focus outline manager. | 90 * Gets a per document singleton focus outline manager. |
| 83 * @param {Document} doc The document to get the |FocusOutlineManager| for. | 91 * @param {Document} doc The document to get the |FocusOutlineManager| for. |
| 84 * @return {cr.ui.FocusOutlineManager} The per document singleton focus | 92 * @return {cr.ui.FocusOutlineManager} The per document singleton focus |
| 85 * outline manager. | 93 * outline manager. |
| 86 */ | 94 */ |
| 87 FocusOutlineManager.forDocument = function(doc) { | 95 FocusOutlineManager.forDocument = function(doc) { |
| 88 for (var i = 0; i < docsToManager.length; i++) { | 96 for (var i = 0; i < docsToManager.length; i++) { |
| 89 if (doc == docsToManager[i][0]) | 97 if (doc == docsToManager[i][0]) |
| 90 return docsToManager[i][1]; | 98 return docsToManager[i][1]; |
| 91 } | 99 } |
| 92 var manager = new FocusOutlineManager(doc); | 100 var manager = new FocusOutlineManager(doc); |
| 93 docsToManager.push([doc, manager]); | 101 docsToManager.push([doc, manager]); |
| 94 return manager; | 102 return manager; |
| 95 }; | 103 }; |
| 96 | 104 |
| 97 return {FocusOutlineManager: FocusOutlineManager}; | 105 return {FocusOutlineManager: FocusOutlineManager}; |
| 98 }); | 106 }); |
| OLD | NEW |