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 std::mutex StepperItemModel::mutex_;
27
GetInstance()28 StepperItemModel* StepperItemModel::GetInstance()
29 {
30 if (!instance_) {
31 std::lock_guard<std::mutex> lock(mutex_);
32 if (!instance_) {
33 #ifdef NG_BUILD
34 instance_.reset(new NG::StepperItemModelNG());
35 #else
36 if (Container::IsCurrentUseNewPipeline()) {
37 instance_.reset(new NG::StepperItemModelNG());
38 } else {
39 instance_.reset(new Framework::StepperItemModelImpl());
40 }
41 #endif
42 }
43 }
44 return instance_.get();
45 }
46
47 } // namespace OHOS::Ace
48
49 namespace OHOS::Ace::Framework {
50
51 namespace {
52 constexpr int32_t PLATFORM_VERSION_TEN = 10;
53 } // namespace
54
Create(const JSCallbackInfo & info)55 void JSStepperItem::Create(const JSCallbackInfo& info)
56 {
57 StepperItemModel::GetInstance()->Create();
58 }
59
JSBind(BindingTarget globalObj)60 void JSStepperItem::JSBind(BindingTarget globalObj)
61 {
62 JSClass<JSStepperItem>::Declare("StepperItem");
63 MethodOptions opt = MethodOptions::NONE;
64 JSClass<JSStepperItem>::StaticMethod("create", &JSStepperItem::Create, opt);
65
66 JSClass<JSStepperItem>::StaticMethod("prevLabel", &JSStepperItem::SetPrevLabel);
67 JSClass<JSStepperItem>::StaticMethod("nextLabel", &JSStepperItem::SetNextLabel);
68 JSClass<JSStepperItem>::StaticMethod("status", &JSStepperItem::SetStatus);
69 JSClass<JSStepperItem>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
70 JSClass<JSStepperItem>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
71 JSClass<JSStepperItem>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
72 JSClass<JSStepperItem>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
73 JSClass<JSStepperItem>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
74 JSClass<JSStepperItem>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
75 JSClass<JSStepperItem>::InheritAndBind<JSContainerBase>(globalObj);
76 }
77
SetPrevLabel(const JSCallbackInfo & info)78 void JSStepperItem::SetPrevLabel(const JSCallbackInfo& info)
79 {
80 if (info.Length() < 1) {
81 LOGE("The arg is wrong, it is supposed to have at least 1 arguments");
82 return;
83 }
84
85 auto pipeline = PipelineBase::GetCurrentContext();
86 CHECK_NULL_VOID(pipeline);
87 if (pipeline->GetMinPlatformVersion() >= PLATFORM_VERSION_TEN && (info[0]->IsUndefined() || info[0]->IsNull())) {
88 StepperItemModel::GetInstance()->ResetPrevLabel();
89 return;
90 }
91
92 if (!info[0]->IsString()) {
93 LOGE("Arg is not String.");
94 }
95
96 StepperItemModel::GetInstance()->SetPrevLabel(info[0]->ToString());
97 }
98
SetNextLabel(const JSCallbackInfo & info)99 void JSStepperItem::SetNextLabel(const JSCallbackInfo& info)
100 {
101 if (info.Length() < 1) {
102 LOGE("The arg is wrong, it is supposed to have at least 1 arguments");
103 return;
104 }
105
106 auto pipeline = PipelineBase::GetCurrentContext();
107 CHECK_NULL_VOID(pipeline);
108 if (pipeline->GetMinPlatformVersion() >= PLATFORM_VERSION_TEN && (info[0]->IsUndefined() || info[0]->IsNull())) {
109 StepperItemModel::GetInstance()->ResetNextLabel();
110 return;
111 }
112
113 if (!info[0]->IsString()) {
114 LOGE("Arg is not String.");
115 }
116
117 StepperItemModel::GetInstance()->SetNextLabel(info[0]->ToString());
118 }
119
SetStatus(const JSCallbackInfo & info)120 void JSStepperItem::SetStatus(const JSCallbackInfo& info)
121 {
122 const std::array<std::string, 4> statusArray = { "normal", "disabled", "waiting", "skip" };
123 std::string status = statusArray[0];
124 do {
125 if (info.Length() < 1) {
126 LOGE("The arg is wrong, it is supposed to have at least 1 arguments");
127 break;
128 }
129 if (!info[0]->IsNumber()) {
130 LOGE("Arg is not Number.");
131 break;
132 }
133 auto index = info[0]->ToNumber<uint32_t>();
134 if (index < 0 || index >= statusArray.size()) {
135 LOGE("The value of the index is not in the normal range.");
136 break;
137 }
138 status = statusArray.at(index);
139 } while (false);
140 StepperItemModel::GetInstance()->SetStatus(status);
141 }
142
143 } // namespace OHOS::Ace::Framework