• 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_navigation_bar.h"
17 
18 #include "base/log/event_report.h"
19 #include "base/utils/linear_map.h"
20 #include "base/utils/utils.h"
21 #include "frameworks/bridge/common/dom/dom_type.h"
22 #include "frameworks/bridge/common/utils/utils.h"
23 
24 namespace OHOS::Ace::Framework {
25 namespace {
26 
27 const char EMPHASIZE[] = "emphasize";
GetType(const std::string & value)28 NavigationBarType GetType(const std::string& value)
29 {
30     return value == EMPHASIZE ? NavigationBarType::EMPHASIZE : NavigationBarType::NORMAL;
31 }
32 
33 } // namespace
34 
DomNavigationBar(NodeId nodeId,const std::string & nodeName)35 DomNavigationBar::DomNavigationBar(NodeId nodeId, const std::string& nodeName)
36     : DOMNode(nodeId, nodeName),
37       navigationBarComponent_(AceType::MakeRefPtr<NavigationBarComponent>(std::to_string(nodeId), nodeName)),
38       navigationBarData_(navigationBarComponent_->GetData())
39 {
40     if (IsRightToLeft()) {
41         navigationBarComponent_->SetTextDirection(TextDirection::RTL);
42     }
43 }
44 
InitializeStyle()45 void DomNavigationBar::InitializeStyle()
46 {
47     auto theme = GetTheme<NavigationBarTheme>();
48     navigationBarData_->theme = theme;
49     navigationBarData_->subTitleColor = theme->GetSubTitleColor();
50     navigationBarData_->titleColor = theme->GetTitleColor();
51 }
52 
ResetInitializedStyle()53 void DomNavigationBar::ResetInitializedStyle()
54 {
55     InitializeStyle();
56 }
57 
58 #ifndef WEARABLE_PRODUCT
OnChildNodeRemoved(const RefPtr<DOMNode> & child)59 void DomNavigationBar::OnChildNodeRemoved(const RefPtr<DOMNode>& child)
60 {
61     if (!child || !navigationBarData_) {
62         return;
63     }
64 
65     auto menu = AceType::DynamicCast<MenuComponent>(child->GetSpecializedComponent());
66     if (menu) {
67         navigationBarData_->menu = nullptr;
68         navigationBarData_->allMenuItems.clear();
69         return;
70     }
71     auto tabBar = AceType::DynamicCast<TabBarComponent>(child->GetSpecializedComponent());
72     if (tabBar) {
73         navigationBarData_->tabBar = nullptr;
74         return;
75     }
76 
77     auto selectPopup = AceType::DynamicCast<SelectComponent>(child->GetSpecializedComponent());
78     if (selectPopup) {
79         navigationBarData_->selectPopup = nullptr;
80     } else {
81         LOGW("navigation bar child node type not support");
82     }
83 }
84 
OnChildNodeAdded(const RefPtr<DOMNode> & child,int32_t slot)85 void DomNavigationBar::OnChildNodeAdded(const RefPtr<DOMNode>& child, int32_t slot)
86 {
87     if (!child || !navigationBarComponent_) {
88         return;
89     }
90 
91     auto menu = AceType::DynamicCast<MenuComponent>(child->GetSpecializedComponent());
92     if (menu) {
93         navigationBarData_->menu = menu;
94         return;
95     }
96     auto tabBar = AceType::DynamicCast<TabBarComponent>(child->GetSpecializedComponent());
97     if (tabBar) {
98         navigationBarData_->tabBar = tabBar;
99         return;
100     }
101 
102     auto selectPopup = AceType::DynamicCast<SelectComponent>(child->GetSpecializedComponent());
103     if (selectPopup) {
104         navigationBarData_->selectPopup = selectPopup;
105     } else {
106         LOGW("navigation bar child node type not support");
107     }
108 }
109 #endif
110 
CallSpecializedMethod(const std::string & method,const std::string & args)111 void DomNavigationBar::CallSpecializedMethod(const std::string& method, const std::string& args)
112 {
113     if (!navigationBarData_->controller) {
114         LOGE("get navigation bar controller failed");
115         EventReport::SendComponentException(ComponentExcepType::NAVIGATION_BAR_ERR);
116         return;
117     }
118     if (method == DOM_NAVIGATION_BAR_METHOD_SHOW) {
119         navigationBarData_->controller->Show();
120     } else if (method == DOM_NAVIGATION_BAR_METHOD_HIDE) {
121         navigationBarData_->controller->Hide();
122     } else {
123         LOGE("call not support method: %{private}s", method.c_str());
124     }
125 }
126 
PrepareSpecializedComponent()127 void DomNavigationBar::PrepareSpecializedComponent()
128 {
129     navigationBarData_->imageFill = GetImageFill();
130 }
131 
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)132 bool DomNavigationBar::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
133 {
134     static const LinearMapNode<void (*)(const std::string&, const RefPtr<NavigationBarData>&)> attrOperators[] = {
135         { DOM_NAVIGATION_BAR_BACK_ENABLED,
136             [](const std::string& val, const RefPtr<NavigationBarData>& navigationBar) {
137                 navigationBar->backEnabled = StringToBool(val);
138             } },
139         { DOM_NAVIGATION_BAR_END_ICON,
140             [](const std::string& val, const RefPtr<NavigationBarData>& navigationBar) {
141                 navigationBar->endIcon = val;
142             } },
143         { DOM_NAVIGATION_BAR_HEADER, [](const std::string& val,
144                                      const RefPtr<NavigationBarData>& navigationBar) { navigationBar->header = val; } },
145         { DOM_NAVIGATION_BAR_LOGO,
146             [](const std::string& val, const RefPtr<NavigationBarData>& navigationBar) { navigationBar->logo = val; } },
147         { DOM_NAVIGATION_BAR_START_ICON,
148             [](const std::string& val, const RefPtr<NavigationBarData>& navigationBar) {
149                 navigationBar->startIcon = val;
150             } },
151         { DOM_NAVIGATION_BAR_SUBTITLE,
152             [](const std::string& val, const RefPtr<NavigationBarData>& navigationBar) {
153                 navigationBar->subTitle = val;
154             } },
155         { DOM_NAVIGATION_BAR_TITLE, [](const std::string& val,
156                                     const RefPtr<NavigationBarData>& navigationBar) { navigationBar->title = val; } },
157         { DOM_NAVIGATION_BAR_TYPE,
158             [](const std::string& val, const RefPtr<NavigationBarData>& navigationBar) {
159                 navigationBar->type = GetType(val);
160             } },
161     };
162     auto operatorIter = BinarySearchFindIndex(attrOperators, ArraySize(attrOperators), attr.first.c_str());
163     if (operatorIter != -1) {
164         attrOperators[operatorIter].value(attr.second, navigationBarData_);
165         return true;
166     }
167     return false;
168 }
169 
SetSpecializedStyle(const std::pair<std::string,std::string> & style)170 bool DomNavigationBar::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
171 {
172     static const LinearMapNode<void (*)(const std::string&, const DomNavigationBar&)> navigationBarStyleOperators[] = {
173         { DOM_NAVIGATION_BAR_SUBTITLE_COLOR,
174             [](const std::string& val, const DomNavigationBar& node) {
175                 node.navigationBarData_->subTitleColor = node.ParseColor(val);
176             } },
177         { DOM_NAVIGATION_BAR_TITLE_COLOR,
178             [](const std::string& val, const DomNavigationBar& node) {
179                 node.navigationBarData_->titleColor = node.ParseColor(val);
180             } },
181     };
182     auto operatorIter =
183         BinarySearchFindIndex(navigationBarStyleOperators, ArraySize(navigationBarStyleOperators), style.first.c_str());
184     if (operatorIter != -1) {
185         navigationBarStyleOperators[operatorIter].value(style.second, *this);
186         return true;
187     }
188     return false;
189 }
190 
AddSpecializedEvent(int32_t pageId,const std::string & event)191 bool DomNavigationBar::AddSpecializedEvent(int32_t pageId, const std::string& event)
192 {
193     if (event == DOM_NAVIGATION_BAR_EVENT_BACK_CLICK) {
194         navigationBarData_->backClickMarker = EventMarker(GetNodeIdForEvent(), event, pageId);
195     } else if (event == DOM_NAVIGATION_BAR_EVENT_START_CLICK) {
196         navigationBarData_->startClickMarker = EventMarker(GetNodeIdForEvent(), event, pageId);
197     } else if (event == DOM_NAVIGATION_BAR_EVENT_END_CLICK) {
198         navigationBarData_->endClickMarker = EventMarker(GetNodeIdForEvent(), event, pageId);
199     } else {
200         return false;
201     }
202     return true;
203 }
204 
205 } // namespace OHOS::Ace::Framework
206