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_tabs_function.h"
17
18 namespace OHOS::Ace::Framework {
19
JSBind(BindingTarget globalObj)20 void JsTabContentTransitionProxy::JSBind(BindingTarget globalObj)
21 {
22 JSClass<JsTabContentTransitionProxy>::Declare("TabContentTransitionProxy");
23 JSClass<JsTabContentTransitionProxy>::CustomProperty(
24 "from", &JsTabContentTransitionProxy::GetFromIndex, &JsTabContentTransitionProxy::SetFromIndex);
25 JSClass<JsTabContentTransitionProxy>::CustomProperty(
26 "to", &JsTabContentTransitionProxy::GetToIndex, &JsTabContentTransitionProxy::SetToIndex);
27 JSClass<JsTabContentTransitionProxy>::CustomMethod(
28 "finishTransition", &JsTabContentTransitionProxy::FinishTransition);
29 JSClass<JsTabContentTransitionProxy>::Bind(
30 globalObj, &JsTabContentTransitionProxy::Constructor, &JsTabContentTransitionProxy::Destructor);
31 }
32
SetFromIndex(const JSCallbackInfo & args)33 void JsTabContentTransitionProxy::SetFromIndex(const JSCallbackInfo& args)
34 {
35 TAG_LOGD(AceLogTag::ACE_TABS, "TabContentTransitionProxy can not support set from value.");
36 }
37
GetFromIndex(const JSCallbackInfo & args)38 void JsTabContentTransitionProxy::GetFromIndex(const JSCallbackInfo& args)
39 {
40 auto fromIndex = 0;
41 if (proxy_) {
42 fromIndex = proxy_->GetFromIndex();
43 }
44 auto fromRef = JSRef<JSVal>::Make(JSVal(ToJSValue(fromIndex)));
45 args.SetReturnValue(fromRef);
46 }
47
SetToIndex(const JSCallbackInfo & args)48 void JsTabContentTransitionProxy::SetToIndex(const JSCallbackInfo& args)
49 {
50 TAG_LOGD(AceLogTag::ACE_TABS, "TabContentTransitionProxy can not support set to value.");
51 }
52
GetToIndex(const JSCallbackInfo & args)53 void JsTabContentTransitionProxy::GetToIndex(const JSCallbackInfo& args)
54 {
55 auto toIndex = 0;
56 if (proxy_) {
57 toIndex = proxy_->GetToIndex();
58 }
59 auto toRef = JSRef<JSVal>::Make(JSVal(ToJSValue(toIndex)));
60 args.SetReturnValue(toRef);
61 }
62
FinishTransition(const JSCallbackInfo & args)63 void JsTabContentTransitionProxy::FinishTransition(const JSCallbackInfo& args)
64 {
65 if (proxy_) {
66 proxy_->FinishTransition();
67 }
68 }
69
Constructor(const JSCallbackInfo & args)70 void JsTabContentTransitionProxy::Constructor(const JSCallbackInfo& args)
71 {
72 auto proxy = Referenced::MakeRefPtr<JsTabContentTransitionProxy>();
73 proxy->IncRefCount();
74 args.SetReturnValue(Referenced::RawPtr(proxy));
75 }
76
Destructor(JsTabContentTransitionProxy * proxy)77 void JsTabContentTransitionProxy::Destructor(JsTabContentTransitionProxy* proxy)
78 {
79 if (proxy != nullptr) {
80 proxy->DecRefCount();
81 }
82 }
83
JSBind(BindingTarget globalObj)84 void JsTabsFunction::JSBind(BindingTarget globalObj)
85 {
86 JsTabContentTransitionProxy::JSBind(globalObj);
87 }
88
Execute(int32_t fromIndex,int32_t toIndex)89 JSRef<JSVal> JsTabsFunction::Execute(int32_t fromIndex, int32_t toIndex)
90 {
91 JSRef<JSVal> from = JSRef<JSVal>::Make(ToJSValue(fromIndex));
92 JSRef<JSVal> to = JSRef<JSVal>::Make(ToJSValue(toIndex));
93
94 JSRef<JSVal> params[] = { from, to };
95 return JsFunction::ExecuteJS(2, params);
96 }
97
Execute(const RefPtr<TabContentTransitionProxy> & proxy)98 void JsTabsFunction::Execute(const RefPtr<TabContentTransitionProxy>& proxy)
99 {
100 JSRef<JSObject> proxyObj = JSClass<JsTabContentTransitionProxy>::NewInstance();
101 auto jsProxy = Referenced::Claim(proxyObj->Unwrap<JsTabContentTransitionProxy>());
102 jsProxy->SetProxy(proxy);
103
104 JSRef<JSVal> param = JSRef<JSObject>::Cast(proxyObj);
105 JsFunction::ExecuteJS(1, ¶m);
106 }
107
108 } // namespace OHOS::Ace::Framework
109