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(const JSRef<JSObject>& info); 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 RefPtr<NG::UINode> CreateNodeByIndex(int32_t index, const WeakPtr<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 int32_t CheckNavDestinationExists(const JSRef<JSObject>& navPathInfo); 105 void ClearPreBuildNodeList() override; 106 std::vector<std::string> DumpStackInfo() const override; 107 void FireNavigationInterception(bool isBefore, const RefPtr<NG::NavDestinationContext>& from, 108 const RefPtr<NG::NavDestinationContext>& to, NG::NavigationOperation operation, bool isAnimated) override; 109 void FireNavigationModeChange(NG::NavigationMode mode) override; 110 JSRef<JSVal> GetParamByIndex(int32_t index) const; 111 int32_t GetJsIndexFromNativeIndex(int32_t index) override; 112 void MoveIndexToTop(int32_t index) override; 113 void UpdatePathInfoIfNeeded(RefPtr<NG::UINode>& uiNode, int32_t index) override; 114 void RecoveryNavigationStack() override; 115 bool NeedBuildNewInstance(int32_t index) override; 116 void SetNeedBuildNewInstance(int32_t index, bool need) override; 117 void SetIsEntryByIndex(int32_t index, bool isEntry) override; 118 119 std::string GetStringifyParamByIndex(int32_t index) const override; 120 void SetPathArray(const std::vector<NG::NavdestinationRecoveryInfo>& navdestinationsInfo) override; 121 bool IsFromRecovery(int32_t index) override; 122 void SetFromRecovery(int32_t index, bool fromRecovery) override; 123 int32_t GetRecoveredDestinationMode(int32_t index) override; 124 125 protected: 126 JSRef<JSObject> dataSourceObj_; 127 JSRef<JSFunc> navDestBuilderFunc_; 128 JSExecutionContext executionContext_; 129 std::function<void()> onStateChangedCallback_; 130 131 private: 132 JSRef<JSArray> GetJsPathArray(); 133 JSRef<JSObject> GetJsPathInfo(int32_t index); 134 std::string GetNameByIndex(int32_t index); 135 JSRef<JSVal> GetOnPopByIndex(int32_t index) const; 136 bool GetIsEntryByIndex(int32_t index); 137 JSRef<JSObject> CreatePathInfoWithNecessaryProperty(const RefPtr<NG::NavDestinationContext>& context); 138 bool GetNavDestinationNodeInUINode(RefPtr<NG::UINode> node, RefPtr<NG::NavDestinationGroupNode>& desNode); 139 int32_t GetSize() const; 140 void SetJSParentStack(JSRef<JSVal> parent); 141 std::string ConvertParamToString(const JSRef<JSVal>& param, bool needLimit = false) const; 142 void ParseJsObject( 143 std::unique_ptr<JsonValue>& json, const JSRef<JSObject>& obj, int32_t depthLimit, bool needLimit) const; 144 static void UpdateOnStateChangedCallback(JSRef<JSObject> obj, std::function<void()> callback); 145 static void UpdateCheckNavDestinationExistsFunc(JSRef<JSObject> obj, 146 std::function<int32_t(JSRef<JSObject>)> checkFunc); 147 148 int LoadDestination(const std::string& name, const JSRef<JSVal>& param, const WeakPtr<NG::UINode>& customNode, 149 RefPtr<NG::UINode>& node, RefPtr<NG::NavDestinationGroupNode>& desNode); 150 bool LoadDestinationByBuilder(const std::string& name, const JSRef<JSVal>& param, RefPtr<NG::UINode>& node, 151 RefPtr<NG::NavDestinationGroupNode>& desNode); 152 bool GetFlagByIndex(int32_t index) const; 153 bool CallByPushDestination(int32_t index); 154 void SaveNodeToPreBuildList(const std::string& name, const JSRef<JSVal>& param, RefPtr<NG::UINode>& node); 155 bool GetNodeFromPreBuildList(int32_t index, const std::string& name, 156 const JSRef<JSVal>& param, RefPtr<NG::UINode>& node); 157 bool CheckAndGetInterceptionFunc(const std::string& name, JSRef<JSFunc>& func); 158 159 bool GetNeedUpdatePathInfo(int32_t index); 160 void SetNeedUpdatePathInfo(int32_t index, bool need); 161 162 JSRef<JSArray> GetPathArray(); 163 JSRef<JSObject> GetPathInfo(int32_t index); 164 private: 165 std::vector<NavPathInfoUINode> preBuildNodeList_; 166 JSRef<JSObject> thisObj_; 167 }; 168 } // namespace OHOS::Ace::Framework 169 170 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_NAVIGATION_STACK_H 171