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 */ 15class Font { 16 /** 17 * Construct new instance of Font. 18 * initialzie with instanceId. 19 * @param instanceId obtained on the c++ side. 20 * @since 10 21 */ 22 constructor(instanceId) { 23 this.instanceId_ = instanceId; 24 this.ohos_font = globalThis.requireNapi('font'); 25 } 26 registerFont(options) { 27 __JSScopeUtil__.syncInstanceId(this.instanceId_); 28 this.ohos_font.registerFont(options); 29 __JSScopeUtil__.restoreInstanceId(); 30 } 31} 32 33class MediaQuery { 34 /** 35 * Construct new instance of MediaQuery. 36 * initialzie with instanceId. 37 * @param instanceId obtained on the c++ side. 38 * @since 10 39 */ 40 constructor(instanceId) { 41 this.instanceId_ = instanceId; 42 this.ohos_mediaQuery = globalThis.requireNapi('mediaquery'); 43 } 44 matchMediaSync(condition) { 45 __JSScopeUtil__.syncInstanceId(this.instanceId_); 46 let mediaQueryListener = this.ohos_mediaQuery.matchMediaSync(condition); 47 __JSScopeUtil__.restoreInstanceId(); 48 return mediaQueryListener; 49 } 50} 51 52class UIInspector { 53 /** 54 * Construct new instance of ArkUIInspector. 55 * initialzie with instanceId. 56 * @param instanceId obtained on the c++ side. 57 * @since 10 58 */ 59 constructor(instanceId) { 60 this.instanceId_ = instanceId; 61 this.ohos_UIInspector = globalThis.requireNapi('arkui.inspector'); 62 } 63 createComponentObserver(id) { 64 __JSScopeUtil__.syncInstanceId(this.instanceId_); 65 let componentObserver = this.ohos_UIInspector.createComponentObserver(id); 66 __JSScopeUtil__.restoreInstanceId(); 67 return componentObserver; 68 } 69} 70 71class UIContext { 72 /** 73 * Construct new instance of UIContext. 74 * initialzie with instanceId. 75 * @param instanceId obtained on the c++ side. 76 * @since 10 77 */ 78 constructor(instanceId) { 79 this.instanceId_ = instanceId; 80 } 81 82 getFont() { 83 this.font_ = new Font(this.instanceId_); 84 return this.font_; 85 } 86 87 getRouter() { 88 this.router_ = new Router(this.instanceId_); 89 return this.router_; 90 } 91 92 createAnimator(options) { 93 __JSScopeUtil__.syncInstanceId(this.instanceId_); 94 this.animator_ = globalThis.requireNapi('animator'); 95 let animatorResult = this.animator_.create(options); 96 __JSScopeUtil__.restoreInstanceId(); 97 return animatorResult; 98 } 99 100 getPromptAction() { 101 this.promptAction_ = new PromptAction(this.instanceId_); 102 return this.promptAction_; 103 } 104 105 getMediaQuery() { 106 this.mediaQuery_ = new MediaQuery(this.instanceId_); 107 return this.mediaQuery_; 108 } 109 110 getUIInspector(){ 111 this.UIInspector_ = new UIInspector(this.instanceId_); 112 return this.UIInspector_; 113 } 114 115 getComponentUtils() { 116 if(this.componentUtils_ == null) { 117 this.componentUtils_ = new ComponentUtils(this.instanceId_); 118 } 119 return this.componentUtils_; 120 } 121 animateTo(value, event) { 122 __JSScopeUtil__.syncInstanceId(this.instanceId_); 123 Context.animateTo(value, event); 124 __JSScopeUtil__.restoreInstanceId(); 125 } 126 127 showAlertDialog(options) { 128 __JSScopeUtil__.syncInstanceId(this.instanceId_); 129 AlertDialog.show(options); 130 __JSScopeUtil__.restoreInstanceId(); 131 } 132 133 showActionSheet(value) { 134 __JSScopeUtil__.syncInstanceId(this.instanceId_); 135 ActionSheet.show(value); 136 __JSScopeUtil__.restoreInstanceId(); 137 } 138 139 showDatePickerDialog(options) { 140 __JSScopeUtil__.syncInstanceId(this.instanceId_); 141 DatePickerDialog.show(options); 142 __JSScopeUtil__.restoreInstanceId(); 143 } 144 145 showTimePickerDialog(options) { 146 __JSScopeUtil__.syncInstanceId(this.instanceId_); 147 TimePickerDialog.show(options); 148 __JSScopeUtil__.restoreInstanceId(); 149 } 150 151 showTextPickerDialog(options) { 152 __JSScopeUtil__.syncInstanceId(this.instanceId_); 153 TextPickerDialog.show(options); 154 __JSScopeUtil__.restoreInstanceId(); 155 } 156 157 runScopedTask(callback) { 158 __JSScopeUtil__.syncInstanceId(this.instanceId_); 159 if (callback !== undefined) { 160 callback(); 161 } 162 __JSScopeUtil__.restoreInstanceId(); 163 } 164} 165class ComponentUtils { 166 /** 167 * Construct new instance of ComponentUtils. 168 * initialzie with instanceId. 169 * @param instanceId obtained on the c++ side. 170 * @since 10 171 */ 172 constructor(instanceId) { 173 this.instanceId_ = instanceId; 174 this.ohos_componentUtils = globalThis.requireNapi('componentUtils'); 175 } 176 getRectangleById(id) { 177 __JSScopeUtil__.syncInstanceId(this.instanceId_); 178 let componentInformation = this.ohos_componentUtils.getRectangleById(id); 179 __JSScopeUtil__.restoreInstanceId(); 180 return componentInformation; 181 } 182} 183 184class Router { 185 /** 186 * Construct new instance of Font. 187 * initialzie with instanceId. 188 * @param instanceId obtained on the c++ side. 189 * @since 10 190 */ 191 constructor(instanceId) { 192 this.instanceId_ = instanceId; 193 this.ohos_router = globalThis.requireNapi('router'); 194 } 195 196 pushUrl(options, modeOrCallback, callback) { 197 __JSScopeUtil__.syncInstanceId(this.instanceId_); 198 if (typeof callback === 'undefined' && typeof modeOrCallback === 'undefined') { 199 let promise = this.ohos_router.pushUrl(options); 200 __JSScopeUtil__.restoreInstanceId(); 201 return promise; 202 } 203 else if (typeof callback !== 'undefined' && typeof modeOrCallback !== 'undefined') { 204 this.ohos_router.pushUrl(options, modeOrCallback, callback); 205 __JSScopeUtil__.restoreInstanceId(); 206 } 207 else if (typeof callback === 'undefined' && typeof modeOrCallback !== 'undefined') { 208 let promise = this.ohos_router.pushUrl(options, modeOrCallback); 209 __JSScopeUtil__.restoreInstanceId(); 210 if(promise) 211 { 212 return promise; 213 } 214 } 215 } 216 217 replaceUrl(options, modeOrCallback, callback) { 218 __JSScopeUtil__.syncInstanceId(this.instanceId_); 219 if (typeof callback === 'undefined' && typeof modeOrCallback === 'undefined') { 220 let promise = this.ohos_router.replaceUrl(options); 221 __JSScopeUtil__.restoreInstanceId(); 222 return promise; 223 } 224 else if (typeof callback !== 'undefined' && typeof modeOrCallback !== 'undefined') { 225 this.ohos_router.replaceUrl(options, modeOrCallback, callback); 226 __JSScopeUtil__.restoreInstanceId(); 227 } 228 else if (typeof callback === 'undefined' && typeof modeOrCallback !== 'undefined') { 229 let promise = this.ohos_router.replaceUrl(options, modeOrCallback); 230 __JSScopeUtil__.restoreInstanceId(); 231 if(promise) 232 { 233 return promise; 234 } 235 } 236 } 237 238 back(options) { 239 __JSScopeUtil__.syncInstanceId(this.instanceId_); 240 this.ohos_router.back(options); 241 __JSScopeUtil__.restoreInstanceId(); 242 } 243 244 clear() { 245 __JSScopeUtil__.syncInstanceId(this.instanceId_); 246 this.ohos_router.clear(); 247 __JSScopeUtil__.restoreInstanceId(); 248 } 249 250 getLength() { 251 __JSScopeUtil__.syncInstanceId(this.instanceId_); 252 let result = this.ohos_router.getLength(); 253 __JSScopeUtil__.restoreInstanceId(); 254 return result; 255 } 256 257 getState() { 258 __JSScopeUtil__.syncInstanceId(this.instanceId_); 259 let state = this.ohos_router.getState(); 260 __JSScopeUtil__.restoreInstanceId(); 261 return state; 262 } 263 264 showAlertBeforeBackPage(options) { 265 __JSScopeUtil__.syncInstanceId(this.instanceId_); 266 this.ohos_router.showAlertBeforeBackPage(options); 267 __JSScopeUtil__.restoreInstanceId(); 268 } 269 270 hideAlertBeforeBackPage() { 271 __JSScopeUtil__.syncInstanceId(this.instanceId_); 272 this.ohos_router.hideAlertBeforeBackPage(); 273 __JSScopeUtil__.restoreInstanceId(); 274 } 275 276 getParams() { 277 __JSScopeUtil__.syncInstanceId(this.instanceId_); 278 let object = this.ohos_router.getParams(); 279 __JSScopeUtil__.restoreInstanceId(); 280 return object; 281 } 282 283 pushNamedRoute(options, modeOrCallback, callback) { 284 __JSScopeUtil__.syncInstanceId(this.instanceId_); 285 if (typeof callback === 'undefined' && typeof modeOrCallback === 'undefined') { 286 let promise = this.ohos_router.pushNamedRoute(options); 287 __JSScopeUtil__.restoreInstanceId(); 288 return promise; 289 } 290 else if (typeof callback !== 'undefined' && typeof modeOrCallback !== 'undefined') { 291 this.ohos_router.pushNamedRoute(options, modeOrCallback, callback); 292 __JSScopeUtil__.restoreInstanceId(); 293 } 294 else if (typeof callback === 'undefined' && typeof modeOrCallback !== 'undefined') { 295 let promise = this.ohos_router.pushNamedRoute(options, modeOrCallback); 296 __JSScopeUtil__.restoreInstanceId(); 297 if(promise) 298 { 299 return promise; 300 } 301 } 302 } 303 304 replaceNamedRoute(options, modeOrCallback, callback) { 305 __JSScopeUtil__.syncInstanceId(this.instanceId_); 306 if (typeof callback === 'undefined' && typeof modeOrCallback === 'undefined') { 307 let promise = this.ohos_router.replaceNamedRoute(options); 308 __JSScopeUtil__.restoreInstanceId(); 309 return promise; 310 } 311 else if (typeof callback !== 'undefined' && typeof modeOrCallback !== 'undefined') { 312 this.ohos_router.replaceNamedRoute(options, modeOrCallback, callback); 313 __JSScopeUtil__.restoreInstanceId(); 314 } 315 else if (typeof callback === 'undefined' && typeof modeOrCallback !== 'undefined') { 316 let promise = this.ohos_router.replaceNamedRoute(options, modeOrCallback); 317 __JSScopeUtil__.restoreInstanceId(); 318 if(promise) 319 { 320 return promise; 321 } 322 } 323 } 324} 325 326class PromptAction { 327 /** 328 * Construct new instance of PromptAction. 329 * initialzie with instanceId. 330 * @param instanceId obtained on the c++ side. 331 * @since 10 332 */ 333 constructor(instanceId) { 334 this.instanceId_ = instanceId; 335 this.ohos_prompt = globalThis.requireNapi('promptAction'); 336 } 337 338 showToast(options) { 339 __JSScopeUtil__.syncInstanceId(this.instanceId_); 340 this.ohos_prompt.showToast(options); 341 __JSScopeUtil__.restoreInstanceId(); 342 } 343 344 showDialog(options, callback) { 345 __JSScopeUtil__.syncInstanceId(this.instanceId_); 346 if (typeof callback !== 'undefined') { 347 this.ohos_prompt.showDialog(options, callback); 348 __JSScopeUtil__.restoreInstanceId(); 349 } 350 else { 351 let showDialogSuccessResponse = this.ohos_prompt.showDialog(options); 352 __JSScopeUtil__.restoreInstanceId(); 353 return showDialogSuccessResponse; 354 } 355 } 356 357 showActionMenu(options, callback) { 358 __JSScopeUtil__.syncInstanceId(this.instanceId_); 359 if (typeof callback !== 'undefined') { 360 this.ohos_prompt.showActionMenu(options, callback); 361 __JSScopeUtil__.restoreInstanceId(); 362 } 363 else { 364 let actionMenuSuccessResponse = this.ohos_prompt.showActionMenu(options); 365 __JSScopeUtil__.restoreInstanceId(); 366 return actionMenuSuccessResponse; 367 } 368 } 369} 370 371/** 372 * Get UIContext instance. 373 * @param instanceId obtained on the c++ side. 374 * @returns UIContext instance. 375 */ 376function __getUIContext__(instanceId) { 377 return new UIContext(instanceId); 378} 379