1 /*
2  * Copyright (c) 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 "bridge/declarative_frontend/jsview/models/list_model_impl.h"
17 
18 #include <memory>
19 
20 #include "base/memory/referenced.h"
21 #include "bridge/declarative_frontend/jsview/js_interactable_view.h"
22 #include "bridge/declarative_frontend/jsview/js_scroller.h"
23 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
24 #include "core/components_v2/list/list_position_controller.h"
25 #include "core/components_v2/list/list_properties.h"
26 
27 namespace OHOS::Ace::Framework {
28 
Create()29 void ListModelImpl::Create()
30 {
31     auto listComponent = AceType::MakeRefPtr<V2::ListComponent>();
32     ViewStackProcessor::GetInstance()->ClaimElementId(listComponent);
33     ViewStackProcessor::GetInstance()->Push(listComponent);
34     JSInteractableView::SetFocusable(true);
35     JSInteractableView::SetFocusNode(true);
36 }
37 
SetSpace(const Dimension & space)38 void ListModelImpl::SetSpace(const Dimension& space)
39 {
40     JSViewSetProperty(&V2::ListComponent::SetSpace, space);
41 }
42 
SetInitialIndex(int32_t initialIndex)43 void ListModelImpl::SetInitialIndex(int32_t initialIndex)
44 {
45     JSViewSetProperty(&V2::ListComponent::SetInitialIndex, initialIndex);
46 }
47 
CreateScrollController()48 RefPtr<ScrollControllerBase> ListModelImpl::CreateScrollController()
49 {
50     return AceType::MakeRefPtr<V2::ListPositionController>();
51 }
52 
SetScroller(RefPtr<ScrollControllerBase> scroller,RefPtr<ScrollProxy> proxy)53 void ListModelImpl::SetScroller(RefPtr<ScrollControllerBase> scroller, RefPtr<ScrollProxy> proxy)
54 {
55     auto listScroller = AceType::DynamicCast<V2::ListPositionController>(scroller);
56     JSViewSetProperty(&V2::ListComponent::SetScrollController, listScroller);
57     auto scrollBarProxy = AceType::DynamicCast<ScrollBarProxy>(proxy);
58     JSViewSetProperty(&V2::ListComponent::SetScrollBarProxy, scrollBarProxy);
59 }
60 
SetListDirection(Axis axis)61 void ListModelImpl::SetListDirection(Axis axis)
62 {
63     JSViewSetProperty(&V2::ListComponent::SetDirection, axis);
64 }
65 
SetScrollBar(DisplayMode scrollBar)66 void ListModelImpl::SetScrollBar(DisplayMode scrollBar)
67 {
68     JSViewSetProperty(&V2::ListComponent::SetScrollBar, scrollBar);
69 }
70 
SetEdgeEffect(EdgeEffect edgeEffect,bool alwaysEnabled)71 void ListModelImpl::SetEdgeEffect(EdgeEffect edgeEffect, bool alwaysEnabled)
72 {
73     JSViewSetProperty(&V2::ListComponent::SetEdgeEffect, edgeEffect);
74 }
75 
SetEditMode(bool editMode)76 void ListModelImpl::SetEditMode(bool editMode)
77 {
78     JSViewSetProperty(&V2::ListComponent::SetEditMode, editMode);
79 }
80 
SetListItemAlign(V2::ListItemAlign listItemAlign)81 void ListModelImpl::SetListItemAlign(V2::ListItemAlign listItemAlign)
82 {
83     JSViewSetProperty(&V2::ListComponent::SetListItemAlign, listItemAlign);
84 }
85 
SetDivider(const V2::ItemDivider & divider)86 void ListModelImpl::SetDivider(const V2::ItemDivider& divider)
87 {
88     auto dividerPtr = std::make_unique<V2::ItemDivider>(divider);
89     JSViewSetProperty(&V2::ListComponent::SetItemDivider, std::move(dividerPtr));
90 }
91 
SetChainAnimation(bool enableChainAnimation)92 void ListModelImpl::SetChainAnimation(bool enableChainAnimation)
93 {
94     JSViewSetProperty(&V2::ListComponent::SetChainAnimation, enableChainAnimation);
95 }
96 
SetLanes(int32_t lanes)97 void ListModelImpl::SetLanes(int32_t lanes)
98 {
99     JSViewSetProperty(&V2::ListComponent::SetLanes, lanes);
100 }
101 
SetLaneConstrain(const Dimension & laneMinLength,const Dimension & laneMaxLength)102 void ListModelImpl::SetLaneConstrain(const Dimension& laneMinLength, const Dimension& laneMaxLength)
103 {
104     auto component = AceType::DynamicCast<V2::ListComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
105     if (!component) {
106         LOGW("Failed to get ListComponent in view stack");
107         return;
108     }
109     component->SetLaneConstrain(laneMinLength, laneMaxLength);
110 }
111 
SetCachedCount(int32_t cachedCount,bool show)112 void ListModelImpl::SetCachedCount(int32_t cachedCount, bool show)
113 {
114     JSViewSetProperty(&V2::ListComponent::SetCachedCount, cachedCount);
115 }
116 
SetMultiSelectable(bool selectable)117 void ListModelImpl::SetMultiSelectable(bool selectable)
118 {
119     JSViewSetProperty(&V2::ListComponent::SetMultiSelectable, selectable);
120 }
121 
SetHasWidth(bool hasWidth)122 void ListModelImpl::SetHasWidth(bool hasWidth)
123 {
124     JSViewSetProperty(&V2::ListComponent::SetHasWidth, hasWidth);
125 }
126 
SetHasHeight(bool hasHeight)127 void ListModelImpl::SetHasHeight(bool hasHeight)
128 {
129     JSViewSetProperty(&V2::ListComponent::SetHasHeight, hasHeight);
130 }
131 
SetSticky(V2::StickyStyle stickyStyle)132 void ListModelImpl::SetSticky(V2::StickyStyle stickyStyle)
133 {
134     JSViewSetProperty(&V2::ListComponent::SetSticky, stickyStyle);
135 }
136 
SetContentStartOffset(float startOffset)137 void ListModelImpl::SetContentStartOffset(float startOffset)
138 {
139     JSViewSetProperty(&V2::ListComponent::SetContentStartOffset, startOffset);
140 }
141 
SetContentEndOffset(float endOffset)142 void ListModelImpl::SetContentEndOffset(float endOffset)
143 {
144     JSViewSetProperty(&V2::ListComponent::SetContentEndOffset, endOffset);
145 }
146 
SetOnScroll(OnScrollEvent && onScroll)147 void ListModelImpl::SetOnScroll(OnScrollEvent&& onScroll)
148 {
149     JSViewSetProperty(&V2::ListComponent::SetOnScroll, std::move(onScroll));
150 }
151 
SetOnScrollBegin(OnScrollBeginEvent && onScrollBegin)152 void ListModelImpl::SetOnScrollBegin(OnScrollBeginEvent&& onScrollBegin)
153 {
154     JSViewSetProperty(&V2::ListComponent::SetOnScrollBegin, std::move(onScrollBegin));
155 }
156 
SetOnScrollFrameBegin(OnScrollFrameBeginEvent && onScrollFrameBegin)157 void ListModelImpl::SetOnScrollFrameBegin(OnScrollFrameBeginEvent&& onScrollFrameBegin)
158 {
159 }
160 
SetOnScrollStop(OnScrollStopEvent && onScrollStop)161 void ListModelImpl::SetOnScrollStop(OnScrollStopEvent&& onScrollStop)
162 {
163     JSViewSetProperty(&V2::ListComponent::SetOnScrollStop, std::move(onScrollStop));
164 }
165 
SetOnScrollIndex(OnScrollIndexEvent && onScrollIndex)166 void ListModelImpl::SetOnScrollIndex(OnScrollIndexEvent&& onScrollIndex)
167 {
168     JSViewSetProperty(&V2::ListComponent::SetOnScrollIndex, std::move(onScrollIndex));
169 }
170 
SetOnReachStart(OnReachEvent && onReachStart)171 void ListModelImpl::SetOnReachStart(OnReachEvent&& onReachStart)
172 {
173     JSViewSetProperty(&V2::ListComponent::SetOnReachStart, std::move(onReachStart));
174 }
175 
SetOnReachEnd(OnReachEvent && onReachEnd)176 void ListModelImpl::SetOnReachEnd(OnReachEvent&& onReachEnd)
177 {
178     JSViewSetProperty(&V2::ListComponent::SetOnReachEnd, std::move(onReachEnd));
179 }
180 
SetOnItemDelete(OnItemDeleteEvent && onItemDelete)181 void ListModelImpl::SetOnItemDelete(OnItemDeleteEvent&& onItemDelete)
182 {
183     JSViewSetProperty(&V2::ListComponent::SetOnItemDelete, std::move(onItemDelete));
184 }
185 
SetOnItemMove(OnItemMoveEvent && onItemMove)186 void ListModelImpl::SetOnItemMove(OnItemMoveEvent&& onItemMove)
187 {
188     JSViewSetProperty(&V2::ListComponent::SetOnItemMove, std::move(onItemMove));
189 }
190 
SetOnItemDragStart(OnItemDragStartFunc && onItemDragStart)191 void ListModelImpl::SetOnItemDragStart(OnItemDragStartFunc&& onItemDragStart)
192 {
193     JSViewSetProperty(&V2::ListComponent::SetOnItemDragStartId, std::move(onItemDragStart));
194 }
195 
SetOnItemDragEnter(OnItemDragEnterFunc && onItemDragEnter)196 void ListModelImpl::SetOnItemDragEnter(OnItemDragEnterFunc&& onItemDragEnter)
197 {
198     JSViewSetProperty(&V2::ListComponent::SetOnItemDragEnterId, std::move(onItemDragEnter));
199 }
200 
SetOnItemDragMove(OnItemDragMoveFunc && onItemDragMove)201 void ListModelImpl::SetOnItemDragMove(OnItemDragMoveFunc&& onItemDragMove)
202 {
203     JSViewSetProperty(&V2::ListComponent::SetOnItemDragMoveId, std::move(onItemDragMove));
204 }
205 
SetOnItemDragLeave(OnItemDragLeaveFunc && onItemDragLeave)206 void ListModelImpl::SetOnItemDragLeave(OnItemDragLeaveFunc&& onItemDragLeave)
207 {
208     JSViewSetProperty(&V2::ListComponent::SetOnItemDragLeaveId, std::move(onItemDragLeave));
209 }
210 
SetOnItemDrop(OnItemDropFunc && onItemDrop)211 void ListModelImpl::SetOnItemDrop(OnItemDropFunc&& onItemDrop)
212 {
213     JSViewSetProperty(&V2::ListComponent::SetOnItemDropId, std::move(onItemDrop));
214 }
215 
216 } // namespace OHOS::Ace::Framework
217