/* * Copyright (c) 2021 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_stepper.h" #include "bridge/declarative_frontend/jsview/models/stepper_model_impl.h" #include "core/components_ng/pattern/stepper/stepper_model_ng.h" #include "frameworks/bridge/declarative_frontend/jsview/js_view_common_def.h" namespace OHOS::Ace { std::unique_ptr StepperModel::instance_ = nullptr; StepperModel* StepperModel::GetInstance() { if (!instance_) { #ifdef NG_BUILD instance_.reset(new NG::StepperModelNG()); #else if (Container::IsCurrentUseNewPipeline()) { instance_.reset(new NG::StepperModelNG()); } else { instance_.reset(new Framework::StepperModelImpl()); } #endif } return instance_.get(); } } // namespace OHOS::Ace namespace OHOS::Ace::Framework { void JSStepper::Create(const JSCallbackInfo& info) { uint32_t index = 0; if (info.Length() < 1 || !info[0]->IsObject()) { index = 0; } else { JSRef obj = JSRef::Cast(info[0]); JSRef stepperValue = obj->GetProperty("index"); if (stepperValue->IsNumber()) { index = stepperValue->ToNumber(); } } StepperModel::GetInstance()->Create(index); } void JSStepper::JSBind(BindingTarget globalObj) { JSClass::Declare("Stepper"); MethodOptions opt = MethodOptions::NONE; JSClass::StaticMethod("create", &JSStepper::Create, opt); JSClass::StaticMethod("onFinish", &JSStepper::OnFinish); JSClass::StaticMethod("onSkip", &JSStepper::OnSkip); JSClass::StaticMethod("onChange", &JSStepper::OnChange); JSClass::StaticMethod("onNext", &JSStepper::OnNext); JSClass::StaticMethod("onPrevious", &JSStepper::OnPrevious); JSClass::Inherit(); JSClass::Inherit(); JSClass::Bind<>(globalObj); } void JSStepper::OnFinish(const JSCallbackInfo& info) { if (info.Length() < 1) { LOGW("Must contain at least 1 argument"); return; } if (!info[0]->IsFunction()) { LOGW("Argument is not a function object"); return; } StepperModel::GetInstance()->SetOnFinish( JsEventCallback(info.GetExecutionContext(), JSRef::Cast(info[0]))); info.ReturnSelf(); } void JSStepper::OnSkip(const JSCallbackInfo& info) { if (info.Length() < 1) { LOGW("Must contain at least 1 argument"); return; } if (!info[0]->IsFunction()) { LOGW("Argument is not a function object"); return; } StepperModel::GetInstance()->SetOnSkip( JsEventCallback(info.GetExecutionContext(), JSRef::Cast(info[0]))); info.ReturnSelf(); } void JSStepper::OnChange(const JSCallbackInfo& info) { if (info.Length() < 1) { LOGW("Must contain at least 1 argument"); return; } if (!info[0]->IsFunction()) { LOGW("Argument is not a function object"); return; } StepperModel::GetInstance()->SetOnChange( JsEventCallback(info.GetExecutionContext(), JSRef::Cast(info[0]))); info.ReturnSelf(); } void JSStepper::OnNext(const JSCallbackInfo& info) { if (info.Length() < 1) { LOGW("Must contain at least 1 argument"); return; } if (!info[0]->IsFunction()) { LOGW("Argument is not a function object"); return; } StepperModel::GetInstance()->SetOnNext( JsEventCallback(info.GetExecutionContext(), JSRef::Cast(info[0]))); info.ReturnSelf(); } void JSStepper::OnPrevious(const JSCallbackInfo& info) { if (info.Length() < 1) { LOGW("Must contain at least 1 argument"); return; } if (!info[0]->IsFunction()) { LOGW("Argument is not a function object"); return; } StepperModel::GetInstance()->SetOnPrevious( JsEventCallback(info.GetExecutionContext(), JSRef::Cast(info[0]))); info.ReturnSelf(); } } // namespace OHOS::Ace::Framework