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

212 lines
6.6 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">
<script>
2016-01-29 19:43:11 -07:00
/**
* @struct
* @constructor
*/
Polymer.IronOverlayManagerClass = function() {
this._overlays = [];
2016-02-23 22:36:48 -07:00
// Used to keep track of the last focused node before an overlay gets opened.
this._lastFocusedNodes = [];
2016-01-29 19:43:11 -07:00
/**
* iframes have a default z-index of 100, so this default should be at least
* that.
* @private {number}
*/
this._minimumZ = 101;
2015-06-19 09:36:51 -07:00
2016-01-29 19:43:11 -07:00
this._backdrops = [];
2016-02-10 12:16:48 -07:00
this._backdropElement = null;
Object.defineProperty(this, 'backdropElement', {
get: function() {
if (!this._backdropElement) {
this._backdropElement = document.createElement('iron-overlay-backdrop');
}
return this._backdropElement;
}.bind(this)
});
2016-02-23 22:36:48 -07:00
/**
* The deepest active element.
* returns {?Node} element the active element
*/
this.deepActiveElement = null;
Object.defineProperty(this, 'deepActiveElement', {
get: function() {
var active = document.activeElement;
// document.activeElement can be null
// https://developer.mozilla.org/en-US/docs/Web/API/Document/activeElement
while (active && active.root && Polymer.dom(active.root).activeElement) {
active = Polymer.dom(active.root).activeElement;
}
return active;
}.bind(this)
});
};
/**
* If a node is contained in an overlay.
* @private
* @param {Node} node
* @returns {Boolean}
*/
Polymer.IronOverlayManagerClass.prototype._isChildOfOverlay = function(node) {
while (node && node !== document.body) {
// Use logical parentNode, or native ShadowRoot host.
node = Polymer.dom(node).parentNode || node.host;
// Check if it is an overlay.
if (node && node.behaviors && node.behaviors.indexOf(Polymer.IronOverlayBehaviorImpl) !== -1) {
return true;
}
}
return false;
};
2015-12-14 08:43:03 -07:00
2016-01-29 19:43:11 -07:00
Polymer.IronOverlayManagerClass.prototype._applyOverlayZ = function(overlay, aboveZ) {
this._setZ(overlay, aboveZ + 2);
};
2015-12-14 08:43:03 -07:00
2016-01-29 19:43:11 -07:00
Polymer.IronOverlayManagerClass.prototype._setZ = function(element, z) {
element.style.zIndex = z;
};
2015-12-14 08:43:03 -07:00
2016-01-29 19:43:11 -07:00
/**
* track overlays for z-index and focus managemant
*/
Polymer.IronOverlayManagerClass.prototype.addOverlay = function(overlay) {
var minimumZ = Math.max(this.currentOverlayZ(), this._minimumZ);
this._overlays.push(overlay);
var newZ = this.currentOverlayZ();
if (newZ <= minimumZ) {
this._applyOverlayZ(overlay, minimumZ);
}
2016-02-23 22:36:48 -07:00
var element = this.deepActiveElement;
// If already in other overlay, don't reset focus there.
if (this._isChildOfOverlay(element)) {
element = null;
}
this._lastFocusedNodes.push(element);
2016-01-29 19:43:11 -07:00
};
2015-12-14 08:43:03 -07:00
2016-01-29 19:43:11 -07:00
Polymer.IronOverlayManagerClass.prototype.removeOverlay = function(overlay) {
var i = this._overlays.indexOf(overlay);
if (i >= 0) {
this._overlays.splice(i, 1);
this._setZ(overlay, '');
2016-02-23 22:36:48 -07:00
var node = this._lastFocusedNodes[i];
// Focus only if still contained in document.body
if (overlay.restoreFocusOnClose && node && Polymer.dom(document.body).deepContains(node)) {
node.focus();
}
this._lastFocusedNodes.splice(i, 1);
2016-01-29 19:43:11 -07:00
}
};
2015-06-19 09:36:51 -07:00
2016-01-29 19:43:11 -07:00
Polymer.IronOverlayManagerClass.prototype.currentOverlay = function() {
var i = this._overlays.length - 1;
while (this._overlays[i] && !this._overlays[i].opened) {
--i;
}
return this._overlays[i];
};
2015-06-19 09:36:51 -07:00
2016-01-29 19:43:11 -07:00
Polymer.IronOverlayManagerClass.prototype.currentOverlayZ = function() {
return this._getOverlayZ(this.currentOverlay());
};
2015-06-19 09:36:51 -07:00
2016-01-29 19:43:11 -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
*/
Polymer.IronOverlayManagerClass.prototype.ensureMinimumZ = function(minimumZ) {
this._minimumZ = Math.max(this._minimumZ, minimumZ);
};
2015-12-14 08:43:03 -07:00
2016-01-29 19:43:11 -07:00
Polymer.IronOverlayManagerClass.prototype.focusOverlay = function() {
var current = 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-01-29 19:43:11 -07:00
Polymer.IronOverlayManagerClass.prototype.trackBackdrop = function(element) {
// backdrops contains the overlays with a backdrop that are currently
// visible
var index = this._backdrops.indexOf(element);
if (element.opened && element.withBackdrop) {
// no duplicates
if (index === -1) {
2015-12-14 08:43:03 -07:00
this._backdrops.push(element);
2015-06-19 09:36:51 -07:00
}
2016-01-29 19:43:11 -07:00
} else if (index >= 0) {
this._backdrops.splice(index, 1);
}
};
2015-06-19 09:36:51 -07:00
2016-01-29 19:43:11 -07:00
Polymer.IronOverlayManagerClass.prototype.getBackdrops = function() {
return this._backdrops;
};
2015-06-19 09:36:51 -07:00
2016-01-29 19:43:11 -07:00
/**
* Returns the z-index for the backdrop.
*/
Polymer.IronOverlayManagerClass.prototype.backdropZ = function() {
return this._getOverlayZ(this._overlayWithBackdrop()) - 1;
2015-12-14 08:43:03 -07:00
};
2015-06-19 09:36:51 -07:00
2016-01-29 19:43:11 -07:00
/**
* Returns the first opened overlay that has a backdrop.
*/
Polymer.IronOverlayManagerClass.prototype._overlayWithBackdrop = function() {
for (var i = 0; i < this._overlays.length; i++) {
if (this._overlays[i].opened && this._overlays[i].withBackdrop) {
return this._overlays[i];
}
}
};
/**
* Calculates the minimum z-index for the overlay.
*/
Polymer.IronOverlayManagerClass.prototype._getOverlayZ = function(overlay) {
var z = this._minimumZ;
if (overlay) {
var z1 = Number(window.getComputedStyle(overlay).zIndex);
// Check if is a number
// Number.isNaN not supported in IE 10+
if (z1 === z1) {
z = z1;
}
}
return z;
};
Polymer.IronOverlayManager = new Polymer.IronOverlayManagerClass();
2015-06-19 09:36:51 -07:00
</script>