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_counter.h"
17
18 #include "bridge/declarative_frontend/jsview/models/counter_model_impl.h"
19 #include "core/components_ng/pattern/counter/counter_model_ng.h"
20 #include "frameworks/bridge/common/utils/utils.h"
21 #include "frameworks/bridge/declarative_frontend/engine/bindings.h"
22 #include "frameworks/bridge/declarative_frontend/engine/functions/js_click_function.h"
23 #include "frameworks/bridge/declarative_frontend/engine/js_ref_ptr.h"
24 #include "frameworks/bridge/declarative_frontend/jsview/js_view_common_def.h"
25 #include "frameworks/core/components/counter/counter_theme.h"
26
27 namespace OHOS::Ace {
28
29 std::unique_ptr<CounterModel> CounterModel::instance_ = nullptr;
30 std::mutex CounterModel::mutex_;
31
GetInstance()32 CounterModel* CounterModel::GetInstance()
33 {
34 if (!instance_) {
35 std::lock_guard<std::mutex> lock(mutex_);
36 if (!instance_) {
37 #ifdef NG_BUILD
38 instance_.reset(new NG::CounterModelNG());
39 #else
40 if (Container::IsCurrentUseNewPipeline()) {
41 instance_.reset(new NG::CounterModelNG());
42 } else {
43 instance_.reset(new Framework::CounterModelImpl());
44 }
45 #endif
46 }
47 }
48 return instance_.get();
49 }
50
51 } // namespace OHOS::Ace
52
53 namespace OHOS::Ace::Framework {
54
JSBind(BindingTarget globalObj)55 void JSCounter::JSBind(BindingTarget globalObj)
56 {
57 JSClass<JSCounter>::Declare("Counter");
58 JSClass<JSCounter>::StaticMethod("create", &JSCounter::Create, MethodOptions::NONE);
59 JSClass<JSCounter>::StaticMethod("enableDec", &JSCounter::JsEnableDec);
60 JSClass<JSCounter>::StaticMethod("enableInc", &JSCounter::JsEnableInc);
61 JSClass<JSCounter>::StaticMethod("onInc", &JSCounter::JsOnInc);
62 JSClass<JSCounter>::StaticMethod("onDec", &JSCounter::JsOnDec);
63 JSClass<JSCounter>::StaticMethod("height", &JSCounter::JSHeight);
64 JSClass<JSCounter>::StaticMethod("width", &JSCounter::JSWidth);
65 JSClass<JSCounter>::StaticMethod("size", &JSCounter::SetSize);
66 JSClass<JSCounter>::StaticMethod("controlWidth", &JSCounter::JSControlWidth);
67 JSClass<JSCounter>::StaticMethod("state", &JSCounter::JSStateChange);
68 JSClass<JSCounter>::StaticMethod("backgroundColor", &JSCounter::JsBackgroundColor);
69 JSClass<JSCounter>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
70 JSClass<JSCounter>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
71 JSClass<JSCounter>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
72 JSClass<JSCounter>::InheritAndBind<JSContainerBase>(globalObj);
73 }
74
JsEnableDec(const JSCallbackInfo & args)75 void JSCounter::JsEnableDec(const JSCallbackInfo& args)
76 {
77 if (args.Length() < 1) {
78 LOGW("Must contain at least 1 argument");
79 return;
80 }
81 if (!args[0]->IsBoolean()) {
82 CounterModel::GetInstance()->SetEnableDec(true);
83 } else {
84 CounterModel::GetInstance()->SetEnableDec(args[0]->ToBoolean());
85 }
86 }
87
JsEnableInc(const JSCallbackInfo & args)88 void JSCounter::JsEnableInc(const JSCallbackInfo& args)
89 {
90 if (args.Length() < 1) {
91 LOGW("Must contain at least 1 argument");
92 return;
93 }
94 if (!args[0]->IsBoolean()) {
95 CounterModel::GetInstance()->SetEnableInc(true);
96 } else {
97 CounterModel::GetInstance()->SetEnableInc(args[0]->ToBoolean());
98 }
99 }
100
JsOnInc(const JSCallbackInfo & args)101 void JSCounter::JsOnInc(const JSCallbackInfo& args)
102 {
103 if (args.Length() < 1) {
104 LOGW("Must contain at least 1 argument");
105 return;
106 }
107 if (!args[0]->IsFunction()) {
108 LOGW("Argument is not a function object");
109 return;
110 }
111 CounterModel::GetInstance()->SetOnInc(
112 JsEventCallback<void()>(args.GetExecutionContext(), JSRef<JSFunc>::Cast(args[0])));
113 args.ReturnSelf();
114 }
115
JsOnDec(const JSCallbackInfo & args)116 void JSCounter::JsOnDec(const JSCallbackInfo& args)
117 {
118 if (args.Length() < 1) {
119 LOGW("Must contain at least 1 argument");
120 return;
121 }
122 if (!args[0]->IsFunction()) {
123 LOGW("Argument is not a function object");
124 return;
125 }
126 CounterModel::GetInstance()->SetOnDec(
127 JsEventCallback<void()>(args.GetExecutionContext(), JSRef<JSFunc>::Cast(args[0])));
128 args.ReturnSelf();
129 }
130
JSHeight(const JSCallbackInfo & args)131 void JSCounter::JSHeight(const JSCallbackInfo& args)
132 {
133 if (args.Length() < 1) {
134 LOGE("The arg is wrong, it is supposed to have at least 1 arguments");
135 return;
136 }
137
138 Dimension value;
139 if (!ConvertFromJSValue(args[0], value)) {
140 LOGE("args can not set height");
141 return;
142 }
143
144 if (LessNotEqual(value.Value(), 0.0)) {
145 return;
146 }
147 CounterModel::GetInstance()->SetHeight(value);
148 args.ReturnSelf();
149 }
150
JSWidth(const JSCallbackInfo & args)151 void JSCounter::JSWidth(const JSCallbackInfo& args)
152 {
153 if (args.Length() < 1) {
154 LOGE("The arg is wrong, it is supposed to have at least 1 arguments");
155 return;
156 }
157
158 Dimension value;
159 if (!ConvertFromJSValue(args[0], value)) {
160 LOGE("args can not set width");
161 return;
162 }
163
164 if (LessNotEqual(value.Value(), 0.0)) {
165 return;
166 }
167 CounterModel::GetInstance()->SetWidth(value);
168 args.ReturnSelf();
169 }
170
SetSize(const JSCallbackInfo & args)171 void JSCounter::SetSize(const JSCallbackInfo& args)
172 {
173 if (args.Length() >= 1 && args[0]->IsObject()) {
174 JSRef<JSObject> obj = JSRef<JSObject>::Cast(args[0]);
175
176 Dimension height;
177 if (ConvertFromJSValue(obj->GetProperty("height"), height) && height.IsValid()) {
178 if (GreatOrEqual(height.Value(), 0.0)) {
179 CounterModel::GetInstance()->SetHeight(height);
180 }
181 }
182
183 Dimension width;
184 if (ConvertFromJSValue(obj->GetProperty("width"), width) && width.IsValid()) {
185 if (GreatOrEqual(width.Value(), 0.0)) {
186 CounterModel::GetInstance()->SetWidth(width);
187 }
188 }
189 }
190 args.ReturnSelf();
191 }
192
JSControlWidth(const JSCallbackInfo & args)193 void JSCounter::JSControlWidth(const JSCallbackInfo& args)
194 {
195 if (args.Length() < 1) {
196 LOGE("The arg is wrong, it is supposed to have at least 1 arguments");
197 return;
198 }
199
200 Dimension value;
201 if (!ConvertFromJSValue(args[0], value)) {
202 LOGE("args can not set control width");
203 return;
204 }
205 CounterModel::GetInstance()->SetControlWidth(value);
206 args.ReturnSelf();
207 }
208
JSStateChange(bool state)209 void JSCounter::JSStateChange(bool state)
210 {
211 CounterModel::GetInstance()->SetStateChange(state);
212 }
213
JsBackgroundColor(const JSCallbackInfo & args)214 void JSCounter::JsBackgroundColor(const JSCallbackInfo& args)
215 {
216 if (args.Length() < 1) {
217 LOGE("The arg is wrong, it is supposed to have at least 1 arguments");
218 return;
219 }
220
221 Color color;
222 if (!ParseJsColor(args[0], color)) {
223 LOGE("args can not set backgroundColor");
224 return;
225 }
226 CounterModel::GetInstance()->SetBackgroundColor(color);
227 args.ReturnSelf();
228 }
229
Create()230 void JSCounter::Create()
231 {
232 CounterModel::GetInstance()->Create();
233 }
234
235 } // namespace OHOS::Ace::Framework
236