1 /*
2 * Copyright (c) 2021-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 #include "frameworks/bridge/declarative_frontend/jsview/js_rating.h"
17
18 #include "base/log/ace_scoring_log.h"
19 #include "bridge/declarative_frontend/jsview/models/rating_model_impl.h"
20 #include "core/components_ng/pattern/rating/rating_model_ng.h"
21
22 namespace OHOS::Ace {
23
24 std::unique_ptr<RatingModel> RatingModel::instance_ = nullptr;
25 std::mutex RatingModel::mutex_;
26
GetInstance()27 RatingModel* RatingModel::GetInstance()
28 {
29 if (!instance_) {
30 std::lock_guard<std::mutex> lock(mutex_);
31 if (!instance_) {
32 #ifdef NG_BUILD
33 instance_.reset(new NG::RatingModelNG());
34 #else
35 if (Container::IsCurrentUseNewPipeline()) {
36 instance_.reset(new NG::RatingModelNG());
37 } else {
38 instance_.reset(new Framework::RatingModelImpl());
39 }
40 #endif
41 }
42 }
43 return instance_.get();
44 }
45
46 } // namespace OHOS::Ace
47
48 namespace OHOS::Ace::Framework {
49 namespace {
50 constexpr double RATING_SCORE_DEFAULT = 0;
51 constexpr int32_t STARS_DEFAULT = 5;
52 constexpr double STEPS_DEFAULT = 0.5;
53 } // namespace
54
ParseRatingObject(const JSCallbackInfo & info,const JSRef<JSVal> & changeEventVal)55 void ParseRatingObject(const JSCallbackInfo& info, const JSRef<JSVal>& changeEventVal)
56 {
57 CHECK_NULL_VOID(changeEventVal->IsFunction());
58
59 auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(changeEventVal));
60 auto onChangeEvent = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc)](const std::string& value) {
61 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
62 ACE_SCORING_EVENT("Rating.onChangeEvent");
63 auto newJSVal = JSRef<JSVal>::Make(ToJSValue(stod(value)));
64 func->ExecuteJS(1, &newJSVal);
65 };
66 RatingModel::GetInstance()->SetOnChangeEvent(std::move(onChangeEvent));
67 }
68
Create(const JSCallbackInfo & info)69 void JSRating::Create(const JSCallbackInfo& info)
70 {
71 double rating = RATING_SCORE_DEFAULT;
72 bool indicator = false;
73 JSRef<JSVal> changeEventVal;
74 if (info.Length() >= 1 && info[0]->IsObject()) {
75 auto paramObject = JSRef<JSObject>::Cast(info[0]);
76 auto getRating = paramObject->GetProperty("rating");
77 auto getIndicator = paramObject->GetProperty("indicator");
78 if (getRating->IsNumber()) {
79 rating = getRating->ToNumber<double>();
80 } else if (getRating->IsObject()) {
81 JSRef<JSObject> ratingObj = JSRef<JSObject>::Cast(getRating);
82 changeEventVal = ratingObj->GetProperty("changeEvent");
83 auto ratingValue = ratingObj->GetProperty("value");
84 if (ratingValue->IsNumber()) {
85 rating = ratingValue->ToNumber<double>();
86 }
87 } else {
88 LOGE("create rating fail because the rating is not value");
89 }
90 if (rating < 0) {
91 LOGW("rating number is invalid, and it will use 0 by default.");
92 rating = RATING_SCORE_DEFAULT;
93 }
94 if (getIndicator->IsBoolean()) {
95 indicator = getIndicator->ToBoolean();
96 } else {
97 LOGE("create rating fail because the indicator is not value");
98 }
99 }
100 RatingModel::GetInstance()->Create(rating, indicator);
101 if (!changeEventVal->IsUndefined() && changeEventVal->IsFunction()) {
102 ParseRatingObject(info, changeEventVal);
103 }
104 }
105
SetStars(const JSCallbackInfo & info)106 void JSRating::SetStars(const JSCallbackInfo& info)
107 {
108 if (!info[0]->IsNumber()) {
109 return;
110 }
111
112 auto stars = info[0]->ToNumber<int32_t>();
113 if (stars <= 0) {
114 stars = STARS_DEFAULT;
115 }
116 RatingModel::GetInstance()->SetStars(stars);
117 }
118
SetStepSize(const JSCallbackInfo & info)119 void JSRating::SetStepSize(const JSCallbackInfo& info)
120 {
121 if (!info[0]->IsNumber()) {
122 return;
123 }
124 static const double stepSizeMin = 0.1;
125 auto steps = info[0]->ToNumber<double>();
126 if (LessNotEqual(steps, stepSizeMin)) {
127 steps = STEPS_DEFAULT;
128 }
129 RatingModel::GetInstance()->SetStepSize(steps);
130 }
131
SetStarStyle(const JSCallbackInfo & info)132 void JSRating::SetStarStyle(const JSCallbackInfo& info)
133 {
134 if (!info[0]->IsObject()) {
135 return;
136 }
137 auto paramObject = JSRef<JSObject>::Cast(info[0]);
138 auto getBackgroundUri = paramObject->GetProperty("backgroundUri");
139 auto getForegroundUri = paramObject->GetProperty("foregroundUri");
140 auto getSecondaryUri = paramObject->GetProperty("secondaryUri");
141 std::string backgroundUri;
142 if (getBackgroundUri->IsString()) {
143 backgroundUri = getBackgroundUri->ToString();
144 RatingModel::GetInstance()->SetBackgroundSrc(backgroundUri, false);
145 } else {
146 RatingModel::GetInstance()->SetBackgroundSrc("", true);
147 }
148
149 if (getForegroundUri->IsString()) {
150 RatingModel::GetInstance()->SetForegroundSrc(getForegroundUri->ToString(), false);
151 } else {
152 RatingModel::GetInstance()->SetForegroundSrc("", true);
153 }
154
155 if (getSecondaryUri->IsString()) {
156 RatingModel::GetInstance()->SetSecondarySrc(getSecondaryUri->ToString(), false);
157 } else if (getBackgroundUri->IsString()) {
158 RatingModel::GetInstance()->SetSecondarySrc(backgroundUri, false);
159 } else {
160 RatingModel::GetInstance()->SetSecondarySrc("", true);
161 }
162 }
163
SetOnChange(const JSCallbackInfo & info)164 void JSRating::SetOnChange(const JSCallbackInfo& info)
165 {
166 if (!info[0]->IsFunction()) {
167 return;
168 }
169
170 auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
171 auto onChange = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc)](const std::string& value) {
172 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
173 ACE_SCORING_EVENT("Rating.onChange");
174 auto newJSVal = JSRef<JSVal>::Make(ToJSValue(stod(value)));
175 func->ExecuteJS(1, &newJSVal);
176 };
177
178 RatingModel::GetInstance()->SetOnChange(onChange);
179 }
180
JSBind(BindingTarget globalObj)181 void JSRating::JSBind(BindingTarget globalObj)
182 {
183 JSClass<JSRating>::Declare("Rating");
184 MethodOptions opt = MethodOptions::NONE;
185 JSClass<JSRating>::StaticMethod("create", &JSRating::Create, opt);
186 JSClass<JSRating>::StaticMethod("stars", &JSRating::SetStars, opt);
187 JSClass<JSRating>::StaticMethod("stepSize", &JSRating::SetStepSize, opt);
188 JSClass<JSRating>::StaticMethod("starStyle", &JSRating::SetStarStyle, opt);
189 JSClass<JSRating>::StaticMethod("onChange", &JSRating::SetOnChange);
190 JSClass<JSRating>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
191 JSClass<JSRating>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
192 JSClass<JSRating>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
193 JSClass<JSRating>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
194 JSClass<JSRating>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
195 JSClass<JSRating>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
196 JSClass<JSRating>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
197 JSClass<JSRating>::InheritAndBind<JSViewAbstract>(globalObj);
198 }
199 } // namespace OHOS::Ace::Framework
200