• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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("onDisAppear", &JSInteractableView::JsOnDisAppear);
72     JSClass<JSStepperItem>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
73     JSClass<JSStepperItem>::InheritAndBind<JSContainerBase>(globalObj);
74 }
75 
SetPrevLabel(const JSCallbackInfo & info)76 void JSStepperItem::SetPrevLabel(const JSCallbackInfo& info)
77 {
78     if (info.Length() < 1) {
79         return;
80     }
81 
82     if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TEN) &&
83         (info[0]->IsUndefined() || info[0]->IsNull())) {
84         StepperItemModel::GetInstance()->ResetPrevLabel();
85         return;
86     }
87 
88     if (!info[0]->IsString()) {
89         return;
90     }
91 
92     StepperItemModel::GetInstance()->SetPrevLabel(info[0]->ToString());
93 }
94 
SetNextLabel(const JSCallbackInfo & info)95 void JSStepperItem::SetNextLabel(const JSCallbackInfo& info)
96 {
97     if (info.Length() < 1) {
98         return;
99     }
100 
101     if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TEN) &&
102         (info[0]->IsUndefined() || info[0]->IsNull())) {
103         StepperItemModel::GetInstance()->ResetNextLabel();
104         return;
105     }
106 
107     if (!info[0]->IsString()) {
108         return;
109     }
110 
111     StepperItemModel::GetInstance()->SetNextLabel(info[0]->ToString());
112 }
113 
SetStatus(const JSCallbackInfo & info)114 void JSStepperItem::SetStatus(const JSCallbackInfo& info)
115 {
116     const std::array<std::string, 4> statusArray = { "normal", "disabled", "waiting", "skip" };
117     std::string status = statusArray[0];
118     do {
119         if (info.Length() < 1) {
120             break;
121         }
122         if (!info[0]->IsNumber()) {
123             break;
124         }
125         auto index = info[0]->ToNumber<uint32_t>();
126         if (index < 0 || index >= statusArray.size()) {
127             break;
128         }
129         status = statusArray.at(index);
130     } while (false);
131     StepperItemModel::GetInstance()->SetStatus(status);
132 }
133 
134 } // namespace OHOS::Ace::Framework