1 /*
2 * Copyright (c) 2025 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_button.h"
17
18 #include "base/log/ace_trace.h"
19 #include "base/log/log_wrapper.h"
20 #include "base/utils/utils.h"
21 #include "bridge/declarative_frontend/engine/functions/js_click_function.h"
22 #include "bridge/declarative_frontend/engine/js_types.h"
23 #include "bridge/declarative_frontend/jsview/js_utils.h"
24 #include "bridge/declarative_frontend/jsview/models/form_model_impl.h"
25 #include "core/common/container.h"
26 #include "core/components_ng/base/view_abstract.h"
27 #include "core/components_ng/pattern/form_button/form_button_model.h"
28 #include "core/components_ng/pattern/form_button/form_button_model_ng.h"
29 #include "frameworks/bridge/declarative_frontend/engine/js_ref_ptr.h"
30 #include "frameworks/bridge/declarative_frontend/jsview/js_stack.h"
31 #include "frameworks/bridge/declarative_frontend/jsview/js_view_common_def.h"
32
33 namespace OHOS::Ace {
34
GetInstance()35 FormButtonModel* FormButtonModel::GetInstance()
36 {
37 static NG::FormButtonModelNG instance;
38 return &instance;
39 }
40 } // namespace OHOS::Ace
41
42 namespace OHOS::Ace::Framework {
43
44 const static std::array<Alignment, 9> ALIGNMENT_ARR { Alignment::TOP_LEFT, Alignment::TOP_CENTER, Alignment::TOP_RIGHT,
45 Alignment::CENTER_LEFT, Alignment::CENTER, Alignment::CENTER_RIGHT, Alignment::BOTTOM_LEFT,
46 Alignment::BOTTOM_CENTER, Alignment::BOTTOM_RIGHT };
47
JSBind(BindingTarget globalObj)48 void JSFormButton::JSBind(BindingTarget globalObj)
49 {
50 JSClass<JSFormButton>::Declare("FormButton");
51 MethodOptions opt = MethodOptions::NONE;
52 JSClass<JSFormButton>::StaticMethod("create", &JSFormButton::Create, opt);
53 JSClass<JSFormButton>::InheritAndBind<JSViewAbstract>(globalObj);
54 }
55
Create(const JSCallbackInfo & info)56 void JSFormButton::Create(const JSCallbackInfo& info)
57 {
58 Alignment alignment = Alignment::CENTER;
59 if (info.Length() == 0 || !info[0]->IsObject()) {
60 return;
61 }
62 auto action = info[0]->ToString();
63 RequestFormInfo formInfo;
64
65 if (info.Length() > 0 && info[0]->IsObject()) {
66 JSRef<JSObject> obj = JSRef<JSObject>::Cast(info[0]);
67 JSRef<JSVal> stackAlign = obj->GetProperty("alignContent");
68 JSRef<JSVal> bundleName = obj->GetProperty("bundleName");
69 JSRef<JSVal> abilityName = obj->GetProperty("abilityName");
70 JSRef<JSVal> dimension = obj->GetProperty("dimension");
71 JSRef<JSVal> name = obj->GetProperty("name");
72 JSRef<JSVal> moduleName = obj->GetProperty("moduleName");
73
74 formInfo.cardName = name->ToString();
75 formInfo.bundleName = bundleName->ToString();
76 formInfo.abilityName = abilityName->ToString();
77 formInfo.moduleName = moduleName->ToString();
78 if (!dimension->IsNull() && !dimension->IsEmpty()) {
79 formInfo.dimension = dimension->ToNumber<int32_t>();
80 }
81
82 if (stackAlign->IsNumber()) {
83 int32_t value = stackAlign->ToNumber<int32_t>();
84 if (value >= 0 && value < static_cast<int>(ALIGNMENT_ARR.size())) {
85 alignment = ALIGNMENT_ARR[value];
86 } else {
87 FormButtonModel::GetInstance()->Create(action, formInfo);
88 return;
89 }
90 } else {
91 FormButtonModel::GetInstance()->Create(action, formInfo);
92 return;
93 }
94 } else {
95 FormButtonModel::GetInstance()->Create(action, formInfo);
96 return;
97 }
98
99 FormButtonModel::GetInstance()->Create(alignment, action, formInfo);
100 }
101
102 } // namespace OHOS::Ace::Framework