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