1 /*
2 * Copyright (c) 2021-2022 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_stepper_item.h"
17
18 #include <array>
19
20 #include "bridge/declarative_frontend/jsview/models/stepper_item_model_impl.h"
21 #include "core/components_ng/pattern/stepper/stepper_item_model_ng.h"
22
23 namespace OHOS::Ace {
24
25 std::unique_ptr<StepperItemModel> StepperItemModel::instance_ = nullptr;
26
GetInstance()27 StepperItemModel* StepperItemModel::GetInstance()
28 {
29 if (!instance_) {
30 #ifdef NG_BUILD
31 instance_.reset(new NG::ImageModelNG());
32 #else
33 if (Container::IsCurrentUseNewPipeline()) {
34 instance_.reset(new NG::StepperItemModelNG());
35 } else {
36 instance_.reset(new Framework::StepperItemModelImpl());
37 }
38 #endif
39 }
40 return instance_.get();
41 }
42
43 } // namespace OHOS::Ace
44
45 namespace OHOS::Ace::Framework {
46
Create(const JSCallbackInfo & info)47 void JSStepperItem::Create(const JSCallbackInfo& info)
48 {
49 StepperItemModel::GetInstance()->Create();
50 }
51
JSBind(BindingTarget globalObj)52 void JSStepperItem::JSBind(BindingTarget globalObj)
53 {
54 JSClass<JSStepperItem>::Declare("StepperItem");
55 MethodOptions opt = MethodOptions::NONE;
56 JSClass<JSStepperItem>::StaticMethod("create", &JSStepperItem::Create, opt);
57
58 JSClass<JSStepperItem>::StaticMethod("prevLabel", &JSStepperItem::SetPrevLabel);
59 JSClass<JSStepperItem>::StaticMethod("nextLabel", &JSStepperItem::SetNextLabel);
60 JSClass<JSStepperItem>::StaticMethod("status", &JSStepperItem::SetStatus);
61 JSClass<JSStepperItem>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
62 JSClass<JSStepperItem>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
63 JSClass<JSStepperItem>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
64 JSClass<JSStepperItem>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
65 JSClass<JSStepperItem>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
66 JSClass<JSStepperItem>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
67 JSClass<JSStepperItem>::Inherit<JSContainerBase>();
68 JSClass<JSStepperItem>::Inherit<JSViewAbstract>();
69 JSClass<JSStepperItem>::Bind<>(globalObj);
70 }
71
SetPrevLabel(const JSCallbackInfo & info)72 void JSStepperItem::SetPrevLabel(const JSCallbackInfo& info)
73 {
74 if (info.Length() < 1) {
75 LOGE("The arg is wrong, it is supposed to have at least 1 arguments");
76 return;
77 }
78 if (!info[0]->IsString()) {
79 LOGE("Arg is not String.");
80 }
81
82 StepperItemModel::GetInstance()->SetPrevLabel(info[0]->ToString());
83 }
84
SetNextLabel(const JSCallbackInfo & info)85 void JSStepperItem::SetNextLabel(const JSCallbackInfo& info)
86 {
87 if (info.Length() < 1) {
88 LOGE("The arg is wrong, it is supposed to have at least 1 arguments");
89 return;
90 }
91 if (!info[0]->IsString()) {
92 LOGE("Arg is not String.");
93 }
94
95 StepperItemModel::GetInstance()->SetNextLabel(info[0]->ToString());
96 }
97
SetStatus(const JSCallbackInfo & info)98 void JSStepperItem::SetStatus(const JSCallbackInfo& info)
99 {
100 const std::array<std::string, 4> statusArray = { "normal", "disabled", "waiting", "skip" };
101 std::string status = statusArray[0];
102 do {
103 if (info.Length() < 1) {
104 LOGE("The arg is wrong, it is supposed to have at least 1 arguments");
105 break;
106 }
107 if (!info[0]->IsNumber()) {
108 LOGE("Arg is not Number.");
109 break;
110 }
111 auto index = info[0]->ToNumber<uint32_t>();
112 if (index < 0 || index >= statusArray.size()) {
113 LOGE("The value of the index is not in the normal range.");
114 break;
115 }
116 status = statusArray.at(index);
117 } while (false);
118 StepperItemModel::GetInstance()->SetStatus(status);
119 }
120
121 } // namespace OHOS::Ace::Framework