• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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_form.h"
17 
18 #include "base/geometry/dimension.h"
19 #include "base/geometry/ng/size_t.h"
20 #include "base/log/ace_scoring_log.h"
21 #include "bridge/declarative_frontend/jsview/models/form_model_impl.h"
22 #include "core/components_ng/base/view_abstract.h"
23 #include "core/components_ng/pattern/form/form_model_ng.h"
24 #include "frameworks/bridge/declarative_frontend/jsview/js_utils.h"
25 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
26 
27 #if !defined(WEARABLE_PRODUCT)
28 #include "frameworks/core/components/form/form_component.h"
29 #endif
30 
31 namespace OHOS::Ace {
32 
33 std::unique_ptr<FormModel> FormModel::instance_ = nullptr;
34 std::mutex FormModel::mutex_;
35 
GetInstance()36 FormModel* FormModel::GetInstance()
37 {
38     if (!instance_) {
39         std::lock_guard<std::mutex> lock(mutex_);
40         if (!instance_) {
41 #ifdef NG_BUILD
42             instance_.reset(new NG::FormModelNG());
43 #else
44             if (Container::IsCurrentUseNewPipeline()) {
45                 instance_.reset(new NG::FormModelNG());
46             } else {
47                 instance_.reset(new Framework::FormModelImpl());
48             }
49 #endif
50         }
51     }
52     return instance_.get();
53 }
54 
55 } // namespace OHOS::Ace
56 
57 namespace OHOS::Ace::Framework {
58 
Create(const JSCallbackInfo & info)59 void JSForm::Create(const JSCallbackInfo& info)
60 {
61     if (info.Length() == 0 || !info[0]->IsObject()) {
62         LOGE("form create fail due to FormComponent construct param is empty or type is not Object");
63         return;
64     }
65     auto obj = JSRef<JSObject>::Cast(info[0]);
66     JSRef<JSVal> id = obj->GetProperty("id");
67     JSRef<JSVal> name = obj->GetProperty("name");
68     JSRef<JSVal> bundle = obj->GetProperty("bundle");
69     JSRef<JSVal> ability = obj->GetProperty("ability");
70     JSRef<JSVal> module = obj->GetProperty("module");
71     JSRef<JSVal> dimension = obj->GetProperty("dimension");
72     JSRef<JSVal> temporary = obj->GetProperty("temporary");
73     JSRef<JSVal> wantValue = obj->GetProperty("want");
74 
75     LOGD("js form create id:%{public}d, name:%{public}s, bundle:%{public}s, ability:%{public}s, module:%{public}s, "
76          "temporary:%{public}s",
77         id->ToNumber<int32_t>(), name->ToString().c_str(), bundle->ToString().c_str(), ability->ToString().c_str(),
78         module->ToString().c_str(), temporary->ToString().c_str());
79 
80     RequestFormInfo fomInfo;
81     fomInfo.id = id->ToNumber<int32_t>();
82     fomInfo.cardName = name->ToString();
83     fomInfo.bundleName = bundle->ToString();
84     fomInfo.abilityName = ability->ToString();
85     fomInfo.moduleName = module->ToString();
86     if (!dimension->IsNull() && !dimension->IsEmpty()) {
87         fomInfo.dimension = dimension->ToNumber<int32_t>();
88     }
89     fomInfo.temporary = temporary->ToBoolean();
90     if (!wantValue->IsNull() && wantValue->IsObject()) {
91         fomInfo.wantWrap = CreateWantWrapFromNapiValue(wantValue);
92     }
93     FormModel::GetInstance()->Create(fomInfo);
94 }
95 
SetSize(const JSCallbackInfo & info)96 void JSForm::SetSize(const JSCallbackInfo& info)
97 {
98     JSViewAbstract::JsSize(info);
99 
100     if (info.Length() == 0 || !info[0]->IsObject()) {
101         LOGW("form set size fail due to FormComponent construct param is empty or type is not Object");
102         return;
103     }
104     Dimension width = 0.0_vp;
105     Dimension height = 0.0_vp;
106 
107     JSRef<JSObject> sizeObj = JSRef<JSObject>::Cast(info[0]);
108     JSRef<JSVal> widthValue = sizeObj->GetProperty("width");
109     if (!widthValue->IsNull() && !widthValue->IsEmpty()) {
110         if (widthValue->IsNumber()) {
111             width = Dimension(widthValue->ToNumber<double>(), DimensionUnit::VP);
112         } else if (widthValue->IsString()) {
113             width = StringUtils::StringToDimension(widthValue->ToString(), true);
114         }
115     }
116 
117     JSRef<JSVal> heightValue = sizeObj->GetProperty("height");
118     if (!heightValue->IsNull() && !heightValue->IsEmpty()) {
119         if (heightValue->IsNumber()) {
120             height = Dimension(heightValue->ToNumber<double>(), DimensionUnit::VP);
121         } else if (heightValue->IsString()) {
122             height = StringUtils::StringToDimension(heightValue->ToString(), true);
123         }
124     }
125     FormModel::GetInstance()->SetSize(width, height);
126 }
127 
SetDimension(int32_t value)128 void JSForm::SetDimension(int32_t value)
129 {
130     FormModel::GetInstance()->SetDimension(value);
131 }
132 
AllowUpdate(const JSCallbackInfo & info)133 void JSForm::AllowUpdate(const JSCallbackInfo& info)
134 {
135     if (info.Length() <= 0 || !info[0]->IsBoolean()) {
136         LOGE("param is not valid");
137         return;
138     }
139 
140     auto allowUpdate = info[0]->ToBoolean();
141     FormModel::GetInstance()->AllowUpdate(allowUpdate);
142 }
143 
SetVisibility(const JSCallbackInfo & info)144 void JSForm::SetVisibility(const JSCallbackInfo& info)
145 {
146     if (info.Length() <= 0 || !info[0]->IsNumber()) {
147         LOGE("param is not valid");
148         return;
149     }
150 
151     auto type = info[0]->ToNumber<int32_t>();
152     FormModel::GetInstance()->SetVisibility(VisibleType(type));
153 }
154 
SetModuleName(const JSCallbackInfo & info)155 void JSForm::SetModuleName(const JSCallbackInfo& info)
156 {
157     if (info.Length() <= 0 || !info[0]->IsString()) {
158         LOGE("param is not valid");
159         return;
160     }
161 
162     auto moduleName = info[0]->ToString();
163     FormModel::GetInstance()->SetModuleName(moduleName);
164 }
165 
JsOnAcquired(const JSCallbackInfo & info)166 void JSForm::JsOnAcquired(const JSCallbackInfo& info)
167 {
168     if (info[0]->IsFunction()) {
169         auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
170         auto onAcquired = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc)](const std::string& param) {
171             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
172             ACE_SCORING_EVENT("Form.onAcquired");
173             std::vector<std::string> keys = { "id" };
174             func->Execute(keys, param);
175         };
176         FormModel::GetInstance()->SetOnAcquired(std::move(onAcquired));
177     }
178 }
179 
JsOnError(const JSCallbackInfo & info)180 void JSForm::JsOnError(const JSCallbackInfo& info)
181 {
182     if (info[0]->IsFunction()) {
183         auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
184         auto onError = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc)](const std::string& param) {
185             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
186             ACE_SCORING_EVENT("Form.onError");
187             std::vector<std::string> keys = { "errcode", "msg" };
188             func->Execute(keys, param);
189         };
190         FormModel::GetInstance()->SetOnError(std::move(onError));
191     }
192 }
193 
JsOnUninstall(const JSCallbackInfo & info)194 void JSForm::JsOnUninstall(const JSCallbackInfo& info)
195 {
196     if (info[0]->IsFunction()) {
197         auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
198         auto onUninstall = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc)](const std::string& param) {
199             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
200             ACE_SCORING_EVENT("Form.onUninstall");
201             std::vector<std::string> keys = { "id" };
202             func->Execute(keys, param);
203         };
204         FormModel::GetInstance()->SetOnUninstall(std::move(onUninstall));
205     }
206 }
207 
JsOnRouter(const JSCallbackInfo & info)208 void JSForm::JsOnRouter(const JSCallbackInfo& info)
209 {
210     if (info[0]->IsFunction()) {
211         auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
212         auto onRouter = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc)](const std::string& param) {
213             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
214             ACE_SCORING_EVENT("Form.onRouter");
215             std::vector<std::string> keys = { "action" };
216             func->Execute(keys, param);
217         };
218         FormModel::GetInstance()->SetOnRouter(std::move(onRouter));
219     }
220 }
221 
JsOnLoad(const JSCallbackInfo & info)222 void JSForm::JsOnLoad(const JSCallbackInfo& info)
223 {
224     LOGI("JsOnLoad");
225     if (info[0]->IsFunction()) {
226         auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
227         auto onLoad = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc)](const std::string& param) {
228             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
229             ACE_SCORING_EVENT("Form.onLoad");
230             std::vector<std::string> keys;
231             func->Execute(keys, param);
232         };
233         FormModel::GetInstance()->SetOnLoad(std::move(onLoad));
234     }
235 }
236 
JSBind(BindingTarget globalObj)237 void JSForm::JSBind(BindingTarget globalObj)
238 {
239     JSClass<JSForm>::Declare("FormComponent");
240     MethodOptions opt = MethodOptions::NONE;
241     JSClass<JSForm>::StaticMethod("create", &JSForm::Create, opt);
242     JSClass<JSForm>::StaticMethod("size", &JSForm::SetSize, opt);
243     JSClass<JSForm>::StaticMethod("dimension", &JSForm::SetDimension, opt);
244     JSClass<JSForm>::StaticMethod("allowUpdate", &JSForm::AllowUpdate, opt);
245     JSClass<JSForm>::StaticMethod("visibility", &JSForm::SetVisibility, opt);
246     JSClass<JSForm>::StaticMethod("moduleName", &JSForm::SetModuleName, opt);
247     JSClass<JSForm>::StaticMethod("clip", &JSViewAbstract::JsClip, opt);
248 
249     JSClass<JSForm>::StaticMethod("onAcquired", &JSForm::JsOnAcquired);
250     JSClass<JSForm>::StaticMethod("onError", &JSForm::JsOnError);
251     JSClass<JSForm>::StaticMethod("onUninstall", &JSForm::JsOnUninstall);
252     JSClass<JSForm>::StaticMethod("onRouter", &JSForm::JsOnRouter);
253     JSClass<JSForm>::StaticMethod("onLoad", &JSForm::JsOnLoad);
254     JSClass<JSForm>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
255     JSClass<JSForm>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
256     JSClass<JSForm>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
257     JSClass<JSForm>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
258     JSClass<JSForm>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
259     JSClass<JSForm>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
260 
261     JSClass<JSForm>::InheritAndBind<JSViewAbstract>(globalObj);
262 }
263 
264 } // namespace OHOS::Ace::Framework
265