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 return;
79 }
80 if (!args[0]->IsBoolean()) {
81 CounterModel::GetInstance()->SetEnableDec(true);
82 } else {
83 CounterModel::GetInstance()->SetEnableDec(args[0]->ToBoolean());
84 }
85 }
86
JsEnableInc(const JSCallbackInfo & args)87 void JSCounter::JsEnableInc(const JSCallbackInfo& args)
88 {
89 if (args.Length() < 1) {
90 return;
91 }
92 if (!args[0]->IsBoolean()) {
93 CounterModel::GetInstance()->SetEnableInc(true);
94 } else {
95 CounterModel::GetInstance()->SetEnableInc(args[0]->ToBoolean());
96 }
97 }
98
JsOnInc(const JSCallbackInfo & args)99 void JSCounter::JsOnInc(const JSCallbackInfo& args)
100 {
101 if (args.Length() < 1) {
102 return;
103 }
104 if (!args[0]->IsFunction()) {
105 return;
106 }
107 CounterModel::GetInstance()->SetOnInc(
108 JsEventCallback<void()>(args.GetExecutionContext(), JSRef<JSFunc>::Cast(args[0])));
109 args.ReturnSelf();
110 }
111
JsOnDec(const JSCallbackInfo & args)112 void JSCounter::JsOnDec(const JSCallbackInfo& args)
113 {
114 if (args.Length() < 1) {
115 return;
116 }
117 if (!args[0]->IsFunction()) {
118 return;
119 }
120 CounterModel::GetInstance()->SetOnDec(
121 JsEventCallback<void()>(args.GetExecutionContext(), JSRef<JSFunc>::Cast(args[0])));
122 args.ReturnSelf();
123 }
124
JSHeight(const JSCallbackInfo & args)125 void JSCounter::JSHeight(const JSCallbackInfo& args)
126 {
127 if (args.Length() < 1) {
128 return;
129 }
130
131 Dimension value;
132 if (!ConvertFromJSValue(args[0], value)) {
133 return;
134 }
135
136 if (LessNotEqual(value.Value(), 0.0)) {
137 return;
138 }
139 CounterModel::GetInstance()->SetHeight(value);
140 args.ReturnSelf();
141 }
142
JSWidth(const JSCallbackInfo & args)143 void JSCounter::JSWidth(const JSCallbackInfo& args)
144 {
145 if (args.Length() < 1) {
146 return;
147 }
148
149 Dimension value;
150 if (!ConvertFromJSValue(args[0], value)) {
151 return;
152 }
153
154 if (LessNotEqual(value.Value(), 0.0)) {
155 return;
156 }
157 CounterModel::GetInstance()->SetWidth(value);
158 args.ReturnSelf();
159 }
160
SetSize(const JSCallbackInfo & args)161 void JSCounter::SetSize(const JSCallbackInfo& args)
162 {
163 if (args.Length() >= 1 && args[0]->IsObject()) {
164 JSRef<JSObject> obj = JSRef<JSObject>::Cast(args[0]);
165
166 Dimension height;
167 if (ConvertFromJSValue(obj->GetProperty("height"), height) && height.IsValid()) {
168 if (GreatOrEqual(height.Value(), 0.0)) {
169 CounterModel::GetInstance()->SetHeight(height);
170 }
171 }
172
173 Dimension width;
174 if (ConvertFromJSValue(obj->GetProperty("width"), width) && width.IsValid()) {
175 if (GreatOrEqual(width.Value(), 0.0)) {
176 CounterModel::GetInstance()->SetWidth(width);
177 }
178 }
179 }
180 args.ReturnSelf();
181 }
182
JSControlWidth(const JSCallbackInfo & args)183 void JSCounter::JSControlWidth(const JSCallbackInfo& args)
184 {
185 if (args.Length() < 1) {
186 return;
187 }
188
189 Dimension value;
190 if (!ConvertFromJSValue(args[0], value)) {
191 return;
192 }
193 CounterModel::GetInstance()->SetControlWidth(value);
194 args.ReturnSelf();
195 }
196
JSStateChange(bool state)197 void JSCounter::JSStateChange(bool state)
198 {
199 CounterModel::GetInstance()->SetStateChange(state);
200 }
201
JsBackgroundColor(const JSCallbackInfo & args)202 void JSCounter::JsBackgroundColor(const JSCallbackInfo& args)
203 {
204 if (args.Length() < 1) {
205 return;
206 }
207
208 Color color;
209 if (!ParseJsColor(args[0], color)) {
210 return;
211 }
212 CounterModel::GetInstance()->SetBackgroundColor(color);
213 args.ReturnSelf();
214 }
215
Create()216 void JSCounter::Create()
217 {
218 CounterModel::GetInstance()->Create();
219 }
220
221 } // namespace OHOS::Ace::Framework
222