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 "core/components_v2/inspector/navigation_composed_element.h"
17 
18 #include "base/log/dump_log.h"
19 #include "core/components/common/layout/constants.h"
20 #include "core/components/navigation_bar/navigation_container_component.h"
21 #include "core/components_v2/inspector/utils.h"
22 
23 namespace OHOS::Ace::V2 {
24 
25 namespace {
26 
27 const std::unordered_map<std::string, std::function<std::string(const NavigationComposedElement&)>>
28     CREATE_JSON_STRING_MAP {
29         { "title",
__anon0fac038c0202(const NavigationComposedElement& inspector) 30             [](const NavigationComposedElement& inspector) { return inspector.GetNavigationTitle(); } },
31         { "subTitle",
__anon0fac038c0302(const NavigationComposedElement& inspector) 32             [](const NavigationComposedElement& inspector) { return inspector.GetNavigationSubTitle(); } },
33         { "titleMode",
__anon0fac038c0402(const NavigationComposedElement& inspector) 34             [](const NavigationComposedElement& inspector) { return inspector.GetNavigationSubTitleMode(); } },
35         { "toolBar",
__anon0fac038c0502(const NavigationComposedElement& inspector) 36             [](const NavigationComposedElement& inspector) { return inspector.GetNavigationToolBar(); } },
37     };
38 
39 const std::unordered_map<std::string, std::function<bool(const NavigationComposedElement&)>>
40     CREATE_JSON_BOOL_MAP {
41         { "hideBackButton",
__anon0fac038c0602(const NavigationComposedElement& inspector) 42             [](const NavigationComposedElement& inspector) { return inspector.GetHideNavigationBackButton(); } },
43         { "hideTitleBar",
__anon0fac038c0702(const NavigationComposedElement& inspector) 44             [](const NavigationComposedElement& inspector) { return inspector.GetHideNavigationBar(); } },
45         { "hideToolBar",
__anon0fac038c0802(const NavigationComposedElement& inspector) 46             [](const NavigationComposedElement& inspector) { return inspector.GetNavigationHideToolBar(); } }
47     };
48 
49 using JsonFuncType = std::function<std::unique_ptr<JsonValue>(const NavigationComposedElement&)>;
50 const std::unordered_map<std::string, JsonFuncType> CREATE_JSON_VALUE_MAP {
__anon0fac038c0902() 51     { "menus", [](const NavigationComposedElement& inspector) { return inspector.GetMenus(); } }
52 };
53 
54 }
55 
Dump()56 void NavigationComposedElement::Dump()
57 {
58     InspectorComposedElement::Dump();
59     DumpLog::GetInstance().AddDesc(
60         std::string("navigationTitle: ").append(GetNavigationTitle()));
61     DumpLog::GetInstance().AddDesc(
62         std::string("navigationSubTitle: ").append(GetNavigationSubTitle()));
63     DumpLog::GetInstance().AddDesc(
64         std::string("navigationSubTitleMode: ").append(GetNavigationSubTitleMode()));
65     DumpLog::GetInstance().AddDesc(
66         std::string("navigationToolBar: ").append(GetNavigationToolBar()));
67     DumpLog::GetInstance().AddDesc(
68         std::string("hideNavigationBackButton: ").append(GetHideNavigationBackButton() ? "true" : "false"));
69     DumpLog::GetInstance().AddDesc(
70         std::string("hideNavigationBar: ").append(GetHideNavigationBar() ? "true" : "false"));
71     DumpLog::GetInstance().AddDesc(
72         std::string("hideNavigationToolBar: ").append(GetNavigationHideToolBar() ? "true" : "false"));
73 }
74 
ToJsonObject() const75 std::unique_ptr<JsonValue> NavigationComposedElement::ToJsonObject() const
76 {
77     auto resultJson = InspectorComposedElement::ToJsonObject();
78     for (const auto& value : CREATE_JSON_STRING_MAP) {
79         resultJson->Put(value.first.c_str(), value.second(*this).c_str());
80     }
81     for (const auto& value : CREATE_JSON_BOOL_MAP) {
82         resultJson->Put(value.first.c_str(), value.second(*this));
83     }
84     for (const auto& value : CREATE_JSON_VALUE_MAP) {
85         resultJson->Put(value.first.c_str(), value.second(*this));
86     }
87     return resultJson;
88 }
89 
GetNavigationTitle() const90 std::string NavigationComposedElement::GetNavigationTitle() const
91 {
92     auto render = GetRenderNavigation();
93     if (!render) {
94         return "";
95     }
96     return render->GetTitle();
97 }
98 
GetNavigationSubTitle() const99 std::string NavigationComposedElement::GetNavigationSubTitle() const
100 {
101     auto render = GetRenderNavigation();
102     if (!render) {
103         return "";
104     }
105     return render->GetSubTitle();
106 }
107 
GetNavigationToolBar() const108 std::string NavigationComposedElement::GetNavigationToolBar() const
109 {
110     auto render = GetRenderNavigation();
111     auto jsonValue = JsonUtil::Create(true);
112     if (!render) {
113         return "";
114     }
115     auto toolBarItem = render->GetToolBarItems();
116     if (!toolBarItem.empty()) {
117         auto jsonOptions = JsonUtil::CreateArray(true);
118         std::list<RefPtr<ToolBarItem>>::iterator iter;
119         int32_t i = 0;
120         for (iter = toolBarItem.begin(); iter != toolBarItem.end(); ++iter, i++) {
121             auto jsonToolBarItem = JsonUtil::CreateArray(true);
122             auto toolBarItem_ = *iter;
123             jsonToolBarItem->Put("value", toolBarItem_->value.c_str());
124             jsonToolBarItem->Put("icon", toolBarItem_->icon.c_str());
125             auto index_ = std::to_string(i);
126             jsonOptions->Put(index_.c_str(), jsonToolBarItem);
127         }
128         jsonValue->Put("items", jsonOptions);
129         return jsonValue->ToString();
130     }
131     return "";
132 }
133 
GetNavigationSubTitleMode() const134 std::string NavigationComposedElement::GetNavigationSubTitleMode() const
135 {
136     auto node = GetInspectorNode(NavigationContainerElement::TypeId());
137     if (!node) {
138         return "NavigationTitleMode.Free";
139     }
140     auto renderNavigation = AceType::DynamicCast<RenderNavigationContainer>(node);
141     if (renderNavigation) {
142         if (renderNavigation->GetTitleMode() == NavigationTitleMode::MINI) {
143             return "NavigationTitleMode.Mini";
144         } else if (renderNavigation->GetTitleMode() == NavigationTitleMode::FREE) {
145             return "NavigationTitleMode.Free";
146         } else {
147             return "NavigationTitleMode.Full";
148         }
149     }
150     return "NavigationTitleMode.Free";
151 }
152 
GetHideNavigationBackButton() const153 bool NavigationComposedElement::GetHideNavigationBackButton() const
154 {
155     auto render = GetRenderNavigation();
156     if (!render) {
157         return false;
158     }
159     return render->GetHideBackButton();
160 }
161 
GetHideNavigationBar() const162 bool NavigationComposedElement::GetHideNavigationBar() const
163 {
164     auto render = GetRenderNavigation();
165     if (!render) {
166         return false;
167     }
168     return render->GetHideNavigationBar();
169 }
170 
GetNavigationHideToolBar() const171 bool NavigationComposedElement::GetNavigationHideToolBar() const
172 {
173     auto render = GetRenderNavigation();
174     if (!render) {
175         return false;
176     }
177     return render->GetHideNavigationToolBar();
178 }
179 
GetMenus() const180 std::unique_ptr<JsonValue> NavigationComposedElement::GetMenus() const
181 {
182     auto render = GetRenderNavigation();
183     auto menusArray = JsonUtil::CreateArray(true);
184     if (!render) {
185         return menusArray;
186     }
187     auto menuItems = render->GetMenuItems();
188     if (!menuItems.empty()) {
189         std::list<RefPtr<ToolBarItem>>::iterator iter;
190         int32_t i = 0;
191         for (iter = menuItems.begin(); iter != menuItems.end(); ++iter, i++) {
192             auto jsonMenusItem = JsonUtil::CreateArray(true);
193             auto menus = *iter;
194             jsonMenusItem->Put("value", menus->value.c_str());
195             jsonMenusItem->Put("icon", menus->icon.c_str());
196             auto index = std::to_string(i);
197             menusArray->Put(index.c_str(), jsonMenusItem);
198         }
199         return menusArray;
200     }
201     return menusArray;
202 }
203 
GetRenderNavigation() const204 RefPtr<RenderNavigationContainer> NavigationComposedElement::GetRenderNavigation() const
205 {
206     auto node = GetInspectorNode(NavigationContainerElement::TypeId());
207     if (!node) {
208         return nullptr;
209     }
210     return AceType::DynamicCast<RenderNavigationContainer>(node);
211 }
212 
213 } // namespace OHOS::Ace::V2