• 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 CalendarPickerHeightModifier extends ModifierWithKey<Length> {
114  constructor(value: Length) {
115    super(value);
116  }
117  static identity: Symbol = Symbol('calendarPickerHeight');
118  applyPeer(node: KNode, reset: boolean): void {
119    if (reset) {
120      getUINativeModule().calendarPicker.resetCalendarPickerHeight(node);
121    } else {
122      getUINativeModule().calendarPicker.setCalendarPickerHeight(node, this.value);
123    }
124  }
125
126  checkObjectDiff(): boolean {
127    return !isBaseOrResourceEqual(this.stageValue, this.value);
128  }
129}
130
131class CalendarPickerBorderRadiusModifier extends ModifierWithKey<Length | BorderRadiuses | LocalizedBorderRadius> {
132  constructor(value: Length | BorderRadiuses | LocalizedBorderRadius) {
133    super(value);
134  }
135  static identity: Symbol = Symbol('calendarPickerBorderRadius');
136  applyPeer(node: KNode, reset: boolean): void {
137    if (reset) {
138      getUINativeModule().calendarPicker.resetCalendarPickerBorderRadius(node);
139    } else {
140      if (isNumber(this.value) || isString(this.value) || isResource(this.value)) {
141        getUINativeModule().calendarPicker.setCalendarPickerBorderRadius(node, this.value, this.value, this.value, this.value);
142      } else {
143        if ((Object.keys(this.value).indexOf('topStart') >= 0) ||
144            (Object.keys(this.value).indexOf('topEnd') >= 0) ||
145            (Object.keys(this.value).indexOf('bottomStart') >= 0) ||
146            (Object.keys(this.value).indexOf('bottomEnd') >= 0)) {
147          getUINativeModule().calendarPicker.setCalendarPickerBorderRadius(node,
148            (this.value as LocalizedBorderRadius).topStart,
149            (this.value as LocalizedBorderRadius).topEnd,
150            (this.value as LocalizedBorderRadius).bottomStart,
151            (this.value as LocalizedBorderRadius).bottomEnd);
152        } else {
153          getUINativeModule().calendarPicker.setCalendarPickerBorderRadius(node,
154            (this.value as BorderRadiuses).topLeft,
155            (this.value as BorderRadiuses).topRight,
156            (this.value as BorderRadiuses).bottomLeft,
157            (this.value as BorderRadiuses).bottomRight);
158        }
159      }
160    }
161  }
162
163  checkObjectDiff(): boolean {
164    if (isResource(this.stageValue) && isResource(this.value)) {
165      return !isResourceEqual(this.stageValue, this.value);
166    } else if (!isResource(this.stageValue) && !isResource(this.value)) {
167      if ((Object.keys(this.value).indexOf('topStart') >= 0) ||
168          (Object.keys(this.value).indexOf('topEnd') >= 0) ||
169          (Object.keys(this.value).indexOf('bottomStart') >= 0) ||
170          (Object.keys(this.value).indexOf('bottomEnd') >= 0)) {
171        return !((this.stageValue as LocalizedBorderRadius).topStart === (this.value as LocalizedBorderRadius).topStart &&
172          (this.stageValue as LocalizedBorderRadius).topEnd === (this.value as LocalizedBorderRadius).topEnd &&
173          (this.stageValue as LocalizedBorderRadius).bottomStart === (this.value as LocalizedBorderRadius).bottomStart &&
174          (this.stageValue as LocalizedBorderRadius).bottomEnd === (this.value as LocalizedBorderRadius).bottomEnd);
175      }
176      return !((this.stageValue as BorderRadiuses).topLeft === (this.value as BorderRadiuses).topLeft &&
177        (this.stageValue as BorderRadiuses).topRight === (this.value as BorderRadiuses).topRight &&
178        (this.stageValue as BorderRadiuses).bottomLeft === (this.value as BorderRadiuses).bottomLeft &&
179        (this.stageValue as BorderRadiuses).bottomRight === (this.value as BorderRadiuses).bottomRight);
180    } else {
181      return true;
182    }
183  }
184}
185
186class CalendarPickerBorderColorModifier extends ModifierWithKey<ResourceColor | EdgeColors | LocalizedEdgeColors> {
187  constructor(value: ResourceColor | EdgeColors | LocalizedEdgeColors) {
188    super(value);
189  }
190  static identity: Symbol = Symbol('calendarPickerBorderColor');
191  applyPeer(node: KNode, reset: boolean): void {
192    if (reset) {
193      getUINativeModule().calendarPicker.resetCalendarPickerBorderColor(node);
194    } else {
195      const valueType: string = typeof this.value;
196      if (valueType === 'number' || valueType === 'string' || isResource(this.value)) {
197        getUINativeModule().calendarPicker.setCalendarPickerBorderColor(node, this.value, this.value, this.value, this.value);
198      } else {
199        if ((Object.keys(this.value).indexOf('start') >= 0) ||
200            (Object.keys(this.value).indexOf('end') >= 0)) {
201          getUINativeModule().calendarPicker.setCalendarPickerBorderColor(node,
202            (this.value as LocalizedEdgeColors).top,
203            (this.value as LocalizedEdgeColors).end,
204            (this.value as LocalizedEdgeColors).bottom,
205            (this.value as LocalizedEdgeColors).start,
206            true);
207        } else {
208          getUINativeModule().calendarPicker.setCalendarPickerBorderColor(node,
209            (this.value as EdgeColors).top,
210            (this.value as EdgeColors).right,
211            (this.value as EdgeColors).bottom,
212            (this.value as EdgeColors).left,
213            false);
214        }
215      }
216
217    }
218  }
219
220  checkObjectDiff(): boolean {
221    if (isResource(this.stageValue) && isResource(this.value)) {
222      return !isResourceEqual(this.stageValue, this.value);
223    } else if (!isResource(this.stageValue) && !isResource(this.value)) {
224      if ((Object.keys(this.value).indexOf('start') >= 0) ||
225          (Object.keys(this.value).indexOf('end') >= 0)) {
226        return !((this.stageValue as LocalizedEdgeColors).start === (this.value as LocalizedEdgeColors).start &&
227          (this.stageValue as LocalizedEdgeColors).end === (this.value as LocalizedEdgeColors).end &&
228          (this.stageValue as LocalizedEdgeColors).top === (this.value as LocalizedEdgeColors).top &&
229          (this.stageValue as LocalizedEdgeColors).bottom === (this.value as LocalizedEdgeColors).bottom);
230      }
231      return !((this.stageValue as EdgeColors).left === (this.value as EdgeColors).left &&
232        (this.stageValue as EdgeColors).right === (this.value as EdgeColors).right &&
233        (this.stageValue as EdgeColors).top === (this.value as EdgeColors).top &&
234        (this.stageValue as EdgeColors).bottom === (this.value as EdgeColors).bottom);
235    } else {
236      return true;
237    }
238  }
239}
240
241class CalendarPickerMarkTodayModifier extends ModifierWithKey<boolean> {
242  constructor(value: boolean) {
243    super(value);
244  }
245  static identity: Symbol = Symbol('calendarPickerMarkToday');
246  applyPeer(node: KNode, reset: boolean): void {
247    if (reset) {
248      getUINativeModule().calendarPicker.resetCalendarPickerMarkToday(node);
249    } else {
250      getUINativeModule().calendarPicker.setCalendarPickerMarkToday(node, this.value);
251    }
252  }
253  checkObjectDiff(): boolean {
254    return !isBaseOrResourceEqual(this.stageValue, this.value);
255  }
256}
257
258class CalendarPickerOnChangeModifier extends ModifierWithKey<Callback<Date>>{
259  constructor(value: Callback<Date>) {
260    super(value);
261  }
262  static identity: Symbol = Symbol('calendarPickerOnChange');
263  applyPeer(node: KNode, reset: boolean): void {
264    if (reset) {
265      getUINativeModule().calendarPicker.resetCalendarPickerOnChange(node);
266    } else {
267      getUINativeModule().calendarPicker.setCalendarPickerOnChange(node, this.value);
268    }
269  }
270}
271
272class ArkCalendarPickerComponent extends ArkComponent implements CalendarPickerAttribute {
273  constructor(nativePtr: KNode, classType?: ModifierType) {
274    super(nativePtr, classType);
275  }
276  edgeAlign(alignType: CalendarAlign, offset?: Offset | undefined): this {
277    let arkEdgeAlign = new ArkEdgeAlign();
278    arkEdgeAlign.alignType = alignType;
279    arkEdgeAlign.offset = offset;
280    modifierWithKey(this._modifiersWithKeys, EdgeAlignModifier.identity, EdgeAlignModifier, arkEdgeAlign);
281    return this;
282  }
283  textStyle(value: PickerTextStyle): this {
284    modifierWithKey(this._modifiersWithKeys, TextStyleModifier.identity, TextStyleModifier, value);
285    return this;
286  }
287  onChange(callback: Callback<Date>): this {
288    modifierWithKey(this._modifiersWithKeys,CalendarPickerOnChangeModifier.identity,CalendarPickerOnChangeModifier,callback);
289    return this;
290  }
291  padding(value: Padding | Length): this {
292    let arkValue = new ArkPadding();
293    if (value !== null && value !== undefined) {
294      if (isLengthType(value) || isResource(value)) {
295        arkValue.top = <Length>value;
296        arkValue.right = <Length>value;
297        arkValue.bottom = <Length>value;
298        arkValue.left = <Length>value;
299      } else {
300        arkValue.top = (<Margin>value).top;
301        arkValue.right = (<Margin>value).right;
302        arkValue.bottom = (<Margin>value).bottom;
303        arkValue.left = (<Margin>value).left;
304      }
305      modifierWithKey(this._modifiersWithKeys, CalendarPickerPaddingModifier.identity,
306        CalendarPickerPaddingModifier, arkValue);
307    } else {
308      modifierWithKey(this._modifiersWithKeys, CalendarPickerPaddingModifier.identity,
309        CalendarPickerPaddingModifier, undefined);
310    }
311    return this;
312  }
313  border(value: BorderOptions): this {
314    let arkBorder = new ArkBorder();
315    if (isUndefined(value)) {
316      arkBorder = undefined;
317    }
318
319    if (!isUndefined(value?.width) && value?.width !== null) {
320      if (isNumber(value.width) || isString(value.width) || isResource(value.width)) {
321        arkBorder.arkWidth.left = value.width;
322        arkBorder.arkWidth.right = value.width;
323        arkBorder.arkWidth.top = value.width;
324        arkBorder.arkWidth.bottom = value.width;
325      } else {
326        arkBorder.arkWidth.left = (value.width as EdgeWidths).left;
327        arkBorder.arkWidth.right = (value.width as EdgeWidths).right;
328        arkBorder.arkWidth.top = (value.width as EdgeWidths).top;
329        arkBorder.arkWidth.bottom = (value.width as EdgeWidths).bottom;
330      }
331    }
332    if (!isUndefined(value?.color) && value?.color !== null) {
333      if (isNumber(value.color) || isString(value.color) || isResource(value.color)) {
334        arkBorder.arkColor.leftColor = value.color;
335        arkBorder.arkColor.rightColor = value.color;
336        arkBorder.arkColor.topColor = value.color;
337        arkBorder.arkColor.bottomColor = value.color;
338      } else {
339        arkBorder.arkColor.leftColor = (value.color as EdgeColors).left;
340        arkBorder.arkColor.rightColor = (value.color as EdgeColors).right;
341        arkBorder.arkColor.topColor = (value.color as EdgeColors).top;
342        arkBorder.arkColor.bottomColor = (value.color as EdgeColors).bottom;
343      }
344    }
345    if (!isUndefined(value?.radius) && value?.radius !== null) {
346      if (isNumber(value.radius) || isString(value.radius) || isResource(value.radius)) {
347        arkBorder.arkRadius.topLeft = value.radius;
348        arkBorder.arkRadius.topRight = value.radius;
349        arkBorder.arkRadius.bottomLeft = value.radius;
350        arkBorder.arkRadius.bottomRight = value.radius;
351      } else {
352        arkBorder.arkRadius.topLeft = (value.radius as BorderRadiuses)?.topLeft;
353        arkBorder.arkRadius.topRight = (value.radius as BorderRadiuses)?.topRight;
354        arkBorder.arkRadius.bottomLeft = (value.radius as BorderRadiuses)?.bottomLeft;
355        arkBorder.arkRadius.bottomRight = (value.radius as BorderRadiuses)?.bottomRight;
356      }
357    }
358    if (!isUndefined(value?.style) && value?.style !== null) {
359      let arkBorderStyle = new ArkBorderStyle();
360      if (arkBorderStyle.parseBorderStyle(value.style)) {
361        if (!isUndefined(arkBorderStyle.style)) {
362          arkBorder.arkStyle.top = arkBorderStyle.style;
363          arkBorder.arkStyle.left = arkBorderStyle.style;
364          arkBorder.arkStyle.bottom = arkBorderStyle.style;
365          arkBorder.arkStyle.right = arkBorderStyle.style;
366        } else {
367          arkBorder.arkStyle.top = arkBorderStyle.top;
368          arkBorder.arkStyle.left = arkBorderStyle.left;
369          arkBorder.arkStyle.bottom = arkBorderStyle.bottom;
370          arkBorder.arkStyle.right = arkBorderStyle.right;
371        }
372      }
373    }
374    modifierWithKey(this._modifiersWithKeys, CalendarPickerBorderModifier.identity, CalendarPickerBorderModifier, arkBorder);
375    return this;
376  }
377  height(value: Length): this {
378    modifierWithKey(this._modifiersWithKeys, CalendarPickerHeightModifier.identity, CalendarPickerHeightModifier, value);
379    return this;
380  }
381  borderRadius(value: Length | BorderRadiuses): this {
382    modifierWithKey(this._modifiersWithKeys, CalendarPickerBorderRadiusModifier.identity, CalendarPickerBorderRadiusModifier, value);
383    return this;
384  }
385  borderColor(value: ResourceColor | EdgeColors): this {
386    modifierWithKey(this._modifiersWithKeys, CalendarPickerBorderColorModifier.identity, CalendarPickerBorderColorModifier, value);
387    return this;
388  }
389  markToday(value: boolean): this {
390    modifierWithKey(this._modifiersWithKeys, CalendarPickerMarkTodayModifier.identity, CalendarPickerMarkTodayModifier, value);
391    return this;
392  }
393}
394// @ts-ignore
395globalThis.CalendarPicker.attributeModifier = function (modifier: ArkComponent): void {
396  attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => {
397    return new ArkCalendarPickerComponent(nativePtr);
398  }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => {
399    return new modifierJS.CalendarPickerModifier(nativePtr, classType);
400  });
401};
402