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)35 void ListItemModelImpl::Create(std::function<void(int32_t)>&& deepRenderFunc)
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 LOGD("ListItem elmtId %{public}d DeepRender JS function execution start ....", elmtId);
44 jsDeepRenderFunc(elmtId);
45 RefPtr<Component> component = ViewStackProcessor::GetInstance()->Finish();
46 ACE_DCHECK(AceType::DynamicCast<V2::ListItemComponent>(component) != nullptr);
47 LOGD("ListItem elmtId %{public}d DeepRender JS function execution - done ", elmtId);
48 return component;
49 }; // listItemDeepRenderFunc lambda
50
51 listItemComponent->SetDeepRenderFunc(listItemDeepRenderFunc);
52 ViewStackProcessor::GetInstance()->Push(listItemComponent);
53 JSInteractableView::SetFocusable(true);
54 JSInteractableView::SetFocusNode(true);
55 }
56
SetBorderRadius(const Dimension & borderRadius)57 void ListItemModelImpl::SetBorderRadius(const Dimension& borderRadius)
58 {
59 JSViewSetProperty(&V2::ListItemComponent::SetBorderRadius, borderRadius);
60 }
61
SetType(const std::string & type)62 void ListItemModelImpl::SetType(const std::string& type)
63 {
64 JSViewSetProperty(&V2::ListItemComponent::SetType, type);
65 }
66
SetIsLazyCreating(bool isLazy)67 void ListItemModelImpl::SetIsLazyCreating(bool isLazy)
68 {
69 JSViewSetProperty(&V2::ListItemComponent::SetIsLazyCreating, isLazy);
70 }
71
SetSticky(V2::StickyMode stickyMode)72 void ListItemModelImpl::SetSticky(V2::StickyMode stickyMode)
73 {
74 JSViewSetProperty(&V2::ListItemComponent::SetSticky, stickyMode);
75 }
76
SetEditMode(uint32_t editMode)77 void ListItemModelImpl::SetEditMode(uint32_t editMode)
78 {
79 JSViewSetProperty(&V2::ListItemComponent::SetEditMode, editMode);
80 if ((V2::EditMode::MOVABLE & editMode) == 0) {
81 auto* stack = ViewStackProcessor::GetInstance();
82 auto box = stack->GetBoxComponent();
83 box->SetEnableDragStart(false);
84 }
85 }
86
SetSelectable(bool selectable)87 void ListItemModelImpl::SetSelectable(bool selectable)
88 {
89 JSViewSetProperty(&V2::ListItemComponent::SetSelectable, selectable);
90 }
91
SetSwiperAction(std::function<void ()> && startAction,std::function<void ()> && endAction,V2::SwipeEdgeEffect edgeEffect)92 void ListItemModelImpl::SetSwiperAction(
93 std::function<void()>&& startAction, std::function<void()>&& endAction, V2::SwipeEdgeEffect edgeEffect)
94 {
95 auto listItem = AceType::DynamicCast<V2::ListItemComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
96 if (!listItem) {
97 LOGW("Failed to get '%{public}s' in view stack", AceType::TypeName<V2::ListItemComponent>());
98 return;
99 }
100 if (startAction) {
101 ScopedViewStackProcessor builderViewStackProcessor;
102 startAction();
103 RefPtr<Component> customComponent = ViewStackProcessor::GetInstance()->Finish();
104 listItem->SetSwiperStartComponent(customComponent);
105 }
106 if (endAction) {
107 ScopedViewStackProcessor builderViewStackProcessor;
108 endAction();
109 RefPtr<Component> customComponent = ViewStackProcessor::GetInstance()->Finish();
110 listItem->SetSwiperEndComponent(customComponent);
111 }
112 listItem->SetEdgeEffect(edgeEffect);
113 }
114
SetSelectCallback(OnSelectFunc && selectCallback)115 void ListItemModelImpl::SetSelectCallback(OnSelectFunc&& selectCallback)
116 {
117 JSViewSetProperty(&V2::ListItemComponent::SetOnSelectId, std::move(selectCallback));
118 }
119
SetOnDragStart(NG::OnDragStartFunc && onDragStart)120 void ListItemModelImpl::SetOnDragStart(NG::OnDragStartFunc&& onDragStart)
121 {
122 auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
123 box->SetOnDragStartId(ViewAbstractModelImpl::ToDragFunc(std::move(onDragStart)));
124 JSViewSetProperty(&V2::ListItemComponent::MarkIsDragStart, true);
125 }
126
127 } // namespace OHOS::Ace::Framework
128