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