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 LOGD("Create component: JSNavigator");
56
57 NavigatorModel::GetInstance()->Create();
58 if (info.Length() > 0 && info[0]->IsObject()) {
59 JSRef<JSObject> obj = JSRef<JSObject>::Cast(info[0]);
60 JSRef<JSVal> target = obj->GetProperty("target");
61 if (target->IsString()) {
62 NavigatorModel::GetInstance()->SetUri(target->ToString());
63 }
64
65 JSRef<JSVal> type = obj->GetProperty("type");
66 if (type->IsNumber()) {
67 auto navigatorType = NavigatorType(type->ToNumber<uint32_t>());
68 if (navigatorType == NavigatorType::DEFAULT) {
69 NavigatorModel::GetInstance()->SetType(NavigatorType::PUSH);
70 } else {
71 NavigatorModel::GetInstance()->SetType(navigatorType);
72 }
73 }
74 } else {
75 NavigatorModel::GetInstance()->SetType(NavigatorType::BACK);
76 }
77 }
78
SetTarget(const std::string & value)79 void JSNavigator::SetTarget(const std::string& value)
80 {
81 NavigatorModel::GetInstance()->SetUri(value);
82 }
83
SetType(int32_t value)84 void JSNavigator::SetType(int32_t value)
85 {
86 auto navigatorType = NavigatorType(value);
87 if (navigatorType == NavigatorType::DEFAULT) {
88 NavigatorModel::GetInstance()->SetType(NavigatorType::PUSH);
89 } else {
90 NavigatorModel::GetInstance()->SetType(navigatorType);
91 }
92 }
93
JSBind(BindingTarget globalObj)94 void JSNavigator::JSBind(BindingTarget globalObj)
95 {
96 JSClass<JSNavigator>::Declare("Navigator");
97
98 MethodOptions opt = MethodOptions::NONE;
99 JSClass<JSNavigator>::StaticMethod("create", &JSNavigator::Create, opt);
100 JSClass<JSNavigator>::StaticMethod("target", &JSNavigator::SetTarget, opt);
101 JSClass<JSNavigator>::StaticMethod("type", &JSNavigator::SetType, opt);
102 JSClass<JSNavigator>::StaticMethod("active", &JSNavigator::SetActive, opt);
103 JSClass<JSNavigator>::StaticMethod("params", &JSNavigator::SetParams, opt);
104 JSClass<JSNavigator>::StaticMethod("width", &JSNavigator::JsWidth);
105 JSClass<JSNavigator>::StaticMethod("height", &JSNavigator::JsHeight);
106 JSClass<JSNavigator>::StaticMethod("size", &JSNavigator::JsSize);
107 JSClass<JSNavigator>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
108 JSClass<JSNavigator>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
109 JSClass<JSNavigator>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
110 JSClass<JSNavigator>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
111 JSClass<JSNavigator>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
112 JSClass<JSNavigator>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
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 LOGE("The arg is wrong, it is supposed to have atleast 1 arguments");
133 return;
134 }
135
136 JsWidth(info[0]);
137 }
138
JsWidth(const JSRef<JSVal> & jsValue)139 void JSNavigator::JsWidth(const JSRef<JSVal>& jsValue)
140 {
141 NavigatorModel::GetInstance()->SetIsDefWidth(true);
142 JSViewAbstract::JsWidth(jsValue);
143 }
144
JsHeight(const JSCallbackInfo & info)145 void JSNavigator::JsHeight(const JSCallbackInfo& info)
146 {
147 if (info.Length() < 1) {
148 LOGE("The arg is wrong, it is supposed to have atleast 1 arguments");
149 return;
150 }
151
152 JsHeight(info[0]);
153 }
154
JsHeight(const JSRef<JSVal> & jsValue)155 void JSNavigator::JsHeight(const JSRef<JSVal>& jsValue)
156 {
157 NavigatorModel::GetInstance()->SetIsDefHeight(true);
158 JSViewAbstract::JsHeight(jsValue);
159 }
160
JsSize(const JSCallbackInfo & info)161 void JSNavigator::JsSize(const JSCallbackInfo& info)
162 {
163 if (info.Length() < 1) {
164 LOGE("The arg is wrong, it is supposed to have atleast 1 arguments");
165 return;
166 }
167
168 if (!info[0]->IsObject()) {
169 LOGE("arg is not Object or String.");
170 return;
171 }
172
173 JSRef<JSObject> sizeObj = JSRef<JSObject>::Cast(info[0]);
174 JsWidth(sizeObj->GetProperty("width"));
175 JsHeight(sizeObj->GetProperty("height"));
176 }
177
178 } // namespace OHOS::Ace::Framework
179