@haxtheweb/haxcms-nodejs
Version:
HAXcms single and multisite nodejs server, api, and administration
6 lines • 2.52 kB
JavaScript
/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
import{SizeGapPaddingBaseLayout as e,gap2 as i}from"./SizeGapPaddingBaseLayout.js";import{dim1 as t}from"./BaseLayout.js";export class GridBaseLayout extends e{constructor(){super(...arguments),this._metrics=null,this.flex=null,this.justify=null}_getDefaultConfig(){return Object.assign({},super._getDefaultConfig(),{flex:!1,justify:"start"})}set gap(e){super._setGap(e)}_updateLayout(){const e=this.justify,[s,a]=this._padding1,[n,r]=this._padding2;["_gap1","_gap2"].forEach((t=>{const s=this[t];if(s===1/0&&!["space-between","space-around","space-evenly"].includes(e))throw new Error("grid layout: gap can only be set to 'auto' when justify is set to 'space-between', 'space-around' or 'space-evenly'");if(s===1/0&&"_gap2"===t)throw new Error(`grid layout: ${i(this.direction)}-gap cannot be set to 'auto' when direction is set to ${this.direction}`)}));const o=this.flex||["start","center","end"].includes(e),d={rolumns:-1,itemSize1:-1,itemSize2:-1,gap1:this._gap1===1/0?-1:this._gap1,gap2:o?this._gap2:0,padding1:{start:s===1/0?this._gap1:s,end:a===1/0?this._gap1:a},padding2:o?{start:n===1/0?this._gap2:n,end:r===1/0?this._gap2:r}:{start:0,end:0},positions:[]},p=this._viewDim2-d.padding2.start-d.padding2.end;if(p<=0)d.rolumns=0;else{const i=o?d.gap2:0;let n,r=0,h=0;if(p>=this._idealSize2&&(r=Math.floor((p-this._idealSize2)/(this._idealSize2+i))+1,h=r*this._idealSize2+(r-1)*i),this.flex){(p-h)/(this._idealSize2+i)>=.5&&(r+=1),d.rolumns=r,d.itemSize2=Math.round((p-i*(r-1))/r);switch(!0===this.flex?"area":this.flex.preserve){case"aspect-ratio":d.itemSize1=Math.round(this._idealSize1/this._idealSize2*d.itemSize2);break;case t(this.direction):d.itemSize1=Math.round(this._idealSize1);break;default:d.itemSize1=Math.round(this._idealSize1*this._idealSize2/d.itemSize2)}}else d.itemSize1=this._idealSize1,d.itemSize2=this._idealSize2,d.rolumns=r;if(o){const i=d.rolumns*d.itemSize2+(d.rolumns-1)*d.gap2;n=this.flex||"start"===e?d.padding2.start:"end"===e?this._viewDim2-d.padding2.end-i:Math.round(this._viewDim2/2-i/2)}else{const i=p-d.rolumns*d.itemSize2;"space-between"===e?(d.gap2=Math.round(i/(d.rolumns-1)),n=0):"space-around"===e?(d.gap2=Math.round(i/d.rolumns),n=Math.round(d.gap2/2)):(d.gap2=Math.round(i/(d.rolumns+1)),n=d.gap2),this._gap1===1/0&&(d.gap1=d.gap2,s===1/0&&(d.padding1.start=n),a===1/0&&(d.padding1.end=n))}for(let e=0;e<d.rolumns;e++)d.positions.push(n),n+=d.itemSize2+d.gap2}this._metrics=d}}