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