• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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_stepper_item.h"
17 
18 #include "base/utils/linear_map.h"
19 #include "base/utils/utils.h"
20 #include "core/components/focus_animation/focus_animation_theme.h"
21 #include "core/components/stepper/stepper_theme.h"
22 #include "frameworks/bridge/common/dom/dom_reflect_map.h"
23 #include "frameworks/bridge/common/utils/utils.h"
24 
25 namespace OHOS::Ace::Framework {
26 namespace {
27 
28 constexpr double DEFAULT_OPACITY = 1.0;
29 
30 } // namespace
31 
DOMStepperItem(NodeId nodeId,const std::string & nodeName,int32_t index)32 DOMStepperItem::DOMStepperItem(NodeId nodeId, const std::string& nodeName, int32_t index) : DOMNode(nodeId, nodeName)
33 {
34     itemIndex_ = index;
35     stepperItemComponent_ = AceType::MakeRefPtr<StepperItemComponent>(RefPtr<Component>());
36 }
37 
SetSpecializedStyle(const std::pair<std::string,std::string> & style)38 bool DOMStepperItem::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
39 {
40     static const LinearMapNode<void (*)(const std::string&, DOMStepperItem&)> stepperItemStyleOperators[] = {
41         { DOM_ALIGN_ITEMS, [](const std::string& val,
42             DOMStepperItem& stepperItem) { stepperItem.flexCrossAlign_ = ConvertStrToFlexAlign(val); } },
43         { DOM_TEXT_ALLOW_SCALE,
44             [](const std::string& value, DOMStepperItem& stepperItem) {
45               stepperItem.textStyle_.SetAllowScale(StringToBool(value));
46             } },
47         { DOM_STEPPER_TEXT_COLOR,
48             [](const std::string& value, DOMStepperItem& stepperItem) {
49               stepperItem.textStyle_.SetTextColor(stepperItem.ParseColor(value));
50             } },
51         { DOM_FLEX_DIRECTION, [](const std::string& val,
52             DOMStepperItem& stepperItem) { stepperItem.flexDirection_ = ConvertStrToFlexDirection(val); } },
53         { DOM_STEPPER_FONT_FAMILY,
54             [](const std::string& value, DOMStepperItem& stepperItem) {
55               stepperItem.textStyle_.SetFontFamilies(ConvertStrToFontFamilies(value));
56             } },
57         { DOM_STEPPER_FONT_SIZE,
58             [](const std::string& value, DOMStepperItem& stepperItem) {
59               stepperItem.textStyle_.SetFontSize(stepperItem.ParseDimension(value));
60             } },
61         { DOM_STEPPER_FONT_STYLE,
62             [](const std::string& value, DOMStepperItem& stepperItem) {
63               stepperItem.textStyle_.SetFontStyle(ConvertStrToFontStyle(value));
64             } },
65         { DOM_STEPPER_FONT_WEIGHT,
66             [](const std::string& value, DOMStepperItem& stepperItem) {
67               stepperItem.textStyle_.SetFontWeight(ConvertStrToFontWeight(value));
68             } },
69         { DOM_JUSTIFY_CONTENT, [](const std::string& val,
70             DOMStepperItem& stepperItem) { stepperItem.flexMainAlign_ = ConvertStrToFlexAlign(val); } },
71         { DOM_STEPPER_TEXT_DECORATION,
72             [](const std::string& value, DOMStepperItem& stepperItem) {
73               stepperItem.textStyle_.SetTextDecoration(ConvertStrToTextDecoration(value));
74             } },
75     };
76     auto operatorIter = BinarySearchFindIndex(stepperItemStyleOperators,
77         ArraySize(stepperItemStyleOperators), style.first.c_str());
78     if (operatorIter != -1) {
79         stepperItemStyleOperators[operatorIter].value(style.second, *this);
80         return true;
81     }
82     return  false;
83 }
84 
InitializeStyle()85 void DOMStepperItem::InitializeStyle()
86 {
87     auto theme = GetTheme<StepperTheme>();
88     if (!theme) {
89         return;
90     }
91     textStyle_ = theme->GetTextStyle();
92     textStyle_.SetAdaptTextSize(theme->GetTextStyle().GetFontSize(), theme->GetMinFontSize());
93     textStyle_.SetMaxLines(theme->GetTextMaxLines());
94     textStyle_.SetTextOverflow(TextOverflow::ELLIPSIS);
95 }
96 
AddSpecializedEvent(int32_t pageId,const std::string & event)97 bool DOMStepperItem::AddSpecializedEvent(int32_t pageId, const std::string& event)
98 {
99     if (event == DOM_STEPPER_ITEM_EVENT_APPEAR) {
100         appearEventId_ = EventMarker(GetNodeIdForEvent(), event, pageId);
101         stepperItemComponent_->SetAppearEventId(appearEventId_);
102         return true;
103     } else if (event == DOM_STEPPER_ITEM_EVENT_DISAPPEAR) {
104         disappearEventId_ = EventMarker(GetNodeIdForEvent(), event, pageId);
105         stepperItemComponent_->SetDisappearEventId(disappearEventId_);
106         return true;
107     } else {
108         return false;
109     }
110 }
111 
AddStepperItem(const RefPtr<DOMNode> & node,int32_t slot)112 void DOMStepperItem::AddStepperItem(const RefPtr<DOMNode>& node, int32_t slot)
113 {
114     const auto& childComponent = node->GetRootComponent();
115     if (!flexComponent_) {
116         flexComponent_ = AceType::MakeRefPtr<FlexComponent>(
117             flexDirection_, flexMainAlign_, flexCrossAlign_, std::list<RefPtr<Component>>());
118     }
119     flexComponent_->InsertChild(slot, childComponent);
120     Component::MergeRSNode(childComponent, flexComponent_);
121 }
122 
RemoveStepperItem(const RefPtr<DOMNode> & node)123 void DOMStepperItem::RemoveStepperItem(const RefPtr<DOMNode>& node)
124 {
125     const auto& childComponent = node->GetRootComponent();
126     if (flexComponent_) {
127         flexComponent_->RemoveChild(childComponent);
128     }
129 }
130 
OnChildNodeAdded(const RefPtr<DOMNode> & child,int32_t slot)131 void DOMStepperItem::OnChildNodeAdded(const RefPtr<DOMNode>& child, int32_t slot)
132 {
133     if (child) {
134         AddStepperItem(child, slot);
135     }
136 }
137 
OnChildNodeRemoved(const RefPtr<DOMNode> & child)138 void DOMStepperItem::OnChildNodeRemoved(const RefPtr<DOMNode>& child)
139 {
140     if (child) {
141         RemoveStepperItem(child);
142     }
143 }
144 
PrepareSpecializedComponent()145 void DOMStepperItem::PrepareSpecializedComponent()
146 {
147     if (!stepperItemComponent_) {
148         stepperItemComponent_ = AceType::MakeRefPtr<StepperItemComponent>(RefPtr<Component>());
149     }
150     stepperItemComponent_->SetLabel(GetLabel());
151     stepperItemComponent_->SetTextStyle(textStyle_);
152     ResetInitializedStyle();
153 
154     if (flexComponent_) {
155         flexComponent_->SetDirection(flexDirection_);
156         flexComponent_->SetMainAxisAlign(flexMainAlign_);
157         flexComponent_->SetCrossAxisAlign(flexCrossAlign_);
158     } else {
159         flexComponent_ = AceType::MakeRefPtr<FlexComponent>(
160             flexDirection_, flexMainAlign_, flexCrossAlign_, std::list<RefPtr<Component>>());
161         flexComponent_->SetTextDirection(IsRightToLeft() ? TextDirection::RTL : TextDirection::LTR);
162     }
163 
164     if (displayComponent_) {
165         displayComponent_->SetVisible(VisibleType::VISIBLE);
166         displayComponent_->SetOpacity(DEFAULT_OPACITY);
167     } else {
168         displayComponent_ = AceType::MakeRefPtr<DisplayComponent>();
169     }
170 }
171 
CompositeSpecializedComponent(const std::vector<RefPtr<SingleChild>> & components)172 RefPtr<Component> DOMStepperItem::CompositeSpecializedComponent(const std::vector<RefPtr<SingleChild>>& components)
173 {
174     auto scroll = AceType::MakeRefPtr<ScrollComponent>(flexComponent_);
175     scroll->SetTakeBoundary(false);
176     RefPtr<Component> component = scroll;
177 
178     if (!components.empty()) {
179         const auto& parent = components.back();
180         if (parent) {
181             parent->SetChild(component);
182         }
183         component = AceType::DynamicCast<Component>(components.front());
184     }
185 
186     if (stepperItemComponent_) {
187         if (displayComponent_) {
188             displayComponent_->SetChild(component);
189             stepperItemComponent_->SetChild(displayComponent_);
190         } else {
191             stepperItemComponent_->SetChild(component);
192         }
193     }
194     return stepperItemComponent_;
195 }
196 
ResetInitializedStyle()197 void DOMStepperItem::ResetInitializedStyle()
198 {
199     if (!stepperItemComponent_) {
200         return;
201     }
202     auto theme = GetTheme<FocusAnimationTheme>();
203     if (theme) {
204         stepperItemComponent_->SetFocusAnimationColor(theme->GetColor());
205     }
206 }
207 
208 } // namespace OHOS::Ace::Framework
209