• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 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_NG_SYNTAX_FOR_EACH_NODE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SYNTAX_FOR_EACH_NODE_H
18 
19 #include <cstdint>
20 #include <list>
21 #include <string>
22 #include <type_traits>
23 
24 #include "base/utils/macros.h"
25 #include "core/components_ng/base/ui_node.h"
26 #include "core/components_ng/syntax/for_each_base_node.h"
27 #include "core/components_v2/inspector/inspector_constants.h"
28 
29 namespace OHOS::Ace::NG {
30 
31 class ACE_EXPORT ForEachNode : public ForEachBaseNode {
32     DECLARE_ACE_TYPE(ForEachNode, ForEachBaseNode);
33 
34 public:
35     static RefPtr<ForEachNode> GetOrCreateForEachNode(int32_t nodeId);
36     static RefPtr<ForEachNode> GetOrCreateRepeatNode(int32_t nodeId);
37 
ForEachNode(int32_t nodeId)38     explicit ForEachNode(int32_t nodeId) : ForEachBaseNode(V2::JS_FOR_EACH_ETS_TAG, nodeId) {}
39 
40     ~ForEachNode() override = default;
41 
IsAtomicNode()42     bool IsAtomicNode() const override
43     {
44         return false;
45     }
46 
47     void CreateTempItems();
48 
49     void CollectRemovingIds(std::list<int32_t>& removedElmtId);
50 
51     void CompareAndUpdateChildren();
52 
53     void FlushUpdateAndMarkDirty() override;
54 
55     // RepeatNode only
56     void FinishRepeatRender(std::list<int32_t>& removedElmtId);
57 
58     // RepeatNode only
59     void MoveChild(uint32_t fromIndex);
60 
GetTempIds()61     const std::list<std::string>& GetTempIds() const
62     {
63         return tempIds_;
64     }
65 
SetIds(std::list<std::string> && ids)66     void SetIds(std::list<std::string>&& ids)
67     {
68         ids_ = std::move(ids);
69     }
70 
71     void SetItemDragHandler(std::function<void(int32_t)>&& onLongPress, std::function<void(int32_t)>&& onDragStart,
72         std::function<void(int32_t, int32_t)>&& onMoveThrough, std::function<void(int32_t)>&& onDrop);
73     void SetOnMove(std::function<void(int32_t, int32_t)>&& onMove);
74     void MoveData(int32_t from, int32_t to) override;
75     RefPtr<FrameNode> GetFrameNode(int32_t index) override;
76     void InitDragManager(const RefPtr<UINode>& childNode);
77     void InitAllChildrenDragManager(bool init);
78     void MappingChildWithId(std::unordered_set<std::string>& oldIdsSet, std::list<RefPtr<UINode>>& additionalChildComps,
79         std::map<std::string, RefPtr<UINode>>& oldNodeByIdMap);
80 private:
81     std::list<std::string> ids_;
82 
83     // temp items use to compare each update.
84     std::list<std::string> tempIds_;
85     std::list<RefPtr<UINode>> tempChildren_;
86     std::unordered_set<std::string> tempOldIdsSet_;
87 
88     // create map id -> Node
89     std::map<std::string, RefPtr<UINode>> oldNodeByIdMap_;
90 
91     // RepeatNode only
92     std::vector<RefPtr<UINode>> tempChildrenOfRepeat_;
93 
94     // true when this is actually RepeatNode (not "ForEach")
95     bool isThisRepeatNode_ = false;
96 
97     ACE_DISALLOW_COPY_AND_MOVE(ForEachNode);
98 };
99 
100 } // namespace OHOS::Ace::NG
101 
102 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SYNTAX_FOR_EACH_NODE_H
103