Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 /** | 5 /** |
| 6 * @fileoverview 'cr-dialog' is a component for showing a modal dialog. If the | 6 * @fileoverview 'cr-dialog' is a component for showing a modal dialog. If the |
| 7 * dialog is closed via close(), a 'close' event is fired. If the dialog is | 7 * dialog is closed via close(), a 'close' event is fired. If the dialog is |
| 8 * canceled via cancel(), a 'cancel' event is fired followed by a 'close' event. | 8 * canceled via cancel(), a 'cancel' event is fired followed by a 'close' event. |
| 9 * Additionally clients can inspect the dialog's |returnValue| property inside | 9 * Additionally clients can inspect the dialog's |returnValue| property inside |
| 10 * the 'close' event listener to determine whether it was canceled or just | 10 * the 'close' event listener to determine whether it was canceled or just |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 57 // all open dialogs should be cancelled. | 57 // all open dialogs should be cancelled. |
| 58 window.addEventListener('popstate', function() { | 58 window.addEventListener('popstate', function() { |
| 59 if (!this.ignorePopstate && this.open) | 59 if (!this.ignorePopstate && this.open) |
| 60 this.cancel(); | 60 this.cancel(); |
| 61 }.bind(this)); | 61 }.bind(this)); |
| 62 | 62 |
| 63 if (!this.ignoreEnterKey) | 63 if (!this.ignoreEnterKey) |
| 64 this.addEventListener('keypress', this.onKeypress_.bind(this)); | 64 this.addEventListener('keypress', this.onKeypress_.bind(this)); |
| 65 }, | 65 }, |
| 66 | 66 |
| 67 /** @private {Function} */ | |
| 68 onResize_: null, | |
| 69 | |
| 67 /** @override */ | 70 /** @override */ |
| 68 attached: function() { | 71 attached: function() { |
| 69 if (!this.showScrollBorders) | 72 if (!this.showScrollBorders) |
| 70 return; | 73 return; |
| 71 | 74 |
| 72 this.mutationObserver_ = new MutationObserver(function() { | 75 this.mutationObserver_ = new MutationObserver(function() { |
| 73 if (this.open) { | 76 if (this.open) { |
| 74 this.addIntersectionObserver_(); | 77 this.addIntersectionObserver_(); |
| 78 this.adjustHeight_(); | |
| 79 | |
| 80 this.onResize_ = this.adjustHeight_.bind(this); | |
| 81 window.addEventListener('resize', this.onResize_); | |
| 75 } else { | 82 } else { |
| 76 this.removeIntersectionObserver_(); | 83 this.removeIntersectionObserver_(); |
| 77 } | 84 } |
| 78 }.bind(this)); | 85 }.bind(this)); |
| 79 | 86 |
| 80 this.mutationObserver_.observe(this, { | 87 this.mutationObserver_.observe(this, { |
| 81 attributes: true, | 88 attributes: true, |
| 82 attributeFilter: ['open'], | 89 attributeFilter: ['open'], |
| 83 }); | 90 }); |
| 84 }, | 91 }, |
| 85 | 92 |
| 86 /** @override */ | 93 /** @override */ |
| 87 detached: function() { | 94 detached: function() { |
| 88 this.removeIntersectionObserver_(); | 95 this.removeIntersectionObserver_(); |
| 89 if (this.mutationObserver_) { | 96 if (this.mutationObserver_) { |
| 90 this.mutationObserver_.disconnect(); | 97 this.mutationObserver_.disconnect(); |
| 91 this.mutationObserver_ = null; | 98 this.mutationObserver_ = null; |
| 92 } | 99 } |
| 100 window.removeEventListener('resize', this.onResize_); | |
| 101 }, | |
| 102 | |
| 103 adjustHeight_: function() { | |
|
dpapad
2017/04/27 22:33:24
Can we add a test for the auto-shrink logic? Also,
scottchen
2017/04/28 23:12:55
It did not jump around.
However, based on our off
| |
| 104 // Try to first render with just CSS styling. | |
| 105 this.$$('.body-container').style.maxHeight = null; | |
| 106 | |
| 107 var delta = this.offsetHeight - window.innerHeight; | |
| 108 | |
| 109 // If dialog is taller than visible viewport height, shrink by difference. | |
| 110 if (delta > 0) { | |
| 111 this.$$('.body-container').style.maxHeight = | |
| 112 (this.$$('.body-container').offsetHeight - delta) + 'px'; | |
| 113 } | |
| 93 }, | 114 }, |
| 94 | 115 |
| 95 /** @private */ | 116 /** @private */ |
| 96 addIntersectionObserver_: function() { | 117 addIntersectionObserver_: function() { |
| 97 if (this.intersectionObserver_) | 118 if (this.intersectionObserver_) |
| 98 return; | 119 return; |
| 99 | 120 |
| 100 var bodyContainer = this.$$('.body-container'); | 121 var bodyContainer = this.$$('.body-container'); |
| 101 | 122 |
| 102 var bottomMarker = this.$.bodyBottomMarker; | 123 var bottomMarker = this.$.bodyBottomMarker; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 165 return; | 186 return; |
| 166 | 187 |
| 167 var actionButton = | 188 var actionButton = |
| 168 this.querySelector('.action-button:not([disabled]):not([hidden])'); | 189 this.querySelector('.action-button:not([disabled]):not([hidden])'); |
| 169 if (actionButton) { | 190 if (actionButton) { |
| 170 actionButton.click(); | 191 actionButton.click(); |
| 171 e.preventDefault(); | 192 e.preventDefault(); |
| 172 } | 193 } |
| 173 }, | 194 }, |
| 174 }); | 195 }); |
| OLD | NEW |