/* * Copyright (c) 2021-2022 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_tabs.h" #include "base/log/ace_scoring_log.h" #include "bridge/declarative_frontend/jsview/js_tabs_controller.h" #include "bridge/declarative_frontend/jsview/models/tabs_model_impl.h" #include "core/components_ng/pattern/tabs/tabs_model_ng.h" namespace OHOS::Ace { std::unique_ptr TabsModel::instance_ = nullptr; TabsModel* TabsModel::GetInstance() { if (!instance_) { #ifdef NG_BUILD instance_.reset(new NG::TabsModelNG()); #else if (Container::IsCurrentUseNewPipeline()) { instance_.reset(new NG::TabsModelNG()); } else { instance_.reset(new Framework::TabsModelImpl()); } #endif } return instance_.get(); } } // namespace OHOS::Ace namespace OHOS::Ace::Framework { namespace { const std::vector BAR_POSITIONS = { BarPosition::START, BarPosition::END }; JSRef TabContentChangeEventToJSValue(const TabContentChangeEvent& eventInfo) { return JSRef::Make(ToJSValue(eventInfo.GetIndex())); } } // namespace void JSTabs::SetOnChange(const JSCallbackInfo& info) { if (!info[0]->IsFunction()) { return; } auto changeHandler = AceType::MakeRefPtr>( JSRef::Cast(info[0]), TabContentChangeEventToJSValue); auto onChange = [executionContext = info.GetExecutionContext(), func = std::move(changeHandler)]( const BaseEventInfo* info) { JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(executionContext); const auto* tabsInfo = TypeInfoHelper::DynamicCast(info); if (!tabsInfo) { LOGE("HandleChangeEvent tabsInfo == nullptr"); return; } ACE_SCORING_EVENT("Tabs.onChange"); func->Execute(*tabsInfo); }; TabsModel::GetInstance()->SetOnChange(std::move(onChange)); } void JSTabs::Create(const JSCallbackInfo& info) { BarPosition barPosition = BarPosition::START; RefPtr tabController; RefPtr swiperController; int32_t index = 0; if (info[0]->IsObject()) { JSRef obj = JSRef::Cast(info[0]); JSRef val = obj->GetProperty("barPosition"); if (val->IsNumber()) { auto barPositionVal = val->ToNumber(); if (barPositionVal >= 0 && barPositionVal < static_cast(BAR_POSITIONS.size())) { barPosition = BAR_POSITIONS[barPositionVal]; } } JSRef controller = obj->GetProperty("controller"); if (controller->IsObject()) { auto* jsTabsController = JSRef::Cast(controller)->Unwrap(); if (jsTabsController) { tabController = jsTabsController->GetController(); swiperController = jsTabsController->GetSwiperController(); } } JSRef indexVal = obj->GetProperty("index"); if (indexVal->IsNumber()) { index = indexVal->ToNumber(); if (!tabController) { tabController = JSTabsController::CreateController(); } tabController->SetInitialIndex(index); } } TabsModel::GetInstance()->Create(barPosition, index, tabController, swiperController); } void JSTabs::Pop() { TabsModel::GetInstance()->Pop(); } void JSTabs::SetBarPosition(const JSCallbackInfo& info) { BarPosition barVal = BarPosition::START; if (info.Length() > 0 && info[0]->IsNumber()) { auto barPositionVal = info[0]->ToNumber(); if (barPositionVal >= 0 && barPositionVal < static_cast(BAR_POSITIONS.size())) { barVal = BAR_POSITIONS[barPositionVal]; } } TabsModel::GetInstance()->SetTabBarPosition(barVal); } void JSTabs::SetVertical(const std::string& value) { TabsModel::GetInstance()->SetIsVertical(StringToBool(value)); } void JSTabs::SetScrollable(const std::string& value) { if (value == "undefined") { TabsModel::GetInstance()->SetScrollable(true); return; } TabsModel::GetInstance()->SetScrollable(StringToBool(value)); } void JSTabs::SetBarMode(const std::string& value) { if (value == "undefined") { TabsModel::GetInstance()->SetTabBarMode(TabBarMode::FIXED); return; } TabsModel::GetInstance()->SetTabBarMode(ConvertStrToTabBarMode(value)); } void JSTabs::SetBarWidth(const JSCallbackInfo& info) { if (info.Length() < 1) { LOGE("The arg is wrong, it is supposed to have atleast 1 arguments"); return; } Dimension width = Dimension(-1.0, DimensionUnit::VP); if (!ParseJsDimensionVp(info[0], width)) { LOGE("The arg is wrong, fail to parse dimension"); } TabsModel::GetInstance()->SetTabBarWidth(width); } void JSTabs::SetBarHeight(const JSCallbackInfo& info) { if (info.Length() < 1) { LOGE("The arg is wrong, it is supposed to have atleast 1 arguments"); return; } Dimension height = Dimension(-1.0, DimensionUnit::VP); if (!ParseJsDimensionVp(info[0], height)) { LOGE("The arg is wrong, fail to parse dimension"); } TabsModel::GetInstance()->SetTabBarHeight(height); } void JSTabs::SetIndex(int32_t index) { TabsModel::GetInstance()->SetIndex(index); } void JSTabs::SetAnimationDuration(float value) { if (std::isnan(value)) { LOGI("The arg is nan, use default value"); auto pipelineContext = PipelineContext::GetCurrentContext(); CHECK_NULL_VOID(pipelineContext); auto tabTheme = pipelineContext->GetTheme(); CHECK_NULL_VOID(tabTheme); TabsModel::GetInstance()->SetAnimationDuration(static_cast(tabTheme->GetTabContentAnimationDuration())); return; } TabsModel::GetInstance()->SetAnimationDuration(value); } void JSTabs::JSBind(BindingTarget globalObj) { JSClass::Declare("Tabs"); JSClass::StaticMethod("create", &JSTabs::Create); JSClass::StaticMethod("pop", &JSTabs::Pop); JSClass::StaticMethod("vertical", &JSTabs::SetVertical); JSClass::StaticMethod("barPosition", &JSTabs::SetBarPosition); JSClass::StaticMethod("scrollable", &JSTabs::SetScrollable); JSClass::StaticMethod("barMode", &JSTabs::SetBarMode); JSClass::StaticMethod("barWidth", &JSTabs::SetBarWidth); JSClass::StaticMethod("barHeight", &JSTabs::SetBarHeight); JSClass::StaticMethod("index", &JSTabs::SetIndex); JSClass::StaticMethod("animationDuration", &JSTabs::SetAnimationDuration); JSClass::StaticMethod("onChange", &JSTabs::SetOnChange); JSClass::StaticMethod("onAppear", &JSInteractableView::JsOnAppear); 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::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage); JSClass::Inherit(); JSClass::Bind<>(globalObj); } } // namespace OHOS::Ace::Framework