• 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' />
17
18class RatingStarsModifier extends ModifierWithKey<number> {
19  constructor(value: number) {
20    super(value);
21  }
22  static identity: Symbol = Symbol('ratingStars');
23  applyPeer(node: KNode, reset: boolean) {
24    if (reset) {
25      getUINativeModule().rating.resetStars(node);
26    } else {
27      getUINativeModule().rating.setStars(node, this.value);
28    }
29  }
30}
31
32class RatingStepSizeModifier extends ModifierWithKey<number> {
33  constructor(value: number) {
34    super(value);
35  }
36  static identity: Symbol = Symbol('ratingStepSize');
37  applyPeer(node: KNode, reset: boolean) {
38    if (reset) {
39      getUINativeModule().rating.resetStepSize(node);
40    } else {
41      getUINativeModule().rating.setStepSize(node, this.value);
42    }
43  }
44}
45
46class RatingStarStyleModifier extends ModifierWithKey<ArkStarStyle> {
47  constructor(value: ArkStarStyle) {
48    super(value);
49  }
50  static identity: Symbol = Symbol('ratingStarStyle');
51  applyPeer(node: KNode, reset: boolean) {
52    if (reset) {
53      getUINativeModule().rating.resetStarStyle(node);
54    } else {
55      getUINativeModule().rating.setStarStyle(node,
56        this.value?.backgroundUri, this.value?.foregroundUri, this.value?.secondaryUri);
57    }
58  }
59
60  checkObjectDiff(): boolean {
61    return this.stageValue?.backgroundUri !== this.value?.backgroundUri ||
62      this.stageValue?.foregroundUri !== this.value?.foregroundUri || this.stageValue?.secondaryUri !== this.value?.secondaryUri;
63  }
64}
65
66class ArkRatingComponent extends ArkComponent implements RatingAttribute {
67  constructor(nativePtr: KNode) {
68    super(nativePtr);
69  }
70  onGestureJudgeBegin(callback: (gestureInfo: GestureInfo, event: BaseGestureEvent) => GestureJudgeResult): this {
71    throw new Error('Method not implemented.');
72  }
73  stars(value: number): this {
74    modifierWithKey(this._modifiersWithKeys, RatingStarsModifier.identity, RatingStarsModifier, value);
75    return this;
76  }
77  stepSize(value: number): this {
78    modifierWithKey(this._modifiersWithKeys, RatingStepSizeModifier.identity, RatingStepSizeModifier, value);
79    return this;
80  }
81  starStyle(value: { backgroundUri: string; foregroundUri: string; secondaryUri?: string | undefined; }): this {
82    let starStyle = new ArkStarStyle();
83    if (!isUndefined(value)) {
84      starStyle.backgroundUri = value.backgroundUri;
85      starStyle.foregroundUri = value.foregroundUri;
86      starStyle.secondaryUri = value.secondaryUri;
87
88      modifierWithKey(this._modifiersWithKeys, RatingStarStyleModifier.identity, RatingStarStyleModifier, value);
89    } else {
90      modifierWithKey(this._modifiersWithKeys, RatingStarStyleModifier.identity, RatingStarStyleModifier, undefined);
91    }
92    return this;
93  }
94  onChange(callback: (value: number) => void): this {
95    throw new Error('Method not implemented.');
96  }
97}
98// @ts-ignore
99globalThis.Rating.attributeModifier = function (modifier) {
100  const elmtId = ViewStackProcessor.GetElmtIdToAccountFor();
101  let nativeNode = getUINativeModule().getFrameNodeById(elmtId);
102  let component = this.createOrGetNode(elmtId, () => {
103    return new ArkRatingComponent(nativeNode);
104  });
105  applyUIAttributes(modifier, nativeNode, component);
106  component.applyModifierPatch();
107};
108