1 /*
2 * Copyright (c) 2021 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 "frameworks/bridge/declarative_frontend/jsview/js_navigator.h"
17
18 #include "core/components/box/box_component.h"
19 #include "core/components/navigator/navigator_component.h"
20 #include "core/components_ng/pattern/navigator/navigator_model.h"
21 #include "core/components_ng/pattern/navigator/navigator_model_ng.h"
22 #include "frameworks/bridge/declarative_frontend/jsview/models/navigator_model_impl.h"
23 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
24
25 namespace OHOS::Ace {
26
27 std::unique_ptr<NavigatorModel> NavigatorModel::instance_ = nullptr;
28 std::mutex NavigatorModel::mutex_;
29
GetInstance()30 NavigatorModel* NavigatorModel::GetInstance()
31 {
32 if (!instance_) {
33 std::lock_guard<std::mutex> lock(mutex_);
34 if (!instance_) {
35 #ifdef NG_BUILD
36 instance_.reset(new NG::NavigatorModelNG());
37 #else
38 if (Container::IsCurrentUseNewPipeline()) {
39 instance_.reset(new NG::NavigatorModelNG());
40 } else {
41 instance_.reset(new Framework::NavigatorModelImpl());
42 }
43 #endif
44 }
45 }
46 return instance_.get();
47 }
48
49 } // namespace OHOS::Ace
50
51 namespace OHOS::Ace::Framework {
52
Create(const JSCallbackInfo & info)53 void JSNavigator::Create(const JSCallbackInfo& info)
54 {
55 NavigatorModel::GetInstance()->Create();
56 if (info.Length() > 0 && info[0]->IsObject()) {
57 JSRef<JSObject> obj = JSRef<JSObject>::Cast(info[0]);
58 JSRef<JSVal> target = obj->GetProperty("target");
59 if (target->IsString()) {
60 NavigatorModel::GetInstance()->SetUri(target->ToString());
61 }
62
63 JSRef<JSVal> type = obj->GetProperty("type");
64 if (type->IsNumber()) {
65 auto navigatorType = NavigatorType(type->ToNumber<uint32_t>());
66 if (navigatorType == NavigatorType::DEFAULT) {
67 NavigatorModel::GetInstance()->SetType(NavigatorType::PUSH);
68 } else {
69 NavigatorModel::GetInstance()->SetType(navigatorType);
70 }
71 }
72 } else {
73 NavigatorModel::GetInstance()->SetType(NavigatorType::BACK);
74 }
75 }
76
SetTarget(const std::string & value)77 void JSNavigator::SetTarget(const std::string& value)
78 {
79 NavigatorModel::GetInstance()->SetUri(value);
80 }
81
SetType(int32_t value)82 void JSNavigator::SetType(int32_t value)
83 {
84 auto navigatorType = NavigatorType(value);
85 if (navigatorType == NavigatorType::DEFAULT) {
86 NavigatorModel::GetInstance()->SetType(NavigatorType::PUSH);
87 } else {
88 NavigatorModel::GetInstance()->SetType(navigatorType);
89 }
90 }
91
JSBind(BindingTarget globalObj)92 void JSNavigator::JSBind(BindingTarget globalObj)
93 {
94 JSClass<JSNavigator>::Declare("Navigator");
95
96 MethodOptions opt = MethodOptions::NONE;
97 JSClass<JSNavigator>::StaticMethod("create", &JSNavigator::Create, opt);
98 JSClass<JSNavigator>::StaticMethod("target", &JSNavigator::SetTarget, opt);
99 JSClass<JSNavigator>::StaticMethod("type", &JSNavigator::SetType, opt);
100 JSClass<JSNavigator>::StaticMethod("active", &JSNavigator::SetActive, opt);
101 JSClass<JSNavigator>::StaticMethod("params", &JSNavigator::SetParams, opt);
102 JSClass<JSNavigator>::StaticMethod("width", &JSNavigator::JsWidth);
103 JSClass<JSNavigator>::StaticMethod("height", &JSNavigator::JsHeight);
104 JSClass<JSNavigator>::StaticMethod("size", &JSNavigator::JsSize);
105 JSClass<JSNavigator>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
106 JSClass<JSNavigator>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
107 JSClass<JSNavigator>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
108 JSClass<JSNavigator>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
109 JSClass<JSNavigator>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
110 JSClass<JSNavigator>::StaticMethod("onAttach", &JSInteractableView::JsOnAttach);
111 JSClass<JSNavigator>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
112 JSClass<JSNavigator>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
113 JSClass<JSNavigator>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
114 JSClass<JSNavigator>::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage);
115 JSClass<JSNavigator>::InheritAndBind<JSContainerBase>(globalObj);
116 }
117
SetActive(bool active)118 void JSNavigator::SetActive(bool active)
119 {
120 NavigatorModel::GetInstance()->SetActive(active);
121 }
122
SetParams(const JSCallbackInfo & args)123 void JSNavigator::SetParams(const JSCallbackInfo& args)
124 {
125 JSRef<JSVal> val = JSRef<JSVal>::Cast(args[0]);
126 NavigatorModel::GetInstance()->SetParams(val->ToString());
127 }
128
JsWidth(const JSCallbackInfo & info)129 void JSNavigator::JsWidth(const JSCallbackInfo& info)
130 {
131 if (info.Length() < 1) {
132 return;
133 }
134
135 JsWidth(info[0]);
136 }
137
JsWidth(const JSRef<JSVal> & jsValue)138 void JSNavigator::JsWidth(const JSRef<JSVal>& jsValue)
139 {
140 NavigatorModel::GetInstance()->SetIsDefWidth(true);
141 JSViewAbstract::JsWidth(jsValue);
142 }
143
JsHeight(const JSCallbackInfo & info)144 void JSNavigator::JsHeight(const JSCallbackInfo& info)
145 {
146 if (info.Length() < 1) {
147 return;
148 }
149
150 JsHeight(info[0]);
151 }
152
JsHeight(const JSRef<JSVal> & jsValue)153 void JSNavigator::JsHeight(const JSRef<JSVal>& jsValue)
154 {
155 NavigatorModel::GetInstance()->SetIsDefHeight(true);
156 JSViewAbstract::JsHeight(jsValue);
157 }
158
JsSize(const JSCallbackInfo & info)159 void JSNavigator::JsSize(const JSCallbackInfo& info)
160 {
161 if (info.Length() < 1) {
162 return;
163 }
164
165 if (!info[0]->IsObject()) {
166 return;
167 }
168
169 JSRef<JSObject> sizeObj = JSRef<JSObject>::Cast(info[0]);
170 JsWidth(sizeObj->GetProperty("width"));
171 JsHeight(sizeObj->GetProperty("height"));
172 }
173
174 } // namespace OHOS::Ace::Framework
175