• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/js_list_item_group.h"
17 
18 #include "bridge/declarative_frontend/jsview/js_list_item.h"
19 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
20 #include "bridge/declarative_frontend/jsview/models/list_item_group_model_impl.h"
21 #include "bridge/declarative_frontend/view_stack_processor.h"
22 #include "core/components_v2/list/list_item_group_component.h"
23 #include "core/components_ng/pattern/list/list_item_group_model.h"
24 #include "core/components_ng/pattern/list/list_item_group_model_ng.h"
25 
26 namespace OHOS::Ace {
27 
28 std::unique_ptr<ListItemGroupModel> ListItemGroupModel::instance_ = nullptr;
29 std::mutex ListItemGroupModel::mutex_;
30 
GetInstance()31 ListItemGroupModel* ListItemGroupModel::GetInstance()
32 {
33     if (!instance_) {
34         std::lock_guard<std::mutex> lock(mutex_);
35         if (!instance_) {
36 #ifdef NG_BUILD
37             instance_.reset(new NG::ListItemGroupModelNG());
38 #else
39             if (Container::IsCurrentUseNewPipeline()) {
40                 instance_.reset(new NG::ListItemGroupModelNG());
41             } else {
42                 instance_.reset(new Framework::ListItemGroupModelImpl());
43             }
44 #endif
45         }
46     }
47     return instance_.get();
48 }
49 
50 } // namespace OHOS::Ace
51 namespace OHOS::Ace::Framework {
52 
Create(const JSCallbackInfo & args)53 void JSListItemGroup::Create(const JSCallbackInfo& args)
54 {
55     V2::ListItemGroupStyle listItemGroupStyle = V2::ListItemGroupStyle::NONE;
56     if (args.Length() >= 1 && args[0]->IsObject()) {
57         JSRef<JSObject> obj = JSRef<JSObject>::Cast(args[0]);
58         auto styleObject = obj->GetProperty("style");
59         listItemGroupStyle = styleObject->IsNumber()
60                                  ? static_cast<V2::ListItemGroupStyle>(styleObject->ToNumber<int32_t>())
61                                  : V2::ListItemGroupStyle::NONE;
62     }
63     ListItemGroupModel::GetInstance()->Create(listItemGroupStyle);
64     if (args.Length() >= 1 && args[0]->IsObject()) {
65         JSRef<JSObject> obj = JSRef<JSObject>::Cast(args[0]);
66 
67         Dimension space;
68         if (ConvertFromJSValue(obj->GetProperty("space"), space) && space.IsValid()) {
69             ListItemGroupModel::GetInstance()->SetSpace(space);
70         }
71 
72         auto headerObject = obj->GetProperty("header");
73         if (headerObject->IsFunction()) {
74             auto builderFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSFunc>::Cast(headerObject));
75             auto headerAction = [builderFunc]() { builderFunc->Execute(); };
76             ListItemGroupModel::GetInstance()->SetHeader(headerAction);
77         }
78 
79         auto footerObject = obj->GetProperty("footer");
80         if (footerObject->IsFunction()) {
81             auto builderFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSFunc>::Cast(footerObject));
82             auto footerAction = [builderFunc]() { builderFunc->Execute(); };
83             ListItemGroupModel::GetInstance()->SetFooter(footerAction);
84         }
85     }
86     args.ReturnSelf();
87 }
88 
SetDivider(const JSCallbackInfo & args)89 void JSListItemGroup::SetDivider(const JSCallbackInfo& args)
90 {
91     V2::ItemDivider divider;
92     if (args.Length() >= 1 && args[0]->IsObject()) {
93         JSRef<JSObject> obj = JSRef<JSObject>::Cast(args[0]);
94         if (!ConvertFromJSValue(obj->GetProperty("strokeWidth"), divider.strokeWidth)) {
95             LOGW("Invalid strokeWidth of divider");
96             divider.strokeWidth.Reset();
97         }
98         if (!ConvertFromJSValue(obj->GetProperty("color"), divider.color)) {
99             // Failed to get color from param, using default color defined in theme
100             RefPtr<ListTheme> listTheme = GetTheme<ListTheme>();
101             if (listTheme) {
102                 divider.color = listTheme->GetDividerColor();
103             }
104         }
105         ConvertFromJSValue(obj->GetProperty("startMargin"), divider.startMargin);
106         ConvertFromJSValue(obj->GetProperty("endMargin"), divider.endMargin);
107     }
108     ListItemGroupModel::GetInstance()->SetDivider(divider);
109     args.ReturnSelf();
110 }
111 
SetAspectRatio(const JSCallbackInfo & args)112 void JSListItemGroup::SetAspectRatio(const JSCallbackInfo& args)
113 {
114 }
115 
JSBind(BindingTarget globalObj)116 void JSListItemGroup::JSBind(BindingTarget globalObj)
117 {
118     JSClass<JSListItemGroup>::Declare("ListItemGroup");
119     JSClass<JSListItemGroup>::StaticMethod("create", &JSListItemGroup::Create);
120 
121     JSClass<JSListItemGroup>::StaticMethod("divider", &JSListItemGroup::SetDivider);
122     JSClass<JSListItemGroup>::StaticMethod("aspectRatio", &JSListItemGroup::SetAspectRatio);
123     JSClass<JSListItemGroup>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
124     JSClass<JSListItemGroup>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
125     JSClass<JSListItemGroup>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
126 
127     JSClass<JSListItemGroup>::Inherit<JSInteractableView>();
128     JSClass<JSListItemGroup>::InheritAndBind<JSContainerBase>(globalObj);
129 }
130 
131 } // namespace OHOS::Ace::Framework
132