1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16/// <reference path='./import.ts' /> 17namespace BreakpointConstants { 18 export const XS = 0; 19 export const SM = 1; 20 export const MD = 2; 21 export const LG = 3; 22 export const XL = 4; 23 export const XXL = 5; 24}; 25class GridRowAlignItemsModifier extends ModifierWithKey<number> { 26 constructor(value: number) { 27 super(value); 28 } 29 static identity: Symbol = Symbol('gridRowAlignItems'); 30 applyPeer(node: KNode, reset: boolean): void { 31 if (reset) { 32 getUINativeModule().gridRow.resetAlignItems(node); 33 } else { 34 getUINativeModule().gridRow.setAlignItems(node, this.value!); 35 } 36 } 37 checkObjectDiff(): boolean { 38 return !isBaseOrResourceEqual(this.stageValue, this.value); 39 } 40} 41class SetDirectionModifier extends ModifierWithKey<number> { 42 constructor(value: number) { 43 super(value); 44 } 45 static identity: Symbol = Symbol('gridRowDirection'); 46 applyPeer(node: KNode, reset: boolean): void { 47 if (reset) { 48 getUINativeModule().gridRow.resetDirection(node); 49 } else { 50 getUINativeModule().gridRow.setDirection(node, this.value); 51 } 52 } 53} 54class SetBreakpointsModifier extends ModifierWithKey<BreakPoints> { 55 constructor(value: BreakPoints) { 56 super(value); 57 } 58 static identity: Symbol = Symbol('gridRowBreakpoints'); 59 applyPeer(node: KNode, reset: boolean): void { 60 if (reset) { 61 getUINativeModule().gridRow.resetBreakpoints(node); 62 } else { 63 getUINativeModule().gridRow.setBreakpoints(node, this.value.value, this.value.reference); 64 } 65 } 66} 67class SetColumnsModifier extends ModifierWithKey<number | GridRowColumnOption> { 68 constructor(value: number | GridRowColumnOption) { 69 super(value); 70 } 71 static identity: Symbol = Symbol('gridRowColumns'); 72 applyPeer(node: KNode, reset: boolean): void { 73 if (reset) { 74 getUINativeModule().gridRow.resetColumns(node); 75 } else { 76 if (isNumber(this.value)) { 77 getUINativeModule().gridRow.setColumns(node, this.value, 78 this.value, this.value, this.value, this.value, this.value); 79 } else { 80 getUINativeModule().gridRow.setColumns(node, this.value.xs, 81 this.value.sm, this.value.md, this.value.lg, this.value.xl, this.value.xxl); 82 } 83 } 84 } 85} 86class SetGutterModifier extends ModifierWithKey<number | GutterOption> { 87 constructor(value: number | GutterOption) { 88 super(value); 89 } 90 static identity: Symbol = Symbol('gridRowGutter'); 91 parseGutter(value: number | GridRowSizeOption): number[] { 92 let gutters: number[] = [0, 0, 0, 0, 0, 0]; 93 if (isNumber(value)) { 94 gutters[BreakpointConstants.XS] = value; 95 gutters[BreakpointConstants.SM] = value; 96 gutters[BreakpointConstants.MD] = value; 97 gutters[BreakpointConstants.LG] = value; 98 gutters[BreakpointConstants.XL] = value; 99 gutters[BreakpointConstants.XXL] = value; 100 } else { 101 gutters[BreakpointConstants.XS] = value?.xs; 102 gutters[BreakpointConstants.SM] = value?.sm; 103 gutters[BreakpointConstants.MD] = value?.md; 104 gutters[BreakpointConstants.LG] = value?.lg; 105 gutters[BreakpointConstants.XL] = value?.xl; 106 gutters[BreakpointConstants.XXL] = value?.xxl; 107 } 108 return gutters; 109 } 110 applyPeer(node: KNode, reset: boolean): void { 111 if (reset) { 112 getUINativeModule().gridRow.resetGutter(node); 113 return; 114 } 115 if (isNumber(this.value)) { 116 getUINativeModule().gridRow.setGutter(node, this.value, 117 this.value, this.value, this.value, this.value, this.value, 118 this.value, this.value, this.value, this.value, this.value, this.value); 119 return; 120 } 121 let xGutters: number[] = this.parseGutter(this.value.x); 122 let yGutters: number[] = this.parseGutter(this.value.y); 123 getUINativeModule().gridRow.setGutter(node, 124 xGutters[BreakpointConstants.XS], xGutters[BreakpointConstants.SM], xGutters[BreakpointConstants.MD], 125 xGutters[BreakpointConstants.LG], xGutters[BreakpointConstants.XL], xGutters[BreakpointConstants.XXL], 126 yGutters[BreakpointConstants.XS], yGutters[BreakpointConstants.SM], yGutters[BreakpointConstants.MD], 127 yGutters[BreakpointConstants.LG], yGutters[BreakpointConstants.XL], yGutters[BreakpointConstants.XXL] 128 ); 129 } 130} 131class GridRowOnBreakpointChangeModifier extends ModifierWithKey<(breakpoints: string) => void> { 132 constructor(value: (breakpoints: string) => void) { 133 super(value); 134 } 135 static identity = Symbol('gridRowOnBreakpointChange'); 136 applyPeer(node: KNode, reset: boolean): void { 137 if (reset) { 138 getUINativeModule().gridRow.resetOnBreakpointChange(node); 139 } else { 140 getUINativeModule().gridRow.setOnBreakpointChange(node, this.value); 141 } 142 } 143} 144interface GridRowParam { 145 gutter?: number | GutterOption; 146 columns?: number | GridRowColumnOption; 147 breakpoints?: BreakPoints; 148 direction?: number; 149} 150class ArkGridRowComponent extends ArkComponent implements CommonMethod<GridRowAttribute> { 151 constructor(nativePtr: KNode, classType?: ModifierType) { 152 super(nativePtr, classType); 153 } 154 allowChildTypes(): string[] { 155 return ["GridCol"]; 156 } 157 onBreakpointChange(callback: (breakpoints: string) => void): GridRowAttribute { 158 modifierWithKey(this._modifiersWithKeys, GridRowOnBreakpointChangeModifier.identity, GridRowOnBreakpointChangeModifier, callback); 159 return this; 160 } 161 alignItems(value: ItemAlign): GridRowAttribute { 162 modifierWithKey(this._modifiersWithKeys, GridRowAlignItemsModifier.identity, GridRowAlignItemsModifier, value); 163 return this; 164 } 165 setDirection(value: number): GridRowAttribute { 166 modifierWithKey(this._modifiersWithKeys, SetDirectionModifier.identity, SetDirectionModifier, value); 167 return this; 168 } 169 setBreakpoints(value: BreakPoints): GridRowAttribute { 170 modifierWithKey(this._modifiersWithKeys, SetBreakpointsModifier.identity, SetBreakpointsModifier, value); 171 return this; 172 } 173 setColumns(value: number | GridRowColumnOption): GridRowAttribute { 174 modifierWithKey(this._modifiersWithKeys, SetColumnsModifier.identity, SetColumnsModifier, value); 175 return this; 176 } 177 setGutter(value: number | GutterOption): GridRowAttribute { 178 modifierWithKey(this._modifiersWithKeys, SetGutterModifier.identity, SetGutterModifier, value); 179 return this; 180 } 181 initialize(value: Object[]): GridRowAttribute { 182 if (value[0] !== undefined) { 183 this.setGutter((value[0] as GridRowParam).gutter); 184 this.setColumns((value[0] as GridRowParam).columns); 185 this.setBreakpoints((value[0] as GridRowParam).breakpoints); 186 this.setDirection((value[0] as GridRowParam).direction); 187 } else { 188 this.setGutter(null); 189 this.setColumns(null); 190 this.setBreakpoints(null); 191 this.setDirection(null); 192 } 193 return this; 194 } 195} 196// @ts-ignore 197globalThis.GridRow.attributeModifier = function (modifier: ArkComponent): void { 198 attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => { 199 return new ArkGridRowComponent(nativePtr); 200 }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => { 201 return new modifierJS.GridRowModifier(nativePtr, classType); 202 }); 203}; 204