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} 65 66class RatingContentModifier extends ModifierWithKey<ContentModifier<RatingConfiguration>> { 67 constructor(value: ContentModifier<RatingConfiguration>) { 68 super(value); 69 } 70 static identity: Symbol = Symbol('ratingContentModifier'); 71 applyPeer(node: KNode, reset: boolean, component: ArkComponent): void { 72 let ratingComponent = component as ArkRatingComponent; 73 ratingComponent.setContentModifier(this.value); 74 } 75} 76 77class ArkRatingComponent extends ArkComponent implements RatingAttribute { 78 builder: WrappedBuilder<Object[]> | null = null; 79 ratingNode: BuilderNode<[RatingConfiguration]> | null = null; 80 modifier: ContentModifier<RatingConfiguration>; 81 needRebuild: boolean = false; 82 constructor(nativePtr: KNode, classType?: ModifierType) { 83 super(nativePtr, classType); 84 } 85 stars(value: number): this { 86 modifierWithKey(this._modifiersWithKeys, RatingStarsModifier.identity, RatingStarsModifier, value); 87 return this; 88 } 89 stepSize(value: number): this { 90 modifierWithKey(this._modifiersWithKeys, RatingStepSizeModifier.identity, RatingStepSizeModifier, value); 91 return this; 92 } 93 starStyle(value: { backgroundUri: string; foregroundUri: string; secondaryUri?: string | undefined; }): this { 94 let starStyle = new ArkStarStyle(); 95 if (!isUndefined(value)) { 96 starStyle.backgroundUri = value.backgroundUri; 97 starStyle.foregroundUri = value.foregroundUri; 98 starStyle.secondaryUri = value.secondaryUri; 99 100 modifierWithKey(this._modifiersWithKeys, RatingStarStyleModifier.identity, RatingStarStyleModifier, value); 101 } else { 102 modifierWithKey(this._modifiersWithKeys, RatingStarStyleModifier.identity, RatingStarStyleModifier, undefined); 103 } 104 return this; 105 } 106 onChange(callback: (value: number) => void): this { 107 throw new Error('Method not implemented.'); 108 } 109 contentModifier(value: ContentModifier<RatingConfiguration>): this { 110 modifierWithKey(this._modifiersWithKeys, RatingContentModifier.identity, RatingContentModifier, value); 111 return this; 112 } 113 setContentModifier(modifier: ContentModifier<RatingConfiguration>): this { 114 if (modifier === undefined || modifier === null) { 115 getUINativeModule().rating.setContentModifierBuilder(this.nativePtr, false); 116 return; 117 } 118 this.needRebuild = false; 119 if (this.builder !== modifier.applyContent()) { 120 this.needRebuild = true; 121 } 122 this.builder = modifier.applyContent(); 123 this.modifier = modifier; 124 getUINativeModule().rating.setContentModifierBuilder(this.nativePtr, this); 125 } 126 makeContentModifierNode(context: UIContext, ratingConfiguration: RatingConfiguration): FrameNode | null { 127 ratingConfiguration.contentModifier = this.modifier; 128 if (isUndefined(this.ratingNode || this.needRebuild)) { 129 const xNode = globalThis.requireNapi('arkui.node'); 130 this.ratingNode = new xNode.BuilderNode(context); 131 this.ratingNode.build(this.builder, ratingConfiguration); 132 this.needRebuild = false; 133 } else { 134 this.ratingNode.update(ratingConfiguration); 135 } 136 return this.ratingNode.getFrameNode(); 137 } 138} 139// @ts-ignore 140globalThis.Rating.attributeModifier = function (modifier: ArkComponent): void { 141 attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => { 142 return new ArkRatingComponent(nativePtr); 143 }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => { 144 return new modifierJS.RatingModifier(nativePtr, classType); 145 }); 146}; 147 148// @ts-ignore 149globalThis.Rating.contentModifier = function (modifier): void { 150 const elmtId = ViewStackProcessor.GetElmtIdToAccountFor(); 151 let nativeNode = getUINativeModule().getFrameNodeById(elmtId); 152 let component = this.createOrGetNode(elmtId, () => { 153 return new ArkRatingComponent(nativeNode); 154 }); 155 component.setContentModifier(modifier); 156}; 157