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 {} // namespace
52 
Create(const JSCallbackInfo & info)53 void JSStepperItem::Create(const JSCallbackInfo& info)
54 {
55     StepperItemModel::GetInstance()->Create();
56 }
57 
JSBind(BindingTarget globalObj)58 void JSStepperItem::JSBind(BindingTarget globalObj)
59 {
60     JSClass<JSStepperItem>::Declare("StepperItem");
61     MethodOptions opt = MethodOptions::NONE;
62     JSClass<JSStepperItem>::StaticMethod("create", &JSStepperItem::Create, opt);
63 
64     JSClass<JSStepperItem>::StaticMethod("prevLabel", &JSStepperItem::SetPrevLabel);
65     JSClass<JSStepperItem>::StaticMethod("nextLabel", &JSStepperItem::SetNextLabel);
66     JSClass<JSStepperItem>::StaticMethod("status", &JSStepperItem::SetStatus);
67     JSClass<JSStepperItem>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
68     JSClass<JSStepperItem>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
69     JSClass<JSStepperItem>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
70     JSClass<JSStepperItem>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
71     JSClass<JSStepperItem>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
72     JSClass<JSStepperItem>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
73     JSClass<JSStepperItem>::StaticMethod("onAttach", &JSInteractableView::JsOnAttach);
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         return;
82     }
83 
84     if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TEN) &&
85         (info[0]->IsUndefined() || info[0]->IsNull())) {
86         StepperItemModel::GetInstance()->ResetPrevLabel();
87         return;
88     }
89 
90     if (!info[0]->IsString()) {
91         return;
92     }
93 
94     StepperItemModel::GetInstance()->SetPrevLabel(info[0]->ToString());
95 }
96 
SetNextLabel(const JSCallbackInfo & info)97 void JSStepperItem::SetNextLabel(const JSCallbackInfo& info)
98 {
99     if (info.Length() < 1) {
100         return;
101     }
102 
103     if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TEN) &&
104         (info[0]->IsUndefined() || info[0]->IsNull())) {
105         StepperItemModel::GetInstance()->ResetNextLabel();
106         return;
107     }
108 
109     if (!info[0]->IsString()) {
110         return;
111     }
112 
113     StepperItemModel::GetInstance()->SetNextLabel(info[0]->ToString());
114 }
115 
SetStatus(const JSCallbackInfo & info)116 void JSStepperItem::SetStatus(const JSCallbackInfo& info)
117 {
118     const std::array<std::string, 4> statusArray = { "normal", "disabled", "waiting", "skip" };
119     std::string status = statusArray[0];
120     do {
121         if (info.Length() < 1) {
122             break;
123         }
124         if (!info[0]->IsNumber()) {
125             break;
126         }
127         auto index = info[0]->ToNumber<uint32_t>();
128         if (index < 0 || index >= statusArray.size()) {
129             break;
130         }
131         status = statusArray.at(index);
132     } while (false);
133     StepperItemModel::GetInstance()->SetStatus(status);
134 }
135 
136 } // namespace OHOS::Ace::Framework