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_flex.h"
17 
18 #include "core/components_ng/base/view_abstract_model.h"
19 #include "core/components_ng/pattern/flex/flex_model.h"
20 #include "frameworks/bridge/declarative_frontend/engine/js_ref_ptr.h"
21 #include "frameworks/bridge/declarative_frontend/jsview/js_view_common_def.h"
22 #include "frameworks/bridge/declarative_frontend/jsview/models/flex_model_impl.h"
23 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
24 #include "frameworks/core/components_ng/pattern/flex/flex_model_ng.h"
25 
26 namespace OHOS::Ace {
GetInstance()27 FlexModel* FlexModel::GetInstance()
28 {
29 #ifdef NG_BUILD
30     static NG::FlexModelNG instance;
31     return &instance;
32 #else
33     if (Container::IsCurrentUseNewPipeline()) {
34         static NG::FlexModelNG instance;
35         return &instance;
36     } else {
37         static Framework::FlexModelImpl instance;
38         return &instance;
39     }
40 #endif
41 }
42 } // namespace OHOS::Ace
43 namespace OHOS::Ace::Framework {
44 
SetFillParent()45 void JSFlex::SetFillParent()
46 {
47     /* Deprecated */
48 }
49 
SetWrapContent()50 void JSFlex::SetWrapContent()
51 {
52     /* Deprecated */
53 }
54 
SetJustifyContent(int32_t value)55 void JSFlex::SetJustifyContent(int32_t value)
56 {
57     if ((value == static_cast<int32_t>(FlexAlign::FLEX_START)) ||
58         (value == static_cast<int32_t>(FlexAlign::FLEX_END)) || (value == static_cast<int32_t>(FlexAlign::CENTER)) ||
59         (value == static_cast<int32_t>(FlexAlign::SPACE_AROUND)) ||
60         (value == static_cast<int32_t>(FlexAlign::SPACE_BETWEEN)) ||
61         (value == static_cast<int32_t>(FlexAlign::SPACE_EVENLY))) {
62         FlexModel::GetInstance()->SetJustifyContent(value);
63     } else if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TEN)) {
64         FlexModel::GetInstance()->SetJustifyContent(static_cast<int32_t>(FlexAlign::FLEX_START));
65     }
66 }
67 
SetAlignItems(int32_t value)68 void JSFlex::SetAlignItems(int32_t value)
69 {
70     if ((value == static_cast<int32_t>(FlexAlign::FLEX_START)) ||
71         (value == static_cast<int32_t>(FlexAlign::FLEX_END)) || (value == static_cast<int32_t>(FlexAlign::CENTER)) ||
72         (value == static_cast<int32_t>(FlexAlign::STRETCH))) {
73         FlexModel::GetInstance()->SetAlignItems(value);
74     } else if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TEN)) {
75         FlexModel::GetInstance()->SetAlignItems(static_cast<int32_t>(FlexAlign::FLEX_START));
76     }
77 }
78 
SetAlignContent(int32_t value)79 void JSFlex::SetAlignContent(int32_t value)
80 {
81     if ((value == static_cast<int32_t>(WrapAlignment::START)) ||
82         (value == static_cast<int32_t>(WrapAlignment::CENTER)) || (value == static_cast<int32_t>(WrapAlignment::END)) ||
83         (value == static_cast<int32_t>(WrapAlignment::SPACE_AROUND)) ||
84         (value == static_cast<int32_t>(WrapAlignment::SPACE_BETWEEN)) ||
85         (value == static_cast<int32_t>(WrapAlignment::STRETCH))) {
86         FlexModel::GetInstance()->SetAlignContent(value);
87     }
88 }
89 
JsHeight(const JSCallbackInfo& info)90 void JSFlex::JsHeight(const JSCallbackInfo& info)
91 {
92     if (info.Length() < 1) {
93         return;
94     }
95 
96     SetHeight(info[0]);
97 }
98 
SetHeight(const JSRef<JSVal>& jsValue)99 void JSFlex::SetHeight(const JSRef<JSVal>& jsValue)
100 {
101     if (!JSViewAbstract::JsHeight(jsValue)) {
102         // JsHeight return false, just return.
103         return;
104     }
105     FlexModel::GetInstance()->SetHasHeight();
106 }
107 
JsWidth(const JSCallbackInfo& info)108 void JSFlex::JsWidth(const JSCallbackInfo& info)
109 {
110     if (info.Length() < 1) {
111         return;
112     }
113 
114     SetWidth(info[0]);
115 }
116 
SetWidth(const JSRef<JSVal>& jsValue)117 void JSFlex::SetWidth(const JSRef<JSVal>& jsValue)
118 {
119     if (!JSViewAbstract::JsWidth(jsValue)) {
120         // JsWidth return false, just return.
121         return;
122     }
123     FlexModel::GetInstance()->SetHasWidth();
124 }
125 
JsSize(const JSCallbackInfo& info)126 void JSFlex::JsSize(const JSCallbackInfo& info)
127 {
128     if (info.Length() < 1) {
129         return;
130     }
131 
132     if (info[0]->IsUndefined()) {
133         ViewAbstractModel::GetInstance()->ClearWidthOrHeight(true);
134         ViewAbstractModel::GetInstance()->ClearWidthOrHeight(false);
135         return;
136     }
137 
138     if (!info[0]->IsObject()) {
139         return;
140     }
141 
142     JSRef<JSObject> sizeObj = JSRef<JSObject>::Cast(info[0]);
143     SetWidth(sizeObj->GetProperty("width"));
144     SetHeight(sizeObj->GetProperty("height"));
145 }
146 
147 }; // namespace OHOS::Ace::Framework
148