• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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' />
17class TextStyleModifier extends ModifierWithKey<PickerTextStyle> {
18  constructor(value: PickerTextStyle) {
19    super(value);
20  }
21  static identity: Symbol = Symbol('textStyle');
22  applyPeer(node: KNode, reset: boolean): void {
23    if (reset) {
24      getUINativeModule().calendarPicker.resetTextStyle(node);
25    } else {
26      getUINativeModule().calendarPicker.setTextStyle(node,
27        this.value?.color ?? undefined,
28        this.value?.font?.size ?? undefined,
29        this.value?.font?.weight ?? undefined);
30    }
31  }
32
33  checkObjectDiff(): boolean {
34    if (!(this.stageValue?.font?.weight === this.value?.font?.weight)) {
35      return true;
36    } else {
37      return !isBaseOrResourceEqual(this.stageValue?.color, this.value?.color) ||
38        !isBaseOrResourceEqual(this.stageValue?.font?.size, this.value?.font?.size);
39    }
40  }
41}
42
43class EdgeAlignModifier extends ModifierWithKey<ArkEdgeAlign> {
44  constructor(value: ArkEdgeAlign) {
45    super(value);
46  }
47  static identity: Symbol = Symbol('edgeAlign');
48  applyPeer(node: KNode, reset: boolean): void {
49    if (reset) {
50      getUINativeModule().calendarPicker.resetEdgeAlign(node);
51    } else {
52      getUINativeModule().calendarPicker.setEdgeAlign(node,
53        this.value?.alignType ?? undefined,
54        this.value?.offset?.dx ?? undefined,
55        this.value?.offset?.dy ?? undefined);
56    }
57  }
58
59  checkObjectDiff(): boolean {
60    if (!(this.stageValue.alignType === this.value.alignType)) {
61      return true;
62    } else {
63      return !isBaseOrResourceEqual(this.stageValue?.offset?.dx, this.value?.offset?.dx) ||
64        !isBaseOrResourceEqual(this.stageValue?.offset?.dy, this.value?.offset?.dy);
65    }
66  }
67}
68
69class CalendarPickerPaddingModifier extends ModifierWithKey<ArkPadding> {
70  constructor(value: ArkPadding) {
71    super(value);
72  }
73  static identity: Symbol = Symbol('calendarPickerPadding');
74  applyPeer(node: KNode, reset: boolean): void {
75    if (reset) {
76      getUINativeModule().calendarPicker.resetCalendarPickerPadding(node);
77    } else {
78      getUINativeModule().calendarPicker.setCalendarPickerPadding(node, this.value.top,
79        this.value.right, this.value.bottom, this.value.left);
80    }
81  }
82
83  checkObjectDiff(): boolean {
84    return !isBaseOrResourceEqual(this.stageValue.top, this.value.top) ||
85      !isBaseOrResourceEqual(this.stageValue.right, this.value.right) ||
86      !isBaseOrResourceEqual(this.stageValue.bottom, this.value.bottom) ||
87      !isBaseOrResourceEqual(this.stageValue.left, this.value.left);
88  }
89}
90
91class CalendarPickerBorderModifier extends ModifierWithKey<ArkBorder> {
92  constructor(value: ArkBorder) {
93    super(value);
94  }
95  static identity: Symbol = Symbol('calendarPickerBorder');
96  applyPeer(node: KNode, reset: boolean): void {
97    if (reset) {
98      getUINativeModule().calendarPicker.resetCalendarPickerBorder(node);
99    } else {
100      getUINativeModule().calendarPicker.setCalendarPickerBorder(node,
101        this.value.arkWidth.left, this.value.arkWidth.right, this.value.arkWidth.top, this.value.arkWidth.bottom,
102        this.value.arkColor.leftColor, this.value.arkColor.rightColor, this.value.arkColor.topColor, this.value.arkColor.bottomColor,
103        this.value.arkRadius.topLeft, this.value.arkRadius.topRight, this.value.arkRadius.bottomLeft, this.value.arkRadius.bottomRight,
104        this.value.arkStyle.top, this.value.arkStyle.right, this.value.arkStyle.bottom, this.value.arkStyle.left);
105    }
106  }
107
108  checkObjectDiff(): boolean {
109    return this.value.checkObjectDiff(this.stageValue);
110  }
111}
112
113class ArkCalendarPickerComponent extends ArkComponent implements CalendarPickerAttribute {
114  constructor(nativePtr: KNode) {
115    super(nativePtr);
116  }
117  edgeAlign(alignType: CalendarAlign, offset?: Offset | undefined): this {
118    let arkEdgeAlign = new ArkEdgeAlign();
119    arkEdgeAlign.alignType = alignType;
120    arkEdgeAlign.offset = offset;
121    modifierWithKey(this._modifiersWithKeys, EdgeAlignModifier.identity, EdgeAlignModifier, arkEdgeAlign);
122    return this;
123  }
124  textStyle(value: PickerTextStyle): this {
125    modifierWithKey(this._modifiersWithKeys, TextStyleModifier.identity, TextStyleModifier, value);
126    return this;
127  }
128  onChange(callback: (value: Date) => void): this {
129    throw new Error('Method not implemented.');
130  }
131  padding(value: Padding | Length): this {
132    let arkValue = new ArkPadding();
133    if (value !== null && value !== undefined) {
134      if (isLengthType(value) || isResource(value)) {
135        arkValue.top = <Length>value;
136        arkValue.right = <Length>value;
137        arkValue.bottom = <Length>value;
138        arkValue.left = <Length>value;
139      } else {
140        arkValue.top = (<Margin>value).top;
141        arkValue.right = (<Margin>value).right;
142        arkValue.bottom = (<Margin>value).bottom;
143        arkValue.left = (<Margin>value).left;
144      }
145      modifierWithKey(this._modifiersWithKeys, CalendarPickerPaddingModifier.identity,
146        CalendarPickerPaddingModifier, arkValue);
147    } else {
148      modifierWithKey(this._modifiersWithKeys, CalendarPickerPaddingModifier.identity,
149        CalendarPickerPaddingModifier, undefined);
150    }
151    return this;
152  }
153  border(value: BorderOptions): this {
154    let arkBorder = new ArkBorder();
155    if (isUndefined(value)) {
156      arkBorder = undefined;
157    }
158
159    if (!isUndefined(value?.width) && value?.width !== null) {
160      if (isNumber(value.width) || isString(value.width) || isResource(value.width)) {
161        arkBorder.arkWidth.left = value.width;
162        arkBorder.arkWidth.right = value.width;
163        arkBorder.arkWidth.top = value.width;
164        arkBorder.arkWidth.bottom = value.width;
165      } else {
166        arkBorder.arkWidth.left = (value.width as EdgeWidths).left;
167        arkBorder.arkWidth.right = (value.width as EdgeWidths).right;
168        arkBorder.arkWidth.top = (value.width as EdgeWidths).top;
169        arkBorder.arkWidth.bottom = (value.width as EdgeWidths).bottom;
170      }
171    }
172    if (!isUndefined(value?.color) && value?.color !== null) {
173      if (isNumber(value.color) || isString(value.color) || isResource(value.color)) {
174        arkBorder.arkColor.leftColor = value.color;
175        arkBorder.arkColor.rightColor = value.color;
176        arkBorder.arkColor.topColor = value.color;
177        arkBorder.arkColor.bottomColor = value.color;
178      } else {
179        arkBorder.arkColor.leftColor = (value.color as EdgeColors).left;
180        arkBorder.arkColor.rightColor = (value.color as EdgeColors).right;
181        arkBorder.arkColor.topColor = (value.color as EdgeColors).top;
182        arkBorder.arkColor.bottomColor = (value.color as EdgeColors).bottom;
183      }
184    }
185    if (!isUndefined(value?.radius) && value?.radius !== null) {
186      if (isNumber(value.radius) || isString(value.radius) || isResource(value.radius)) {
187        arkBorder.arkRadius.topLeft = value.radius;
188        arkBorder.arkRadius.topRight = value.radius;
189        arkBorder.arkRadius.bottomLeft = value.radius;
190        arkBorder.arkRadius.bottomRight = value.radius;
191      } else {
192        arkBorder.arkRadius.topLeft = (value.radius as BorderRadiuses)?.topLeft;
193        arkBorder.arkRadius.topRight = (value.radius as BorderRadiuses)?.topRight;
194        arkBorder.arkRadius.bottomLeft = (value.radius as BorderRadiuses)?.bottomLeft;
195        arkBorder.arkRadius.bottomRight = (value.radius as BorderRadiuses)?.bottomRight;
196      }
197    }
198    if (!isUndefined(value?.style) && value?.style !== null) {
199      let arkBorderStyle = new ArkBorderStyle();
200      if (arkBorderStyle.parseBorderStyle(value.style)) {
201        if (!isUndefined(arkBorderStyle.style)) {
202          arkBorder.arkStyle.top = arkBorderStyle.style;
203          arkBorder.arkStyle.left = arkBorderStyle.style;
204          arkBorder.arkStyle.bottom = arkBorderStyle.style;
205          arkBorder.arkStyle.right = arkBorderStyle.style;
206        } else {
207          arkBorder.arkStyle.top = arkBorderStyle.top;
208          arkBorder.arkStyle.left = arkBorderStyle.left;
209          arkBorder.arkStyle.bottom = arkBorderStyle.bottom;
210          arkBorder.arkStyle.right = arkBorderStyle.right;
211        }
212      }
213    }
214    modifierWithKey(this._modifiersWithKeys, CalendarPickerBorderModifier.identity, CalendarPickerBorderModifier, arkBorder);
215    return this;
216  }
217}
218// @ts-ignore
219globalThis.CalendarPicker.attributeModifier = function (modifier) {
220  const elmtId = ViewStackProcessor.GetElmtIdToAccountFor();
221  let nativeNode = getUINativeModule().getFrameNodeById(elmtId);
222  let component = this.createOrGetNode(elmtId, () => {
223    return new ArkCalendarPickerComponent(nativeNode);
224  });
225  applyUIAttributes(modifier, nativeNode, component);
226  component.applyModifierPatch();
227};
228