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 LOGE("Stepper theme is null");
90 return;
91 }
92 textStyle_ = theme->GetTextStyle();
93 textStyle_.SetAdaptTextSize(theme->GetTextStyle().GetFontSize(), theme->GetMinFontSize());
94 textStyle_.SetMaxLines(theme->GetTextMaxLines());
95 textStyle_.SetTextOverflow(TextOverflow::ELLIPSIS);
96 }
97
AddSpecializedEvent(int32_t pageId,const std::string & event)98 bool DOMStepperItem::AddSpecializedEvent(int32_t pageId, const std::string& event)
99 {
100 if (event == DOM_STEPPER_ITEM_EVENT_APPEAR) {
101 appearEventId_ = EventMarker(GetNodeIdForEvent(), event, pageId);
102 stepperItemComponent_->SetAppearEventId(appearEventId_);
103 return true;
104 } else if (event == DOM_STEPPER_ITEM_EVENT_DISAPPEAR) {
105 disappearEventId_ = EventMarker(GetNodeIdForEvent(), event, pageId);
106 stepperItemComponent_->SetDisappearEventId(disappearEventId_);
107 return true;
108 } else {
109 return false;
110 }
111 }
112
AddStepperItem(const RefPtr<DOMNode> & node,int32_t slot)113 void DOMStepperItem::AddStepperItem(const RefPtr<DOMNode>& node, int32_t slot)
114 {
115 const auto& childComponent = node->GetRootComponent();
116 if (!flexComponent_) {
117 flexComponent_ = AceType::MakeRefPtr<FlexComponent>(
118 flexDirection_, flexMainAlign_, flexCrossAlign_, std::list<RefPtr<Component>>());
119 }
120 flexComponent_->InsertChild(slot, childComponent);
121 Component::MergeRSNode(childComponent, flexComponent_);
122 }
123
RemoveStepperItem(const RefPtr<DOMNode> & node)124 void DOMStepperItem::RemoveStepperItem(const RefPtr<DOMNode>& node)
125 {
126 const auto& childComponent = node->GetRootComponent();
127 if (flexComponent_) {
128 flexComponent_->RemoveChild(childComponent);
129 }
130 }
131
OnChildNodeAdded(const RefPtr<DOMNode> & child,int32_t slot)132 void DOMStepperItem::OnChildNodeAdded(const RefPtr<DOMNode>& child, int32_t slot)
133 {
134 if (child) {
135 AddStepperItem(child, slot);
136 }
137 }
138
OnChildNodeRemoved(const RefPtr<DOMNode> & child)139 void DOMStepperItem::OnChildNodeRemoved(const RefPtr<DOMNode>& child)
140 {
141 if (child) {
142 RemoveStepperItem(child);
143 }
144 }
145
PrepareSpecializedComponent()146 void DOMStepperItem::PrepareSpecializedComponent()
147 {
148 if (!stepperItemComponent_) {
149 stepperItemComponent_ = AceType::MakeRefPtr<StepperItemComponent>(RefPtr<Component>());
150 }
151 stepperItemComponent_->SetLabel(GetLabel());
152 stepperItemComponent_->SetTextStyle(textStyle_);
153 ResetInitializedStyle();
154
155 if (flexComponent_) {
156 flexComponent_->SetDirection(flexDirection_);
157 flexComponent_->SetMainAxisAlign(flexMainAlign_);
158 flexComponent_->SetCrossAxisAlign(flexCrossAlign_);
159 } else {
160 flexComponent_ = AceType::MakeRefPtr<FlexComponent>(
161 flexDirection_, flexMainAlign_, flexCrossAlign_, std::list<RefPtr<Component>>());
162 flexComponent_->SetTextDirection(IsRightToLeft() ? TextDirection::RTL : TextDirection::LTR);
163 }
164
165 if (displayComponent_) {
166 displayComponent_->SetVisible(VisibleType::VISIBLE);
167 displayComponent_->SetOpacity(DEFAULT_OPACITY);
168 } else {
169 displayComponent_ = AceType::MakeRefPtr<DisplayComponent>();
170 }
171 }
172
CompositeSpecializedComponent(const std::vector<RefPtr<SingleChild>> & components)173 RefPtr<Component> DOMStepperItem::CompositeSpecializedComponent(const std::vector<RefPtr<SingleChild>>& components)
174 {
175 auto scroll = AceType::MakeRefPtr<ScrollComponent>(flexComponent_);
176 scroll->SetTakeBoundary(false);
177 RefPtr<Component> component = scroll;
178
179 if (!components.empty()) {
180 const auto& parent = components.back();
181 if (parent) {
182 parent->SetChild(component);
183 }
184 component = AceType::DynamicCast<Component>(components.front());
185 }
186
187 if (stepperItemComponent_) {
188 if (displayComponent_) {
189 displayComponent_->SetChild(component);
190 stepperItemComponent_->SetChild(displayComponent_);
191 } else {
192 stepperItemComponent_->SetChild(component);
193 }
194 }
195 return stepperItemComponent_;
196 }
197
ResetInitializedStyle()198 void DOMStepperItem::ResetInitializedStyle()
199 {
200 if (!stepperItemComponent_) {
201 LOGE("stepper item is null, reset style failed.");
202 return;
203 }
204 auto theme = GetTheme<FocusAnimationTheme>();
205 if (theme) {
206 stepperItemComponent_->SetFocusAnimationColor(theme->GetColor());
207 }
208 }
209
210 } // namespace OHOS::Ace::Framework
211