Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 // Action links are elements that are used to perform an in-page navigation or | 5 // Action links are elements that are used to perform an in-page navigation or |
| 6 // action (e.g. showing a dialog). | 6 // action (e.g. showing a dialog). |
| 7 // | 7 // |
| 8 // They look like normal anchor (<a>) tags as their text color is blue. However, | 8 // They look like normal anchor (<a>) tags as their text color is blue. However, |
| 9 // they're subtly different as they're not initially underlined (giving users a | 9 // they're subtly different as they're not initially underlined (giving users a |
| 10 // clue that underlined links navigate while action links don't). | 10 // clue that underlined links navigate while action links don't). |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 47 if (!this.disabled && e.key == 'Enter' && !this.href) { | 47 if (!this.disabled && e.key == 'Enter' && !this.href) { |
| 48 // Schedule a click asynchronously because other 'keydown' handlers | 48 // Schedule a click asynchronously because other 'keydown' handlers |
| 49 // may still run later (e.g. document.addEventListener('keydown')). | 49 // may still run later (e.g. document.addEventListener('keydown')). |
| 50 // Specifically options dialogs break when this timeout isn't here. | 50 // Specifically options dialogs break when this timeout isn't here. |
| 51 // NOTE: this affects the "trusted" state of the ensuing click. I | 51 // NOTE: this affects the "trusted" state of the ensuing click. I |
| 52 // haven't found anything that breaks because of this (yet). | 52 // haven't found anything that breaks because of this (yet). |
| 53 window.setTimeout(this.click.bind(this), 0); | 53 window.setTimeout(this.click.bind(this), 0); |
| 54 } | 54 } |
| 55 }); | 55 }); |
| 56 | 56 |
| 57 this.addEventListener('keyup', function(e) { | |
| 58 // If focus started via Tab press, allow outline to show. | |
| 59 if (e.key === 'Tab') | |
|
Dan Beam
2017/03/22 03:28:53
this isn't the only way focus can get to an elemen
| |
| 60 this.classList.remove('no-outline'); | |
| 61 }); | |
| 62 | |
| 57 function preventDefault(e) { | 63 function preventDefault(e) { |
| 58 e.preventDefault(); | 64 e.preventDefault(); |
| 59 } | 65 } |
| 60 | 66 |
| 61 function removePreventDefault() { | 67 function removePreventDefault() { |
| 62 document.removeEventListener('selectstart', preventDefault); | 68 document.removeEventListener('selectstart', preventDefault); |
| 63 document.removeEventListener('mouseup', removePreventDefault); | 69 document.removeEventListener('mouseup', removePreventDefault); |
| 64 } | 70 } |
| 65 | 71 |
| 66 this.addEventListener('mousedown', function() { | 72 this.addEventListener('mousedown', function() { |
| 67 // This handlers strives to match the behavior of <a href="...">. | 73 // This handlers strives to match the behavior of <a href="...">. |
| 68 | 74 |
| 69 // While the mouse is down, prevent text selection from dragging. | 75 // While the mouse is down, prevent text selection from dragging. |
| 70 document.addEventListener('selectstart', preventDefault); | 76 document.addEventListener('selectstart', preventDefault); |
| 71 document.addEventListener('mouseup', removePreventDefault); | 77 document.addEventListener('mouseup', removePreventDefault); |
| 72 | 78 |
| 73 // If focus started via mouse press, don't show an outline. | 79 // If focus started via mouse press, suppress outline. |
| 74 if (document.activeElement != this) | 80 if (document.activeElement != this) |
| 75 this.classList.add('no-outline'); | 81 this.classList.add('no-outline'); |
| 76 }); | 82 }); |
| 77 | |
| 78 this.addEventListener('blur', function(e) { | |
| 79 // This check helps us exclude external events like application switch. | |
| 80 if (e.sourceCapabilities) | |
| 81 this.classList.remove('no-outline'); | |
| 82 }); | |
| 83 }, | 83 }, |
| 84 | 84 |
| 85 /** @type {boolean} */ | 85 /** @type {boolean} */ |
| 86 set disabled(disabled) { | 86 set disabled(disabled) { |
| 87 if (disabled) | 87 if (disabled) |
| 88 HTMLAnchorElement.prototype.setAttribute.call(this, 'disabled', ''); | 88 HTMLAnchorElement.prototype.setAttribute.call(this, 'disabled', ''); |
| 89 else | 89 else |
| 90 HTMLAnchorElement.prototype.removeAttribute.call(this, 'disabled'); | 90 HTMLAnchorElement.prototype.removeAttribute.call(this, 'disabled'); |
| 91 this.tabIndex = disabled ? -1 : 0; | 91 this.tabIndex = disabled ? -1 : 0; |
| 92 }, | 92 }, |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 106 removeAttribute: function(attr) { | 106 removeAttribute: function(attr) { |
| 107 if (attr.toLowerCase() == 'disabled') | 107 if (attr.toLowerCase() == 'disabled') |
| 108 this.disabled = false; | 108 this.disabled = false; |
| 109 else | 109 else |
| 110 HTMLAnchorElement.prototype.removeAttribute.apply(this, arguments); | 110 HTMLAnchorElement.prototype.removeAttribute.apply(this, arguments); |
| 111 }, | 111 }, |
| 112 }, | 112 }, |
| 113 | 113 |
| 114 extends: 'a', | 114 extends: 'a', |
| 115 }); | 115 }); |
| OLD | NEW |