• 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): void {
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): void {
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): void {
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}
65class RatingOnChangeModifier extends ModifierWithKey<(value: number) => void> {
66  constructor(value: (value: number) => void) {
67    super(value);
68  }
69  static identity: Symbol = Symbol('ratingOnChange');
70  applyPeer(node: KNode, reset: boolean): void {
71    if (reset) {
72      getUINativeModule().rating.resetOnChange(node);
73    } else {
74      getUINativeModule().rating.setOnChange(node, this.value);
75    }
76  }
77}
78class RatingContentModifier extends ModifierWithKey<ContentModifier<RatingConfiguration>> {
79  constructor(value: ContentModifier<RatingConfiguration>) {
80    super(value);
81  }
82  static identity: Symbol = Symbol('ratingContentModifier');
83  applyPeer(node: KNode, reset: boolean, component: ArkComponent): void {
84    let ratingComponent = component as ArkRatingComponent;
85    ratingComponent.setContentModifier(this.value);
86  }
87}
88
89class ArkRatingComponent extends ArkComponent implements RatingAttribute {
90  builder: WrappedBuilder<Object[]> | null = null;
91  ratingNode: BuilderNode<[RatingConfiguration]> | null = null;
92  modifier: ContentModifier<RatingConfiguration>;
93  needRebuild: boolean = false;
94  constructor(nativePtr: KNode, classType?: ModifierType) {
95    super(nativePtr, classType);
96  }
97  allowChildCount(): number {
98    return 0;
99  }
100  initialize(value: Object[]): this {
101    if (!value.length) {
102      return this;
103    }
104    if (!isUndefined(value[0]) && !isNull(value[0]) && isObject(value[0])) {
105      modifierWithKey(this._modifiersWithKeys, RatingOptionsModifier.identity, RatingOptionsModifier, value[0]);
106    } else {
107      modifierWithKey(this._modifiersWithKeys, RatingOptionsModifier.identity, RatingOptionsModifier, undefined);
108    }
109    return this;
110  }
111  stars(value: number): this {
112    modifierWithKey(this._modifiersWithKeys, RatingStarsModifier.identity, RatingStarsModifier, value);
113    return this;
114  }
115  stepSize(value: number): this {
116    modifierWithKey(this._modifiersWithKeys, RatingStepSizeModifier.identity, RatingStepSizeModifier, value);
117    return this;
118  }
119  starStyle(value: { backgroundUri: string; foregroundUri: string; secondaryUri?: string | undefined; }): this {
120    let starStyle = new ArkStarStyle();
121    if (!isUndefined(value)) {
122      starStyle.backgroundUri = value.backgroundUri;
123      starStyle.foregroundUri = value.foregroundUri;
124      starStyle.secondaryUri = value.secondaryUri;
125
126      modifierWithKey(this._modifiersWithKeys, RatingStarStyleModifier.identity, RatingStarStyleModifier, value);
127    } else {
128      modifierWithKey(this._modifiersWithKeys, RatingStarStyleModifier.identity, RatingStarStyleModifier, undefined);
129    }
130    return this;
131  }
132  onChange(callback: (value: number) => void): this {
133    modifierWithKey(this._modifiersWithKeys, RatingOnChangeModifier.identity, RatingOnChangeModifier, callback);
134    return this;
135  }
136  contentModifier(value: ContentModifier<RatingConfiguration>): this {
137    modifierWithKey(this._modifiersWithKeys, RatingContentModifier.identity, RatingContentModifier, value);
138    return this;
139  }
140  setContentModifier(modifier: ContentModifier<RatingConfiguration>): this {
141    if (modifier === undefined || modifier === null) {
142      getUINativeModule().rating.setContentModifierBuilder(this.nativePtr, false);
143      return;
144    }
145    this.needRebuild = false;
146    if (this.builder !== modifier.applyContent()) {
147      this.needRebuild = true;
148    }
149    this.builder = modifier.applyContent();
150    this.modifier = modifier;
151    getUINativeModule().rating.setContentModifierBuilder(this.nativePtr, this);
152  }
153  makeContentModifierNode(context: UIContext, ratingConfiguration: RatingConfiguration): FrameNode | null {
154    ratingConfiguration.contentModifier = this.modifier;
155    if (isUndefined(this.ratingNode || this.needRebuild)) {
156      const xNode = globalThis.requireNapi('arkui.node');
157      this.ratingNode = new xNode.BuilderNode(context);
158      this.ratingNode.build(this.builder, ratingConfiguration);
159      this.needRebuild = false;
160    } else {
161      this.ratingNode.update(ratingConfiguration);
162    }
163    return this.ratingNode.getFrameNode();
164  }
165}
166
167class RatingOptionsModifier extends ModifierWithKey<object> {
168  constructor(value: object) {
169    super(value);
170  }
171  static identity: Symbol = Symbol('ratingOptions');
172  applyPeer(node: KNode, reset: boolean): void {
173    if (reset) {
174      getUINativeModule().radio.setRatingOptions(node, undefined, undefined);
175    } else {
176      getUINativeModule().radio.setRatingOptions(node, this.value?.rating, this.value?.indicator);
177    }
178  }
179
180  checkObjectDiff(): boolean {
181    return !isBaseOrResourceEqual(this.stageValue?.rating, this.value?.rating) ||
182      !isBaseOrResourceEqual(this.stageValue?.indicator, this.value?.indicator);
183  }
184}
185
186// @ts-ignore
187globalThis.Rating.attributeModifier = function (modifier: ArkComponent): void {
188  attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => {
189    return new ArkRatingComponent(nativePtr);
190  }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => {
191    return new modifierJS.RatingModifier(nativePtr, classType);
192  });
193};
194
195// @ts-ignore
196globalThis.Rating.contentModifier = function (modifier): void {
197  const elmtId = ViewStackProcessor.GetElmtIdToAccountFor();
198  let nativeNode = getUINativeModule().getFrameNodeById(elmtId);
199  let component = this.createOrGetNode(elmtId, () => {
200    return new ArkRatingComponent(nativeNode);
201  });
202  component.setContentModifier(modifier);
203};
204