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