mirror of
https://github.com/jellyfin/jellyfin-web.git
synced 2024-11-18 03:18:19 -07:00
588 lines
17 KiB
HTML
588 lines
17 KiB
HTML
<!--
|
|
@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">
|
|
<link rel="import" href="../iron-a11y-keys-behavior/iron-a11y-keys-behavior.html">
|
|
<link rel="import" href="../iron-fit-behavior/iron-fit-behavior.html">
|
|
<link rel="import" href="../iron-resizable-behavior/iron-resizable-behavior.html">
|
|
<link rel="import" href="iron-overlay-backdrop.html">
|
|
<link rel="import" href="iron-overlay-manager.html">
|
|
|
|
<script>
|
|
|
|
/**
|
|
Use `Polymer.IronOverlayBehavior` to implement an element that can be hidden or shown, and displays
|
|
on top of other content. It includes an optional backdrop, and can be used to implement a variety
|
|
of UI controls including dialogs and drop downs. Multiple overlays may be displayed at once.
|
|
|
|
### Closing and canceling
|
|
|
|
A dialog may be hidden by closing or canceling. The difference between close and cancel is user
|
|
intent. Closing generally implies that the user acknowledged the content on the overlay. By default,
|
|
it will cancel whenever the user taps outside it or presses the escape key. This behavior is
|
|
configurable with the `no-cancel-on-esc-key` and the `no-cancel-on-outside-click` properties.
|
|
`close()` should be called explicitly by the implementer when the user interacts with a control
|
|
in the overlay element. When the dialog is canceled, the overlay fires an 'iron-overlay-canceled'
|
|
event. Call `preventDefault` on this event to prevent the overlay from closing.
|
|
|
|
### Positioning
|
|
|
|
By default the element is sized and positioned to fit and centered inside the window. You can
|
|
position and size it manually using CSS. See `Polymer.IronFitBehavior`.
|
|
|
|
### Backdrop
|
|
|
|
Set the `with-backdrop` attribute to display a backdrop behind the overlay. The backdrop is
|
|
appended to `<body>` and is of type `<iron-overlay-backdrop>`. See its doc page for styling
|
|
options.
|
|
|
|
### Limitations
|
|
|
|
The element is styled to appear on top of other content by setting its `z-index` property. You
|
|
must ensure no element has a stacking context with a higher `z-index` than its parent stacking
|
|
context. You should place this element as a child of `<body>` whenever possible.
|
|
|
|
@demo demo/index.html
|
|
@polymerBehavior Polymer.IronOverlayBehavior
|
|
*/
|
|
|
|
Polymer.IronOverlayBehaviorImpl = {
|
|
|
|
properties: {
|
|
|
|
/**
|
|
* True if the overlay is currently displayed.
|
|
*/
|
|
opened: {
|
|
observer: '_openedChanged',
|
|
type: Boolean,
|
|
value: false,
|
|
notify: true
|
|
},
|
|
|
|
/**
|
|
* True if the overlay was canceled when it was last closed.
|
|
*/
|
|
canceled: {
|
|
observer: '_canceledChanged',
|
|
readOnly: true,
|
|
type: Boolean,
|
|
value: false
|
|
},
|
|
|
|
/**
|
|
* Set to true to display a backdrop behind the overlay.
|
|
*/
|
|
withBackdrop: {
|
|
observer: '_withBackdropChanged',
|
|
type: Boolean
|
|
},
|
|
|
|
/**
|
|
* Set to true to disable auto-focusing the overlay or child nodes with
|
|
* the `autofocus` attribute` when the overlay is opened.
|
|
*/
|
|
noAutoFocus: {
|
|
type: Boolean,
|
|
value: false
|
|
},
|
|
|
|
/**
|
|
* Set to true to disable canceling the overlay with the ESC key.
|
|
*/
|
|
noCancelOnEscKey: {
|
|
type: Boolean,
|
|
value: false
|
|
},
|
|
|
|
/**
|
|
* Set to true to disable canceling the overlay by clicking outside it.
|
|
*/
|
|
noCancelOnOutsideClick: {
|
|
type: Boolean,
|
|
value: false
|
|
},
|
|
|
|
/**
|
|
* Returns the reason this dialog was last closed.
|
|
*/
|
|
closingReason: {
|
|
// was a getter before, but needs to be a property so other
|
|
// behaviors can override this.
|
|
type: Object
|
|
},
|
|
|
|
/**
|
|
* The HTMLElement that will be firing relevant KeyboardEvents.
|
|
* Used for capturing esc and tab. Overridden from `IronA11yKeysBehavior`.
|
|
*/
|
|
keyEventTarget: {
|
|
type: Object,
|
|
value: document
|
|
},
|
|
|
|
/**
|
|
* Set to true to enable restoring of focus when overlay is closed.
|
|
*/
|
|
restoreFocusOnClose: {
|
|
type: Boolean,
|
|
value: false
|
|
},
|
|
|
|
_manager: {
|
|
type: Object,
|
|
value: Polymer.IronOverlayManager
|
|
},
|
|
|
|
_boundOnCaptureClick: {
|
|
type: Function,
|
|
value: function() {
|
|
return this._onCaptureClick.bind(this);
|
|
}
|
|
},
|
|
|
|
_boundOnCaptureFocus: {
|
|
type: Function,
|
|
value: function() {
|
|
return this._onCaptureFocus.bind(this);
|
|
}
|
|
},
|
|
|
|
/**
|
|
* The node being focused.
|
|
* @type {?Node}
|
|
*/
|
|
_focusedChild: {
|
|
type: Object
|
|
}
|
|
|
|
},
|
|
|
|
keyBindings: {
|
|
'esc': '__onEsc',
|
|
'tab': '__onTab'
|
|
},
|
|
|
|
listeners: {
|
|
'iron-resize': '_onIronResize'
|
|
},
|
|
|
|
/**
|
|
* The backdrop element.
|
|
* @type {Node}
|
|
*/
|
|
get backdropElement() {
|
|
return this._manager.backdropElement;
|
|
},
|
|
|
|
/**
|
|
* Returns the node to give focus to.
|
|
* @type {Node}
|
|
*/
|
|
get _focusNode() {
|
|
return this._focusedChild || Polymer.dom(this).querySelector('[autofocus]') || this;
|
|
},
|
|
|
|
/**
|
|
* Array of nodes that can receive focus (overlay included), ordered by `tabindex`.
|
|
* This is used to retrieve which is the first and last focusable nodes in order
|
|
* to wrap the focus for overlays `with-backdrop`.
|
|
*
|
|
* If you know what is your content (specifically the first and last focusable children),
|
|
* you can override this method to return only `[firstFocusable, lastFocusable];`
|
|
* @type {[Node]}
|
|
* @protected
|
|
*/
|
|
get _focusableNodes() {
|
|
// Elements that can be focused even if they have [disabled] attribute.
|
|
var FOCUSABLE_WITH_DISABLED = [
|
|
'a[href]',
|
|
'area[href]',
|
|
'iframe',
|
|
'[tabindex]',
|
|
'[contentEditable=true]'
|
|
];
|
|
|
|
// Elements that cannot be focused if they have [disabled] attribute.
|
|
var FOCUSABLE_WITHOUT_DISABLED = [
|
|
'input',
|
|
'select',
|
|
'textarea',
|
|
'button'
|
|
];
|
|
|
|
// Discard elements with tabindex=-1 (makes them not focusable).
|
|
var selector = FOCUSABLE_WITH_DISABLED.join(':not([tabindex="-1"]),') +
|
|
':not([tabindex="-1"]),' +
|
|
FOCUSABLE_WITHOUT_DISABLED.join(':not([disabled]):not([tabindex="-1"]),') +
|
|
':not([disabled]):not([tabindex="-1"])';
|
|
|
|
var focusables = Polymer.dom(this).querySelectorAll(selector);
|
|
if (this.tabIndex >= 0) {
|
|
// Insert at the beginning because we might have all elements with tabIndex = 0,
|
|
// and the overlay should be the first of the list.
|
|
focusables.splice(0, 0, this);
|
|
}
|
|
// Sort by tabindex.
|
|
return focusables.sort(function (a, b) {
|
|
if (a.tabIndex === b.tabIndex) {
|
|
return 0;
|
|
}
|
|
if (a.tabIndex === 0 || a.tabIndex > b.tabIndex) {
|
|
return 1;
|
|
}
|
|
return -1;
|
|
});
|
|
},
|
|
|
|
ready: function() {
|
|
// with-backdrop needs tabindex to be set in order to trap the focus.
|
|
// If it is not set, IronOverlayBehavior will set it, and remove it if with-backdrop = false.
|
|
this.__shouldRemoveTabIndex = false;
|
|
// Used for wrapping the focus on TAB / Shift+TAB.
|
|
this.__firstFocusableNode = this.__lastFocusableNode = null;
|
|
this._ensureSetup();
|
|
},
|
|
|
|
attached: function() {
|
|
// Call _openedChanged here so that position can be computed correctly.
|
|
if (this.opened) {
|
|
this._openedChanged();
|
|
}
|
|
this._observer = Polymer.dom(this).observeNodes(this._onNodesChange);
|
|
},
|
|
|
|
detached: function() {
|
|
Polymer.dom(this).unobserveNodes(this._observer);
|
|
this._observer = null;
|
|
this.opened = false;
|
|
this._manager.trackBackdrop(this);
|
|
this._manager.removeOverlay(this);
|
|
},
|
|
|
|
/**
|
|
* Toggle the opened state of the overlay.
|
|
*/
|
|
toggle: function() {
|
|
this._setCanceled(false);
|
|
this.opened = !this.opened;
|
|
},
|
|
|
|
/**
|
|
* Open the overlay.
|
|
*/
|
|
open: function() {
|
|
this._setCanceled(false);
|
|
this.opened = true;
|
|
},
|
|
|
|
/**
|
|
* Close the overlay.
|
|
*/
|
|
close: function() {
|
|
this._setCanceled(false);
|
|
this.opened = false;
|
|
},
|
|
|
|
/**
|
|
* Cancels the overlay.
|
|
* @param {?Event} event The original event
|
|
*/
|
|
cancel: function(event) {
|
|
var cancelEvent = this.fire('iron-overlay-canceled', event, {cancelable: true});
|
|
if (cancelEvent.defaultPrevented) {
|
|
return;
|
|
}
|
|
|
|
this._setCanceled(true);
|
|
this.opened = false;
|
|
},
|
|
|
|
_ensureSetup: function() {
|
|
if (this._overlaySetup) {
|
|
return;
|
|
}
|
|
this._overlaySetup = true;
|
|
this.style.outline = 'none';
|
|
this.style.display = 'none';
|
|
},
|
|
|
|
_openedChanged: function() {
|
|
if (this.opened) {
|
|
this.removeAttribute('aria-hidden');
|
|
} else {
|
|
this.setAttribute('aria-hidden', 'true');
|
|
}
|
|
|
|
// wait to call after ready only if we're initially open
|
|
if (!this._overlaySetup) {
|
|
return;
|
|
}
|
|
|
|
this._manager.trackBackdrop(this);
|
|
|
|
if (this.opened) {
|
|
this._prepareRenderOpened();
|
|
}
|
|
|
|
if (this._openChangedAsync) {
|
|
this.cancelAsync(this._openChangedAsync);
|
|
}
|
|
// Async here to allow overlay layer to become visible, and to avoid
|
|
// listeners to immediately close via a click.
|
|
this._openChangedAsync = this.async(function() {
|
|
// overlay becomes visible here
|
|
this.style.display = '';
|
|
// Force layout to ensure transition will go. Set offsetWidth to itself
|
|
// so that compilers won't remove it.
|
|
this.offsetWidth = this.offsetWidth;
|
|
if (this.opened) {
|
|
this._renderOpened();
|
|
} else {
|
|
this._renderClosed();
|
|
}
|
|
this._toggleListeners();
|
|
this._openChangedAsync = null;
|
|
}, 1);
|
|
},
|
|
|
|
_canceledChanged: function() {
|
|
this.closingReason = this.closingReason || {};
|
|
this.closingReason.canceled = this.canceled;
|
|
},
|
|
|
|
_withBackdropChanged: function() {
|
|
// If tabindex is already set, no need to override it.
|
|
if (this.withBackdrop && !this.hasAttribute('tabindex')) {
|
|
this.setAttribute('tabindex', '-1');
|
|
this.__shouldRemoveTabIndex = true;
|
|
} else if (this.__shouldRemoveTabIndex) {
|
|
this.removeAttribute('tabindex');
|
|
this.__shouldRemoveTabIndex = false;
|
|
}
|
|
if (this.opened) {
|
|
this._manager.trackBackdrop(this);
|
|
if (this.withBackdrop) {
|
|
this.backdropElement.prepare();
|
|
// Give time to be added to document.
|
|
this.async(function(){
|
|
this.backdropElement.open();
|
|
}, 1);
|
|
} else {
|
|
this.backdropElement.close();
|
|
}
|
|
}
|
|
},
|
|
|
|
_toggleListener: function(enable, node, event, boundListener, capture) {
|
|
if (enable) {
|
|
// enable document-wide tap recognizer
|
|
if (event === 'tap') {
|
|
Polymer.Gestures.add(document, 'tap', null);
|
|
}
|
|
node.addEventListener(event, boundListener, capture);
|
|
} else {
|
|
// disable document-wide tap recognizer
|
|
if (event === 'tap') {
|
|
Polymer.Gestures.remove(document, 'tap', null);
|
|
}
|
|
node.removeEventListener(event, boundListener, capture);
|
|
}
|
|
},
|
|
|
|
_toggleListeners: function() {
|
|
this._toggleListener(this.opened, document, 'tap', this._boundOnCaptureClick, true);
|
|
this._toggleListener(this.opened, document, 'focus', this._boundOnCaptureFocus, true);
|
|
},
|
|
|
|
// tasks which must occur before opening; e.g. making the element visible
|
|
_prepareRenderOpened: function() {
|
|
|
|
this._manager.addOverlay(this);
|
|
|
|
// Needed to calculate the size of the overlay so that transitions on its size
|
|
// will have the correct starting points.
|
|
this._preparePositioning();
|
|
this.fit();
|
|
this._finishPositioning();
|
|
|
|
if (this.withBackdrop) {
|
|
this.backdropElement.prepare();
|
|
}
|
|
|
|
// Safari will apply the focus to the autofocus element when displayed for the first time,
|
|
// so we blur it. Later, _applyFocus will set the focus if necessary.
|
|
if (this.noAutoFocus && document.activeElement === this._focusNode) {
|
|
this._focusNode.blur();
|
|
}
|
|
},
|
|
|
|
// tasks which cause the overlay to actually open; typically play an
|
|
// animation
|
|
_renderOpened: function() {
|
|
if (this.withBackdrop) {
|
|
this.backdropElement.open();
|
|
}
|
|
this._finishRenderOpened();
|
|
},
|
|
|
|
_renderClosed: function() {
|
|
if (this.withBackdrop) {
|
|
this.backdropElement.close();
|
|
}
|
|
this._finishRenderClosed();
|
|
},
|
|
|
|
_finishRenderOpened: function() {
|
|
// This ensures the overlay is visible before we set the focus
|
|
// (by calling _onIronResize -> refit).
|
|
this.notifyResize();
|
|
// Focus the child node with [autofocus]
|
|
this._applyFocus();
|
|
|
|
this.fire('iron-overlay-opened');
|
|
},
|
|
|
|
_finishRenderClosed: function() {
|
|
// Hide the overlay and remove the backdrop.
|
|
this.resetFit();
|
|
this.style.display = 'none';
|
|
this._manager.removeOverlay(this);
|
|
|
|
this._applyFocus();
|
|
this.notifyResize();
|
|
|
|
this.fire('iron-overlay-closed', this.closingReason);
|
|
},
|
|
|
|
_preparePositioning: function() {
|
|
this.style.transition = this.style.webkitTransition = 'none';
|
|
this.style.transform = this.style.webkitTransform = 'none';
|
|
this.style.display = '';
|
|
},
|
|
|
|
_finishPositioning: function() {
|
|
this.style.display = 'none';
|
|
this.style.transform = this.style.webkitTransform = '';
|
|
// Force layout layout to avoid application of transform.
|
|
// Set offsetWidth to itself so that compilers won't remove it.
|
|
this.offsetWidth = this.offsetWidth;
|
|
this.style.transition = this.style.webkitTransition = '';
|
|
},
|
|
|
|
_applyFocus: function() {
|
|
if (this.opened) {
|
|
if (!this.noAutoFocus) {
|
|
this._focusNode.focus();
|
|
}
|
|
} else {
|
|
this._focusNode.blur();
|
|
this._focusedChild = null;
|
|
this._manager.focusOverlay();
|
|
}
|
|
},
|
|
|
|
_onCaptureClick: function(event) {
|
|
if (this._manager.currentOverlay() === this &&
|
|
Polymer.dom(event).path.indexOf(this) === -1) {
|
|
if (this.noCancelOnOutsideClick) {
|
|
this._applyFocus();
|
|
} else {
|
|
this.cancel(event);
|
|
}
|
|
}
|
|
},
|
|
|
|
_onCaptureFocus: function (event) {
|
|
if (this._manager.currentOverlay() === this && this.withBackdrop) {
|
|
var path = Polymer.dom(event).path;
|
|
if (path.indexOf(this) === -1) {
|
|
event.stopPropagation();
|
|
this._applyFocus();
|
|
} else {
|
|
this._focusedChild = path[0];
|
|
}
|
|
}
|
|
},
|
|
|
|
_onIronResize: function() {
|
|
if (this.opened) {
|
|
this.refit();
|
|
}
|
|
},
|
|
|
|
/**
|
|
* @protected
|
|
* Will call notifyResize if overlay is opened.
|
|
* Can be overridden in order to avoid multiple observers on the same node.
|
|
*/
|
|
_onNodesChange: function() {
|
|
if (this.opened) {
|
|
this.notifyResize();
|
|
}
|
|
// Store it so we don't query too much.
|
|
var focusableNodes = this._focusableNodes;
|
|
this.__firstFocusableNode = focusableNodes[0];
|
|
this.__lastFocusableNode = focusableNodes[focusableNodes.length - 1];
|
|
},
|
|
|
|
__onEsc: function(event) {
|
|
// Not opened or not on top, so return.
|
|
if (this._manager.currentOverlay() !== this) {
|
|
return;
|
|
}
|
|
if (!this.noCancelOnEscKey) {
|
|
this.cancel(event);
|
|
}
|
|
},
|
|
|
|
__onTab: function(event) {
|
|
// Not opened or not on top, so return.
|
|
if (this._manager.currentOverlay() !== this) {
|
|
return;
|
|
}
|
|
// TAB wraps from last to first focusable.
|
|
// Shift + TAB wraps from first to last focusable.
|
|
var shift = event.detail.keyboardEvent.shiftKey;
|
|
var nodeToCheck = shift ? this.__firstFocusableNode : this.__lastFocusableNode;
|
|
var nodeToSet = shift ? this.__lastFocusableNode : this.__firstFocusableNode;
|
|
if (this.withBackdrop && this._focusedChild === nodeToCheck) {
|
|
// We set here the _focusedChild so that _onCaptureFocus will handle the
|
|
// wrapping of the focus (the next event after tab is focus).
|
|
this._focusedChild = nodeToSet;
|
|
}
|
|
}
|
|
};
|
|
|
|
/** @polymerBehavior */
|
|
Polymer.IronOverlayBehavior = [Polymer.IronA11yKeysBehavior, Polymer.IronFitBehavior, Polymer.IronResizableBehavior, Polymer.IronOverlayBehaviorImpl];
|
|
|
|
/**
|
|
* Fired after the `iron-overlay` opens.
|
|
* @event iron-overlay-opened
|
|
*/
|
|
|
|
/**
|
|
* Fired when the `iron-overlay` is canceled, but before it is closed.
|
|
* Cancel the event to prevent the `iron-overlay` from closing.
|
|
* @event iron-overlay-canceled
|
|
* @param {Event} event The closing of the `iron-overlay` can be prevented
|
|
* by calling `event.preventDefault()`. The `event.detail` is the original event that originated
|
|
* the canceling (e.g. ESC keyboard event or click event outside the `iron-overlay`).
|
|
*/
|
|
|
|
/**
|
|
* Fired after the `iron-overlay` closes.
|
|
* @event iron-overlay-closed
|
|
* @param {{canceled: (boolean|undefined)}} closingReason Contains `canceled` (whether the overlay was canceled).
|
|
*/
|
|
|
|
</script>
|