1 /*
2 * Copyright (c) 2023 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 "frameworks/bridge/declarative_frontend/engine/functions/js_navigation_function.h"
17
18 #include "js_navigation_function.h"
19
20 namespace OHOS::Ace::Framework {
21
JSBind(BindingTarget globalObj)22 void JsNavigationTransitionProxy::JSBind(BindingTarget globalObj)
23 {
24 JSClass<JsNavigationTransitionProxy>::Declare("NavigationTransitionProxy");
25 JSClass<JsNavigationTransitionProxy>::CustomProperty(
26 "from", &JsNavigationTransitionProxy::GetFromContentInfo, &JsNavigationTransitionProxy::SetFromContentInfo);
27 JSClass<JsNavigationTransitionProxy>::CustomProperty(
28 "to", &JsNavigationTransitionProxy::GetToContentInfo, &JsNavigationTransitionProxy::SetToContentInfo);
29 JSClass<JsNavigationTransitionProxy>::CustomMethod(
30 "finishTransition", &JsNavigationTransitionProxy::FinishTransition);
31 JSClass<JsNavigationTransitionProxy>::Bind(
32 globalObj, &JsNavigationTransitionProxy::Constructor, &JsNavigationTransitionProxy::Destructor);
33 }
34
SetFromContentInfo(const JSCallbackInfo & args)35 void JsNavigationTransitionProxy::SetFromContentInfo(const JSCallbackInfo& args)
36 {
37 TAG_LOGI(AceLogTag::ACE_NAVIGATION, "navigation transition proxy can not support modify from attribute");
38 }
39
GetFromContentInfo(const JSCallbackInfo & args)40 void JsNavigationTransitionProxy::GetFromContentInfo(const JSCallbackInfo& args)
41 {
42 NG::NavContentInfo from;
43 if (proxy_) {
44 from = proxy_->GetPreDestination();
45 }
46 auto fromVal = ConvertContentInfo(from);
47 args.SetReturnValue(fromVal);
48 }
49
SetToContentInfo(const JSCallbackInfo & args)50 void JsNavigationTransitionProxy::SetToContentInfo(const JSCallbackInfo& args)
51 {
52 TAG_LOGI(AceLogTag::ACE_NAVIGATION, "navigation transition context can not support modify to attribute");
53 }
54
GetToContentInfo(const JSCallbackInfo & args)55 void JsNavigationTransitionProxy::GetToContentInfo(const JSCallbackInfo& args)
56 {
57 NG::NavContentInfo to;
58 if (proxy_) {
59 to = proxy_->GetTopDestination();
60 }
61 auto toVal = ConvertContentInfo(to);
62 args.SetReturnValue(toVal);
63 }
64
FinishTransition(const JSCallbackInfo & info)65 void JsNavigationTransitionProxy::FinishTransition(const JSCallbackInfo& info)
66 {
67 if (proxy_) {
68 proxy_->FireFinishCallback();
69 }
70 }
71
Constructor(const JSCallbackInfo & info)72 void JsNavigationTransitionProxy::Constructor(const JSCallbackInfo& info)
73 {
74 auto proxy = Referenced::MakeRefPtr<JsNavigationTransitionProxy>();
75 proxy->IncRefCount();
76 info.SetReturnValue(Referenced::RawPtr(proxy));
77 }
78
Destructor(JsNavigationTransitionProxy * proxy)79 void JsNavigationTransitionProxy::Destructor(JsNavigationTransitionProxy* proxy)
80 {
81 if (proxy != nullptr) {
82 proxy->DecRefCount();
83 }
84 }
85
ConvertContentInfo(NG::NavContentInfo info)86 JSRef<JSVal> JsNavigationTransitionProxy::ConvertContentInfo(NG::NavContentInfo info)
87 {
88 auto value = JSRef<JSObject>::New();
89 if (info.index == -1) {
90 // current nav content is navBar.Don't need to set name and mode.
91 value->SetProperty<int32_t>("index", -1);
92 } else {
93 value->SetProperty<std::string>("name", info.name);
94 value->SetProperty<int32_t>("index", info.index);
95 value->SetProperty<int32_t>("mode", static_cast<int32_t>(info.mode));
96 }
97 return value;
98 }
99
JSBind(BindingTarget globalObj)100 void JsNavigationFunction::JSBind(BindingTarget globalObj)
101 {
102 JsNavigationTransitionProxy::JSBind(globalObj);
103 }
104
Execute(NG::NavContentInfo from,NG::NavContentInfo to,NG::NavigationOperation navigationOperation)105 JSRef<JSVal> JsNavigationFunction::Execute(
106 NG::NavContentInfo from, NG::NavContentInfo to, NG::NavigationOperation navigationOperation)
107 {
108 JSRef<JSVal> fromVal = JsNavigationTransitionProxy::ConvertContentInfo(from);
109 JSRef<JSVal> toVal = JsNavigationTransitionProxy::ConvertContentInfo(to);
110 JSRef<JSVal> operation = JSRef<JSVal>::Make(ToJSValue(static_cast<int32_t>(navigationOperation)));
111 JSRef<JSVal> params[] = { fromVal, toVal, operation };
112 const int32_t argsNum = 3;
113 return JsFunction::ExecuteJS(argsNum, params);
114 }
115
Execute(const RefPtr<NG::NavigationTransitionProxy> & proxy)116 void JsNavigationFunction::Execute(const RefPtr<NG::NavigationTransitionProxy>& proxy)
117 {
118 JSRef<JSObject> proxyObj = JSClass<JsNavigationTransitionProxy>::NewInstance();
119 auto jsProxy = Referenced::Claim(proxyObj->Unwrap<JsNavigationTransitionProxy>());
120 jsProxy->SetProxy(proxy);
121 JSRef<JSVal> param = JSRef<JSObject>::Cast(proxyObj);
122 JsFunction::ExecuteJS(1, ¶m);
123 }
124 }; // namespace OHOS::Ace::Framework