• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_grid_item.h"
17 
18 #include "base/log/ace_scoring_log.h"
19 #include "bridge/declarative_frontend/engine/functions/js_mouse_function.h"
20 #include "bridge/declarative_frontend/jsview/models/grid_item_model_impl.h"
21 #include "core/common/container.h"
22 #include "core/components_ng/base/view_stack_processor.h"
23 #include "core/components_ng/pattern/grid/grid_item_model_ng.h"
24 
25 namespace OHOS::Ace {
26 
27 std::unique_ptr<GridItemModel> GridItemModel::instance_ = nullptr;
28 std::mutex GridItemModel::mutex_;
29 
GetInstance()30 GridItemModel* GridItemModel::GetInstance()
31 {
32     if (!instance_) {
33         std::lock_guard<std::mutex> lock(mutex_);
34         if (!instance_) {
35 #ifdef NG_BUILD
36             instance_.reset(new NG::GridItemModelNG());
37 #else
38             if (Container::IsCurrentUseNewPipeline()) {
39                 instance_.reset(new NG::GridItemModelNG());
40             } else {
41                 instance_.reset(new Framework::GridItemModelImpl());
42             }
43 #endif
44         }
45     }
46     return instance_.get();
47 }
48 
49 } // namespace OHOS::Ace
50 
51 namespace OHOS::Ace::Framework {
52 
Create(const JSCallbackInfo & args)53 void JSGridItem::Create(const JSCallbackInfo& args)
54 {
55     if (Container::IsCurrentUsePartialUpdate()) {
56         CreateForPartialUpdate(args);
57         return;
58     }
59     GridItemModel::GetInstance()->Create(NG::GridItemStyle::NONE);
60 }
61 
CreateForPartialUpdate(const JSCallbackInfo & args)62 void JSGridItem::CreateForPartialUpdate(const JSCallbackInfo& args)
63 {
64     if (args.Length() < 2 || !args[0]->IsFunction() || !args[1]->IsBoolean()) {
65         LOGE("parameter not valid");
66         GridItemModel::GetInstance()->Create(NG::GridItemStyle::NONE);
67         return;
68     }
69 
70     auto isLazy = args[1]->ToBoolean();
71     auto jsDeepRender = AceType::MakeRefPtr<JsFunction>(args.This(), JSRef<JSFunc>::Cast(args[0]));
72     auto gridItemDeepRenderFunc = [execCtx = args.GetExecutionContext(), jsDeepRenderFunc = std::move(jsDeepRender)](
73                                       int32_t elmtId) {
74         ACE_SCOPED_TRACE("JSGridItem::ExecuteDeepRender");
75         ACE_DCHECK(componentsStack_.empty());
76         JAVASCRIPT_EXECUTION_SCOPE(execCtx);
77         JSRef<JSVal> jsParams[2];
78         jsParams[0] = JSRef<JSVal>::Make(ToJSValue(elmtId));
79         jsParams[1] = JSRef<JSVal>::Make(ToJSValue(true));
80         jsDeepRenderFunc->ExecuteJS(2, jsParams);
81     };
82 
83     NG::GridItemStyle gridItemStyle = NG::GridItemStyle::NONE;
84     bool versionControl = Container::LessThanAPIVersion(PlatformVersion::VERSION_ELEVEN);
85     if (!versionControl && args[2]->IsObject()) {
86         JSRef<JSObject> obj = JSRef<JSObject>::Cast(args[2]);
87         JSRef<JSVal> styleObj = obj->GetProperty("style");
88         gridItemStyle = styleObj->IsNumber() ? static_cast<NG::GridItemStyle>(styleObj->ToNumber<int32_t>())
89                                              : NG::GridItemStyle::NONE;
90     }
91     GridItemModel::GetInstance()->Create(std::move(gridItemDeepRenderFunc), isLazy, gridItemStyle);
92 }
93 
SetColumnStart(int32_t columnStart)94 void JSGridItem::SetColumnStart(int32_t columnStart)
95 {
96     GridItemModel::GetInstance()->SetColumnStart(columnStart);
97 }
98 
SetColumnEnd(int32_t columnEnd)99 void JSGridItem::SetColumnEnd(int32_t columnEnd)
100 {
101     GridItemModel::GetInstance()->SetColumnEnd(columnEnd);
102 }
103 
SetRowStart(int32_t rowStart)104 void JSGridItem::SetRowStart(int32_t rowStart)
105 {
106     GridItemModel::GetInstance()->SetRowStart(rowStart);
107 }
108 
SetRowEnd(int32_t rowEnd)109 void JSGridItem::SetRowEnd(int32_t rowEnd)
110 {
111     GridItemModel::GetInstance()->SetRowEnd(rowEnd);
112 }
113 
ForceRebuild(bool forceRebuild)114 void JSGridItem::ForceRebuild(bool forceRebuild)
115 {
116     GridItemModel::GetInstance()->SetForceRebuild(forceRebuild);
117 }
118 
SetSelectable(const JSCallbackInfo & info)119 void JSGridItem::SetSelectable(const JSCallbackInfo& info)
120 {
121     if (info.Length() < 1) {
122         return;
123     }
124     bool selectable = true;
125     if (info[0]->IsBoolean()) {
126         selectable = info[0]->ToBoolean();
127     }
128     GridItemModel::GetInstance()->SetSelectable(selectable);
129 }
130 
SelectCallback(const JSCallbackInfo & args)131 void JSGridItem::SelectCallback(const JSCallbackInfo& args)
132 {
133     if (!args[0]->IsFunction()) {
134         return;
135     }
136 
137     RefPtr<JsMouseFunction> jsOnSelectFunc = AceType::MakeRefPtr<JsMouseFunction>(JSRef<JSFunc>::Cast(args[0]));
138     WeakPtr<NG::FrameNode> targetNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
139     auto onSelectId = [execCtx = args.GetExecutionContext(), func = std::move(jsOnSelectFunc), node = targetNode](
140                           bool isSelected) {
141         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
142         PipelineContext::SetCallBackNode(node);
143         func->SelectExecute(isSelected);
144     };
145     GridItemModel::GetInstance()->SetOnSelect(std::move(onSelectId));
146 }
147 
SetSelected(const JSCallbackInfo & info)148 void JSGridItem::SetSelected(const JSCallbackInfo& info)
149 {
150     if (info.Length() < 1) {
151         return;
152     }
153     bool select = false;
154     JSRef<JSVal> changeEventVal;
155     auto selectedVal = info[0];
156     if (selectedVal->IsObject()) {
157         JSRef<JSObject> obj = JSRef<JSObject>::Cast(selectedVal);
158         selectedVal = obj->GetProperty("value");
159         changeEventVal = obj->GetProperty("$value");
160     } else if (info.Length() > 1) {
161         changeEventVal = info[1];
162     }
163     if (selectedVal->IsBoolean()) {
164         select = selectedVal->ToBoolean();
165     }
166     GridItemModel::GetInstance()->SetSelected(select);
167 
168     if (changeEventVal->IsFunction()) {
169         auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(changeEventVal));
170         auto targetNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
171         auto changeEvent = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc), node = targetNode](
172                                bool param) {
173             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
174             ACE_SCORING_EVENT("GridItem.ChangeEvent");
175             auto newJSVal = JSRef<JSVal>::Make(ToJSValue(param));
176             PipelineContext::SetCallBackNode(node);
177             func->ExecuteJS(1, &newJSVal);
178         };
179         GridItemModel::GetInstance()->SetSelectChangeEvent(std::move(changeEvent));
180     }
181 }
182 
JSBind(BindingTarget globalObj)183 void JSGridItem::JSBind(BindingTarget globalObj)
184 {
185     JSClass<JSGridItem>::Declare("GridItem");
186 
187     MethodOptions opt = MethodOptions::NONE;
188     JSClass<JSGridItem>::StaticMethod("create", &JSGridItem::Create, opt);
189     JSClass<JSGridItem>::StaticMethod("columnStart", &JSGridItem::SetColumnStart, opt);
190     JSClass<JSGridItem>::StaticMethod("columnEnd", &JSGridItem::SetColumnEnd, opt);
191     JSClass<JSGridItem>::StaticMethod("rowStart", &JSGridItem::SetRowStart, opt);
192     JSClass<JSGridItem>::StaticMethod("rowEnd", &JSGridItem::SetRowEnd, opt);
193     JSClass<JSGridItem>::StaticMethod("forceRebuild", &JSGridItem::ForceRebuild, opt);
194     JSClass<JSGridItem>::StaticMethod("selectable", &JSGridItem::SetSelectable, opt);
195     JSClass<JSGridItem>::StaticMethod("onSelect", &JSGridItem::SelectCallback);
196     JSClass<JSGridItem>::StaticMethod("width", &JSGridItem::SetGridItemWidth);
197     JSClass<JSGridItem>::StaticMethod("height", &JSGridItem::SetGridItemHeight);
198     JSClass<JSGridItem>::StaticMethod("selected", &JSGridItem::SetSelected);
199     JSClass<JSGridItem>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
200     JSClass<JSGridItem>::StaticMethod("onAttach", &JSInteractableView::JsOnAttach);
201     JSClass<JSGridItem>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
202     JSClass<JSGridItem>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
203     JSClass<JSGridItem>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
204     JSClass<JSGridItem>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
205     JSClass<JSGridItem>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
206     JSClass<JSGridItem>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
207     JSClass<JSGridItem>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
208     JSClass<JSGridItem>::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage);
209 
210     JSClass<JSGridItem>::InheritAndBind<JSContainerBase>(globalObj);
211 }
212 
213 } // namespace OHOS::Ace::Framework
214