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 | |
5 /** | 4 /** |
6 * @constructor | 5 * @unrestricted |
7 * @extends {WebInspector.Object} | |
8 */ | 6 */ |
9 WebInspector.SwatchPopoverHelper = function() | 7 WebInspector.SwatchPopoverHelper = class extends WebInspector.Object { |
10 { | 8 constructor() { |
| 9 super(); |
11 this._popover = new WebInspector.Popover(); | 10 this._popover = new WebInspector.Popover(); |
12 this._popover.setCanShrink(false); | 11 this._popover.setCanShrink(false); |
13 this._popover.setNoPadding(true); | 12 this._popover.setNoPadding(true); |
14 this._popover.element.addEventListener("mousedown", (e) => e.consume(), fals
e); | 13 this._popover.element.addEventListener('mousedown', (e) => e.consume(), fals
e); |
15 | 14 |
16 this._hideProxy = this.hide.bind(this, true); | 15 this._hideProxy = this.hide.bind(this, true); |
17 this._boundOnKeyDown = this._onKeyDown.bind(this); | 16 this._boundOnKeyDown = this._onKeyDown.bind(this); |
18 this._boundFocusOut = this._onFocusOut.bind(this); | 17 this._boundFocusOut = this._onFocusOut.bind(this); |
19 this._isHidden = true; | 18 this._isHidden = true; |
| 19 } |
| 20 |
| 21 /** |
| 22 * @param {!Event} event |
| 23 */ |
| 24 _onFocusOut(event) { |
| 25 if (!event.relatedTarget || event.relatedTarget.isSelfOrDescendant(this._vie
w.contentElement)) |
| 26 return; |
| 27 this._hideProxy(); |
| 28 } |
| 29 |
| 30 /** |
| 31 * @return {boolean} |
| 32 */ |
| 33 isShowing() { |
| 34 return this._popover.isShowing(); |
| 35 } |
| 36 |
| 37 /** |
| 38 * @param {!WebInspector.Widget} view |
| 39 * @param {!Element} anchorElement |
| 40 * @param {function(boolean)=} hiddenCallback |
| 41 */ |
| 42 show(view, anchorElement, hiddenCallback) { |
| 43 if (this._popover.isShowing()) { |
| 44 if (this._anchorElement === anchorElement) |
| 45 return; |
| 46 |
| 47 // Reopen the picker for another anchor element. |
| 48 this.hide(true); |
| 49 } |
| 50 |
| 51 delete this._isHidden; |
| 52 this._anchorElement = anchorElement; |
| 53 this._view = view; |
| 54 this._hiddenCallback = hiddenCallback; |
| 55 this.reposition(); |
| 56 |
| 57 var document = this._popover.element.ownerDocument; |
| 58 document.addEventListener('mousedown', this._hideProxy, false); |
| 59 document.defaultView.addEventListener('resize', this._hideProxy, false); |
| 60 this._view.contentElement.addEventListener('keydown', this._boundOnKeyDown,
false); |
| 61 } |
| 62 |
| 63 reposition() { |
| 64 // Unbind "blur" listener to avoid reenterability: |popover.showView| will h
ide the popover and trigger it synchronously. |
| 65 this._view.contentElement.removeEventListener('focusout', this._boundFocusOu
t, false); |
| 66 this._popover.showView(this._view, this._anchorElement); |
| 67 this._view.contentElement.addEventListener('focusout', this._boundFocusOut,
false); |
| 68 if (!this._focusRestorer) |
| 69 this._focusRestorer = new WebInspector.WidgetFocusRestorer(this._view); |
| 70 } |
| 71 |
| 72 /** |
| 73 * @param {boolean=} commitEdit |
| 74 */ |
| 75 hide(commitEdit) { |
| 76 if (this._isHidden) |
| 77 return; |
| 78 var document = this._popover.element.ownerDocument; |
| 79 this._isHidden = true; |
| 80 this._popover.hide(); |
| 81 |
| 82 document.removeEventListener('mousedown', this._hideProxy, false); |
| 83 document.defaultView.removeEventListener('resize', this._hideProxy, false); |
| 84 |
| 85 if (this._hiddenCallback) |
| 86 this._hiddenCallback.call(null, !!commitEdit); |
| 87 |
| 88 this._focusRestorer.restore(); |
| 89 delete this._anchorElement; |
| 90 if (this._view) { |
| 91 this._view.detach(); |
| 92 this._view.contentElement.removeEventListener('keydown', this._boundOnKeyD
own, false); |
| 93 this._view.contentElement.removeEventListener('focusout', this._boundFocus
Out, false); |
| 94 delete this._view; |
| 95 } |
| 96 } |
| 97 |
| 98 /** |
| 99 * @param {!Event} event |
| 100 */ |
| 101 _onKeyDown(event) { |
| 102 if (event.key === 'Enter') { |
| 103 this.hide(true); |
| 104 event.consume(true); |
| 105 return; |
| 106 } |
| 107 if (event.key === 'Escape') { |
| 108 this.hide(false); |
| 109 event.consume(true); |
| 110 } |
| 111 } |
20 }; | 112 }; |
21 | |
22 WebInspector.SwatchPopoverHelper.prototype = { | |
23 /** | |
24 * @param {!Event} event | |
25 */ | |
26 _onFocusOut: function(event) | |
27 { | |
28 if (!event.relatedTarget || event.relatedTarget.isSelfOrDescendant(this.
_view.contentElement)) | |
29 return; | |
30 this._hideProxy(); | |
31 }, | |
32 | |
33 /** | |
34 * @return {boolean} | |
35 */ | |
36 isShowing: function() | |
37 { | |
38 return this._popover.isShowing(); | |
39 }, | |
40 | |
41 /** | |
42 * @param {!WebInspector.Widget} view | |
43 * @param {!Element} anchorElement | |
44 * @param {function(boolean)=} hiddenCallback | |
45 */ | |
46 show: function(view, anchorElement, hiddenCallback) | |
47 { | |
48 if (this._popover.isShowing()) { | |
49 if (this._anchorElement === anchorElement) | |
50 return; | |
51 | |
52 // Reopen the picker for another anchor element. | |
53 this.hide(true); | |
54 } | |
55 | |
56 delete this._isHidden; | |
57 this._anchorElement = anchorElement; | |
58 this._view = view; | |
59 this._hiddenCallback = hiddenCallback; | |
60 this.reposition(); | |
61 | |
62 var document = this._popover.element.ownerDocument; | |
63 document.addEventListener("mousedown", this._hideProxy, false); | |
64 document.defaultView.addEventListener("resize", this._hideProxy, false); | |
65 this._view.contentElement.addEventListener("keydown", this._boundOnKeyDo
wn, false); | |
66 }, | |
67 | |
68 reposition: function() | |
69 { | |
70 // Unbind "blur" listener to avoid reenterability: |popover.showView| wi
ll hide the popover and trigger it synchronously. | |
71 this._view.contentElement.removeEventListener("focusout", this._boundFoc
usOut, false); | |
72 this._popover.showView(this._view, this._anchorElement); | |
73 this._view.contentElement.addEventListener("focusout", this._boundFocusO
ut, false); | |
74 if (!this._focusRestorer) | |
75 this._focusRestorer = new WebInspector.WidgetFocusRestorer(this._vie
w); | |
76 }, | |
77 | |
78 /** | |
79 * @param {boolean=} commitEdit | |
80 */ | |
81 hide: function(commitEdit) | |
82 { | |
83 if (this._isHidden) | |
84 return; | |
85 var document = this._popover.element.ownerDocument; | |
86 this._isHidden = true; | |
87 this._popover.hide(); | |
88 | |
89 document.removeEventListener("mousedown", this._hideProxy, false); | |
90 document.defaultView.removeEventListener("resize", this._hideProxy, fals
e); | |
91 | |
92 if (this._hiddenCallback) | |
93 this._hiddenCallback.call(null, !!commitEdit); | |
94 | |
95 this._focusRestorer.restore(); | |
96 delete this._anchorElement; | |
97 if (this._view) { | |
98 this._view.detach(); | |
99 this._view.contentElement.removeEventListener("keydown", this._bound
OnKeyDown, false); | |
100 this._view.contentElement.removeEventListener("focusout", this._boun
dFocusOut, false); | |
101 delete this._view; | |
102 } | |
103 }, | |
104 | |
105 /** | |
106 * @param {!Event} event | |
107 */ | |
108 _onKeyDown: function(event) | |
109 { | |
110 if (event.key === "Enter") { | |
111 this.hide(true); | |
112 event.consume(true); | |
113 return; | |
114 } | |
115 if (event.key === "Escape") { | |
116 this.hide(false); | |
117 event.consume(true); | |
118 } | |
119 }, | |
120 | |
121 __proto__: WebInspector.Object.prototype | |
122 }; | |
OLD | NEW |