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_BRIDGE_COMMON_DOM_DOM_LIST_ITEM_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_DOM_DOM_LIST_ITEM_H
18 
19 #include "core/components/list/list_item_component.h"
20 #include "core/components/theme/card_theme.h"
21 #include "frameworks/bridge/common/dom/dom_node.h"
22 #include "frameworks/bridge/common/dom/dom_type.h"
23 
24 namespace OHOS::Ace::Framework {
25 
26 class DOMListItem : public DOMNode {
27     DECLARE_ACE_TYPE(DOMListItem, DOMNode);
28 
29 public:
30     DOMListItem(NodeId nodeId, const std::string& nodeName, int32_t forIndex);
31     ~DOMListItem() override = default;
32 
33     RefPtr<Component> GetSpecializedComponent() override;
34 
GetItemIndex()35     virtual int32_t GetItemIndex() const
36     {
37         return itemIndex_;
38     }
39 
GetItemKey()40     const std::string& GetItemKey() const
41     {
42         return indexKey_;
43     }
44 
GetListItemComponent()45     const RefPtr<ListItemComponent>& GetListItemComponent() const
46     {
47         return listItemComponent_;
48     }
49 
GetDOMListItem(const RefPtr<DOMNode> & node)50     static RefPtr<DOMListItem> GetDOMListItem(const RefPtr<DOMNode>& node)
51     {
52         RefPtr<DOMNode> domNode = node;
53         while (domNode) {
54             auto listItem = AceType::DynamicCast<DOMListItem>(domNode);
55             if (listItem) {
56                 return listItem;
57             }
58             if (domNode->GetChildList().empty()) {
59                 return nullptr;
60             }
61             domNode = domNode->GetChildList().front();
62         }
63         return nullptr;
64     }
65 
66     NodeId GetDirtyNodeId() const override;
67 
68 protected:
69     bool SetSpecializedAttr(const std::pair<std::string, std::string>& attr) override;
70     bool SetSpecializedStyle(const std::pair<std::string, std::string>& style) override;
71     bool AddSpecializedEvent(int32_t pageId, const std::string& event) override;
72     void OnChildNodeAdded(const RefPtr<DOMNode>& child, int32_t slot) override;
73     void OnChildNodeRemoved(const RefPtr<DOMNode>& child) override;
74     void OnMounted(const RefPtr<DOMNode>& parentNode) override;
75     void PrepareSpecializedComponent() override;
76     RefPtr<Component> CompositeSpecializedComponent(const std::vector<RefPtr<SingleChild>>& components) override;
77     void ResetInitializedStyle() override;
78 
IsSubscriptEnable()79     bool IsSubscriptEnable() const override
80     {
81         if (declaration_) {
82             declaration_->SetIsSubscriptEnable(true);
83         }
84         return true;
85     }
86 
87     std::string type_;
88     Radius topLeftRadius_;
89     Radius topRightRadius_;
90     Radius bottomLeftRadius_;
91     Radius bottomRightRadius_;
92     StickyMode stickyMode_ = StickyMode::NONE;
93 
94 private:
95     void AddListItem(const RefPtr<DOMNode>& node, int32_t slot);
96     void RemoveListItem(const RefPtr<DOMNode>& node);
97     void SetCardThemeAttrs();
98     void SetDividerStyle(const RefPtr<DOMNode>& parentNode);
99     void SetCardTransitionEffect();
100 
101     // common variables
102     int32_t columnSpan_ = DEFAULT_COLUMN_SPAN;
103     // If the item is generated by for loop, its index would be increased from 0.
104     int32_t itemIndex_ = -1;
105     std::string indexKey_;
106     int32_t key_ = -1;
107 
108     // list-item's variables
109     Alignment alignment_ { Alignment::TOP_LEFT };
110     FlexDirection flexDirection_ { FlexDirection::ROW };
111     FlexAlign flexMainAlign_ { FlexAlign::FLEX_START };
112     FlexAlign flexCrossAlign_ { FlexAlign::FLEX_START };
113     FlexAlign alignSelf_ { FlexAlign::AUTO };
114     RefPtr<ListItemComponent> listItemComponent_;
115     RefPtr<FlexComponent> flexComponent_;
116     RefPtr<DisplayComponent> displayComponent_;
117     bool sticky_ = false;
118     bool isTitle_ = false;
119     Dimension stickyRadius_;
120     bool clickEffect_ = true;
121     bool primary_ = false;
122     bool isActive_ = false;
123     Color clickColor_ = Color::TRANSPARENT;
124 
125     EventMarker clickEventId_;
126     EventMarker stickyEventId_;
127     RefPtr<CardTheme> cardTheme_;
128     bool isCardBlur_ = false;
129     bool isCard_ = false;
130 };
131 
132 } // namespace OHOS::Ace::Framework
133 
134 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_DOM_DOM_LIST_ITEM_H
135