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 "core/components_v2/inspector/search_composed_element.h"
17 
18 #include <unordered_map>
19 
20 #include "core/components/search/search_element.h"
21 #include "core/components_v2/inspector/utils.h"
22 
23 #include "base/log/dump_log.h"
24 
25 namespace OHOS::Ace::V2 {
26 
27 const std::unordered_map<std::string, std::function<std::string(const SearchComposedElement&)>> CREATE_JSON_MAP {
__anon00b565720102(const SearchComposedElement& inspector) 28     { "icon", [](const SearchComposedElement& inspector) { return inspector.GetIcon(); } },
__anon00b565720202(const SearchComposedElement& inspector) 29     { "searchButton", [](const SearchComposedElement& inspector) { return inspector.GetSearchButton(); } },
__anon00b565720302(const SearchComposedElement& inspector) 30     { "placeholderColor", [](const SearchComposedElement& inspector) { return inspector.GetColor(); } }
31 };
32 
Dump(void)33 void SearchComposedElement::Dump(void)
34 {
35     InspectorComposedElement::Dump();
36     DumpLog::GetInstance().AddDesc(std::string("search_composed_element"));
37     DumpLog::GetInstance().AddDesc(
38         std::string("icon: ").append(GetIcon()));
39     DumpLog::GetInstance().AddDesc(
40         std::string("searchButton: ").append(GetSearchButton()));
41 }
42 
ToJsonObject() const43 std::unique_ptr<OHOS::Ace::JsonValue> SearchComposedElement::ToJsonObject() const
44 {
45     auto resultJson = InspectorComposedElement::ToJsonObject();
46     for (const auto& value : CREATE_JSON_MAP) {
47         resultJson->Put(value.first.c_str(), value.second(*this).c_str());
48     }
49     return resultJson;
50 }
51 
GetIcon(void) const52 std::string SearchComposedElement::GetIcon(void) const
53 {
54     auto renderSearch = GetRenderSearch();
55     std::string icon = renderSearch ? renderSearch->GetSearchComponent()->GetCloseIconSrc() : "";
56     return icon;
57 }
58 
GetSearchButton(void) const59 std::string SearchComposedElement::GetSearchButton(void) const
60 {
61     auto renderSearch = GetRenderSearch();
62     std::string searchButton = renderSearch ? renderSearch->GetSearchComponent()->GetSearchText() : "";
63     return searchButton;
64 }
65 
GetColor(void) const66 std::string SearchComposedElement::GetColor(void) const
67 {
68     auto renderSearch = GetRenderSearch();
69     if (!renderSearch) {
70         return "";
71     }
72     auto component = renderSearch->GetSearchComponent();
73     if (!component) {
74         return "";
75     }
76     auto textFieldComponent = AceType::DynamicCast<TextFieldComponent>(component->GetChild());
77     if (!textFieldComponent) {
78         return "";
79     }
80     return textFieldComponent->GetPlaceholderColor().ColorToString();
81 }
82 
GetRenderSearch() const83 OHOS::Ace::RefPtr<OHOS::Ace::RenderSearch> SearchComposedElement::GetRenderSearch() const
84 {
85     auto node = GetInspectorNode(SearchElement::TypeId());
86     if (node) {
87         return AceType::DynamicCast<RenderSearch>(node);
88     }
89     return nullptr;
90 }
91 
92 } // namespace OHOS::Ace::V2
93