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