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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_INDEXER_INDEXER_LIST_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_INDEXER_INDEXER_LIST_COMPONENT_H
18 
19 #include "core/components/indexer/indexer_component.h"
20 #include "core/components/list/list_component.h"
21 #include "core/components/stack/stack_component.h"
22 
23 namespace OHOS::Ace {
24 
25 class IndexerListComponent : public SoleChildComponent {
26     DECLARE_ACE_TYPE(IndexerListComponent, SoleChildComponent);
27 
28 public:
29     IndexerListComponent(const RefPtr<ListComponent>& list, bool circleMode = false, bool isRightToLeft = false,
30         bool bubble = true, bool multiLanguage = false)
31     {
32         if (list) {
33             list_ = list;
34             SetRightToLeft(isRightToLeft);
35             indexer_ = AceType::MakeRefPtr<IndexerComponent>(list_, circleMode, bubble, multiLanguage);
36             indexer_->SetController(list_->GetPositionController());
37             BuildChildren();
38             LOGI("[indexer] create ListComponent circle:%{public}d, RTL:%{public}d.", circleMode, isRightToLeft);
39         } else {
40             LOGE("[indexer] list is nullptr, create indexer failed.");
41         }
42     }
43 
44     IndexerListComponent(const RefPtr<ListComponent>& list, const std::vector<std::string>& label,
45         bool circleMode = false, bool isRightToLeft = false, bool bubble = true, bool multiLanguage = false)
46     {
47         if (list) {
48             list_ = list;
49             SetRightToLeft(isRightToLeft);
50             indexer_ = AceType::MakeRefPtr<IndexerComponent>(list_, circleMode, label, bubble, multiLanguage);
51             indexer_->SetController(list_->GetPositionController());
52             BuildChildren();
53             LOGI("[indexer] create ListComponent circle:%{public}d, RTL:%{public}d.", circleMode, isRightToLeft);
54         } else {
55             LOGE("[indexer] list is nullptr, create indexer failed.");
56         }
57     }
58 
59     ~IndexerListComponent() override = default;
60 
61     RefPtr<Element> CreateElement() override;
62     RefPtr<RenderNode> CreateRenderNode() override;
63 
64     void AppendChild(const RefPtr<Component>& child);
65     void InsertChild(uint32_t position, const RefPtr<Component>& child);
66     void RemoveChild(const RefPtr<Component>& child);
67 
68     void BuildChildren();
69 
SetIndexerChangeEvent(const EventMarker & indexChange)70     void SetIndexerChangeEvent(const EventMarker& indexChange)
71     {
72         if (indexer_) {
73             indexer_->SetIndexerChange(indexChange);
74         }
75     }
76 
GetIndexer()77     const RefPtr<IndexerComponent>& GetIndexer() const
78     {
79         return indexer_;
80     }
81 
GetItemCountInList()82     int32_t GetItemCountInList() const
83     {
84         if (!list_) {
85             return 0;
86         }
87         return list_->GetChildren().size();
88     }
89 
SetRightToLeft(bool isRightToLeft)90     void SetRightToLeft(bool isRightToLeft)
91     {
92         isRightToLeft_ = isRightToLeft;
93     }
94 
SetBubbleEnabled(bool enable)95     void SetBubbleEnabled(bool enable)
96     {
97         indexer_->SetBubbleEnabled(enable);
98     }
99 
100 private:
101     RefPtr<ListComponent> list_;
102     RefPtr<IndexerComponent> indexer_;
103     RefPtr<StackComponent> stack_;
104     bool isRightToLeft_ = false;
105 };
106 
107 } // namespace OHOS::Ace
108 
109 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_INDEXER_INDEXER_LIST_COMPONENT_H
110