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