• 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/common/dom/dom_grid_container.h"
17 
18 #include "core/components/common/layout/grid_system_manager.h"
19 #include "frameworks/bridge/common/dom/dom_grid_row.h"
20 #include "frameworks/bridge/common/dom/dom_reflect_map.h"
21 #include "frameworks/bridge/common/utils/utils.h"
22 
23 namespace OHOS::Ace::Framework {
24 
DomGridContainer(NodeId nodeId,const std::string & nodeName)25 DomGridContainer::DomGridContainer(NodeId nodeId, const std::string& nodeName) : DOMDiv(nodeId, nodeName)
26 {
27     direction_ = DOM_FLEX_COLUMN;
28 }
29 
GetSpecializedComponent()30 RefPtr<Component> DomGridContainer::GetSpecializedComponent()
31 {
32     if (!gridContainerInfo_) {
33         gridContainerInfo_ = infoBuilder_.Build();
34         boxComponent_->SetGridLayoutInfo(gridContainerInfo_);
35     }
36     return DOMDiv::GetSpecializedComponent();
37 }
38 
OnChildNodeAdded(const RefPtr<DOMNode> & child,int32_t slot)39 void DomGridContainer::OnChildNodeAdded(const RefPtr<DOMNode>& child, int32_t slot)
40 {
41     ACE_DCHECK(child);
42     DOMDiv::OnChildNodeAdded(child, slot);
43     auto gridRow = AceType::DynamicCast<DomGridRow>(child);
44     if (gridRow) {
45         gridRow->SetParentGridInfo(gridContainerInfo_);
46     }
47 }
48 
OnChildNodeRemoved(const RefPtr<DOMNode> & child)49 void DomGridContainer::OnChildNodeRemoved(const RefPtr<DOMNode>& child)
50 {
51     ACE_DCHECK(child);
52     DOMDiv::OnChildNodeRemoved(child);
53     auto gridRow = AceType::DynamicCast<DomGridRow>(child);
54     if (gridRow) {
55         gridRow->SetParentGridInfo(nullptr);
56     }
57 }
58 
CallSpecializedMethod(const std::string & method,const std::string & args)59 void DomGridContainer::CallSpecializedMethod(const std::string& method, const std::string& args)
60 {
61     std::string result;
62     if (method == DOM_GRID_CONTAINER_GET_COLUMNS) {
63         result = std::to_string(gridContainerInfo_->GetColumns());
64     } else if (method == DOM_GRID_CONTAINER_GET_COLUMN_WIDTH) {
65         result = std::to_string(gridContainerInfo_->GetColumnWidth());
66     } else if (method == DOM_GRID_CONTAINER_GET_GUTTER_WIDTH) {
67         auto dipScale = GridSystemManager::GetInstance().GetDipScale();
68         result = std::to_string(gridContainerInfo_->GetGutterWidth().ConvertToPx(dipScale));
69     } else if (method == DOM_GRID_CONTAINER_GET_SIZE_TYPE) {
70         auto sizeType = gridContainerInfo_->GetSizeType();
71         result.append("\"");
72         if (sizeType == GridSizeType::LG) {
73             result.append(DOM_GRID_SIZE_TYPE_LG);
74         } else if (sizeType == GridSizeType::MD) {
75             result.append(DOM_GRID_SIZE_TYPE_MD);
76         } else if (sizeType == GridSizeType::SM) {
77             result.append(DOM_GRID_SIZE_TYPE_SM);
78         }
79         result.append("\"");
80     } else {
81         return;
82     }
83     std::unique_ptr<JsonValue> argsValue = JsonUtil::ParseJsonString(args);
84     if (!argsValue || !argsValue->IsArray() || argsValue->GetArraySize() != 1) {
85         LOGE("parse args error");
86         return;
87     }
88     auto callbackId = argsValue->GetArrayItem(0)->GetString();
89     auto content = pipelineContext_.Upgrade();
90     if (!content) {
91         return;
92     }
93     content->SendCallbackMessageToFrontend(callbackId, result);
94 }
95 
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)96 bool DomGridContainer::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
97 {
98     static const LinearMapNode<void (*)(const std::string&, DomGridContainer&)> attrOperators[] {
99         { DOM_GRID_CONTAINER_COLUMNS,
100             [](const std::string& value, DomGridContainer& container) {
101                 container.infoBuilder_.SetColumns(StringUtils::StringToInt(value));
102             } },
103         { DOM_GRID_CONTAINER_TEMPLATE,
104             [](const std::string& value, DomGridContainer& container) {
105                 auto templateType = GridTemplateType::NORMAL;
106                 auto iter = GridTemplateMap.find(value);
107                 if (iter != GridTemplateMap.end()) {
108                     templateType = iter->second;
109                 }
110                 container.infoBuilder_.SetGridTemplateType(templateType);
111             } },
112         { DOM_GRID_CONTAINER_GUTTER,
113             [](const std::string& value, DomGridContainer& container) {
114                 container.infoBuilder_.SetGutterWidth(Dimension(StringUtils::StringToDouble(value), DimensionUnit::PX));
115             } },
116         { DOM_GRID_CONTAINER_GUTTER_WIDTH,
117             [](const std::string& value, DomGridContainer& container) {
118                 container.infoBuilder_.SetGutterWidth(Dimension(StringUtils::StringToDouble(value), DimensionUnit::PX));
119             } },
120         { DOM_GRID_CONTAINER_SIZE_TYPE,
121             [](const std::string& value, DomGridContainer& container) {
122                 auto sizeType = GridSizeType::UNDEFINED;
123                 auto iter = GridSizeTypeMap.find(value);
124                 if (iter != GridSizeTypeMap.end()) {
125                     sizeType = iter->second;
126                 }
127                 container.infoBuilder_.SetSizeType(sizeType);
128             } },
129     };
130 
131     auto operatorIter = BinarySearchFindIndex(attrOperators, ArraySize(attrOperators), attr.first.c_str());
132     if (operatorIter != -1) {
133         attrOperators[operatorIter].value(attr.second, *this);
134         return true;
135     }
136     return false;
137 }
138 
SetSpecializedStyle(const std::pair<std::string,std::string> & style)139 bool DomGridContainer::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
140 {
141     static const std::list<const char*> notSupportStyles {
142         DOM_FLEX_DIRECTION,
143         DOM_GRID_COLUMN_END,
144         DOM_GRID_COLUMN_START,
145         DOM_GRID_COLUMN_GAP,
146         DOM_GRID_ROW_END,
147         DOM_GRID_ROW_START,
148         DOM_GRID_ROW_GAP,
149         DOM_GRID_TEMPLATE_COLUMNS,
150         DOM_GRID_TEMPLATE_ROWS,
151     };
152 
153     if (std::find(notSupportStyles.begin(), notSupportStyles.end(), style.first.c_str()) != notSupportStyles.end()) {
154         // not support
155         return true;
156     }
157     if (style.first == DOM_MARGIN_LEFT) {
158         infoBuilder_.SetMarginLeft(StringToDimension(style.second));
159         return DOMNode::SetSpecializedStyle(style);
160     } else if (style.first == DOM_MARGIN_RIGHT) {
161         infoBuilder_.SetMarginRight(StringToDimension(style.second));
162         return DOMNode::SetSpecializedStyle(style);
163     } else if (style.first == DOM_MARGIN_START) {
164         if (IsRightToLeft()) {
165             infoBuilder_.SetMarginRight(StringToDimension(style.second));
166         } else {
167             infoBuilder_.SetMarginLeft(StringToDimension(style.second));
168         }
169         return DOMNode::SetSpecializedStyle(style);
170     } else if (style.first == DOM_MARGIN_END) {
171         if (IsRightToLeft()) {
172             infoBuilder_.SetMarginLeft(StringToDimension(style.second));
173         } else {
174             infoBuilder_.SetMarginRight(StringToDimension(style.second));
175         }
176         return DOMNode::SetSpecializedStyle(style);
177     } else if (style.first == DOM_PADDING_LEFT) {
178         infoBuilder_.SetPaddingLeft(StringToDimension(style.second));
179         return DOMNode::SetSpecializedStyle(style);
180     } else if (style.first == DOM_PADDING_RIGHT) {
181         infoBuilder_.SetPaddingRight(StringToDimension(style.second));
182         return DOMNode::SetSpecializedStyle(style);
183     } else if (style.first == DOM_PADDING_START) {
184         if (IsRightToLeft()) {
185             infoBuilder_.SetPaddingRight(StringToDimension(style.second));
186         } else {
187             infoBuilder_.SetPaddingLeft(StringToDimension(style.second));
188         }
189         return DOMNode::SetSpecializedStyle(style);
190     } else if (style.first == DOM_PADDING_END) {
191         if (IsRightToLeft()) {
192             infoBuilder_.SetPaddingLeft(StringToDimension(style.second));
193         } else {
194             infoBuilder_.SetPaddingRight(StringToDimension(style.second));
195         }
196         return DOMNode::SetSpecializedStyle(style);
197     }
198     return DOMDiv::SetSpecializedStyle(style);
199 }
200 
201 } // namespace OHOS::Ace::Framework
202