/**
@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
*/
import"../polymer/polymer-legacy.js";import{dom as t}from"../polymer/lib/legacy/polymer.dom.js";let i;export const IronFitBehavior={properties:{sizingTarget:{type:Object,value:function(){return this}},fitInto:{type:Object,value:window},noOverlap:{type:Boolean},positionTarget:{type:Element},horizontalAlign:{type:String},verticalAlign:{type:String},dynamicAlign:{type:Boolean},horizontalOffset:{type:Number,value:0,notify:!0},verticalOffset:{type:Number,value:0,notify:!0},autoFitOnAttach:{type:Boolean,value:!1},expandSizingTargetForScrollbars:{type:Boolean,value:!1},_fitInfo:{type:Object}},get _fitWidth(){return this.fitInto===window?this.fitInto.innerWidth:this.fitInto.getBoundingClientRect().width},get _fitHeight(){return this.fitInto===window?this.fitInto.innerHeight:this.fitInto.getBoundingClientRect().height},get _fitLeft(){return this.fitInto===window?0:this.fitInto.getBoundingClientRect().left},get _fitTop(){return this.fitInto===window?0:this.fitInto.getBoundingClientRect().top},get _defaultPositionTarget(){var i=t(this).parentNode;return i&&i.nodeType===Node.DOCUMENT_FRAGMENT_NODE&&(i=i.host),i},get _localeHorizontalAlign(){if(this._isRTL){if("right"===this.horizontalAlign)return"left";if("left"===this.horizontalAlign)return"right"}return this.horizontalAlign},get __shouldPosition(){return(this.horizontalAlign||this.verticalAlign)&&this.positionTarget},get _isRTL(){return void 0===this._memoizedIsRTL&&(this._memoizedIsRTL="rtl"==window.getComputedStyle(this).direction),this._memoizedIsRTL},attached:function(){this.positionTarget=this.positionTarget||this._defaultPositionTarget,this.autoFitOnAttach&&("none"===window.getComputedStyle(this).display?setTimeout(function(){this.fit()}.bind(this)):(window.ShadyDOM&&ShadyDOM.flush(),this.fit()))},detached:function(){this.__deferredFit&&(clearTimeout(this.__deferredFit),this.__deferredFit=null)},fit:function(){this.position(),this.constrain(),this.center()},_discoverInfo:function(){if(!this._fitInfo){var t=window.getComputedStyle(this),i=window.getComputedStyle(this.sizingTarget);this._fitInfo={inlineStyle:{top:this.style.top||"",left:this.style.left||"",position:this.style.position||""},sizerInlineStyle:{maxWidth:this.sizingTarget.style.maxWidth||"",maxHeight:this.sizingTarget.style.maxHeight||"",boxSizing:this.sizingTarget.style.boxSizing||""},positionedBy:{vertically:"auto"!==t.top?"top":"auto"!==t.bottom?"bottom":null,horizontally:"auto"!==t.left?"left":"auto"!==t.right?"right":null},sizedBy:{height:"none"!==i.maxHeight,width:"none"!==i.maxWidth,minWidth:parseInt(i.minWidth,10)||0,minHeight:parseInt(i.minHeight,10)||0},margin:{top:parseInt(t.marginTop,10)||0,right:parseInt(t.marginRight,10)||0,bottom:parseInt(t.marginBottom,10)||0,left:parseInt(t.marginLeft,10)||0}}}},resetFit:function(){var t=this._fitInfo||{};for(var i in t.sizerInlineStyle)this.sizingTarget.style[i]=t.sizerInlineStyle[i];for(var i in t.inlineStyle)this.style[i]=t.inlineStyle[i];this._fitInfo=null},refit:function(){var t=this.sizingTarget.scrollLeft,i=this.sizingTarget.scrollTop;this.resetFit(),this.fit(),this.sizingTarget.scrollLeft=t,this.sizingTarget.scrollTop=i},position:function(){if(!this.__shouldPosition)return;this._discoverInfo(),window.ShadyDOM&&window.ShadyDOM.flush(),this.style.position="fixed",this.sizingTarget.style.boxSizing="border-box",this.style.left="0px",this.style.top="0px";var t=this.getBoundingClientRect(),e=this.__getNormalizedRect(this.positionTarget),o=this.__getNormalizedRect(this.fitInto);let n,h,s,l;this.expandSizingTargetForScrollbars&&(n=this.sizingTarget.offsetWidth,h=this.sizingTarget.offsetHeight,s=this.sizingTarget.clientWidth,l=this.sizingTarget.clientHeight);var r=this._fitInfo.margin,a={width:t.width+r.left+r.right,height:t.height+r.top+r.bottom},f=this.__getPosition(this._localeHorizontalAlign,this.verticalAlign,a,t,e,o),g=f.left+r.left,d=f.top+r.top,p=Math.min(o.right-r.right,g+t.width),c=Math.min(o.bottom-r.bottom,d+t.height);g=Math.max(o.left+r.left,Math.min(g,p-this._fitInfo.sizedBy.minWidth)),d=Math.max(o.top+r.top,Math.min(d,c-this._fitInfo.sizedBy.minHeight));const m=Math.max(p-g,this._fitInfo.sizedBy.minWidth),y=Math.max(c-d,this._fitInfo.sizedBy.minHeight);this.sizingTarget.style.maxWidth=m+"px",this.sizingTarget.style.maxHeight=y+"px";const z=g-t.left,u=d-t.top;if(this.style.left=`${z}px`,this.style.top=`${u}px`,this.expandSizingTargetForScrollbars){const t=this.sizingTarget.offsetHeight,e=t-this.sizingTarget.clientHeight-(h-l);if(e>0){const i=o.height-r.top-r.bottom,n=Math.min(i,y+e);this.sizingTarget.style.maxHeight=`${n}px`;const h=this.sizingTarget.offsetHeight,s=h-t;let l;"top"===f.verticalAlign?l=u:"middle"===f.verticalAlign?l=u-s/2:"bottom"===f.verticalAlign&&(l=u-s),l=Math.max(o.top+r.top,Math.min(l,o.bottom-r.bottom-h)),this.style.top=`${l}px`}const a=this.sizingTarget.offsetWidth,g=a-this.sizingTarget.clientWidth-(n-s);if(g>0){const t=(()=>{if(void 0!==i)return i;const t=document.createElement("div");Object.assign(t.style,{overflow:"auto",position:"fixed",left:"0px",top:"0px",maxWidth:"100px",maxHeight:"100px"});const e=document.createElement("div");return e.style.width="200px",e.style.height="200px",t.appendChild(e),document.body.appendChild(t),i=Math.abs(t.offsetWidth-100)>1?t.offsetWidth-t.clientWidth:0,document.body.removeChild(t),i})(),e=o.width-r.left-r.right,n=Math.min(e,m+g-t);this.sizingTarget.style.maxWidth=`${n}px`;const h=this.sizingTarget.offsetWidth+t,s=h-a;let l;"left"===f.horizontalAlign?l=z:"center"===f.horizontalAlign?l=z-s/2:"right"===f.horizontalAlign&&(l=z-s),l=Math.max(o.left+r.left,Math.min(l,o.right-r.right-h)),this.style.left=`${l}px`}}},constrain:function(){if(!this.__shouldPosition){this._discoverInfo();var t=this._fitInfo;t.positionedBy.vertically||(this.style.position="fixed",this.style.top="0px"),t.positionedBy.horizontally||(this.style.position="fixed",this.style.left="0px"),this.sizingTarget.style.boxSizing="border-box";var i=this.getBoundingClientRect();t.sizedBy.height||this.__sizeDimension(i,t.positionedBy.vertically,"top","bottom","Height"),t.sizedBy.width||this.__sizeDimension(i,t.positionedBy.horizontally,"left","right","Width")}},_sizeDimension:function(t,i,e,o,n){this.__sizeDimension(t,i,e,o,n)},__sizeDimension:function(t,i,e,o,n){var h=this._fitInfo,s=this.__getNormalizedRect(this.fitInto),l="Width"===n?s.width:s.height,r=i===o,a=r?l-t[o]:t[e],f=h.margin[r?e:o],g="offset"+n,d=this[g]-this.sizingTarget[g];this.sizingTarget.style["max"+n]=l-f-a-d+"px"},center:function(){if(!this.__shouldPosition){this._discoverInfo();var t=this._fitInfo.positionedBy;if(!t.vertically||!t.horizontally){this.style.position="fixed",t.vertically||(this.style.top="0px"),t.horizontally||(this.style.left="0px");var i=this.getBoundingClientRect(),e=this.__getNormalizedRect(this.fitInto);if(!t.vertically){var o=e.top-i.top+(e.height-i.height)/2;this.style.top=o+"px"}if(!t.horizontally){var n=e.left-i.left+(e.width-i.width)/2;this.style.left=n+"px"}}}},__getNormalizedRect:function(t){return t===document.documentElement||t===window?{top:0,left:0,width:window.innerWidth,height:window.innerHeight,right:window.innerWidth,bottom:window.innerHeight}:t.getBoundingClientRect()},__getOffscreenArea:function(t,i,e){var o=Math.min(0,t.top)+Math.min(0,e.bottom-(t.top+i.height)),n=Math.min(0,t.left)+Math.min(0,e.right-(t.left+i.width));return Math.abs(o)*i.width+Math.abs(n)*i.height},__getPosition:function(t,i,e,o,n,h){var s,l=[{verticalAlign:"top",horizontalAlign:"left",top:n.top+this.verticalOffset,left:n.left+this.horizontalOffset},{verticalAlign:"top",horizontalAlign:"right",top:n.top+this.verticalOffset,left:n.right-e.width-this.horizontalOffset},{verticalAlign:"bottom",horizontalAlign:"left",top:n.bottom-e.height-this.verticalOffset,left:n.left+this.horizontalOffset},{verticalAlign:"bottom",horizontalAlign:"right",top:n.bottom-e.height-this.verticalOffset,left:n.right-e.width-this.horizontalOffset}];if(this.noOverlap){for(var r=0,a=l.length;r<a;r++){var f={};for(var g in l[r])f[g]=l[r][g];l.push(f)}l[0].top=l[1].top+=n.height,l[2].top=l[3].top-=n.height,l[4].left=l[6].left+=n.width,l[5].left=l[7].left-=n.width}i="auto"===i?null:i,(t="auto"===t?null:t)&&"center"!==t||(l.push({verticalAlign:"top",horizontalAlign:"center",top:n.top+this.verticalOffset+(this.noOverlap?n.height:0),left:n.left-o.width/2+n.width/2+this.horizontalOffset}),l.push({verticalAlign:"bottom",horizontalAlign:"center",top:n.bottom-e.height-this.verticalOffset-(this.noOverlap?n.height:0),left:n.left-o.width/2+n.width/2+this.horizontalOffset})),i&&"middle"!==i||(l.push({verticalAlign:"middle",horizontalAlign:"left",top:n.top-o.height/2+n.height/2+this.verticalOffset,left:n.left+this.horizontalOffset+(this.noOverlap?n.width:0)}),l.push({verticalAlign:"middle",horizontalAlign:"right",top:n.top-o.height/2+n.height/2+this.verticalOffset,left:n.right-e.width-this.horizontalOffset-(this.noOverlap?n.width:0)})),"middle"===i&&"center"===t&&l.push({verticalAlign:"middle",horizontalAlign:"center",top:n.top-o.height/2+n.height/2+this.verticalOffset,left:n.left-o.width/2+n.width/2+this.horizontalOffset});for(r=0;r<l.length;r++){var d=l[r],p=d.verticalAlign===i,c=d.horizontalAlign===t;if(!this.dynamicAlign&&!this.noOverlap&&p&&c){s=d;break}var m=(!i||p)&&(!t||c);if(this.dynamicAlign||m){if(d.offscreenArea=this.__getOffscreenArea(d,e,h),0===d.offscreenArea&&m){s=d;break}s=s||d;var y=d.offscreenArea-s.offscreenArea;(y<0||0===y&&(p||c))&&(s=d)}}return s}};