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 "frameworks/bridge/declarative_frontend/jsview/js_navrouter.h"
17
18 #include "base/log/ace_scoring_log.h"
19 #include "core/components_ng/base/view_stack_processor.h"
20 #include "core/components_ng/pattern/navrouter/navrouter_view.h"
21
22 namespace OHOS::Ace::Framework {
23
Create()24 void JSNavRouter::Create()
25 {
26 if (!Container::IsCurrentUseNewPipeline()) {
27 return;
28 }
29 NG::NavRouterView::Create();
30 }
31
SetOnStateChange(const JSCallbackInfo & info)32 void JSNavRouter::SetOnStateChange(const JSCallbackInfo& info)
33 {
34 if (!Container::IsCurrentUseNewPipeline()) {
35 return;
36 }
37 if (info.Length() < 1) {
38 LOGE("The arg is wrong, it is supposed to have at least one argument");
39 return;
40 }
41 if (info[0]->IsFunction()) {
42 auto onStateChangeCallback = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
43 auto onStateChange = [execCtx = info.GetExecutionContext(), func = std::move(onStateChangeCallback)](
44 bool isActivated) {
45 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
46 ACE_SCORING_EVENT("OnStateChange");
47 JSRef<JSVal> param = JSRef<JSVal>::Make(ToJSValue(isActivated));
48 func->ExecuteJS(1, ¶m);
49 };
50 NG::NavRouterView::SetOnStateChange(std::move(onStateChange));
51 return;
52 }
53 info.ReturnSelf();
54 }
55
JSBind(BindingTarget globalObj)56 void JSNavRouter::JSBind(BindingTarget globalObj)
57 {
58 JSClass<JSNavRouter>::Declare("NavRouter");
59 JSClass<JSNavRouter>::StaticMethod("create", &JSNavRouter::Create);
60 JSClass<JSNavRouter>::StaticMethod("onStateChange", &JSNavRouter::SetOnStateChange);
61 JSClass<JSNavRouter>::Inherit<JSContainerBase>();
62 JSClass<JSNavRouter>::Inherit<JSViewAbstract>();
63 JSClass<JSNavRouter>::Bind<>(globalObj);
64 }
65
66 } // namespace OHOS::Ace::Framework