• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_NAV_PATH_STACK_H
17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_NAV_PATH_STACK_H
18 
19 #include <functional>
20 #include <string>
21 
22 #include "base/memory/referenced.h"
23 #include "bridge/declarative_frontend/engine/bindings.h"
24 #include "bridge/declarative_frontend/engine/functions/js_function.h"
25 #include "bridge/declarative_frontend/engine/js_types.h"
26 #include "bridge/declarative_frontend/engine/js_ref_ptr.h"
27 #include "core/components_ng/pattern/navigation/navigation_pattern.h"
28 
29 namespace OHOS::Ace::Framework {
30 class JSNavPathStack final : public Referenced {
31 public:
JSNavPathStack()32     JSNavPathStack()
33     {
34         containerCurrentId_ = Container::CurrentId();
35     };
36     ~JSNavPathStack() override = default;
37 
38     static void JSBind(BindingTarget globalObj);
39 
OnStateChanged()40     void OnStateChanged()
41     {
42         if (onStateChangedCallback_) {
43             onStateChangedCallback_();
44         }
45     }
46 
SetOnStateChangedCallback(std::function<void ()> callback)47     void SetOnStateChangedCallback(std::function<void()> callback)
48     {
49         onStateChangedCallback_ = callback;
50     }
51 
52     static JSRef<JSObject> CreateNewNavPathStackJSObject();
53     static void SetNativeNavPathStack(JSRef<JSObject> jsStack, JSRef<JSObject> nativeStack);
54 
55     static bool CheckIsValid(JSValueWrapper object);
56 
SetOnPopCallback(const std::function<void (const JSRef<JSVal>)> & popCallback)57     void SetOnPopCallback(const std::function<void(const JSRef<JSVal>)>& popCallback)
58     {
59         onPopCallback_ = popCallback;
60     }
61 
SetIsHomeNameCallback(std::function<bool (const std::string &)> && callback)62     void SetIsHomeNameCallback(std::function<bool(const std::string&)>&& callback)
63     {
64         isHomeNameCallback_ = std::move(callback);
65     }
66 
67     void OnPopCallback(const JSCallbackInfo& info);
68     void GetPathStack(const JSCallbackInfo& info);
69     void SetPathStack(const JSCallbackInfo& info);
70     void IsHomeName(const JSCallbackInfo& info);
71 
72 private:
73     static void Constructor(const JSCallbackInfo& info);
74     static void Destructor(JSNavPathStack* stack);
75 
76     void CopyPathInfo(const JSRef<JSArray>& origin, JSRef<JSArray>& dest, size_t index);
77     bool FindNavInfoInPreArray(
78         JSRef<JSObject>& destInfo, JSRef<JSArray>& originArray, std::string& navIdStr, std::string& nameStr);
79 
80     std::function<void()> onStateChangedCallback_;
81     std::function<void(const JSRef<JSVal>)> onPopCallback_;
82     std::function<bool(const std::string&)> isHomeNameCallback_;
83     int32_t containerCurrentId_;
84 };
85 } // namespace OHOS::Ace::Framework
86 
87 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_NAV_PATH_STACK_H
88