1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/dashboard-ui/bower_components/iron-fit-behavior/iron-fit-behavior.html

553 lines
18 KiB
HTML
Raw Normal View History

2015-06-19 12:36:51 -04: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-05-03 22:18:05 -04:00
`Polymer.IronFitBehavior` fits an element in another element using `max-height` and `max-width`, and
2015-06-19 12:36:51 -04:00
optionally centers it in the window or another element.
The element will only be sized and/or positioned if it has not already been sized and/or positioned
by CSS.
CSS properties | Action
-----------------------------|-------------------------------------------
`position` set | Element is not centered horizontally or vertically
`top` or `bottom` set | Element is not vertically centered
`left` or `right` set | Element is not horizontally centered
2016-05-03 22:18:05 -04:00
`max-height` set | Element respects `max-height`
`max-width` set | Element respects `max-width`
`Polymer.IronFitBehavior` can position an element into another element using
`verticalAlign` and `horizontalAlign`. This will override the element's css position.
<div class="container">
<iron-fit-impl vertical-align="top" horizontal-align="auto">
Positioned into the container
</iron-fit-impl>
</div>
Use `noOverlap` to position the element around another element without overlapping it.
<div class="container">
<iron-fit-impl no-overlap vertical-align="auto" horizontal-align="auto">
Positioned around the container
</iron-fit-impl>
</div>
2015-06-19 12:36:51 -04:00
@demo demo/index.html
@polymerBehavior
*/
Polymer.IronFitBehavior = {
properties: {
/**
* The element that will receive a `max-height`/`width`. By default it is the same as `this`,
* but it can be set to a child element. This is useful, for example, for implementing a
* scrolling region inside the element.
2015-06-25 21:13:51 -04:00
* @type {!Element}
2015-06-19 12:36:51 -04:00
*/
sizingTarget: {
type: Object,
value: function() {
return this;
}
},
/**
* The element to fit `this` into.
*/
fitInto: {
type: Object,
value: window
},
2016-05-03 22:18:05 -04:00
/**
* Will position the element around the positionTarget without overlapping it.
*/
noOverlap: {
type: Boolean
},
/**
* The element that should be used to position the element. If not set, it will
* default to the parent node.
* @type {!Element}
*/
positionTarget: {
type: Element
},
/**
* The orientation against which to align the element horizontally
* relative to the `positionTarget`. Possible values are "left", "right", "auto".
*/
horizontalAlign: {
type: String
},
/**
* The orientation against which to align the element vertically
* relative to the `positionTarget`. Possible values are "top", "bottom", "auto".
*/
verticalAlign: {
type: String
},
/**
2016-05-05 14:29:30 -04:00
* The same as setting margin-left and margin-right css properties.
* @deprecated
2016-05-03 22:18:05 -04:00
*/
horizontalOffset: {
type: Number,
value: 0,
notify: true
},
/**
2016-05-05 14:29:30 -04:00
* The same as setting margin-top and margin-bottom css properties.
* @deprecated
2016-05-03 22:18:05 -04:00
*/
verticalOffset: {
type: Number,
value: 0,
notify: true
},
2015-06-19 12:36:51 -04:00
/**
* Set to true to auto-fit on attach.
*/
autoFitOnAttach: {
type: Boolean,
value: false
},
2015-06-25 21:13:51 -04:00
/** @type {?Object} */
2015-06-19 12:36:51 -04:00
_fitInfo: {
type: Object
}
},
get _fitWidth() {
var fitWidth;
if (this.fitInto === window) {
fitWidth = this.fitInto.innerWidth;
} else {
fitWidth = this.fitInto.getBoundingClientRect().width;
}
return fitWidth;
},
get _fitHeight() {
var fitHeight;
if (this.fitInto === window) {
fitHeight = this.fitInto.innerHeight;
} else {
fitHeight = this.fitInto.getBoundingClientRect().height;
}
return fitHeight;
},
2015-06-25 21:13:51 -04:00
get _fitLeft() {
var fitLeft;
if (this.fitInto === window) {
fitLeft = 0;
} else {
fitLeft = this.fitInto.getBoundingClientRect().left;
}
return fitLeft;
},
get _fitTop() {
var fitTop;
if (this.fitInto === window) {
fitTop = 0;
} else {
fitTop = this.fitInto.getBoundingClientRect().top;
}
return fitTop;
},
2016-05-03 22:18:05 -04:00
/**
* The element that should be used to position the element,
* if no position target is configured.
*/
get _defaultPositionTarget() {
var parent = Polymer.dom(this).parentNode;
2016-05-05 14:29:30 -04:00
if (parent && parent.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
2016-05-03 22:18:05 -04:00
parent = parent.host;
}
return parent;
},
/**
* The horizontal align value, accounting for the RTL/LTR text direction.
*/
get _localeHorizontalAlign() {
if (this._isRTL) {
// In RTL, "left" becomes "right".
if (this.horizontalAlign === 'right') {
return 'left';
}
if (this.horizontalAlign === 'left') {
return 'right';
}
}
return this.horizontalAlign;
},
2015-06-19 12:36:51 -04:00
attached: function() {
2016-05-03 22:18:05 -04:00
// Memoize this to avoid expensive calculations & relayouts.
this._isRTL = window.getComputedStyle(this).direction == 'rtl';
this.positionTarget = this.positionTarget || this._defaultPositionTarget;
2015-06-19 12:36:51 -04:00
if (this.autoFitOnAttach) {
if (window.getComputedStyle(this).display === 'none') {
setTimeout(function() {
this.fit();
}.bind(this));
} else {
this.fit();
}
}
},
/**
2016-05-03 22:18:05 -04:00
* Positions and fits the element into the `fitInto` element.
2015-06-19 12:36:51 -04:00
*/
fit: function() {
this._discoverInfo();
2016-05-03 22:18:05 -04:00
this.position();
2015-06-19 12:36:51 -04:00
this.constrain();
this.center();
},
/**
* Memoize information needed to position and size the target element.
*/
_discoverInfo: function() {
if (this._fitInfo) {
return;
}
var target = window.getComputedStyle(this);
var sizer = window.getComputedStyle(this.sizingTarget);
2016-05-05 14:29:30 -04:00
2015-06-19 12:36:51 -04:00
this._fitInfo = {
2015-06-25 21:13:51 -04:00
inlineStyle: {
top: this.style.top || '',
2016-05-03 22:18:05 -04:00
left: this.style.left || '',
position: this.style.position || ''
},
sizerInlineStyle: {
maxWidth: this.sizingTarget.style.maxWidth || '',
maxHeight: this.sizingTarget.style.maxHeight || '',
boxSizing: this.sizingTarget.style.boxSizing || ''
2015-06-25 21:13:51 -04:00
},
2015-06-19 12:36:51 -04:00
positionedBy: {
vertically: target.top !== 'auto' ? 'top' : (target.bottom !== 'auto' ?
'bottom' : null),
horizontally: target.left !== 'auto' ? 'left' : (target.right !== 'auto' ?
2016-05-03 22:18:05 -04:00
'right' : null)
2015-06-19 12:36:51 -04:00
},
sizedBy: {
height: sizer.maxHeight !== 'none',
2016-05-03 22:18:05 -04:00
width: sizer.maxWidth !== 'none',
minWidth: parseInt(sizer.minWidth, 10) || 0,
minHeight: parseInt(sizer.minHeight, 10) || 0
2015-06-19 12:36:51 -04:00
},
margin: {
top: parseInt(target.marginTop, 10) || 0,
right: parseInt(target.marginRight, 10) || 0,
bottom: parseInt(target.marginBottom, 10) || 0,
left: parseInt(target.marginLeft, 10) || 0
}
};
2016-05-05 14:29:30 -04:00
// Support these properties until they are removed.
if (this.verticalOffset) {
this._fitInfo.margin.top = this._fitInfo.margin.bottom = this.verticalOffset;
this._fitInfo.inlineStyle.marginTop = this.style.marginTop || '';
this._fitInfo.inlineStyle.marginBottom = this.style.marginBottom || '';
this.style.marginTop = this.style.marginBottom = this.verticalOffset + 'px';
}
if (this.horizontalOffset) {
this._fitInfo.margin.left = this._fitInfo.margin.right = this.horizontalOffset;
this._fitInfo.inlineStyle.marginLeft = this.style.marginLeft || '';
this._fitInfo.inlineStyle.marginRight = this.style.marginRight || '';
this.style.marginLeft = this.style.marginRight = this.horizontalOffset + 'px';
}
2015-06-19 12:36:51 -04:00
},
/**
* Resets the target element's position and size constraints, and clear
* the memoized data.
*/
resetFit: function() {
2016-05-03 22:18:05 -04:00
var info = this._fitInfo || {};
for (var property in info.sizerInlineStyle) {
this.sizingTarget.style[property] = info.sizerInlineStyle[property];
2015-06-19 12:36:51 -04:00
}
2016-05-03 22:18:05 -04:00
for (var property in info.inlineStyle) {
this.style[property] = info.inlineStyle[property];
2015-06-19 12:36:51 -04:00
}
2016-05-03 22:18:05 -04:00
2015-06-19 12:36:51 -04:00
this._fitInfo = null;
},
/**
2016-05-03 22:18:05 -04:00
* Equivalent to calling `resetFit()` and `fit()`. Useful to call this after
* the element or the `fitInto` element has been resized, or if any of the
* positioning properties (e.g. `horizontalAlign, verticalAlign`) is updated.
2015-06-19 12:36:51 -04:00
*/
refit: function() {
this.resetFit();
this.fit();
},
/**
2016-05-03 22:18:05 -04:00
* Positions the element according to `horizontalAlign, verticalAlign`.
*/
position: function() {
if (!this.horizontalAlign && !this.verticalAlign) {
// needs to be centered, and it is done after constrain.
return;
}
this.style.position = 'fixed';
// Need border-box for margin/padding.
this.sizingTarget.style.boxSizing = 'border-box';
// Set to 0, 0 in order to discover any offset caused by parent stacking contexts.
this.style.left = '0px';
this.style.top = '0px';
var rect = this.getBoundingClientRect();
var positionRect = this.__getNormalizedRect(this.positionTarget);
var fitRect = this.__getNormalizedRect(this.fitInto);
2016-05-05 14:29:30 -04:00
// Consider the margin as part of the size for position calculations.
var width = rect.width + this._fitInfo.margin.left + this._fitInfo.margin.right;
var height = rect.height + this._fitInfo.margin.top + this._fitInfo.margin.bottom;
var alignRight = this.__isAlignRight(this._localeHorizontalAlign, width, positionRect, fitRect);
var alignBottom = this.__isAlignBottom(this.verticalAlign, height, positionRect, fitRect);
2016-05-03 22:18:05 -04:00
2016-05-05 14:29:30 -04:00
var top = alignBottom ? positionRect.bottom - height : positionRect.top;
var left = alignRight ? positionRect.right - width : positionRect.left;
2016-05-03 22:18:05 -04:00
if (this.noOverlap) {
// We can overlap one of the dimensions, choose the one that minimizes the cropped area.
var noOverlapLeft = left + positionRect.width * (alignRight ? -1 : 1);
var noOverlapTop = top + positionRect.height * (alignBottom ? -1 : 1);
var areaOverlapLeft = this.__getCroppedArea({
top: noOverlapTop,
left: left,
2016-05-05 14:29:30 -04:00
width: width,
height: height
2016-05-03 22:18:05 -04:00
}, fitRect);
var areaOverlapTop = this.__getCroppedArea({
top: top,
left: noOverlapLeft,
2016-05-05 14:29:30 -04:00
width: width,
height: height
2016-05-03 22:18:05 -04:00
}, fitRect);
2016-05-05 14:29:30 -04:00
if (areaOverlapLeft >= areaOverlapTop) {
2016-05-03 22:18:05 -04:00
left = noOverlapLeft;
} else {
top = noOverlapTop;
}
}
2016-05-05 14:29:30 -04:00
left += this._fitInfo.margin.left;
top += this._fitInfo.margin.top;
// Use original size (without margin)
2016-05-03 22:18:05 -04:00
var right = left + rect.width;
var bottom = top + rect.height;
2016-05-05 14:29:30 -04:00
left = Math.max(left, this._fitInfo.margin.left);
top = Math.max(top, this._fitInfo.margin.top);
2016-05-03 22:18:05 -04:00
2016-05-05 14:29:30 -04:00
var maxWidth = Math.min(fitRect.right - this._fitInfo.margin.right, right) - left;
var maxHeight = Math.min(fitRect.bottom - this._fitInfo.margin.bottom, bottom) - top;
2016-05-03 22:18:05 -04:00
var minWidth = this._fitInfo.sizedBy.minWidth;
var minHeight = this._fitInfo.sizedBy.minHeight;
if (maxWidth < minWidth || right < minWidth) {
left = -9999;
maxWidth = 0;
}
if (maxHeight < minHeight || bottom < minHeight) {
top = -9999;
maxHeight = 0;
}
this.sizingTarget.style.maxWidth = maxWidth + 'px';
this.sizingTarget.style.maxHeight = maxHeight + 'px';
// Remove the offset caused by any stacking context.
this.style.left = (left - rect.left) + 'px';
this.style.top = (top - rect.top) + 'px';
},
/**
* Constrains the size of the element to `fitInto` by setting `max-height`
2015-06-19 12:36:51 -04:00
* and/or `max-width`.
*/
constrain: function() {
2016-05-03 22:18:05 -04:00
if (this.horizontalAlign || this.verticalAlign) {
return;
}
2015-06-19 12:36:51 -04:00
var info = this._fitInfo;
// position at (0px, 0px) if not already positioned, so we can measure the natural size.
2016-05-03 22:18:05 -04:00
if (!info.positionedBy.vertically) {
this.style.position = 'fixed';
2015-06-19 12:36:51 -04:00
this.style.top = '0px';
}
2016-05-03 22:18:05 -04:00
if (!info.positionedBy.horizontally) {
2015-12-14 10:43:03 -05:00
this.style.position = 'fixed';
2016-05-03 22:18:05 -04:00
this.style.left = '0px';
2015-12-14 10:43:03 -05:00
}
2016-05-03 22:18:05 -04:00
2015-06-19 12:36:51 -04:00
// need border-box for margin/padding
this.sizingTarget.style.boxSizing = 'border-box';
// constrain the width and height if not already set
var rect = this.getBoundingClientRect();
if (!info.sizedBy.height) {
2016-05-03 22:18:05 -04:00
this.__sizeDimension(rect, info.positionedBy.vertically, 'top', 'bottom', 'Height');
2015-06-19 12:36:51 -04:00
}
if (!info.sizedBy.width) {
2016-05-03 22:18:05 -04:00
this.__sizeDimension(rect, info.positionedBy.horizontally, 'left', 'right', 'Width');
2015-06-19 12:36:51 -04:00
}
},
2016-05-03 22:18:05 -04:00
/**
* @protected
* @deprecated
*/
2015-06-19 12:36:51 -04:00
_sizeDimension: function(rect, positionedBy, start, end, extent) {
2016-05-03 22:18:05 -04:00
this.__sizeDimension(rect, positionedBy, start, end, extent);
},
/**
* @private
*/
__sizeDimension: function(rect, positionedBy, start, end, extent) {
2015-06-19 12:36:51 -04:00
var info = this._fitInfo;
2016-05-03 22:18:05 -04:00
var fitRect = this.__getNormalizedRect(this.fitInto);
var max = extent === 'Width' ? fitRect.width : fitRect.height;
2015-06-19 12:36:51 -04:00
var flip = (positionedBy === end);
var offset = flip ? max - rect[end] : rect[start];
var margin = info.margin[flip ? start : end];
var offsetExtent = 'offset' + extent;
var sizingOffset = this[offsetExtent] - this.sizingTarget[offsetExtent];
this.sizingTarget.style['max' + extent] = (max - margin - offset - sizingOffset) + 'px';
},
/**
* Centers horizontally and vertically if not already positioned. This also sets
* `position:fixed`.
*/
center: function() {
2016-05-03 22:18:05 -04:00
if (this.horizontalAlign || this.verticalAlign) {
return;
}
2016-02-24 16:18:53 -05:00
var positionedBy = this._fitInfo.positionedBy;
if (positionedBy.vertically && positionedBy.horizontally) {
// Already positioned.
return;
2015-06-19 12:36:51 -04:00
}
2016-02-24 16:18:53 -05:00
// Need position:fixed to center
this.style.position = 'fixed';
// Take into account the offset caused by parents that create stacking
// contexts (e.g. with transform: translate3d). Translate to 0,0 and
// measure the bounding rect.
if (!positionedBy.vertically) {
this.style.top = '0px';
}
if (!positionedBy.horizontally) {
this.style.left = '0px';
}
// It will take in consideration margins and transforms
var rect = this.getBoundingClientRect();
2016-05-03 22:18:05 -04:00
var fitRect = this.__getNormalizedRect(this.fitInto);
2016-02-24 16:18:53 -05:00
if (!positionedBy.vertically) {
2016-05-03 22:18:05 -04:00
var top = fitRect.top - rect.top + (fitRect.height - rect.height) / 2;
2015-06-19 12:36:51 -04:00
this.style.top = top + 'px';
}
2016-02-24 16:18:53 -05:00
if (!positionedBy.horizontally) {
2016-05-03 22:18:05 -04:00
var left = fitRect.left - rect.left + (fitRect.width - rect.width) / 2;
2015-06-19 12:36:51 -04:00
this.style.left = left + 'px';
}
2016-05-03 22:18:05 -04:00
},
__getNormalizedRect: function(target) {
if (target === document.documentElement || target === window) {
return {
top: 0,
left: 0,
width: window.innerWidth,
height: window.innerHeight,
right: window.innerWidth,
bottom: window.innerHeight
};
}
return target.getBoundingClientRect();
},
2016-05-05 14:29:30 -04:00
__isAlignRight: function(hAlign, size, positionRect, fitRect) {
2016-05-03 22:18:05 -04:00
if (hAlign === 'right') {
return true;
}
if (hAlign === 'auto') {
2016-05-05 14:29:30 -04:00
// We should align on the right if positionTarget is on the right of fitInto,
// or if we can overlap and aligning on the left would cause more cropping
// than aligning on the right.
var positionTargetCenter = positionRect.left + positionRect.width/2;
var fitIntoCenter = fitRect.left + fitRect.width/2;
var croppedLeft = Math.abs(Math.min(0, positionRect.left));
var croppedRight = Math.abs(Math.min(0, positionRect.right - size));
return positionTargetCenter > fitIntoCenter ||
(!this.noOverlap && croppedLeft > croppedRight);
2016-05-03 22:18:05 -04:00
}
return false;
},
2016-05-05 14:29:30 -04:00
__isAlignBottom: function(vAlign, size, positionRect, fitRect) {
2016-05-03 22:18:05 -04:00
if (vAlign === 'bottom') {
return true;
}
if (vAlign === 'auto') {
2016-05-05 14:29:30 -04:00
// We should align on the bottom if positionTarget is on the bottom of fitInto,
// or if we can overlap and aligning on the top would cause more cropping
// than aligning on the bottom.
var positionTargetCenter = positionRect.top + positionRect.height/2;
var fitIntoCenter = fitRect.top + fitRect.height/2;
var croppedTop = Math.abs(Math.min(0, positionRect.top));
var croppedBottom = Math.abs(Math.min(0, positionRect.bottom - size));
return positionTargetCenter > fitIntoCenter ||
(!this.noOverlap && croppedTop > croppedBottom);
2016-05-03 22:18:05 -04:00
}
return false;
},
__getCroppedArea: function(rect, fitRect) {
var verticalCrop = Math.min(0, rect.top) + Math.min(0, fitRect.bottom - (rect.top + rect.height));
var horizontalCrop = Math.min(0, rect.left) + Math.min(0, fitRect.right - (rect.left + rect.width));
2016-05-05 14:29:30 -04:00
return Math.abs(verticalCrop) * rect.width + Math.abs(horizontalCrop) * rect.height;
2015-06-19 12:36:51 -04:00
}
};
</script>