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
GetInstance()29 NavigatorModel* NavigatorModel::GetInstance()
30 {
31 if (!instance_) {
32 #ifdef NG_BUILD
33 instance_.reset(new NG::NavigatorModelNG());
34 #else
35 if (Container::IsCurrentUseNewPipeline()) {
36 instance_.reset(new NG::NavigatorModelNG());
37 } else {
38 instance_.reset(new Framework::NavigatorModelImpl());
39 }
40 #endif
41 }
42 return instance_.get();
43 }
44
45 } // namespace OHOS::Ace
46
47 namespace OHOS::Ace::Framework {
48
Create(const JSCallbackInfo & info)49 void JSNavigator::Create(const JSCallbackInfo& info)
50 {
51 LOGD("Create component: JSNavigator");
52
53 NavigatorModel::GetInstance()->Create();
54 if (info.Length() > 0 && info[0]->IsObject()) {
55 JSRef<JSObject> obj = JSRef<JSObject>::Cast(info[0]);
56 JSRef<JSVal> target = obj->GetProperty("target");
57 if (target->IsString()) {
58 NavigatorModel::GetInstance()->SetUri(target->ToString());
59 }
60
61 JSRef<JSVal> type = obj->GetProperty("type");
62 if (type->IsNumber()) {
63 auto navigatorType = NavigatorType(type->ToNumber<uint32_t>());
64 if (navigatorType == NavigatorType::DEFAULT) {
65 NavigatorModel::GetInstance()->SetType(NavigatorType::PUSH);
66 } else {
67 NavigatorModel::GetInstance()->SetType(navigatorType);
68 }
69 }
70 } else {
71 NavigatorModel::GetInstance()->SetType(NavigatorType::BACK);
72 }
73 }
74
SetTarget(const std::string & value)75 void JSNavigator::SetTarget(const std::string& value)
76 {
77 NavigatorModel::GetInstance()->SetUri(value);
78 }
79
SetType(int32_t value)80 void JSNavigator::SetType(int32_t value)
81 {
82 auto navigatorType = NavigatorType(value);
83 if (navigatorType == NavigatorType::DEFAULT) {
84 NavigatorModel::GetInstance()->SetType(NavigatorType::PUSH);
85 } else {
86 NavigatorModel::GetInstance()->SetType(navigatorType);
87 }
88 }
89
JSBind(BindingTarget globalObj)90 void JSNavigator::JSBind(BindingTarget globalObj)
91 {
92 JSClass<JSNavigator>::Declare("Navigator");
93
94 MethodOptions opt = MethodOptions::NONE;
95 JSClass<JSNavigator>::StaticMethod("create", &JSNavigator::Create, opt);
96 JSClass<JSNavigator>::StaticMethod("target", &JSNavigator::SetTarget, opt);
97 JSClass<JSNavigator>::StaticMethod("type", &JSNavigator::SetType, opt);
98 JSClass<JSNavigator>::StaticMethod("active", &JSNavigator::SetActive, opt);
99 JSClass<JSNavigator>::StaticMethod("params", &JSNavigator::SetParams, opt);
100 JSClass<JSNavigator>::StaticMethod("width", &JSNavigator::JsWidth);
101 JSClass<JSNavigator>::StaticMethod("height", &JSNavigator::JsHeight);
102 JSClass<JSNavigator>::StaticMethod("size", &JSNavigator::JsSize);
103 JSClass<JSNavigator>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
104 JSClass<JSNavigator>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
105 JSClass<JSNavigator>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
106 JSClass<JSNavigator>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
107 JSClass<JSNavigator>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
108 JSClass<JSNavigator>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
109 JSClass<JSNavigator>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
110 JSClass<JSNavigator>::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage);
111 JSClass<JSNavigator>::Inherit<JSContainerBase>();
112 JSClass<JSNavigator>::Inherit<JSViewAbstract>();
113 JSClass<JSNavigator>::Bind<>(globalObj);
114 }
115
SetActive(bool active)116 void JSNavigator::SetActive(bool active)
117 {
118 NavigatorModel::GetInstance()->SetActive(active);
119 }
120
SetParams(const JSCallbackInfo & args)121 void JSNavigator::SetParams(const JSCallbackInfo& args)
122 {
123 JSRef<JSVal> val = JSRef<JSVal>::Cast(args[0]);
124 NavigatorModel::GetInstance()->SetParams(val->ToString());
125 }
126
JsWidth(const JSCallbackInfo & info)127 void JSNavigator::JsWidth(const JSCallbackInfo& info)
128 {
129 if (info.Length() < 1) {
130 LOGE("The arg is wrong, it is supposed to have atleast 1 arguments");
131 return;
132 }
133
134 JsWidth(info[0]);
135 }
136
JsWidth(const JSRef<JSVal> & jsValue)137 void JSNavigator::JsWidth(const JSRef<JSVal>& jsValue)
138 {
139 NavigatorModel::GetInstance()->SetIsDefWidth(true);
140 JSViewAbstract::JsWidth(jsValue);
141 }
142
JsHeight(const JSCallbackInfo & info)143 void JSNavigator::JsHeight(const JSCallbackInfo& info)
144 {
145 if (info.Length() < 1) {
146 LOGE("The arg is wrong, it is supposed to have atleast 1 arguments");
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 LOGE("The arg is wrong, it is supposed to have atleast 1 arguments");
163 return;
164 }
165
166 if (!info[0]->IsObject()) {
167 LOGE("arg is not Object or String.");
168 return;
169 }
170
171 JSRef<JSObject> sizeObj = JSRef<JSObject>::Cast(info[0]);
172 JsWidth(sizeObj->GetProperty("width"));
173 JsHeight(sizeObj->GetProperty("height"));
174 }
175
176 } // namespace OHOS::Ace::Framework
177