• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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                 index = stepperValueProperty->ToNumber<int32_t>();
75             }
76             changeEventVal = stepperObj->GetProperty("changeEvent");
77         }
78     }
79     StepperModel::GetInstance()->Create(index);
80     if (!changeEventVal->IsUndefined() && changeEventVal->IsFunction()) {
81         ParseStepperIndexObject(info, changeEventVal);
82     }
83 }
84 
JSBind(BindingTarget globalObj)85 void JSStepper::JSBind(BindingTarget globalObj)
86 {
87     JSClass<JSStepper>::Declare("Stepper");
88 
89     MethodOptions opt = MethodOptions::NONE;
90     JSClass<JSStepper>::StaticMethod("create", &JSStepper::Create, opt);
91     JSClass<JSStepper>::StaticMethod("onFinish", &JSStepper::OnFinish);
92     JSClass<JSStepper>::StaticMethod("onSkip", &JSStepper::OnSkip);
93     JSClass<JSStepper>::StaticMethod("onChange", &JSStepper::OnChange);
94     JSClass<JSStepper>::StaticMethod("onNext", &JSStepper::OnNext);
95     JSClass<JSStepper>::StaticMethod("onPrevious", &JSStepper::OnPrevious);
96     JSClass<JSStepper>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
97     JSClass<JSStepper>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
98     JSClass<JSStepper>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
99     JSClass<JSStepper>::InheritAndBind<JSContainerBase>(globalObj);
100 }
101 
OnFinish(const JSCallbackInfo & info)102 void JSStepper::OnFinish(const JSCallbackInfo& info)
103 {
104     if (info.Length() < 1) {
105         return;
106     }
107     if (!info[0]->IsFunction()) {
108         return;
109     }
110     StepperModel::GetInstance()->SetOnFinish(
111         JsEventCallback<void()>(info.GetExecutionContext(), JSRef<JSFunc>::Cast(info[0])));
112     info.ReturnSelf();
113 }
114 
OnSkip(const JSCallbackInfo & info)115 void JSStepper::OnSkip(const JSCallbackInfo& info)
116 {
117     if (info.Length() < 1) {
118         return;
119     }
120     if (!info[0]->IsFunction()) {
121         return;
122     }
123     StepperModel::GetInstance()->SetOnSkip(
124         JsEventCallback<void()>(info.GetExecutionContext(), JSRef<JSFunc>::Cast(info[0])));
125     info.ReturnSelf();
126 }
127 
OnChange(const JSCallbackInfo & info)128 void JSStepper::OnChange(const JSCallbackInfo& info)
129 {
130     if (info.Length() < 1) {
131         return;
132     }
133     if (!info[0]->IsFunction()) {
134         return;
135     }
136     StepperModel::GetInstance()->SetOnChange(
137         JsEventCallback<void(int32_t, int32_t)>(info.GetExecutionContext(), JSRef<JSFunc>::Cast(info[0])));
138     info.ReturnSelf();
139 }
140 
OnNext(const JSCallbackInfo & info)141 void JSStepper::OnNext(const JSCallbackInfo& info)
142 {
143     if (info.Length() < 1) {
144         return;
145     }
146     if (!info[0]->IsFunction()) {
147         return;
148     }
149     StepperModel::GetInstance()->SetOnNext(
150         JsEventCallback<void(int32_t, int32_t)>(info.GetExecutionContext(), JSRef<JSFunc>::Cast(info[0])));
151     info.ReturnSelf();
152 }
153 
OnPrevious(const JSCallbackInfo & info)154 void JSStepper::OnPrevious(const JSCallbackInfo& info)
155 {
156     if (info.Length() < 1) {
157         return;
158     }
159     if (!info[0]->IsFunction()) {
160         return;
161     }
162     StepperModel::GetInstance()->SetOnPrevious(
163         JsEventCallback<void(int32_t, int32_t)>(info.GetExecutionContext(), JSRef<JSFunc>::Cast(info[0])));
164     info.ReturnSelf();
165 }
166 
167 } // namespace OHOS::Ace::Framework
168