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
GetInstance()26 StepperModel* StepperModel::GetInstance()
27 {
28 if (!instance_) {
29 #ifdef NG_BUILD
30 instance_.reset(new NG::StepperModelNG());
31 #else
32 if (Container::IsCurrentUseNewPipeline()) {
33 instance_.reset(new NG::StepperModelNG());
34 } else {
35 instance_.reset(new Framework::StepperModelImpl());
36 }
37 #endif
38 }
39 return instance_.get();
40 }
41
42 } // namespace OHOS::Ace
43
44 namespace OHOS::Ace::Framework {
45
Create(const JSCallbackInfo & info)46 void JSStepper::Create(const JSCallbackInfo& info)
47 {
48 uint32_t index = 0;
49 if (info.Length() < 1 || !info[0]->IsObject()) {
50 index = 0;
51 } else {
52 JSRef<JSObject> obj = JSRef<JSObject>::Cast(info[0]);
53 JSRef<JSVal> stepperValue = obj->GetProperty("index");
54 if (stepperValue->IsNumber()) {
55 index = stepperValue->ToNumber<uint32_t>();
56 }
57 }
58 StepperModel::GetInstance()->Create(index);
59 }
60
JSBind(BindingTarget globalObj)61 void JSStepper::JSBind(BindingTarget globalObj)
62 {
63 JSClass<JSStepper>::Declare("Stepper");
64
65 MethodOptions opt = MethodOptions::NONE;
66 JSClass<JSStepper>::StaticMethod("create", &JSStepper::Create, opt);
67 JSClass<JSStepper>::StaticMethod("onFinish", &JSStepper::OnFinish);
68 JSClass<JSStepper>::StaticMethod("onSkip", &JSStepper::OnSkip);
69 JSClass<JSStepper>::StaticMethod("onChange", &JSStepper::OnChange);
70 JSClass<JSStepper>::StaticMethod("onNext", &JSStepper::OnNext);
71 JSClass<JSStepper>::StaticMethod("onPrevious", &JSStepper::OnPrevious);
72 JSClass<JSStepper>::Inherit<JSContainerBase>();
73 JSClass<JSStepper>::Inherit<JSViewAbstract>();
74 JSClass<JSStepper>::Bind<>(globalObj);
75 }
76
OnFinish(const JSCallbackInfo & info)77 void JSStepper::OnFinish(const JSCallbackInfo& info)
78 {
79 if (info.Length() < 1) {
80 LOGW("Must contain at least 1 argument");
81 return;
82 }
83 if (!info[0]->IsFunction()) {
84 LOGW("Argument is not a function object");
85 return;
86 }
87 StepperModel::GetInstance()->SetOnFinish(
88 JsEventCallback<void()>(info.GetExecutionContext(), JSRef<JSFunc>::Cast(info[0])));
89 info.ReturnSelf();
90 }
91
OnSkip(const JSCallbackInfo & info)92 void JSStepper::OnSkip(const JSCallbackInfo& info)
93 {
94 if (info.Length() < 1) {
95 LOGW("Must contain at least 1 argument");
96 return;
97 }
98 if (!info[0]->IsFunction()) {
99 LOGW("Argument is not a function object");
100 return;
101 }
102 StepperModel::GetInstance()->SetOnSkip(
103 JsEventCallback<void()>(info.GetExecutionContext(), JSRef<JSFunc>::Cast(info[0])));
104 info.ReturnSelf();
105 }
106
OnChange(const JSCallbackInfo & info)107 void JSStepper::OnChange(const JSCallbackInfo& info)
108 {
109 if (info.Length() < 1) {
110 LOGW("Must contain at least 1 argument");
111 return;
112 }
113 if (!info[0]->IsFunction()) {
114 LOGW("Argument is not a function object");
115 return;
116 }
117 StepperModel::GetInstance()->SetOnChange(
118 JsEventCallback<void(int32_t, int32_t)>(info.GetExecutionContext(), JSRef<JSFunc>::Cast(info[0])));
119 info.ReturnSelf();
120 }
121
OnNext(const JSCallbackInfo & info)122 void JSStepper::OnNext(const JSCallbackInfo& info)
123 {
124 if (info.Length() < 1) {
125 LOGW("Must contain at least 1 argument");
126 return;
127 }
128 if (!info[0]->IsFunction()) {
129 LOGW("Argument is not a function object");
130 return;
131 }
132 StepperModel::GetInstance()->SetOnNext(
133 JsEventCallback<void(int32_t, int32_t)>(info.GetExecutionContext(), JSRef<JSFunc>::Cast(info[0])));
134 info.ReturnSelf();
135 }
136
OnPrevious(const JSCallbackInfo & info)137 void JSStepper::OnPrevious(const JSCallbackInfo& info)
138 {
139 if (info.Length() < 1) {
140 LOGW("Must contain at least 1 argument");
141 return;
142 }
143 if (!info[0]->IsFunction()) {
144 LOGW("Argument is not a function object");
145 return;
146 }
147 StepperModel::GetInstance()->SetOnPrevious(
148 JsEventCallback<void(int32_t, int32_t)>(info.GetExecutionContext(), JSRef<JSFunc>::Cast(info[0])));
149 info.ReturnSelf();
150 }
151
152 } // namespace OHOS::Ace::Framework
153