• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_NAVIGATION_STACK_H
16 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_NAVIGATION_STACK_H
17 
18 #include <functional>
19 #include <stdint.h>
20 
21 #include "bridge/declarative_frontend/engine/js_types.h"
22 #include "bridge/declarative_frontend/jsview/js_utils.h"
23 #include "core/components_ng/pattern/navigation/navigation_stack.h"
24 #include "core/components_ng/pattern/navrouter/navdestination_group_node.h"
25 
26 namespace OHOS::Ace::Framework {
27 
28 struct NavPathInfoUINode {
NavPathInfoUINodeNavPathInfoUINode29     NavPathInfoUINode(const std::string& name, const JSRef<JSVal>& param, RefPtr<NG::UINode>& uiNode, int32_t index)
30     {
31         this->name = name;
32         this->param = param;
33         this->uiNode = uiNode;
34         this->index = index;
35     }
36     std::string name;
37     JSRef<JSVal> param;
38     RefPtr<NG::UINode> uiNode;
39     int32_t index = -1;
40 };
41 
42 class JSRouteInfo : public NG::RouteInfo {
43     DECLARE_ACE_TYPE(JSRouteInfo, NG::RouteInfo)
44 public:
45     JSRouteInfo() = default;
46     ~JSRouteInfo() override = default;
47 
48     std::string GetName() override;
49     void SetName(const std::string& name);
50     void SetParam(const JSRef<JSVal>& param);
51     JSRef<JSVal> GetParam() const;
52 
53 protected:
54     std::string name_;
55     JSRef<JSVal> param_;
56 };
57 
58 class JSNavigationStack : public NG::NavigationStack {
59     DECLARE_ACE_TYPE(JSNavigationStack, NG::NavigationStack)
60 public:
61     JSNavigationStack() = default;
62     ~JSNavigationStack() override = default;
63 
SetOnStateChangedCallback(std::function<void ()> callback)64     void SetOnStateChangedCallback(std::function<void()> callback) override
65     {
66         onStateChangedCallback_ = callback;
67     }
68 
UpdateStackInfo(const RefPtr<NavigationStack> & newStack)69     void UpdateStackInfo(const RefPtr<NavigationStack>& newStack) override
70     {
71         auto newJsStack = AceType::DynamicCast<JSNavigationStack>(newStack);
72         if (newJsStack) {
73             SetDataSourceObj(newJsStack->GetDataSourceObj());
74         }
75     }
76     void SetDataSourceObj(const JSRef<JSObject>& dataSourceObj);
77     const JSRef<JSObject>& GetDataSourceObj();
78     void SetNavDestBuilderFunc(const JSRef<JSFunc>& navDestBuilderFunc);
79     bool IsEmpty() override;
80     void Pop() override;
81     void Push(const std::string& name, const RefPtr<NG::RouteInfo>& routeInfo = nullptr) override;
82     void Push(const std::string& name, int32_t index) override;
83     void PushName(const std::string& name, const JSRef<JSVal>& param);
84     void RemoveName(const std::string& name) override;
85     void RemoveIndex(int32_t index) override;
86     void RemoveInvalidPage(int32_t index);
87     void Clear() override;
88     int32_t GetReplaceValue() const override;
89     void UpdateReplaceValue(int32_t isReplace) const override;
90     bool GetAnimatedValue() const override;
91     void UpdateAnimatedValue(bool animated) override;
92     bool GetDisableAnimation() const override;
93     std::vector<std::string> GetAllPathName() override;
94     std::vector<int32_t> GetAllPathIndex() override;
95     void InitNavPathIndex(const std::vector<std::string>& pathNames) override;
96     void SetDestinationIdToJsStack(int32_t index, const std::string& navDestinationId) override;
97     bool CreateNodeByIndex(int32_t index, const WeakPtr<NG::UINode>& customNode, RefPtr<NG::UINode>& node) override;
98     RefPtr<NG::UINode> CreateNodeByRouteInfo(const RefPtr<NG::RouteInfo>& routeInfo,
99         const WeakPtr<NG::UINode>& node) override;
100     void SetJSExecutionContext(const JSExecutionContext& context);
101     std::string GetRouteParam() const override;
102     void OnAttachToParent(RefPtr<NG::NavigationStack> parent) override;
103     void OnDetachFromParent() override;
104     std::vector<std::string> DumpStackInfo() const override;
105     void FireNavigationInterception(bool isBefore, const RefPtr<NG::NavDestinationContext>& from,
106         const RefPtr<NG::NavDestinationContext>& to, NG::NavigationOperation operation, bool isAnimated) override;
107     void FireNavigationModeChange(NG::NavigationMode mode) override;
108     JSRef<JSVal> GetParamByIndex(int32_t index) const;
109     int32_t GetJsIndexFromNativeIndex(int32_t index) override;
110     void MoveIndexToTop(int32_t index) override;
111     void UpdatePathInfoIfNeeded(RefPtr<NG::UINode>& uiNode, int32_t index) override;
112     void RecoveryNavigationStack() override;
113     bool NeedBuildNewInstance(int32_t index) override;
114     void SetNeedBuildNewInstance(int32_t index, bool need) override;
115     void SetIsEntryByIndex(int32_t index, bool isEntry) override;
116 
117     std::string GetStringifyParamByIndex(int32_t index) const override;
118     void SetPathArray(const std::vector<NG::NavdestinationRecoveryInfo>& navdestinationsInfo) override;
119     bool IsFromRecovery(int32_t index) override;
120     void SetFromRecovery(int32_t index, bool fromRecovery) override;
121     int32_t GetRecoveredDestinationMode(int32_t index) override;
122     void SetRecoveryFromReplaceDestination(int32_t index, bool value) override;
123     bool CheckIsReplacedDestination(int32_t index, std::string& replacedName, int32_t& replacedIndex) override;
124 
125     bool HasSingletonMoved() override;
126     bool IsTopFromSingletonMoved() override;
127     void ResetSingletonMoved() override;
128 
129     uint64_t GetNavDestinationIdInt(int32_t index) override;
130     bool GetIsForceSet(int32_t index) override;
131     void ResetIsForceSetFlag(int32_t index) override;
132 
133 protected:
134     JSRef<JSObject> dataSourceObj_;
135     JSRef<JSFunc> navDestBuilderFunc_;
136     JSExecutionContext executionContext_;
137     std::function<void()> onStateChangedCallback_;
138 
139 private:
140     JSRef<JSArray> GetJsPathArray();
141     JSRef<JSObject> GetJsPathInfo(int32_t index);
142     std::string GetNameByIndex(int32_t index);
143     JSRef<JSVal> GetOnPopByIndex(int32_t index) const;
144     bool GetIsEntryByIndex(int32_t index);
145     JSRef<JSObject> CreatePathInfoWithNecessaryProperty(const RefPtr<NG::NavDestinationContext>& context);
146     bool GetNavDestinationNodeInUINode(RefPtr<NG::UINode> node, RefPtr<NG::NavDestinationGroupNode>& desNode);
147     int32_t GetSize() const override;
148     void SetJSParentStack(JSRef<JSVal> parent);
149     std::string ConvertParamToString(const JSRef<JSVal>& param, bool needLimit = false) const;
150     void ParseJsObject(
151         std::unique_ptr<JsonValue>& json, const JSRef<JSObject>& obj, int32_t depthLimit, bool needLimit) const;
152     void UpdateOnStateChangedCallback(JSRef<JSObject> obj, std::function<void()> callback);
153 
154     int LoadDestination(const std::string& name, const JSRef<JSVal>& param, const WeakPtr<NG::UINode>& customNode,
155         RefPtr<NG::UINode>& node, RefPtr<NG::NavDestinationGroupNode>& desNode);
156     bool LoadDestinationByBuilder(const std::string& name, const JSRef<JSVal>& param, RefPtr<NG::UINode>& node,
157         RefPtr<NG::NavDestinationGroupNode>& desNode);
158     bool GetFlagByIndex(int32_t index) const;
159     bool CheckAndGetInterceptionFunc(const std::string& name, JSRef<JSFunc>& func);
160 
161     bool GetNeedUpdatePathInfo(int32_t index);
162     void SetNeedUpdatePathInfo(int32_t index, bool need);
163     int32_t ExecuteBuilderByConfig(const std::string& name,
164         const WeakPtr<NG::UINode>& customNode, const JSRef<JSVal>& param);
165 
166     JSRef<JSArray> GetPathArray();
167     JSRef<JSObject> GetPathInfo(int32_t index);
168 
169     std::string ErrorToMessage(int32_t errorCode);
170 
171     bool RemoveDestinationIfNeeded(const JSRef<JSObject>& param, int32_t errorCode, int32_t index);
172 
173     bool ExecutePopCallback(const RefPtr<NG::UINode>& uiNode, uint64_t navDestinationId, const JSRef<JSVal>& param);
174 private:
175     JSRef<JSObject> thisObj_;
176 };
177 } // namespace OHOS::Ace::Framework
178 
179 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_NAVIGATION_STACK_H
180