• 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_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 
GetInstance()31 CounterModel* CounterModel::GetInstance()
32 {
33     if (!instance_) {
34 #ifdef NG_BUILD
35         instance_.reset(new NG::CounterModelNG());
36 #else
37         if (Container::IsCurrentUseNewPipeline()) {
38             instance_.reset(new NG::CounterModelNG());
39         } else {
40             instance_.reset(new Framework::CounterModelImpl());
41         }
42 #endif
43     }
44     return instance_.get();
45 }
46 
47 } // namespace OHOS::Ace
48 
49 namespace OHOS::Ace::Framework {
50 
JSBind(BindingTarget globalObj)51 void JSCounter::JSBind(BindingTarget globalObj)
52 {
53     JSClass<JSCounter>::Declare("Counter");
54     JSClass<JSCounter>::StaticMethod("create", &JSCounter::Create, MethodOptions::NONE);
55     JSClass<JSCounter>::StaticMethod("onInc", &JSCounter::JsOnInc);
56     JSClass<JSCounter>::StaticMethod("onDec", &JSCounter::JsOnDec);
57     JSClass<JSCounter>::StaticMethod("height", &JSCounter::JSHeight);
58     JSClass<JSCounter>::StaticMethod("width", &JSCounter::JSWidth);
59     JSClass<JSCounter>::StaticMethod("size", &JSCounter::SetSize);
60     JSClass<JSCounter>::StaticMethod("controlWidth", &JSCounter::JSControlWidth);
61     JSClass<JSCounter>::StaticMethod("state", &JSCounter::JSStateChange);
62     JSClass<JSCounter>::StaticMethod("backgroundColor", &JSCounter::JsBackgroundColor);
63     JSClass<JSCounter>::Inherit<JSContainerBase>();
64     JSClass<JSCounter>::Bind(globalObj);
65 }
66 
JsOnInc(const JSCallbackInfo & args)67 void JSCounter::JsOnInc(const JSCallbackInfo& args)
68 {
69     if (args.Length() < 1) {
70         LOGW("Must contain at least 1 argument");
71         return;
72     }
73     if (!args[0]->IsFunction()) {
74         LOGW("Argument is not a function object");
75         return;
76     }
77     CounterModel::GetInstance()->SetOnInc(
78         JsEventCallback<void()>(args.GetExecutionContext(), JSRef<JSFunc>::Cast(args[0])));
79     args.ReturnSelf();
80 }
81 
JsOnDec(const JSCallbackInfo & args)82 void JSCounter::JsOnDec(const JSCallbackInfo& args)
83 {
84     if (args.Length() < 1) {
85         LOGW("Must contain at least 1 argument");
86         return;
87     }
88     if (!args[0]->IsFunction()) {
89         LOGW("Argument is not a function object");
90         return;
91     }
92     CounterModel::GetInstance()->SetOnDec(
93         JsEventCallback<void()>(args.GetExecutionContext(), JSRef<JSFunc>::Cast(args[0])));
94     args.ReturnSelf();
95 }
96 
JSHeight(const JSCallbackInfo & args)97 void JSCounter::JSHeight(const JSCallbackInfo& args)
98 {
99     if (args.Length() < 1) {
100         LOGE("The arg is wrong, it is supposed to have at least 1 arguments");
101         return;
102     }
103 
104     Dimension value;
105     if (!ConvertFromJSValue(args[0], value)) {
106         LOGE("args can not set height");
107         return;
108     }
109 
110     if (LessNotEqual(value.Value(), 0.0)) {
111         return;
112     }
113     CounterModel::GetInstance()->SetHeight(value);
114     args.ReturnSelf();
115 }
116 
JSWidth(const JSCallbackInfo & args)117 void JSCounter::JSWidth(const JSCallbackInfo& args)
118 {
119     if (args.Length() < 1) {
120         LOGE("The arg is wrong, it is supposed to have at least 1 arguments");
121         return;
122     }
123 
124     Dimension value;
125     if (!ConvertFromJSValue(args[0], value)) {
126         LOGE("args can not set width");
127         return;
128     }
129 
130     if (LessNotEqual(value.Value(), 0.0)) {
131         return;
132     }
133     CounterModel::GetInstance()->SetWidth(value);
134     args.ReturnSelf();
135 }
136 
SetSize(const JSCallbackInfo & args)137 void JSCounter::SetSize(const JSCallbackInfo& args)
138 {
139     if (args.Length() >= 1 && args[0]->IsObject()) {
140         JSRef<JSObject> obj = JSRef<JSObject>::Cast(args[0]);
141 
142         Dimension height;
143         if (ConvertFromJSValue(obj->GetProperty("height"), height) && height.IsValid()) {
144             if (GreatOrEqual(height.Value(), 0.0)) {
145                 CounterModel::GetInstance()->SetWidth(height);
146             }
147         }
148 
149         Dimension width;
150         if (ConvertFromJSValue(obj->GetProperty("width"), width) && width.IsValid()) {
151             if (GreatOrEqual(width.Value(), 0.0)) {
152                 CounterModel::GetInstance()->SetWidth(width);
153             }
154         }
155     }
156     args.ReturnSelf();
157 }
158 
JSControlWidth(const JSCallbackInfo & args)159 void JSCounter::JSControlWidth(const JSCallbackInfo& args)
160 {
161     if (args.Length() < 1) {
162         LOGE("The arg is wrong, it is supposed to have at least 1 arguments");
163         return;
164     }
165 
166     Dimension value;
167     if (!ConvertFromJSValue(args[0], value)) {
168         LOGE("args can not set control width");
169         return;
170     }
171     CounterModel::GetInstance()->SetControlWidth(value);
172     args.ReturnSelf();
173 }
174 
JSStateChange(bool state)175 void JSCounter::JSStateChange(bool state)
176 {
177     CounterModel::GetInstance()->SetStateChange(state);
178 }
179 
JsBackgroundColor(const JSCallbackInfo & args)180 void JSCounter::JsBackgroundColor(const JSCallbackInfo& args)
181 {
182     if (args.Length() < 1) {
183         LOGE("The arg is wrong, it is supposed to have at least 1 arguments");
184         return;
185     }
186 
187     Color color;
188     if (!ParseJsColor(args[0], color)) {
189         LOGE("args can not set backgroundColor");
190         return;
191     }
192     CounterModel::GetInstance()->SetBackgroundColor(color);
193     args.ReturnSelf();
194 }
195 
Create()196 void JSCounter::Create()
197 {
198     CounterModel::GetInstance()->Create();
199 }
200 
201 } // namespace OHOS::Ace::Framework
202