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) 30 { 31 this->name = name; 32 this->param = param; 33 this->uiNode = uiNode; 34 } 35 std::string name; 36 JSRef<JSVal> param; 37 RefPtr<NG::UINode> uiNode; 38 }; 39 40 class JSRouteInfo : public NG::RouteInfo { 41 DECLARE_ACE_TYPE(JSRouteInfo, NG::RouteInfo) 42 public: 43 JSRouteInfo() = default; 44 ~JSRouteInfo() override = default; 45 46 std::string GetName() override; 47 void SetName(const std::string& name); 48 void SetParam(const JSRef<JSVal>& param); 49 JSRef<JSVal> GetParam() const; 50 51 protected: 52 std::string name_; 53 JSRef<JSVal> param_; 54 }; 55 56 class JSNavigationStack : public NG::NavigationStack { 57 DECLARE_ACE_TYPE(JSNavigationStack, NG::NavigationStack) 58 public: 59 JSNavigationStack() = default; 60 ~JSNavigationStack() override = default; 61 SetOnStateChangedCallback(std::function<void ()> callback)62 void SetOnStateChangedCallback(std::function<void()> callback) override 63 { 64 onStateChangedCallback_ = callback; 65 } 66 UpdateStackInfo(const RefPtr<NavigationStack> & newStack)67 void UpdateStackInfo(const RefPtr<NavigationStack>& newStack) override 68 { 69 auto newJsStack = AceType::DynamicCast<JSNavigationStack>(newStack); 70 if (newJsStack) { 71 SetDataSourceObj(newJsStack->GetDataSourceObj()); 72 } 73 } 74 void SetDataSourceObj(const JSRef<JSObject>& dataSourceObj); 75 const JSRef<JSObject>& GetDataSourceObj(); 76 void SetNavDestBuilderFunc(const JSRef<JSFunc>& navDestBuilderFunc); 77 bool IsEmpty() override; 78 void Pop() override; 79 void Push(const std::string& name, const RefPtr<NG::RouteInfo>& routeInfo = nullptr) override; 80 void Push(const std::string& name, int32_t index) override; 81 void PushName(const std::string& name, const JSRef<JSVal>& param); 82 void RemoveName(const std::string& name) override; 83 void RemoveIndex(int32_t index) override; 84 void RemoveInvalidPage(const JSRef<JSObject>& info); 85 void Clear() override; 86 void ClearRemoveArray() override; 87 int32_t GetReplaceValue() const override; 88 void UpdateReplaceValue(int32_t isReplace) const override; 89 bool GetAnimatedValue() const override; 90 void UpdateAnimatedValue(bool animated) override; 91 bool GetDisableAnimation() const override; 92 std::vector<std::string> GetAllPathName() override; 93 std::vector<int32_t> GetRemoveArray() override; 94 RefPtr<NG::UINode> CreateNodeByIndex(int32_t index) override; 95 RefPtr<NG::UINode> CreateNodeByRouteInfo(const RefPtr<NG::RouteInfo>& routeInfo) override; 96 void SetJSExecutionContext(const JSExecutionContext& context); 97 std::string GetRouteParam() const override; 98 void OnAttachToParent(RefPtr<NG::NavigationStack> parent) override; 99 void OnDetachFromParent() override; 100 int32_t CheckNavDestinationExists(const JSRef<JSObject>& navPathInfo); 101 void ClearPreBuildNodeList() override; 102 103 protected: 104 JSRef<JSObject> dataSourceObj_; 105 JSRef<JSFunc> navDestBuilderFunc_; 106 JSExecutionContext executionContext_; 107 std::function<void()> onStateChangedCallback_; 108 109 private: 110 std::string GetNameByIndex(int32_t index); 111 JSRef<JSVal> GetParamByIndex(int32_t index) const; 112 JSRef<JSVal> GetOnPopByIndex(int32_t index) const; 113 bool GetNavDestinationNodeInUINode(RefPtr<NG::UINode> node, RefPtr<NG::NavDestinationGroupNode>& desNode); 114 int32_t GetSize() const; 115 void SetJSParentStack(JSRef<JSVal> parent); 116 static std::string ConvertParamToString(const JSRef<JSVal>& param); 117 static void ParseJsObject(std::unique_ptr<JsonValue>& json, const JSRef<JSObject>& obj, int32_t depthLimit); 118 static void UpdateOnStateChangedCallback(JSRef<JSObject> obj, std::function<void()> callback); 119 static void UpdateCheckNavDestinationExistsFunc(JSRef<JSObject> obj, 120 std::function<int32_t(JSRef<JSObject>)> checkFunc); 121 bool GetFlagByIndex(int32_t index) const; 122 void SaveNodeToPreBuildList(const std::string& name, const JSRef<JSVal>& param, RefPtr<NG::UINode>& node); 123 RefPtr<NG::UINode> GetNodeFromPreBuildList(const std::string& name, const JSRef<JSVal>& param); 124 125 private: 126 std::vector<NavPathInfoUINode> preBuildNodeList_; 127 }; 128 } // namespace OHOS::Ace::Framework 129 130 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_NAVIGATION_STACK_H 131