jellyfin-web/dashboard-ui/bower_components/iron-overlay-behavior/iron-overlay-manager.html

403 lines
12 KiB
HTML
Raw Normal View History

2015-06-19 09:36:51 -07:00
<!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<link rel="import" href="../polymer/polymer.html">
2016-03-19 12:39:52 -07:00
<link rel="import" href="../iron-a11y-keys-behavior/iron-a11y-keys-behavior.html">
2016-05-03 19:18:05 -07:00
<link rel="import" href="iron-overlay-backdrop.html">
2015-06-19 09:36:51 -07:00
<script>
2016-01-29 19:43:11 -07:00
/**
* @struct
* @constructor
2016-03-11 20:29:37 -07:00
* @private
2016-01-29 19:43:11 -07:00
*/
Polymer.IronOverlayManagerClass = function() {
2016-03-11 20:29:37 -07:00
/**
* Used to keep track of the opened overlays.
* @private {Array<Element>}
*/
2016-01-29 19:43:11 -07:00
this._overlays = [];
2016-03-11 20:29:37 -07:00
2016-01-29 19:43:11 -07:00
/**
2016-03-11 20:29:37 -07:00
* iframes have a default z-index of 100,
* so this default should be at least that.
2016-01-29 19:43:11 -07:00
* @private {number}
*/
this._minimumZ = 101;
2015-06-19 09:36:51 -07:00
2016-03-11 20:29:37 -07:00
/**
* Memoized backdrop element.
* @private {Element|null}
*/
2016-02-10 12:16:48 -07:00
this._backdropElement = null;
2016-03-19 12:39:52 -07:00
2016-04-04 18:23:42 -07:00
// Listen to mousedown or touchstart to be sure to be the first to capture
// clicks outside the overlay.
var clickEvent = ('ontouchstart' in window) ? 'touchstart' : 'mousedown';
document.addEventListener(clickEvent, this._onCaptureClick.bind(this), true);
2016-03-19 12:39:52 -07:00
document.addEventListener('focus', this._onCaptureFocus.bind(this), true);
document.addEventListener('keydown', this._onCaptureKeyDown.bind(this), true);
2016-03-11 20:29:37 -07:00
};
Polymer.IronOverlayManagerClass.prototype = {
constructor: Polymer.IronOverlayManagerClass,
/**
* The shared backdrop element.
2016-04-15 12:20:04 -07:00
* @type {!Element} backdropElement
2016-03-11 20:29:37 -07:00
*/
get backdropElement() {
if (!this._backdropElement) {
this._backdropElement = document.createElement('iron-overlay-backdrop');
}
return this._backdropElement;
},
2016-02-23 22:36:48 -07:00
/**
* The deepest active element.
2016-04-15 12:20:04 -07:00
* @type {!Element} activeElement the active element
2016-03-11 20:29:37 -07:00
*/
get deepActiveElement() {
// document.activeElement can be null
// https://developer.mozilla.org/en-US/docs/Web/API/Document/activeElement
2016-03-19 12:39:52 -07:00
// In case of null, default it to document.body.
var active = document.activeElement || document.body;
while (active.root && Polymer.dom(active.root).activeElement) {
2016-03-11 20:29:37 -07:00
active = Polymer.dom(active.root).activeElement;
}
return active;
},
2016-02-23 22:36:48 -07:00
2016-03-22 20:04:42 -07:00
/**
* Brings the overlay at the specified index to the front.
* @param {number} i
* @private
*/
_bringOverlayAtIndexToFront: function(i) {
var overlay = this._overlays[i];
2016-04-15 12:20:04 -07:00
if (!overlay) {
return;
}
2016-03-22 20:04:42 -07:00
var lastI = this._overlays.length - 1;
2016-04-15 12:20:04 -07:00
var currentOverlay = this._overlays[lastI];
2016-03-30 19:00:05 -07:00
// Ensure always-on-top overlay stays on top.
2016-04-15 12:20:04 -07:00
if (currentOverlay && this._shouldBeBehindOverlay(overlay, currentOverlay)) {
2016-03-30 19:00:05 -07:00
lastI--;
}
2016-03-22 20:04:42 -07:00
// If already the top element, return.
2016-04-15 12:20:04 -07:00
if (i >= lastI) {
2016-03-22 20:04:42 -07:00
return;
}
// Update z-index to be on top.
var minimumZ = Math.max(this.currentOverlayZ(), this._minimumZ);
if (this._getZ(overlay) <= minimumZ) {
this._applyOverlayZ(overlay, minimumZ);
}
// Shift other overlays behind the new on top.
while (i < lastI) {
this._overlays[i] = this._overlays[i + 1];
i++;
}
this._overlays[lastI] = overlay;
},
/**
* Adds the overlay and updates its z-index if it's opened, or removes it if it's closed.
* Also updates the backdrop z-index.
2016-04-15 12:20:04 -07:00
* @param {!Element} overlay
2016-03-22 20:04:42 -07:00
*/
addOrRemoveOverlay: function(overlay) {
if (overlay.opened) {
this.addOverlay(overlay);
} else {
this.removeOverlay(overlay);
}
this.trackBackdrop();
},
2016-03-11 20:29:37 -07:00
/**
* Tracks overlays for z-index and focus management.
2016-03-30 19:00:05 -07:00
* Ensures the last added overlay with always-on-top remains on top.
2016-04-15 12:20:04 -07:00
* @param {!Element} overlay
2016-03-11 20:29:37 -07:00
*/
addOverlay: function(overlay) {
2016-03-22 20:04:42 -07:00
var i = this._overlays.indexOf(overlay);
if (i >= 0) {
this._bringOverlayAtIndexToFront(i);
return;
}
2016-03-30 19:00:05 -07:00
var insertionIndex = this._overlays.length;
var currentOverlay = this._overlays[insertionIndex - 1];
var minimumZ = Math.max(this._getZ(currentOverlay), this._minimumZ);
var newZ = this._getZ(overlay);
// Ensure always-on-top overlay stays on top.
2016-04-15 12:20:04 -07:00
if (currentOverlay && this._shouldBeBehindOverlay(overlay, currentOverlay)) {
2016-03-30 19:00:05 -07:00
// This bumps the z-index of +2.
this._applyOverlayZ(currentOverlay, minimumZ);
insertionIndex--;
// Update minimumZ to match previous overlay's z-index.
var previousOverlay = this._overlays[insertionIndex - 1];
minimumZ = Math.max(this._getZ(previousOverlay), this._minimumZ);
}
// Update z-index and insert overlay.
2016-03-11 20:29:37 -07:00
if (newZ <= minimumZ) {
this._applyOverlayZ(overlay, minimumZ);
}
2016-03-30 19:00:05 -07:00
this._overlays.splice(insertionIndex, 0, overlay);
// Get focused node.
2016-03-11 20:29:37 -07:00
var element = this.deepActiveElement;
2016-03-19 12:39:52 -07:00
overlay.restoreFocusNode = this._overlayParent(element) ? null : element;
2016-03-11 20:29:37 -07:00
},
2015-12-14 08:43:03 -07:00
2016-03-11 20:29:37 -07:00
/**
2016-04-15 12:20:04 -07:00
* @param {!Element} overlay
2016-03-11 20:29:37 -07:00
*/
removeOverlay: function(overlay) {
var i = this._overlays.indexOf(overlay);
2016-03-22 20:04:42 -07:00
if (i === -1) {
return;
}
this._overlays.splice(i, 1);
var node = overlay.restoreFocusOnClose ? overlay.restoreFocusNode : null;
overlay.restoreFocusNode = null;
// Focus back only if still contained in document.body
if (node && Polymer.dom(document.body).deepContains(node)) {
node.focus();
2016-02-23 22:36:48 -07:00
}
2016-03-11 20:29:37 -07:00
},
2015-06-19 09:36:51 -07:00
2016-03-11 20:29:37 -07:00
/**
2016-03-19 12:39:52 -07:00
* Returns the current overlay.
* @return {Element|undefined}
2016-03-11 20:29:37 -07:00
*/
currentOverlay: function() {
var i = this._overlays.length - 1;
return this._overlays[i];
},
2015-06-19 09:36:51 -07:00
2016-03-11 20:29:37 -07:00
/**
2016-03-19 12:39:52 -07:00
* Returns the current overlay z-index.
* @return {number}
2016-03-11 20:29:37 -07:00
*/
currentOverlayZ: function() {
2016-03-19 12:39:52 -07:00
return this._getZ(this.currentOverlay());
2016-03-11 20:29:37 -07:00
},
2015-06-19 09:36:51 -07:00
2016-03-11 20:29:37 -07:00
/**
* Ensures that the minimum z-index of new overlays is at least `minimumZ`.
* This does not effect the z-index of any existing overlays.
* @param {number} minimumZ
*/
ensureMinimumZ: function(minimumZ) {
this._minimumZ = Math.max(this._minimumZ, minimumZ);
},
2015-12-14 08:43:03 -07:00
2016-03-11 20:29:37 -07:00
focusOverlay: function() {
var current = /** @type {?} */ (this.currentOverlay());
// We have to be careful to focus the next overlay _after_ any current
// transitions are complete (due to the state being toggled prior to the
// transition). Otherwise, we risk infinite recursion when a transitioning
// (closed) overlay becomes the current overlay.
//
// NOTE: We make the assumption that any overlay that completes a transition
// will call into focusOverlay to kick the process back off. Currently:
// transitionend -> _applyFocus -> focusOverlay.
if (current && !current.transitioning) {
current._applyFocus();
}
},
2015-06-19 09:36:51 -07:00
2016-03-11 20:29:37 -07:00
/**
2016-03-22 20:04:42 -07:00
* Updates the backdrop z-index.
2016-03-11 20:29:37 -07:00
*/
2016-03-22 20:04:42 -07:00
trackBackdrop: function() {
2016-05-03 19:18:05 -07:00
var overlay = this._overlayWithBackdrop();
// Avoid creating the backdrop if there is no overlay with backdrop.
if (!overlay && !this._backdropElement) {
return;
}
this.backdropElement.style.zIndex = this._getZ(overlay) - 1;
this.backdropElement.opened = !!overlay;
2016-03-11 20:29:37 -07:00
},
2015-06-19 09:36:51 -07:00
2016-03-11 20:29:37 -07:00
/**
2016-03-19 12:39:52 -07:00
* @return {Array<Element>}
2016-03-11 20:29:37 -07:00
*/
getBackdrops: function() {
2016-03-19 12:39:52 -07:00
var backdrops = [];
for (var i = 0; i < this._overlays.length; i++) {
if (this._overlays[i].withBackdrop) {
backdrops.push(this._overlays[i]);
}
}
return backdrops;
2016-03-11 20:29:37 -07:00
},
2015-06-19 09:36:51 -07:00
2016-03-11 20:29:37 -07:00
/**
2016-03-19 12:39:52 -07:00
* Returns the z-index for the backdrop.
* @return {number}
2016-03-11 20:29:37 -07:00
*/
backdropZ: function() {
2016-03-19 12:39:52 -07:00
return this._getZ(this._overlayWithBackdrop()) - 1;
2016-03-11 20:29:37 -07:00
},
2015-06-19 09:36:51 -07:00
2016-03-11 20:29:37 -07:00
/**
2016-03-19 12:39:52 -07:00
* Returns the first opened overlay that has a backdrop.
* @return {Element|undefined}
2016-03-11 20:29:37 -07:00
* @private
*/
_overlayWithBackdrop: function() {
for (var i = 0; i < this._overlays.length; i++) {
2016-03-19 12:39:52 -07:00
if (this._overlays[i].withBackdrop) {
2016-03-11 20:29:37 -07:00
return this._overlays[i];
}
2016-01-29 19:43:11 -07:00
}
2016-03-11 20:29:37 -07:00
},
2016-01-29 19:43:11 -07:00
2016-03-11 20:29:37 -07:00
/**
* Calculates the minimum z-index for the overlay.
* @param {Element=} overlay
* @private
*/
2016-03-19 12:39:52 -07:00
_getZ: function(overlay) {
2016-03-11 20:29:37 -07:00
var z = this._minimumZ;
if (overlay) {
2016-04-04 18:23:42 -07:00
var z1 = Number(overlay.style.zIndex || window.getComputedStyle(overlay).zIndex);
2016-03-11 20:29:37 -07:00
// Check if is a number
// Number.isNaN not supported in IE 10+
if (z1 === z1) {
z = z1;
}
2016-01-29 19:43:11 -07:00
}
2016-03-11 20:29:37 -07:00
return z;
2016-03-19 12:39:52 -07:00
},
/**
2016-04-15 12:20:04 -07:00
* @param {!Element} element
2016-03-19 12:39:52 -07:00
* @param {number|string} z
* @private
*/
_setZ: function(element, z) {
element.style.zIndex = z;
},
/**
2016-04-15 12:20:04 -07:00
* @param {!Element} overlay
2016-03-19 12:39:52 -07:00
* @param {number} aboveZ
* @private
*/
_applyOverlayZ: function(overlay, aboveZ) {
this._setZ(overlay, aboveZ + 2);
},
/**
* Returns the overlay containing the provided node. If the node is an overlay,
* it returns the node.
* @param {Element=} node
* @return {Element|undefined}
* @private
*/
_overlayParent: function(node) {
while (node && node !== document.body) {
// Check if it is an overlay.
if (node._manager === this) {
return node;
}
// Use logical parentNode, or native ShadowRoot host.
node = Polymer.dom(node).parentNode || node.host;
}
},
2016-03-27 16:22:53 -07:00
/**
* Returns the deepest overlay in the path.
2016-04-02 09:15:48 -07:00
* @param {Array<Element>=} path
2016-03-27 16:22:53 -07:00
* @return {Element|undefined}
* @private
*/
_overlayInPath: function(path) {
path = path || [];
for (var i = 0; i < path.length; i++) {
if (path[i]._manager === this) {
return path[i];
}
}
},
2016-03-19 12:39:52 -07:00
/**
* Ensures the click event is delegated to the right overlay.
* @param {!Event} event
* @private
*/
_onCaptureClick: function(event) {
var overlay = /** @type {?} */ (this.currentOverlay());
2016-03-27 16:22:53 -07:00
// Check if clicked outside of top overlay.
if (overlay && this._overlayInPath(Polymer.dom(event).path) !== overlay) {
2016-04-26 21:22:32 -07:00
if (overlay.withBackdrop) {
// There's no need to stop the propagation as the backdrop element
// already got this mousedown/touchstart event. Calling preventDefault
// on this event ensures that click/tap won't be triggered at all.
event.preventDefault();
}
2016-03-19 12:39:52 -07:00
overlay._onCaptureClick(event);
}
},
/**
* Ensures the focus event is delegated to the right overlay.
* @param {!Event} event
* @private
*/
_onCaptureFocus: function(event) {
var overlay = /** @type {?} */ (this.currentOverlay());
if (overlay) {
overlay._onCaptureFocus(event);
}
},
/**
* Ensures TAB and ESC keyboard events are delegated to the right overlay.
* @param {!Event} event
* @private
*/
_onCaptureKeyDown: function(event) {
var overlay = /** @type {?} */ (this.currentOverlay());
if (overlay) {
if (Polymer.IronA11yKeysBehavior.keyboardEventMatchesKeys(event, 'esc')) {
overlay._onCaptureEsc(event);
} else if (Polymer.IronA11yKeysBehavior.keyboardEventMatchesKeys(event, 'tab')) {
overlay._onCaptureTab(event);
}
}
2016-04-15 12:20:04 -07:00
},
/**
* Returns if the overlay1 should be behind overlay2.
* @param {!Element} overlay1
* @param {!Element} overlay2
* @return {boolean}
* @private
*/
_shouldBeBehindOverlay: function(overlay1, overlay2) {
var o1 = /** @type {?} */ (overlay1);
var o2 = /** @type {?} */ (overlay2);
return !o1.alwaysOnTop && o2.alwaysOnTop;
2016-01-29 19:43:11 -07:00
}
};
Polymer.IronOverlayManager = new Polymer.IronOverlayManagerClass();
2015-06-19 09:36:51 -07:00
</script>