1 /*
2  * Copyright (c) 2021 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.h"
17 
18 #include "bridge/declarative_frontend/jsview/models/stepper_model_impl.h"
19 #include "core/components_ng/pattern/stepper/stepper_model_ng.h"
20 #include "frameworks/bridge/declarative_frontend/jsview/js_view_common_def.h"
21 
22 namespace OHOS::Ace {
23 
24 std::unique_ptr<StepperModel> StepperModel::instance_ = nullptr;
25 std::mutex StepperModel::mutex_;
26 
GetInstance()27 StepperModel* StepperModel::GetInstance()
28 {
29     if (!instance_) {
30         std::lock_guard<std::mutex> lock(mutex_);
31         if (!instance_) {
32 #ifdef NG_BUILD
33             instance_.reset(new NG::StepperModelNG());
34 #else
35             if (Container::IsCurrentUseNewPipeline()) {
36                 instance_.reset(new NG::StepperModelNG());
37             } else {
38                 instance_.reset(new Framework::StepperModelImpl());
39             }
40 #endif
41         }
42     }
43     return instance_.get();
44 }
45 
46 } // namespace OHOS::Ace
47 
48 namespace OHOS::Ace::Framework {
49 
ParseStepperIndexObject(const JSCallbackInfo & info,const JSRef<JSVal> & changeEventVal)50 void ParseStepperIndexObject(const JSCallbackInfo& info, const JSRef<JSVal>& changeEventVal)
51 {
52     CHECK_NULL_VOID(changeEventVal->IsFunction());
53 
54     StepperModel::GetInstance()->SetOnChangeEvent(
55         JsEventCallback<void(int32_t)>(info.GetExecutionContext(), JSRef<JSFunc>::Cast(changeEventVal)));
56 }
57 
Create(const JSCallbackInfo & info)58 void JSStepper::Create(const JSCallbackInfo& info)
59 {
60     uint32_t index = 0;
61     JSRef<JSVal> changeEventVal;
62     if (info.Length() < 1 || !info[0]->IsObject()) {
63         index = 0;
64     } else {
65         JSRef<JSObject> obj = JSRef<JSObject>::Cast(info[0]);
66         JSRef<JSVal> stepperValue = obj->GetProperty("index");
67         if (stepperValue->IsNumber()) {
68             auto indexValue = stepperValue->ToNumber<int32_t>();
69             index = indexValue <= 0 ? 0 : static_cast<uint32_t>(indexValue);
70         } else if (stepperValue->IsObject()) {
71             JSRef<JSObject> stepperObj = JSRef<JSObject>::Cast(stepperValue);
72             auto stepperValueProperty = stepperObj->GetProperty("value");
73             if (stepperValueProperty->IsNumber()) {
74                 auto indexValue = stepperValueProperty->ToNumber<int32_t>();
75                 index = indexValue <= 0 ? 0 : static_cast<uint32_t>(indexValue);
76             }
77             changeEventVal = stepperObj->GetProperty("changeEvent");
78         }
79     }
80     StepperModel::GetInstance()->Create(index);
81     if (!changeEventVal->IsUndefined() && changeEventVal->IsFunction()) {
82         ParseStepperIndexObject(info, changeEventVal);
83     }
84 }
85 
JSBind(BindingTarget globalObj)86 void JSStepper::JSBind(BindingTarget globalObj)
87 {
88     JSClass<JSStepper>::Declare("Stepper");
89 
90     MethodOptions opt = MethodOptions::NONE;
91     JSClass<JSStepper>::StaticMethod("create", &JSStepper::Create, opt);
92     JSClass<JSStepper>::StaticMethod("onFinish", &JSStepper::OnFinish);
93     JSClass<JSStepper>::StaticMethod("onSkip", &JSStepper::OnSkip);
94     JSClass<JSStepper>::StaticMethod("onChange", &JSStepper::OnChange);
95     JSClass<JSStepper>::StaticMethod("onNext", &JSStepper::OnNext);
96     JSClass<JSStepper>::StaticMethod("onPrevious", &JSStepper::OnPrevious);
97     JSClass<JSStepper>::StaticMethod("onAttach", &JSInteractableView::JsOnAttach);
98     JSClass<JSStepper>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
99     JSClass<JSStepper>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
100     JSClass<JSStepper>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
101     JSClass<JSStepper>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
102     JSClass<JSStepper>::InheritAndBind<JSContainerBase>(globalObj);
103 }
104 
OnFinish(const JSCallbackInfo & info)105 void JSStepper::OnFinish(const JSCallbackInfo& info)
106 {
107     if (info.Length() < 1) {
108         return;
109     }
110     if (!info[0]->IsFunction()) {
111         return;
112     }
113     StepperModel::GetInstance()->SetOnFinish(
114         JsEventCallback<void()>(info.GetExecutionContext(), JSRef<JSFunc>::Cast(info[0])));
115     info.ReturnSelf();
116 }
117 
OnSkip(const JSCallbackInfo & info)118 void JSStepper::OnSkip(const JSCallbackInfo& info)
119 {
120     if (info.Length() < 1) {
121         return;
122     }
123     if (!info[0]->IsFunction()) {
124         return;
125     }
126     StepperModel::GetInstance()->SetOnSkip(
127         JsEventCallback<void()>(info.GetExecutionContext(), JSRef<JSFunc>::Cast(info[0])));
128     info.ReturnSelf();
129 }
130 
OnChange(const JSCallbackInfo & info)131 void JSStepper::OnChange(const JSCallbackInfo& info)
132 {
133     if (info.Length() < 1) {
134         return;
135     }
136     if (!info[0]->IsFunction()) {
137         return;
138     }
139     StepperModel::GetInstance()->SetOnChange(
140         JsEventCallback<void(int32_t, int32_t)>(info.GetExecutionContext(), JSRef<JSFunc>::Cast(info[0])));
141     info.ReturnSelf();
142 }
143 
OnNext(const JSCallbackInfo & info)144 void JSStepper::OnNext(const JSCallbackInfo& info)
145 {
146     if (info.Length() < 1) {
147         return;
148     }
149     if (!info[0]->IsFunction()) {
150         return;
151     }
152     StepperModel::GetInstance()->SetOnNext(
153         JsEventCallback<void(int32_t, int32_t)>(info.GetExecutionContext(), JSRef<JSFunc>::Cast(info[0])));
154     info.ReturnSelf();
155 }
156 
OnPrevious(const JSCallbackInfo & info)157 void JSStepper::OnPrevious(const JSCallbackInfo& info)
158 {
159     if (info.Length() < 1) {
160         return;
161     }
162     if (!info[0]->IsFunction()) {
163         return;
164     }
165     StepperModel::GetInstance()->SetOnPrevious(
166         JsEventCallback<void(int32_t, int32_t)>(info.GetExecutionContext(), JSRef<JSFunc>::Cast(info[0])));
167     info.ReturnSelf();
168 }
169 
170 } // namespace OHOS::Ace::Framework
171