/* * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "frameworks/bridge/declarative_frontend/jsview/js_rating.h" #include "base/log/ace_scoring_log.h" #include "bridge/declarative_frontend/jsview/models/rating_model_impl.h" #include "core/components_ng/base/view_stack_processor.h" #include "core/components_ng/pattern/rating/rating_model_ng.h" namespace OHOS::Ace { std::unique_ptr RatingModel::instance_ = nullptr; std::mutex RatingModel::mutex_; RatingModel* RatingModel::GetInstance() { if (!instance_) { std::lock_guard lock(mutex_); if (!instance_) { #ifdef NG_BUILD instance_.reset(new NG::RatingModelNG()); #else if (Container::IsCurrentUseNewPipeline()) { instance_.reset(new NG::RatingModelNG()); } else { instance_.reset(new Framework::RatingModelImpl()); } #endif } } return instance_.get(); } } // namespace OHOS::Ace namespace OHOS::Ace::Framework { namespace { constexpr double RATING_SCORE_DEFAULT = 0; constexpr int32_t STARS_DEFAULT = 5; constexpr double STEPS_DEFAULT = 0.5; } // namespace void ParseRatingObject(const JSCallbackInfo& info, const JSRef& changeEventVal) { CHECK_NULL_VOID(changeEventVal->IsFunction()); auto jsFunc = AceType::MakeRefPtr(JSRef(), JSRef::Cast(changeEventVal)); WeakPtr targetNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode()); auto onChangeEvent = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc), node = targetNode]( const std::string& value) { JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx); ACE_SCORING_EVENT("Rating.onChangeEvent"); PipelineContext::SetCallBackNode(node); auto newJSVal = JSRef::Make(ToJSValue(stod(value))); func->ExecuteJS(1, &newJSVal); }; RatingModel::GetInstance()->SetOnChangeEvent(std::move(onChangeEvent)); } void JSRating::Create(const JSCallbackInfo& info) { double rating = RATING_SCORE_DEFAULT; bool indicator = false; JSRef changeEventVal; if (info.Length() >= 1 && info[0]->IsObject()) { auto paramObject = JSRef::Cast(info[0]); auto getRating = paramObject->GetProperty("rating"); auto getIndicator = paramObject->GetProperty("indicator"); if (getRating->IsNumber()) { rating = getRating->ToNumber(); } else if (getRating->IsObject()) { JSRef ratingObj = JSRef::Cast(getRating); changeEventVal = ratingObj->GetProperty("changeEvent"); auto ratingValue = ratingObj->GetProperty("value"); if (ratingValue->IsNumber()) { rating = ratingValue->ToNumber(); } } if (rating < 0) { rating = RATING_SCORE_DEFAULT; } if (getIndicator->IsBoolean()) { indicator = getIndicator->ToBoolean(); } } RatingModel::GetInstance()->Create(rating, indicator); if (!changeEventVal->IsUndefined() && changeEventVal->IsFunction()) { ParseRatingObject(info, changeEventVal); } } void JSRating::SetStars(const JSCallbackInfo& info) { if (!info[0]->IsNumber()) { RatingModel::GetInstance()->SetStars(STARS_DEFAULT); return; } auto stars = info[0]->ToNumber(); if (stars <= 0) { stars = STARS_DEFAULT; } RatingModel::GetInstance()->SetStars(stars); } void JSRating::SetStepSize(const JSCallbackInfo& info) { if (!info[0]->IsNumber()) { RatingModel::GetInstance()->SetStepSize(STEPS_DEFAULT); return; } static const double stepSizeMin = 0.1; auto steps = info[0]->ToNumber(); if (LessNotEqual(steps, stepSizeMin)) { steps = STEPS_DEFAULT; } RatingModel::GetInstance()->SetStepSize(steps); } void JSRating::SetStarStyle(const JSCallbackInfo& info) { if (!info[0]->IsObject()) { RatingModel::GetInstance()->SetBackgroundSrc("", true); RatingModel::GetInstance()->SetForegroundSrc("", true); RatingModel::GetInstance()->SetSecondarySrc("", true); return; } auto paramObject = JSRef::Cast(info[0]); auto getBackgroundUri = paramObject->GetProperty("backgroundUri"); auto getForegroundUri = paramObject->GetProperty("foregroundUri"); auto getSecondaryUri = paramObject->GetProperty("secondaryUri"); std::string backgroundUri; if (getBackgroundUri->IsString()) { backgroundUri = getBackgroundUri->ToString(); if (backgroundUri.empty()) { RatingModel::GetInstance()->SetBackgroundSrc("", true); } else { RatingModel::GetInstance()->SetBackgroundSrc(backgroundUri, false); } } else { RatingModel::GetInstance()->SetBackgroundSrc("", true); } if (getForegroundUri->IsString()) { std::string foregroundUri = getForegroundUri->ToString(); if (foregroundUri.empty()) { RatingModel::GetInstance()->SetForegroundSrc("", true); } else { RatingModel::GetInstance()->SetForegroundSrc(foregroundUri, false); } } else { RatingModel::GetInstance()->SetForegroundSrc("", true); } if (getSecondaryUri->IsString() && !getSecondaryUri->ToString().empty()) { RatingModel::GetInstance()->SetSecondarySrc(getSecondaryUri->ToString(), false); } else if (getBackgroundUri->IsString() && !backgroundUri.empty()) { RatingModel::GetInstance()->SetSecondarySrc(backgroundUri, false); } else { RatingModel::GetInstance()->SetSecondarySrc("", true); } } void JSRating::SetOnChange(const JSCallbackInfo& info) { if (!info[0]->IsFunction()) { return; } auto jsFunc = AceType::MakeRefPtr(JSRef(), JSRef::Cast(info[0])); WeakPtr targetNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode()); auto onChange = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc), node = targetNode]( const std::string& value) { JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx); ACE_SCORING_EVENT("Rating.onChange"); PipelineContext::SetCallBackNode(node); auto newJSVal = JSRef::Make(ToJSValue(stod(value))); func->ExecuteJS(1, &newJSVal); }; RatingModel::GetInstance()->SetOnChange(onChange); } void JSRating::JSBind(BindingTarget globalObj) { JSClass::Declare("Rating"); MethodOptions opt = MethodOptions::NONE; JSClass::StaticMethod("create", &JSRating::Create, opt); JSClass::StaticMethod("stars", &JSRating::SetStars, opt); JSClass::StaticMethod("stepSize", &JSRating::SetStepSize, opt); JSClass::StaticMethod("starStyle", &JSRating::SetStarStyle, opt); JSClass::StaticMethod("onChange", &JSRating::SetOnChange); JSClass::StaticMethod("onAttach", &JSInteractableView::JsOnAttach); JSClass::StaticMethod("onAppear", &JSInteractableView::JsOnAppear); JSClass::StaticMethod("onDetach", &JSInteractableView::JsOnDetach); JSClass::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear); JSClass::StaticMethod("onTouch", &JSInteractableView::JsOnTouch); JSClass::StaticMethod("onHover", &JSInteractableView::JsOnHover); JSClass::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey); JSClass::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete); JSClass::StaticMethod("onClick", &JSInteractableView::JsOnClick); JSClass::InheritAndBind(globalObj); } } // namespace OHOS::Ace::Framework