@progress/kendo-angular-layout
Version:
Kendo UI for Angular Layout Package - a collection of components to create professional application layoyts
180 lines (179 loc) • 7.72 kB
JavaScript
/**-----------------------------------------------------------------------------------------
* Copyright © 2025 Progress Software Corporation. All rights reserved.
* Licensed under commercial license. See LICENSE.md in the project root for more information
*-------------------------------------------------------------------------------------------*/
import { Component, HostBinding, Input, Renderer2, ElementRef, isDevMode } from '@angular/core';
import { validatePackage } from '@progress/kendo-licensing';
import { packageMetadata } from '../package-metadata';
import { ALIGN_PREFIX, generateGapStyle, generateGridStyle, GRID_JUSTIFY_PREFIX, normalizeGap, VERTICAL_SUFFIX, validateGridLayoutRowsCols } from './util';
import { L10N_PREFIX, LocalizationService } from '@progress/kendo-angular-l10n';
import { isPresent } from '../common/util';
import { isChanged } from '@progress/kendo-angular-common';
import * as i0 from "@angular/core";
import * as i1 from "@progress/kendo-angular-l10n";
/**
* Represents the Kendo UI GridLayout component for Angular.
* Arranges child components in a two-dimensional grid layout with customizable rows, columns, and gaps ([see overview]({% slug overview_gridlayout %})).
*
* @example
* ```html
* <kendo-gridlayout [rows]="[100, 200]" [cols]="['1fr', '2fr']" [gap]="10">
* <kendo-gridlayout-item [row]="1" [col]="1">
* <p>Item 1</p>
* </kendo-gridlayout-item>
* <kendo-gridlayout-item [row]="1" [col]="2">
* <p>Item 2</p>
* </kendo-gridlayout-item>
* </kendo-gridlayout>
* ```
*
* @remarks
* Supported children components are: {@link GridLayoutItemComponent}.
*/
export class GridLayoutComponent {
renderer;
element;
localization;
hostClass = true;
get dir() {
return this.direction;
}
/**
* Specifies the number of rows and their height
* ([More details](slug:layout_gridlayout#toc-rows-and-columns)).
*
* You can define rows by passing an array where the number of elements determines the number of rows or each element defines the size of the corresponding row.
* For example, you can use CSS units like `px`, `%`, `fr`, or `auto` to define the row sizes.
*/
rows;
/**
* Specifies the number of columns and their widths
* ([More details](slug:layout_gridlayout#toc-rows-and-columns)).
*
* You can define columns by passing an array where the number of elements determines the number of columns or each element defines the size of the corresponding column.
* For example, you can use CSS units like `px`, `%`, `fr`, or `auto` to define the column sizes.
*/
cols;
/**
* Specifies the gaps between the elements ([see example](slug:layout_gridlayout#toc-gap)).
*
* @default 0
*/
gap = 0;
/**
* Specifies the horizontal and vertical alignment of the inner GridLayout elements
* ([see example]({% slug layout_gridlayout %}#toc-alignment)).
*/
set align(align) {
this._align = Object.assign({}, this._align, align);
this.handleAlignClasses();
}
get align() {
return this._align;
}
_align = {
horizontal: 'stretch',
vertical: 'stretch'
};
justifyClass;
alignClass;
constructor(renderer, element, localization) {
this.renderer = renderer;
this.element = element;
this.localization = localization;
validatePackage(packageMetadata);
}
ngAfterViewInit() {
this.handleAlignClasses();
this.handleGridTemplateStyling('rows');
this.handleGridTemplateStyling('cols');
this.setGap();
}
ngOnChanges(changes) {
if (isChanged('gap', changes)) {
this.setGap();
}
if (isChanged('rows', changes)) {
this.handleGridTemplateStyling('rows');
}
if (isChanged('cols', changes)) {
this.handleGridTemplateStyling('cols');
}
}
handleAlignClasses() {
const elem = this.element.nativeElement;
if (isPresent(this.justifyClass)) {
this.renderer.removeClass(elem, this.justifyClass);
}
if (isPresent(this.alignClass)) {
this.renderer.removeClass(elem, this.alignClass);
}
this.justifyClass = `${GRID_JUSTIFY_PREFIX}-${this.align.horizontal}`;
this.alignClass = `${ALIGN_PREFIX}-${VERTICAL_SUFFIX[this.align.vertical]}`;
this.renderer.addClass(elem, this.justifyClass);
this.renderer.addClass(elem, this.alignClass);
}
setGap() {
const parsedGap = normalizeGap(this.gap);
const gapStyle = generateGapStyle(parsedGap);
this.renderer.setStyle(this.element.nativeElement, 'gap', gapStyle);
}
handleGridTemplateStyling(type) {
if (!isPresent(this[type])) {
return;
}
const isValid = validateGridLayoutRowsCols(this[type]);
if (!isValid && isDevMode()) {
const valueType = type === 'rows' ? 'GridLayoutRowSize' : 'GridLayoutColSize';
throw new Error(`The provided ${type} value contains invalid elements. The array supports values of type number, string or ${valueType}.`);
}
const gridTemplateStyle = type === 'rows' ? 'grid-template-rows' : 'grid-template-columns';
const gridStyle = generateGridStyle(this[type], type);
this.renderer.setStyle(this.element.nativeElement, gridTemplateStyle, gridStyle.join(' '));
}
get direction() {
return this.localization.rtl ? 'rtl' : 'ltr';
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: GridLayoutComponent, deps: [{ token: i0.Renderer2 }, { token: i0.ElementRef }, { token: i1.LocalizationService }], target: i0.ɵɵFactoryTarget.Component });
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "16.2.12", type: GridLayoutComponent, isStandalone: true, selector: "kendo-gridlayout", inputs: { rows: "rows", cols: "cols", gap: "gap", align: "align" }, host: { properties: { "class.k-grid-layout": "this.hostClass", "attr.dir": "this.dir" } }, providers: [
LocalizationService,
{
provide: L10N_PREFIX,
useValue: 'kendo.gridlayout'
}
], exportAs: ["kendoGridLayout"], usesOnChanges: true, ngImport: i0, template: `
<ng-content></ng-content>
`, isInline: true });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: GridLayoutComponent, decorators: [{
type: Component,
args: [{
exportAs: 'kendoGridLayout',
selector: 'kendo-gridlayout',
providers: [
LocalizationService,
{
provide: L10N_PREFIX,
useValue: 'kendo.gridlayout'
}
],
template: `
<ng-content></ng-content>
`,
standalone: true
}]
}], ctorParameters: function () { return [{ type: i0.Renderer2 }, { type: i0.ElementRef }, { type: i1.LocalizationService }]; }, propDecorators: { hostClass: [{
type: HostBinding,
args: ['class.k-grid-layout']
}], dir: [{
type: HostBinding,
args: ['attr.dir']
}], rows: [{
type: Input
}], cols: [{
type: Input
}], gap: [{
type: Input
}], align: [{
type: Input
}] } });