1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @unrestricted
*/
InlineEditor.SwatchPopoverHelper = class extends Common.Object {
constructor() {
super();
this._popover = new UI.Popover();
this._popover.setCanShrink(false);
this._popover.setNoPadding(true);
this._popover.element.addEventListener('mousedown', (e) => e.consume(), false);
this._hideProxy = this.hide.bind(this, true);
this._boundOnKeyDown = this._onKeyDown.bind(this);
this._boundFocusOut = this._onFocusOut.bind(this);
this._isHidden = true;
}
/**
* @param {!Event} event
*/
_onFocusOut(event) {
if (!event.relatedTarget || event.relatedTarget.isSelfOrDescendant(this._view.contentElement))
return;
this._hideProxy();
}
/**
* @return {boolean}
*/
isShowing() {
return this._popover.isShowing();
}
/**
* @param {!UI.Widget} view
* @param {!Element} anchorElement
* @param {function(boolean)=} hiddenCallback
*/
show(view, anchorElement, hiddenCallback) {
if (this._popover.isShowing()) {
if (this._anchorElement === anchorElement)
return;
// Reopen the picker for another anchor element.
this.hide(true);
}
delete this._isHidden;
this._anchorElement = anchorElement;
this._view = view;
this._hiddenCallback = hiddenCallback;
this.reposition();
view.focus();
var document = this._popover.element.ownerDocument;
document.addEventListener('mousedown', this._hideProxy, false);
document.defaultView.addEventListener('resize', this._hideProxy, false);
this._view.contentElement.addEventListener('keydown', this._boundOnKeyDown, false);
}
reposition() {
// Unbind "blur" listener to avoid reenterability: |popover.showView| will hide the popover and trigger it synchronously.
this._view.contentElement.removeEventListener('focusout', this._boundFocusOut, false);
this._popover.showView(this._view, this._anchorElement);
this._view.contentElement.addEventListener('focusout', this._boundFocusOut, false);
if (!this._focusRestorer)
this._focusRestorer = new UI.WidgetFocusRestorer(this._view);
}
/**
* @param {boolean=} commitEdit
*/
hide(commitEdit) {
if (this._isHidden)
return;
var document = this._popover.element.ownerDocument;
this._isHidden = true;
this._popover.hide();
document.removeEventListener('mousedown', this._hideProxy, false);
document.defaultView.removeEventListener('resize', this._hideProxy, false);
if (this._hiddenCallback)
this._hiddenCallback.call(null, !!commitEdit);
this._focusRestorer.restore();
delete this._anchorElement;
if (this._view) {
this._view.detach();
this._view.contentElement.removeEventListener('keydown', this._boundOnKeyDown, false);
this._view.contentElement.removeEventListener('focusout', this._boundFocusOut, false);
delete this._view;
}
}
/**
* @param {!Event} event
*/
_onKeyDown(event) {
if (event.key === 'Enter') {
this.hide(true);
event.consume(true);
return;
}
if (event.key === 'Escape') {
this.hide(false);
event.consume(true);
}
}
};
|