1 /*
2 * Copyright (c) 2021 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_grid_container.h"
17
18 #include "base/log/ace_trace.h"
19 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
20 #include "frameworks/core/components/common/layout/grid_system_manager.h"
21
22 namespace OHOS::Ace::Framework {
23
24 thread_local std::vector<RefPtr<GridContainerInfo>> JSGridContainer::gridContainerStack_;
25
Create(const JSCallbackInfo & info)26 void JSGridContainer::Create(const JSCallbackInfo& info)
27 {
28 JSColumn::SetInspectorTag("GridContainer");
29 JSColumn::Create(info);
30 JSColumn::ClearInspectorTag();
31 GridContainerInfo::Builder gridContainerInfoBuilder;
32
33 if (info.Length() > 0 && info[0]->IsObject()) {
34 JSRef<JSObject> obj = JSRef<JSObject>::Cast(info[0]);
35
36 // columns?: number | 'auto'
37 JSRef<JSVal> columns = obj->GetProperty("columns");
38 if (columns->IsNumber()) {
39 gridContainerInfoBuilder.SetColumns(columns->ToNumber<int32_t>());
40 }
41
42 // sizeType?: SizeType
43 JSRef<JSVal> sizeType = obj->GetProperty("sizeType");
44 if (sizeType->IsNumber()) {
45 auto value = sizeType->ToNumber<int32_t>();
46 gridContainerInfoBuilder.SetSizeType(static_cast<GridSizeType>(value));
47 }
48
49 Dimension dim;
50 // gutter?: Length
51 if (ParseJsDimensionVp(obj->GetProperty("gutter"), dim)) {
52 gridContainerInfoBuilder.SetGutterWidth(dim);
53 }
54
55 // margin?: Length
56 if (ParseJsDimensionVp(obj->GetProperty("margin"), dim)) {
57 gridContainerInfoBuilder.SetMarginLeft(dim);
58 gridContainerInfoBuilder.SetMarginRight(dim);
59 }
60 }
61
62 auto gridContainerInfo = gridContainerInfoBuilder.Build();
63 gridContainerStack_.emplace_back(gridContainerInfo);
64 ViewStackProcessor::GetInstance()->GetBoxComponent()->SetGridLayoutInfo(gridContainerInfo);
65
66 auto main = ViewStackProcessor::GetInstance()->GetMainComponent();
67 auto column = AceType::DynamicCast<FlexComponent>(main);
68 if (column) {
69 column->SetCrossAxisSize(CrossAxisSize::MAX);
70 }
71 }
72
Pop()73 void JSGridContainer::Pop()
74 {
75 gridContainerStack_.pop_back();
76 JSColumn::Pop();
77 }
78
JSBind(BindingTarget globalObj)79 void JSGridContainer::JSBind(BindingTarget globalObj)
80 {
81 JSClass<JSGridContainer>::Declare("GridContainer");
82 JSClass<JSGridContainer>::StaticMethod("create", &JSGridContainer::Create, MethodOptions::NONE);
83 JSClass<JSGridContainer>::StaticMethod("pop", &JSGridContainer::Pop, MethodOptions::NONE);
84 JSClass<JSGridContainer>::Inherit<JSColumn>();
85 JSClass<JSGridContainer>::Bind<>(globalObj);
86 }
87
88 } // namespace OHOS::Ace::Framework
89