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 "bridge/declarative_frontend/jsview/models/grid_container_model_impl.h"
20 #include "frameworks/core/components/common/layout/grid_system_manager.h"
21 #include "frameworks/core/components_ng/base/view_stack_model.h"
22 #include "frameworks/core/components_ng/pattern/grid_container/grid_container_model_ng.h"
23
24 namespace OHOS::Ace {
GetInstance()25 GridContainerModel* GridContainerModel::GetInstance()
26 {
27 #ifdef NG_BUILD
28 static NG::GridContainerModelNG instance;
29 return &instance;
30 #else
31 if (Container::IsCurrentUseNewPipeline()) {
32 static NG::GridContainerModelNG instance;
33 return &instance;
34 } else {
35 static Framework::GridContainerModelImpl instance;
36 return &instance;
37 }
38 #endif
39 }
40 } // namespace OHOS::Ace
41
42 namespace OHOS::Ace::Framework {
43
Create(const JSCallbackInfo & info)44 void JSGridContainer::Create(const JSCallbackInfo& info)
45 {
46 GridContainerInfo::Builder gridContainerInfoBuilder;
47 if (info.Length() > 0 && info[0]->IsObject()) {
48 JSRef<JSObject> obj = JSRef<JSObject>::Cast(info[0]);
49
50 // columns?: number | 'auto'
51 JSRef<JSVal> columns = obj->GetProperty("columns");
52 if (columns->IsNumber()) {
53 gridContainerInfoBuilder.SetColumns(columns->ToNumber<int32_t>());
54 }
55
56 // sizeType?: SizeType
57 JSRef<JSVal> sizeType = obj->GetProperty("sizeType");
58 if (sizeType->IsNumber()) {
59 auto value = sizeType->ToNumber<int32_t>();
60 gridContainerInfoBuilder.SetSizeType(static_cast<GridSizeType>(value));
61 }
62
63 CalcDimension dim;
64 // gutter?: Length
65 if (ParseJsDimensionVp(obj->GetProperty("gutter"), dim)) {
66 gridContainerInfoBuilder.SetGutterWidth(dim);
67 }
68
69 // margin?: Length
70 if (ParseJsDimensionVp(obj->GetProperty("margin"), dim)) {
71 gridContainerInfoBuilder.SetMarginLeft(dim);
72 gridContainerInfoBuilder.SetMarginRight(dim);
73 }
74 }
75 auto gridContainerInfo = gridContainerInfoBuilder.Build();
76 GridContainerModel::GetInstance()->Create(gridContainerInfo);
77 }
78
Pop()79 void JSGridContainer::Pop()
80 {
81 if (ViewStackModel::GetInstance()->IsPrebuilding()) {
82 return ViewStackModel::GetInstance()->PushPrebuildCompCmd("[JSGridContainer][pop]", &JSGridContainer::Pop);
83 }
84 GridContainerModel::GetInstance()->Pop();
85 JSColumn::Pop();
86 }
87
JSBind(BindingTarget globalObj)88 void JSGridContainer::JSBind(BindingTarget globalObj)
89 {
90 JSClass<JSGridContainer>::Declare("GridContainer");
91 JSClass<JSGridContainer>::StaticMethod("create", &JSGridContainer::Create, MethodOptions::NONE);
92 JSClass<JSGridContainer>::StaticMethod("pop", &JSGridContainer::Pop, MethodOptions::NONE);
93 JSClass<JSGridContainer>::InheritAndBind<JSColumn>(globalObj);
94 }
95
96 } // namespace OHOS::Ace::Framework
97