• 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_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         LOGE("invalid value for justifyContent");
66     }
67 }
68 
SetAlignItems(int32_t value)69 void JSFlex::SetAlignItems(int32_t value)
70 {
71     if ((value == static_cast<int32_t>(FlexAlign::FLEX_START)) ||
72         (value == static_cast<int32_t>(FlexAlign::FLEX_END)) || (value == static_cast<int32_t>(FlexAlign::CENTER)) ||
73         (value == static_cast<int32_t>(FlexAlign::STRETCH))) {
74         FlexModel::GetInstance()->SetAlignItems(value);
75     } else if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TEN)) {
76         FlexModel::GetInstance()->SetAlignItems(static_cast<int32_t>(FlexAlign::FLEX_START));
77         LOGE("invalid value for alignItems");
78     }
79 }
80 
SetAlignContent(int32_t value)81 void JSFlex::SetAlignContent(int32_t value)
82 {
83     if ((value == static_cast<int32_t>(WrapAlignment::START)) ||
84         (value == static_cast<int32_t>(WrapAlignment::CENTER)) || (value == static_cast<int32_t>(WrapAlignment::END)) ||
85         (value == static_cast<int32_t>(WrapAlignment::SPACE_AROUND)) ||
86         (value == static_cast<int32_t>(WrapAlignment::SPACE_BETWEEN)) ||
87         (value == static_cast<int32_t>(WrapAlignment::STRETCH))) {
88         FlexModel::GetInstance()->SetAlignContent(value);
89     } else {
90         LOGE("invalid value for alignContent");
91     }
92 }
93 
JsHeight(const JSCallbackInfo & info)94 void JSFlex::JsHeight(const JSCallbackInfo& info)
95 {
96     if (info.Length() < 1) {
97         LOGE("The arg is wrong, it is supposed to have atleast 1 arguments");
98         return;
99     }
100 
101     SetHeight(info[0]);
102 }
103 
SetHeight(const JSRef<JSVal> & jsValue)104 void JSFlex::SetHeight(const JSRef<JSVal>& jsValue)
105 {
106     if (!JSViewAbstract::JsHeight(jsValue)) {
107         // JsHeight return false, just return.
108         return;
109     }
110     FlexModel::GetInstance()->SetHasHeight();
111 }
112 
JsWidth(const JSCallbackInfo & info)113 void JSFlex::JsWidth(const JSCallbackInfo& info)
114 {
115     if (info.Length() < 1) {
116         LOGE("The arg is wrong, it is supposed to have atleast 1 arguments");
117         return;
118     }
119 
120     SetWidth(info[0]);
121 }
122 
SetWidth(const JSRef<JSVal> & jsValue)123 void JSFlex::SetWidth(const JSRef<JSVal>& jsValue)
124 {
125     if (!JSViewAbstract::JsWidth(jsValue)) {
126         // JsWidth return false, just return.
127         return;
128     }
129     FlexModel::GetInstance()->SetHasWidth();
130 }
131 
JsSize(const JSCallbackInfo & info)132 void JSFlex::JsSize(const JSCallbackInfo& info)
133 {
134     if (info.Length() < 1) {
135         LOGE("The arg is wrong, it is supposed to have atleast 1 arguments");
136         return;
137     }
138 
139     if (info[0]->IsUndefined()) {
140         ViewAbstractModel::GetInstance()->ClearWidthOrHeight(true);
141         ViewAbstractModel::GetInstance()->ClearWidthOrHeight(false);
142         return;
143     }
144 
145     if (!info[0]->IsObject()) {
146         LOGE("arg is not Object or String.");
147         return;
148     }
149 
150     JSRef<JSObject> sizeObj = JSRef<JSObject>::Cast(info[0]);
151     SetWidth(sizeObj->GetProperty("width"));
152     SetHeight(sizeObj->GetProperty("height"));
153 }
154 
155 }; // namespace OHOS::Ace::Framework
156