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_item_model_impl.h"
17 
18 #include "base/memory/referenced.h"
19 #include "bridge/declarative_frontend/jsview/js_interactable_view.h"
20 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
21 #include "bridge/declarative_frontend/jsview/models/view_abstract_model_impl.h"
22 #include "core/components_v2/list/list_item_component.h"
23 
24 namespace OHOS::Ace::Framework {
25 
Create()26 void ListItemModelImpl::Create()
27 {
28     auto listItemComponent = AceType::MakeRefPtr<V2::ListItemComponent>();
29     ViewStackProcessor::GetInstance()->ClaimElementId(listItemComponent);
30     ViewStackProcessor::GetInstance()->Push(listItemComponent);
31     JSInteractableView::SetFocusable(true);
32     JSInteractableView::SetFocusNode(true);
33 }
34 
Create(std::function<void (int32_t)> && deepRenderFunc,V2::ListItemStyle listItemStyle)35 void ListItemModelImpl::Create(std::function<void(int32_t)>&& deepRenderFunc, V2::ListItemStyle listItemStyle)
36 {
37     auto listItemComponent = AceType::MakeRefPtr<V2::ListItemComponent>();
38     ViewStackProcessor::GetInstance()->ClaimElementId(listItemComponent);
39     V2::DeepRenderFunc listItemDeepRenderFunc = [jsDeepRenderFunc = std::move(deepRenderFunc),
40                                                     elmtId = listItemComponent->GetElementId()]() -> RefPtr<Component> {
41         ACE_SCOPED_TRACE("JSListItem::ExecuteDeepRender");
42 
43         jsDeepRenderFunc(elmtId);
44         RefPtr<Component> component = ViewStackProcessor::GetInstance()->Finish();
45         ACE_DCHECK(AceType::DynamicCast<V2::ListItemComponent>(component) != nullptr);
46         return component;
47     }; // listItemDeepRenderFunc lambda
48 
49     listItemComponent->SetDeepRenderFunc(listItemDeepRenderFunc);
50     ViewStackProcessor::GetInstance()->Push(listItemComponent);
51     JSInteractableView::SetFocusable(true);
52     JSInteractableView::SetFocusNode(true);
53 }
54 
SetBorderRadius(const Dimension & borderRadius)55 void ListItemModelImpl::SetBorderRadius(const Dimension& borderRadius)
56 {
57     JSViewSetProperty(&V2::ListItemComponent::SetBorderRadius, borderRadius);
58 }
59 
SetType(const std::string & type)60 void ListItemModelImpl::SetType(const std::string& type)
61 {
62     JSViewSetProperty(&V2::ListItemComponent::SetType, type);
63 }
64 
SetIsLazyCreating(bool isLazy)65 void ListItemModelImpl::SetIsLazyCreating(bool isLazy)
66 {
67     JSViewSetProperty(&V2::ListItemComponent::SetIsLazyCreating, isLazy);
68 }
69 
SetSticky(V2::StickyMode stickyMode)70 void ListItemModelImpl::SetSticky(V2::StickyMode stickyMode)
71 {
72     JSViewSetProperty(&V2::ListItemComponent::SetSticky, stickyMode);
73 }
74 
SetEditMode(uint32_t editMode)75 void ListItemModelImpl::SetEditMode(uint32_t editMode)
76 {
77     JSViewSetProperty(&V2::ListItemComponent::SetEditMode, editMode);
78     if ((V2::EditMode::MOVABLE & editMode) == 0) {
79         auto* stack = ViewStackProcessor::GetInstance();
80         auto box = stack->GetBoxComponent();
81         box->SetEnableDragStart(false);
82     }
83 }
84 
SetSelectable(bool selectable)85 void ListItemModelImpl::SetSelectable(bool selectable)
86 {
87     JSViewSetProperty(&V2::ListItemComponent::SetSelectable, selectable);
88 }
89 
90 void ListItemModelImpl::SetSwiperAction(std::function<void()>&& startAction, std::function<void()>&& endAction,
91     [[maybe_unused]] OnOffsetChangeFunc&& onOffsetChangeFunc, V2::SwipeEdgeEffect edgeEffect)
92 {
93     auto listItem = AceType::DynamicCast<V2::ListItemComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
94     if (!listItem) {
95         LOGW("Failed to get '%{public}s' in view stack", AceType::TypeName<V2::ListItemComponent>());
96         return;
97     }
98     if (startAction) {
99         ScopedViewStackProcessor builderViewStackProcessor;
100         startAction();
101         RefPtr<Component> customComponent = ViewStackProcessor::GetInstance()->Finish();
102         listItem->SetSwiperStartComponent(customComponent);
103     }
104     if (endAction) {
105         ScopedViewStackProcessor builderViewStackProcessor;
106         endAction();
107         RefPtr<Component> customComponent = ViewStackProcessor::GetInstance()->Finish();
108         listItem->SetSwiperEndComponent(customComponent);
109     }
110     listItem->SetEdgeEffect(edgeEffect);
111 }
112 
SetSelectCallback(OnSelectFunc && selectCallback)113 void ListItemModelImpl::SetSelectCallback(OnSelectFunc&& selectCallback)
114 {
115     JSViewSetProperty(&V2::ListItemComponent::SetOnSelectId, std::move(selectCallback));
116 }
117 
SetDeleteArea(std::function<void ()> && builderAction,OnDeleteEvent && onDelete,OnEnterDeleteAreaEvent && onEnterDeleteArea,OnExitDeleteAreaEvent && onExitDeleteArea,OnStateChangedEvent && onStateChange,const Dimension & length,bool isStartArea)118 void ListItemModelImpl::SetDeleteArea(std::function<void()>&& builderAction, OnDeleteEvent&& onDelete,
119     OnEnterDeleteAreaEvent&& onEnterDeleteArea, OnExitDeleteAreaEvent&& onExitDeleteArea,
120     OnStateChangedEvent&& onStateChange, const Dimension& length, bool isStartArea) {};
121 
SetOnDragStart(NG::OnDragStartFunc && onDragStart)122 void ListItemModelImpl::SetOnDragStart(NG::OnDragStartFunc&& onDragStart)
123 {
124     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
125     box->SetOnDragStartId(ViewAbstractModelImpl::ToDragFunc(std::move(onDragStart)));
126     JSViewSetProperty(&V2::ListItemComponent::MarkIsDragStart, true);
127 }
128 
129 } // namespace OHOS::Ace::Framework
130