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 LOGW("Must contain at least 1 argument");
106 return;
107 }
108 if (!info[0]->IsFunction()) {
109 LOGW("Argument is not a function object");
110 return;
111 }
112 StepperModel::GetInstance()->SetOnFinish(
113 JsEventCallback<void()>(info.GetExecutionContext(), JSRef<JSFunc>::Cast(info[0])));
114 info.ReturnSelf();
115 }
116
OnSkip(const JSCallbackInfo & info)117 void JSStepper::OnSkip(const JSCallbackInfo& info)
118 {
119 if (info.Length() < 1) {
120 LOGW("Must contain at least 1 argument");
121 return;
122 }
123 if (!info[0]->IsFunction()) {
124 LOGW("Argument is not a function object");
125 return;
126 }
127 StepperModel::GetInstance()->SetOnSkip(
128 JsEventCallback<void()>(info.GetExecutionContext(), JSRef<JSFunc>::Cast(info[0])));
129 info.ReturnSelf();
130 }
131
OnChange(const JSCallbackInfo & info)132 void JSStepper::OnChange(const JSCallbackInfo& info)
133 {
134 if (info.Length() < 1) {
135 LOGW("Must contain at least 1 argument");
136 return;
137 }
138 if (!info[0]->IsFunction()) {
139 LOGW("Argument is not a function object");
140 return;
141 }
142 StepperModel::GetInstance()->SetOnChange(
143 JsEventCallback<void(int32_t, int32_t)>(info.GetExecutionContext(), JSRef<JSFunc>::Cast(info[0])));
144 info.ReturnSelf();
145 }
146
OnNext(const JSCallbackInfo & info)147 void JSStepper::OnNext(const JSCallbackInfo& info)
148 {
149 if (info.Length() < 1) {
150 LOGW("Must contain at least 1 argument");
151 return;
152 }
153 if (!info[0]->IsFunction()) {
154 LOGW("Argument is not a function object");
155 return;
156 }
157 StepperModel::GetInstance()->SetOnNext(
158 JsEventCallback<void(int32_t, int32_t)>(info.GetExecutionContext(), JSRef<JSFunc>::Cast(info[0])));
159 info.ReturnSelf();
160 }
161
OnPrevious(const JSCallbackInfo & info)162 void JSStepper::OnPrevious(const JSCallbackInfo& info)
163 {
164 if (info.Length() < 1) {
165 LOGW("Must contain at least 1 argument");
166 return;
167 }
168 if (!info[0]->IsFunction()) {
169 LOGW("Argument is not a function object");
170 return;
171 }
172 StepperModel::GetInstance()->SetOnPrevious(
173 JsEventCallback<void(int32_t, int32_t)>(info.GetExecutionContext(), JSRef<JSFunc>::Cast(info[0])));
174 info.ReturnSelf();
175 }
176
177 } // namespace OHOS::Ace::Framework
178