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_row.h"
17
18 #include "base/log/ace_trace.h"
19 #include "core/components_ng/pattern/linear_layout/row_model.h"
20 #include "core/components_ng/pattern/linear_layout/row_model_ng.h"
21 #include "frameworks/bridge/declarative_frontend/jsview/models/row_model_impl.h"
22
23 namespace OHOS::Ace {
24
25 std::unique_ptr<RowModel> RowModel::instance_ = nullptr;
26
GetInstance()27 RowModel* RowModel::GetInstance()
28 {
29 if (!instance_) {
30 #ifdef NG_BUILD
31 instance_.reset(new NG::RowModelNG());
32 #else
33 if (Container::IsCurrentUseNewPipeline()) {
34 instance_.reset(new NG::RowModelNG());
35 } else {
36 instance_.reset(new Framework::RowModelImpl());
37 }
38 #endif
39 }
40 return instance_.get();
41 }
42 } // namespace OHOS::Ace
43
44 namespace OHOS::Ace::Framework {
45
Create(const JSCallbackInfo & info)46 void JSRow::Create(const JSCallbackInfo& info)
47 {
48 std::optional<Dimension> space;
49 if (info.Length() > 0 && info[0]->IsObject()) {
50 JSRef<JSObject> obj = JSRef<JSObject>::Cast(info[0]);
51 JSRef<JSVal> spaceVal = obj->GetProperty("space");
52 Dimension value;
53 if (ParseJsDimensionVp(spaceVal, value)) {
54 space = value;
55 }
56 }
57 VerticalAlignDeclaration* declaration = nullptr;
58 if (info.Length() > 0 && info[0]->IsObject()) {
59 JSRef<JSObject> obj = JSRef<JSObject>::Cast(info[0]);
60 JSRef<JSVal> useAlign = obj->GetProperty("useAlign");
61 if (useAlign->IsObject()) {
62 declaration = JSRef<JSObject>::Cast(useAlign)->Unwrap<VerticalAlignDeclaration>();
63 }
64 }
65
66 RowModel::GetInstance()->Create(space, declaration, "");
67 }
68
CreateWithWrap(const JSCallbackInfo & info)69 void JSRow::CreateWithWrap(const JSCallbackInfo& info)
70 {
71 RowModel::GetInstance()->CreateWithWrap();
72 }
73
SetAlignItems(int32_t value)74 void JSRow::SetAlignItems(int32_t value)
75 {
76 if ((value == static_cast<int32_t>(FlexAlign::FLEX_START)) ||
77 (value == static_cast<int32_t>(FlexAlign::FLEX_END)) || (value == static_cast<int32_t>(FlexAlign::CENTER)) ||
78 (value == static_cast<int32_t>(FlexAlign::STRETCH))) {
79 RowModel::GetInstance()->SetAlignItems(static_cast<FlexAlign>(value));
80 } else {
81 // FIXME: we have a design issue here, setters return void, can not signal error to JS
82 LOGE("invalid value for justifyContent");
83 }
84 }
85
SetJustifyContent(int32_t value)86 void JSRow::SetJustifyContent(int32_t value)
87 {
88 if ((value == static_cast<int32_t>(FlexAlign::FLEX_START)) ||
89 (value == static_cast<int32_t>(FlexAlign::FLEX_END)) || (value == static_cast<int32_t>(FlexAlign::CENTER)) ||
90 (value == static_cast<int32_t>(FlexAlign::SPACE_BETWEEN)) ||
91 (value == static_cast<int32_t>(FlexAlign::SPACE_AROUND)) ||
92 (value == static_cast<int32_t>(FlexAlign::SPACE_EVENLY))) {
93 RowModel::GetInstance()->SetJustifyContent(static_cast<FlexAlign>(value));
94 } else {
95 LOGE("invalid value for justifyContent");
96 }
97 }
98
JSBind(BindingTarget globalObj)99 void JSRow::JSBind(BindingTarget globalObj)
100 {
101 JSClass<JSRow>::Declare("Row");
102 MethodOptions opt = MethodOptions::NONE;
103 JSClass<JSRow>::StaticMethod("create", &JSRow::Create, opt);
104 JSClass<JSRow>::StaticMethod("createWithWrap", &JSRow::CreateWithWrap, opt);
105 JSClass<JSRow>::StaticMethod("fillParent", &JSFlex::SetFillParent, opt);
106 JSClass<JSRow>::StaticMethod("wrapContent", &JSFlex::SetWrapContent, opt);
107 JSClass<JSRow>::StaticMethod("justifyContent", &JSRow::SetJustifyContent, opt);
108 JSClass<JSRow>::StaticMethod("alignItems", &JSRow::SetAlignItems, opt);
109 JSClass<JSRow>::StaticMethod("alignContent", &JSFlex::SetAlignContent, opt);
110 JSClass<JSRow>::StaticMethod("height", &JSFlex::JsHeight, opt);
111 JSClass<JSRow>::StaticMethod("width", &JSFlex::JsWidth, opt);
112 JSClass<JSRow>::StaticMethod("size", &JSFlex::JsSize, opt);
113 JSClass<JSRow>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
114 JSClass<JSRow>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
115 JSClass<JSRow>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
116 JSClass<JSRow>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
117 JSClass<JSRow>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
118 JSClass<JSRow>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
119 JSClass<JSRow>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
120 JSClass<JSRow>::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage);
121 JSClass<JSRow>::Inherit<JSContainerBase>();
122 JSClass<JSRow>::Inherit<JSViewAbstract>();
123 JSClass<JSRow>::Bind<>(globalObj);
124
125 JSClass<VerticalAlignDeclaration>::Declare("VerticalAlignDeclaration");
126 JSClass<VerticalAlignDeclaration>::Bind(
127 globalObj, VerticalAlignDeclaration::ConstructorCallback, VerticalAlignDeclaration::DestructorCallback);
128 }
129
ConstructorCallback(const JSCallbackInfo & args)130 void VerticalAlignDeclaration::ConstructorCallback(const JSCallbackInfo& args)
131 {
132 auto align = VerticalAlign::CENTER;
133 if (args.Length() > 0 && args[0]->IsNumber()) {
134 auto value = args[0]->ToNumber<int32_t>();
135 if (value >= static_cast<int32_t>(VerticalAlign::TOP) && value <= static_cast<int32_t>(VerticalAlign::BOTTOM)) {
136 align = static_cast<VerticalAlign>(value);
137 }
138 }
139 auto obj = new VerticalAlignDeclaration(align);
140 args.SetReturnValue(obj);
141 }
142
DestructorCallback(VerticalAlignDeclaration * obj)143 void VerticalAlignDeclaration::DestructorCallback(VerticalAlignDeclaration* obj)
144 {
145 delete obj;
146 }
147
148 } // namespace OHOS::Ace::Framework
149