• 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_column.h"
17 
18 #include "frameworks/bridge/common/utils/utils.h"
19 
20 namespace OHOS::Ace::Framework {
21 
DomGridColumn(NodeId nodeId,const std::string & nodeName)22 DomGridColumn::DomGridColumn(NodeId nodeId, const std::string& nodeName) : DOMDiv(nodeId, nodeName)
23 {
24     infoBuilder_.SetColumns(1); // default span 1 column
25 }
26 
GetSpecializedComponent()27 RefPtr<Component> DomGridColumn::GetSpecializedComponent()
28 {
29     if (!columnInfo_) {
30         columnInfo_ = infoBuilder_.Build();
31         boxComponent_->SetGridLayoutInfo(columnInfo_);
32         if (!flexItemComponent_) {
33             flexItemComponent_ = AceType::MakeRefPtr<FlexItemComponent>();
34         }
35         flexItemComponent_->SetGridColumnInfoBuilder(boxComponent_->GetGridColumnInfoBuilder());
36     }
37     return DOMDiv::GetSpecializedComponent();
38 }
39 
OnChildNodeAdded(const RefPtr<DOMNode> & child,int32_t slot)40 void DomGridColumn::OnChildNodeAdded(const RefPtr<DOMNode>& child, int32_t slot)
41 {
42     ACE_DCHECK(child);
43     DOMDiv::OnChildNodeAdded(child, slot);
44 }
45 
OnChildNodeRemoved(const RefPtr<DOMNode> & child)46 void DomGridColumn::OnChildNodeRemoved(const RefPtr<DOMNode>& child)
47 {
48     ACE_DCHECK(child);
49     DOMDiv::OnChildNodeRemoved(child);
50 }
51 
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)52 bool DomGridColumn::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
53 {
54     static const LinearMapNode<void (*)(const std::string&, DomGridColumn&)> attrOperators[] {
55         { DOM_GRID_SIZE_TYPE_LG,
56             [](const std::string& value, DomGridColumn& column) {
57                 uint32_t span = 0;
58                 Dimension offset = UNDEFINED_DIMENSION;
59                 column.ParseSpanAndOffset(value, span, offset);
60                 column.infoBuilder_.SetLgSizeColumn(span, offset);
61             } },
62         { DOM_GRID_SIZE_TYPE_MD,
63             [](const std::string& value, DomGridColumn& column) {
64                 uint32_t span = 0;
65                 Dimension offset = UNDEFINED_DIMENSION;
66                 column.ParseSpanAndOffset(value, span, offset);
67                 column.infoBuilder_.SetMdSizeColumn(span, offset);
68             } },
69         { DOM_GRID_COLUMN_OFFSET,
70             [](const std::string& value, DomGridColumn& column) {
71                 auto declaration = column.GetDeclaration();
72                 if (!declaration) {
73                     return;
74                 }
75                 auto& positionStyle =
76                     declaration->MaybeResetStyle<CommonPositionStyle>(StyleTag::COMMON_POSITION_STYLE);
77                 if (positionStyle.IsValid()) {
78                     positionStyle.position = PositionType::PTABSOLUTE;
79                     positionStyle.top = Dimension(0.0, DimensionUnit::PX);
80                     positionStyle.left = column.ParseDimension(value);
81                     declaration->SetHasLeft(true);
82                     declaration->SetHasTop(true);
83                     declaration->SetHasPositionStyle(true);
84                 }
85             } },
86         { DOM_GRID_SIZE_TYPE_SM,
87             [](const std::string& value, DomGridColumn& column) {
88                 uint32_t span = 0;
89                 Dimension offset = UNDEFINED_DIMENSION;
90                 column.ParseSpanAndOffset(value, span, offset);
91                 column.infoBuilder_.SetSmSizeColumn(span, offset);
92             } },
93         { DOM_GRID_COLUMN_SPAN,
94             [](const std::string& value, DomGridColumn& column) {
95                 column.infoBuilder_.SetColumns(StringUtils::StringToUint(value));
96             } },
97         { DOM_GRID_SIZE_TYPE_XS,
98             [](const std::string& value, DomGridColumn& column) {
99                 uint32_t span = 0;
100                 Dimension offset = UNDEFINED_DIMENSION;
101                 column.ParseSpanAndOffset(value, span, offset);
102                 column.infoBuilder_.SetXsSizeColumn(span, offset);
103             } },
104     };
105 
106     auto operatorIter = BinarySearchFindIndex(attrOperators, ArraySize(attrOperators), attr.first.c_str());
107     if (operatorIter != -1) {
108         attrOperators[operatorIter].value(attr.second, *this);
109         return true;
110     }
111     return false;
112 }
113 
ParseSpanAndOffset(const std::string & value,uint32_t & span,Dimension & offset)114 void DomGridColumn::DomGridColumn::ParseSpanAndOffset(const std::string& value, uint32_t& span, Dimension& offset)
115 {
116     span = StringUtils::StringToUint(value);
117     if (span > 0) {
118         return;
119     }
120 
121     std::unique_ptr<JsonValue> jsonValue = JsonUtil::ParseJsonString(value);
122     if (!jsonValue) {
123         LOGE("parse format is error");
124         return;
125     }
126 
127     std::unique_ptr<JsonValue> spanValue = jsonValue->GetValue(DOM_GRID_COLUMN_SPAN);
128     if (spanValue && spanValue->IsNumber()) {
129         span = spanValue->GetUInt();
130     } else {
131         LOGW("get span failed");
132     }
133 
134     std::unique_ptr<JsonValue> offsetValue = jsonValue->GetValue(DOM_GRID_COLUMN_OFFSET);
135     if (!offsetValue) {
136         return;
137     }
138     if (offsetValue->IsNumber()) {
139         offset.SetValue(offsetValue->GetDouble());
140     } else if (offsetValue->IsString()) {
141         offset = ParseDimension(offsetValue->GetString());
142     }
143 }
144 
145 } // namespace OHOS::Ace::Framework
146