OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 /** | |
5 * @unrestricted | |
6 */ | |
7 UI.SwatchPopoverHelper = class extends Common.Object { | |
8 constructor() { | |
9 super(); | |
10 this._popover = new UI.Popover(); | |
11 this._popover.setCanShrink(false); | |
12 this._popover.setNoPadding(true); | |
13 this._popover.element.addEventListener('mousedown', (e) => e.consume(), fals
e); | |
14 | |
15 this._hideProxy = this.hide.bind(this, true); | |
16 this._boundOnKeyDown = this._onKeyDown.bind(this); | |
17 this._boundFocusOut = this._onFocusOut.bind(this); | |
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 {!UI.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 view.focus(); | |
57 | |
58 var document = this._popover.element.ownerDocument; | |
59 document.addEventListener('mousedown', this._hideProxy, false); | |
60 document.defaultView.addEventListener('resize', this._hideProxy, false); | |
61 this._view.contentElement.addEventListener('keydown', this._boundOnKeyDown,
false); | |
62 } | |
63 | |
64 reposition() { | |
65 // Unbind "blur" listener to avoid reenterability: |popover.showView| will h
ide the popover and trigger it synchronously. | |
66 this._view.contentElement.removeEventListener('focusout', this._boundFocusOu
t, false); | |
67 this._popover.showView(this._view, this._anchorElement); | |
68 this._view.contentElement.addEventListener('focusout', this._boundFocusOut,
false); | |
69 if (!this._focusRestorer) | |
70 this._focusRestorer = new UI.WidgetFocusRestorer(this._view); | |
71 } | |
72 | |
73 /** | |
74 * @param {boolean=} commitEdit | |
75 */ | |
76 hide(commitEdit) { | |
77 if (this._isHidden) | |
78 return; | |
79 var document = this._popover.element.ownerDocument; | |
80 this._isHidden = true; | |
81 this._popover.hide(); | |
82 | |
83 document.removeEventListener('mousedown', this._hideProxy, false); | |
84 document.defaultView.removeEventListener('resize', this._hideProxy, false); | |
85 | |
86 if (this._hiddenCallback) | |
87 this._hiddenCallback.call(null, !!commitEdit); | |
88 | |
89 this._focusRestorer.restore(); | |
90 delete this._anchorElement; | |
91 if (this._view) { | |
92 this._view.detach(); | |
93 this._view.contentElement.removeEventListener('keydown', this._boundOnKeyD
own, false); | |
94 this._view.contentElement.removeEventListener('focusout', this._boundFocus
Out, false); | |
95 delete this._view; | |
96 } | |
97 } | |
98 | |
99 /** | |
100 * @param {!Event} event | |
101 */ | |
102 _onKeyDown(event) { | |
103 if (event.key === 'Enter') { | |
104 this.hide(true); | |
105 event.consume(true); | |
106 return; | |
107 } | |
108 if (event.key === 'Escape') { | |
109 this.hide(false); | |
110 event.consume(true); | |
111 } | |
112 } | |
113 }; | |
OLD | NEW |