1 /*
2 * Copyright (c) 2024 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_navdestination_context.h"
17
18 #include "base/log/ace_scoring_log.h"
19 #include "base/memory/ace_type.h"
20 #include "base/utils/utils.h"
21 #include "bridge/declarative_frontend/engine/functions/js_function.h"
22 #include "bridge/declarative_frontend/jsview/js_navigation_stack.h"
23 #include "bridge/declarative_frontend/engine/js_ref_ptr.h"
24 #include "bridge/declarative_frontend/engine/js_types.h"
25 #include "bridge/declarative_frontend/jsview/js_utils.h"
26
27 namespace OHOS::Ace::Framework {
CreateJSNavPathInfo() const28 JSRef<JSObject> JSNavDestinationContext::CreateJSNavPathInfo() const
29 {
30 JSRef<JSObject> obj = JSRef<JSObject>::New();
31 CHECK_NULL_RETURN(pathInfo_, obj);
32 obj->SetProperty<std::string>("name", pathInfo_->GetName());
33 auto jsInfo = AceType::DynamicCast<JSNavPathInfo>(pathInfo_);
34 JSRef<JSVal> param;
35 JSRef<JSVal> onPop;
36 if (jsInfo) {
37 param = jsInfo->GetParam();
38 onPop = jsInfo->GetOnPop();
39 }
40 if (!param->IsEmpty()) {
41 obj->SetPropertyObject("param", param);
42 }
43 if (!onPop->IsEmpty()) {
44 obj->SetPropertyObject("onPop", onPop);
45 }
46 return obj;
47 }
48
CreateJSNavPathStack() const49 JSRef<JSObject> JSNavDestinationContext::CreateJSNavPathStack() const
50 {
51 JSRef<JSObject> obj = JSRef<JSObject>::New();
52 auto stack = navigationStack_.Upgrade();
53 CHECK_NULL_RETURN(stack, obj);
54 auto jsStack = AceType::DynamicCast<JSNavigationStack>(stack);
55 CHECK_NULL_RETURN(jsStack, obj);
56 auto navPathStackObj = jsStack->GetDataSourceObj();
57 if (navPathStackObj->IsEmpty()) {
58 return obj;
59 }
60
61 return navPathStackObj;
62 }
63
CreateJSObject()64 JSRef<JSObject> JSNavDestinationContext::CreateJSObject()
65 {
66 JSRef<JSObject> obj = JSRef<JSObject>::New();
67 obj->SetPropertyObject("pathInfo", CreateJSNavPathInfo());
68 obj->SetPropertyObject("pathStack", CreateJSNavPathStack());
69 return obj;
70 }
71 } // namespace OHOS::Ace::Framework
72