• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16class Font {
17    /**
18     * Construct new instance of Font.
19     * initialzie with instanceId.
20     * @param instanceId obtained on the c++ side.
21     * @since 10
22     */
23    constructor(instanceId) {
24        this.instanceId_ = instanceId;
25        this.ohos_font = globalThis.requireNapi('font');
26    }
27    registerFont(options) {
28        __JSScopeUtil__.syncInstanceId(this.instanceId_);
29        this.ohos_font.registerFont(options);
30        __JSScopeUtil__.restoreInstanceId();
31    }
32
33    getSystemFontList() {
34        __JSScopeUtil__.syncInstanceId(this.instanceId_);
35        let arrayResult_ = this.ohos_font.getSystemFontList();
36        __JSScopeUtil__.restoreInstanceId();
37        return arrayResult_;
38    }
39
40    getFontByName(fontName) {
41        __JSScopeUtil__.syncInstanceId(this.instanceId_);
42        let result_ = this.ohos_font.getFontByName(fontName);
43        __JSScopeUtil__.restoreInstanceId();
44        return result_;
45    }
46}
47
48class MediaQuery {
49    /**
50     * Construct new instance of MediaQuery.
51     * initialzie with instanceId.
52     * @param instanceId obtained on the c++ side.
53     * @since 10
54     */
55    constructor(instanceId) {
56        this.instanceId_ = instanceId;
57        this.ohos_mediaQuery = globalThis.requireNapi('mediaquery');
58    }
59    matchMediaSync(condition) {
60        __JSScopeUtil__.syncInstanceId(this.instanceId_);
61        let mediaQueryListener = this.ohos_mediaQuery.matchMediaSync(condition);
62        __JSScopeUtil__.restoreInstanceId();
63        return mediaQueryListener;
64    }
65}
66
67class UIInspector {
68    /**
69     * Construct new instance of ArkUIInspector.
70     * initialize with instanceId.
71     * @param instanceId obtained on the c++ side.
72     * @since 10
73     */
74    constructor(instanceId) {
75        this.instanceId_ = instanceId;
76        this.ohos_UIInspector = globalThis.requireNapi('arkui.inspector');
77    }
78    createComponentObserver(id) {
79        __JSScopeUtil__.syncInstanceId(this.instanceId_);
80        let componentObserver = this.ohos_UIInspector.createComponentObserver(id);
81        __JSScopeUtil__.restoreInstanceId();
82        return componentObserver;
83    }
84}
85
86class DragController {
87    /**
88     * Construct new instance of DragController.
89     * initialize with instanceId.
90     * @param instanceId obtained on the c++ side.
91     * @since 11
92     */
93    constructor(instanceId) {
94        this.instanceId_ = instanceId;
95        this.ohos_dragController = globalThis.requireNapi('arkui.dragController');
96    }
97
98    executeDrag(custom, dragInfo, callback) {
99        __JSScopeUtil__.syncInstanceId(this.instanceId_);
100        if (typeof callback !== 'undefined') {
101            this.ohos_dragController.executeDrag(custom, dragInfo, callback);
102            __JSScopeUtil__.restoreInstanceId();
103        } else {
104            let eventPromise = this.ohos_dragController.executeDrag(custom, dragInfo);
105            __JSScopeUtil__.restoreInstanceId();
106            return eventPromise;
107        }
108    }
109
110    createDragAction(customs, dragInfo) {
111        __JSScopeUtil__.syncInstanceId(this.instanceId_);
112        let dragAction = this.ohos_dragController.createDragAction(customs, dragInfo);
113        __JSScopeUtil__.restoreInstanceId();
114        return dragAction;
115    }
116
117    getDragPreview() {
118        __JSScopeUtil__.syncInstanceId(this.instanceId_);
119        let dragPreview = this.ohos_dragController.getDragPreview();
120        __JSScopeUtil__.restoreInstanceId();
121        return dragPreview;
122    }
123}
124
125class UIObserver {
126    constructor(instanceId) {
127        this.instanceId_ = instanceId;
128        this.ohos_observer = globalThis.requireNapi('arkui.observer');
129    }
130    on(...args) {
131        __JSScopeUtil__.syncInstanceId(this.instanceId_);
132        this.ohos_observer.on(...args);
133        __JSScopeUtil__.restoreInstanceId();
134    }
135    off(...args) {
136        __JSScopeUtil__.syncInstanceId(this.instanceId_);
137        this.ohos_observer.off(...args);
138        __JSScopeUtil__.restoreInstanceId();
139    }
140}
141
142
143class UIContext {
144    /**
145     * Construct new instance of UIContext.
146     * initialzie with instanceId.
147     * @param instanceId obtained on the c++ side.
148     * @since 10
149     */
150    constructor(instanceId) {
151        this.instanceId_ = instanceId;
152    }
153
154    getDragController() {
155        this.dragController_ = new DragController(this.instanceId_);
156        return this.dragController_;
157    }
158
159    getFont() {
160        this.font_ = new Font(this.instanceId_);
161        return this.font_;
162    }
163
164    getRouter() {
165        this.router_ = new Router(this.instanceId_);
166        return this.router_;
167    }
168
169    createAnimator(options) {
170        __JSScopeUtil__.syncInstanceId(this.instanceId_);
171        this.animator_ = globalThis.requireNapi('animator');
172        let animatorResult = this.animator_.create(options);
173        __JSScopeUtil__.restoreInstanceId();
174        return animatorResult;
175    }
176
177    getPromptAction() {
178        this.promptAction_ = new PromptAction(this.instanceId_);
179        return this.promptAction_;
180    }
181
182    getMediaQuery() {
183        this.mediaQuery_ = new MediaQuery(this.instanceId_);
184        return this.mediaQuery_;
185    }
186
187    getUIInspector() {
188        this.UIInspector_ = new UIInspector(this.instanceId_);
189        return this.UIInspector_;
190    }
191
192    getComponentUtils() {
193        if (this.componentUtils_ == null) {
194            this.componentUtils_ = new ComponentUtils(this.instanceId_);
195        }
196        return this.componentUtils_;
197    }
198    animateTo(value, event) {
199        __JSScopeUtil__.syncInstanceId(this.instanceId_);
200        Context.animateTo(value, event);
201        __JSScopeUtil__.restoreInstanceId();
202    }
203
204    showAlertDialog(options) {
205        __JSScopeUtil__.syncInstanceId(this.instanceId_);
206        AlertDialog.show(options);
207        __JSScopeUtil__.restoreInstanceId();
208    }
209
210    showActionSheet(value) {
211        __JSScopeUtil__.syncInstanceId(this.instanceId_);
212        ActionSheet.show(value);
213        __JSScopeUtil__.restoreInstanceId();
214    }
215
216    showDatePickerDialog(options) {
217        __JSScopeUtil__.syncInstanceId(this.instanceId_);
218        DatePickerDialog.show(options);
219        __JSScopeUtil__.restoreInstanceId();
220    }
221
222    showTimePickerDialog(options) {
223        __JSScopeUtil__.syncInstanceId(this.instanceId_);
224        TimePickerDialog.show(options);
225        __JSScopeUtil__.restoreInstanceId();
226    }
227
228    showTextPickerDialog(options) {
229        __JSScopeUtil__.syncInstanceId(this.instanceId_);
230        TextPickerDialog.show(options);
231        __JSScopeUtil__.restoreInstanceId();
232    }
233
234    runScopedTask(callback) {
235        __JSScopeUtil__.syncInstanceId(this.instanceId_);
236        if (callback !== undefined) {
237            callback();
238        }
239        __JSScopeUtil__.restoreInstanceId();
240    }
241
242    setKeyboardAvoidMode(value) {
243        __JSScopeUtil__.syncInstanceId(this.instanceId_);
244        __KeyboardAvoid__.setKeyboardAvoid(value);
245        __JSScopeUtil__.restoreInstanceId();
246    }
247
248    getKeyboardAvoidMode() {
249        __JSScopeUtil__.syncInstanceId(this.instanceId_);
250        let keyBoardAvoidMode = __KeyboardAvoid__.getKeyboardAvoid();
251        __JSScopeUtil__.restoreInstanceId();
252        return keyBoardAvoidMode;
253    }
254
255    getAtomicServiceBar() {
256        const bundleMgr = globalThis.requireNapi('bundle.bundleManager');
257        let data = bundleMgr.getBundleInfoForSelfSync(bundleMgr.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION);
258        if (data.appInfo.bundleType == 1) {
259            this.atomServiceBar = new AtomicServiceBar(this.instanceId_);
260            return this.atomServiceBar;
261        } else {
262            return undefined;
263        }
264    }
265
266    getUIObserver() {
267        this.observer_ = new UIObserver(this.instanceId_);
268        return this.observer_;
269    }
270
271    keyframeAnimateTo(param, keyframes) {
272        __JSScopeUtil__.syncInstanceId(this.instanceId_);
273        Context.keyframeAnimateTo(param, keyframes);
274        __JSScopeUtil__.restoreInstanceId();
275    }
276}
277class ComponentUtils {
278    /**
279     * Construct new instance of ComponentUtils.
280     * initialzie with instanceId.
281     * @param instanceId obtained on the c++ side.
282     * @since 10
283     */
284    constructor(instanceId) {
285        this.instanceId_ = instanceId;
286        this.ohos_componentUtils = globalThis.requireNapi('arkui.componentUtils');
287    }
288    getRectangleById(id) {
289        __JSScopeUtil__.syncInstanceId(this.instanceId_);
290        let componentInformation = this.ohos_componentUtils.getRectangleById(id);
291        __JSScopeUtil__.restoreInstanceId();
292        return componentInformation;
293    }
294}
295
296class Router {
297    /**
298     * Construct new instance of Font.
299     * initialzie with instanceId.
300     * @param instanceId obtained on the c++ side.
301     * @since 10
302     */
303    constructor(instanceId) {
304        this.instanceId_ = instanceId;
305        this.ohos_router = globalThis.requireNapi('router');
306    }
307
308    pushUrl(options, modeOrCallback, callback) {
309        __JSScopeUtil__.syncInstanceId(this.instanceId_);
310        if (typeof callback === 'undefined' && typeof modeOrCallback === 'undefined') {
311            let promise = this.ohos_router.pushUrl(options);
312            __JSScopeUtil__.restoreInstanceId();
313            return promise;
314        }
315        else if (typeof callback !== 'undefined' && typeof modeOrCallback !== 'undefined') {
316            this.ohos_router.pushUrl(options, modeOrCallback, callback);
317            __JSScopeUtil__.restoreInstanceId();
318        }
319        else if (typeof callback === 'undefined' && typeof modeOrCallback !== 'undefined') {
320            let promise = this.ohos_router.pushUrl(options, modeOrCallback);
321            __JSScopeUtil__.restoreInstanceId();
322            if (promise) {
323                return promise;
324            }
325        }
326    }
327
328    replaceUrl(options, modeOrCallback, callback) {
329        __JSScopeUtil__.syncInstanceId(this.instanceId_);
330        if (typeof callback === 'undefined' && typeof modeOrCallback === 'undefined') {
331            let promise = this.ohos_router.replaceUrl(options);
332            __JSScopeUtil__.restoreInstanceId();
333            return promise;
334        }
335        else if (typeof callback !== 'undefined' && typeof modeOrCallback !== 'undefined') {
336            this.ohos_router.replaceUrl(options, modeOrCallback, callback);
337            __JSScopeUtil__.restoreInstanceId();
338        }
339        else if (typeof callback === 'undefined' && typeof modeOrCallback !== 'undefined') {
340            let promise = this.ohos_router.replaceUrl(options, modeOrCallback);
341            __JSScopeUtil__.restoreInstanceId();
342            if (promise) {
343                return promise;
344            }
345        }
346    }
347
348    back(options) {
349        __JSScopeUtil__.syncInstanceId(this.instanceId_);
350        this.ohos_router.back(options);
351        __JSScopeUtil__.restoreInstanceId();
352    }
353
354    clear() {
355        __JSScopeUtil__.syncInstanceId(this.instanceId_);
356        this.ohos_router.clear();
357        __JSScopeUtil__.restoreInstanceId();
358    }
359
360    getLength() {
361        __JSScopeUtil__.syncInstanceId(this.instanceId_);
362        let result = this.ohos_router.getLength();
363        __JSScopeUtil__.restoreInstanceId();
364        return result;
365    }
366
367    getState() {
368        __JSScopeUtil__.syncInstanceId(this.instanceId_);
369        let state = this.ohos_router.getState();
370        __JSScopeUtil__.restoreInstanceId();
371        return state;
372    }
373
374    showAlertBeforeBackPage(options) {
375        __JSScopeUtil__.syncInstanceId(this.instanceId_);
376        this.ohos_router.showAlertBeforeBackPage(options);
377        __JSScopeUtil__.restoreInstanceId();
378    }
379
380    hideAlertBeforeBackPage() {
381        __JSScopeUtil__.syncInstanceId(this.instanceId_);
382        this.ohos_router.hideAlertBeforeBackPage();
383        __JSScopeUtil__.restoreInstanceId();
384    }
385
386    getParams() {
387        __JSScopeUtil__.syncInstanceId(this.instanceId_);
388        let object = this.ohos_router.getParams();
389        __JSScopeUtil__.restoreInstanceId();
390        return object;
391    }
392
393    pushNamedRoute(options, modeOrCallback, callback) {
394        __JSScopeUtil__.syncInstanceId(this.instanceId_);
395        if (typeof callback === 'undefined' && typeof modeOrCallback === 'undefined') {
396            let promise = this.ohos_router.pushNamedRoute(options);
397            __JSScopeUtil__.restoreInstanceId();
398            return promise;
399        }
400        else if (typeof callback !== 'undefined' && typeof modeOrCallback !== 'undefined') {
401            this.ohos_router.pushNamedRoute(options, modeOrCallback, callback);
402            __JSScopeUtil__.restoreInstanceId();
403        }
404        else if (typeof callback === 'undefined' && typeof modeOrCallback !== 'undefined') {
405            let promise = this.ohos_router.pushNamedRoute(options, modeOrCallback);
406            __JSScopeUtil__.restoreInstanceId();
407            if (promise) {
408                return promise;
409            }
410        }
411    }
412
413    replaceNamedRoute(options, modeOrCallback, callback) {
414        __JSScopeUtil__.syncInstanceId(this.instanceId_);
415        if (typeof callback === 'undefined' && typeof modeOrCallback === 'undefined') {
416            let promise = this.ohos_router.replaceNamedRoute(options);
417            __JSScopeUtil__.restoreInstanceId();
418            return promise;
419        }
420        else if (typeof callback !== 'undefined' && typeof modeOrCallback !== 'undefined') {
421            this.ohos_router.replaceNamedRoute(options, modeOrCallback, callback);
422            __JSScopeUtil__.restoreInstanceId();
423        }
424        else if (typeof callback === 'undefined' && typeof modeOrCallback !== 'undefined') {
425            let promise = this.ohos_router.replaceNamedRoute(options, modeOrCallback);
426            __JSScopeUtil__.restoreInstanceId();
427            if (promise) {
428                return promise;
429            }
430        }
431    }
432}
433
434class PromptAction {
435    /**
436     * Construct new instance of PromptAction.
437     * initialzie with instanceId.
438     * @param instanceId obtained on the c++ side.
439     * @since 10
440     */
441    constructor(instanceId) {
442        this.instanceId_ = instanceId;
443        this.ohos_prompt = globalThis.requireNapi('promptAction');
444    }
445
446    showToast(options) {
447        __JSScopeUtil__.syncInstanceId(this.instanceId_);
448        this.ohos_prompt.showToast(options);
449        __JSScopeUtil__.restoreInstanceId();
450    }
451
452    showDialog(options, callback) {
453        __JSScopeUtil__.syncInstanceId(this.instanceId_);
454        if (typeof callback !== 'undefined') {
455            this.ohos_prompt.showDialog(options, callback);
456            __JSScopeUtil__.restoreInstanceId();
457        }
458        else {
459            let showDialogSuccessResponse = this.ohos_prompt.showDialog(options);
460            __JSScopeUtil__.restoreInstanceId();
461            return showDialogSuccessResponse;
462        }
463    }
464
465    showActionMenu(options, callback) {
466        __JSScopeUtil__.syncInstanceId(this.instanceId_);
467        if (typeof callback !== 'undefined') {
468            this.ohos_prompt.showActionMenu(options, callback);
469            __JSScopeUtil__.restoreInstanceId();
470        }
471        else {
472            let actionMenuSuccessResponse = this.ohos_prompt.showActionMenu(options);
473            __JSScopeUtil__.restoreInstanceId();
474            return actionMenuSuccessResponse;
475        }
476    }
477}
478
479class AtomicServiceBar {
480    /**
481     * Construct new instance of AtomicServiceBar.
482     * initialzie with instanceId.
483     * @param instanceId obtained on the c++ side.
484     * @since 11
485     */
486    constructor(instanceId) {
487        this.instanceId_ = instanceId;
488        this.ohos_atomicServiceBar = globalThis.requireNapi('atomicservicebar');
489    }
490
491    setVisible(visible) {
492        __JSScopeUtil__.syncInstanceId(this.instanceId_);
493        this.ohos_atomicServiceBar.setVisible(visible);
494        __JSScopeUtil__.restoreInstanceId();
495    }
496
497    setBackgroundColor(color) {
498        __JSScopeUtil__.syncInstanceId(this.instanceId_);
499        this.ohos_atomicServiceBar.setBackgroundColor(color);
500        __JSScopeUtil__.restoreInstanceId();
501    }
502
503    setTitleContent(content) {
504        __JSScopeUtil__.syncInstanceId(this.instanceId_);
505        this.ohos_atomicServiceBar.setTitleContent(content);
506        __JSScopeUtil__.restoreInstanceId();
507    }
508
509    setTitleFontStyle(font) {
510        __JSScopeUtil__.syncInstanceId(this.instanceId_);
511        this.ohos_atomicServiceBar.setTitleFontStyle(font);
512        __JSScopeUtil__.restoreInstanceId();
513    }
514
515    setIconColor(color) {
516        __JSScopeUtil__.syncInstanceId(this.instanceId_);
517        this.ohos_atomicServiceBar.setIconColor(color);
518        __JSScopeUtil__.restoreInstanceId();
519    }
520}
521
522/**
523 * Get UIContext instance.
524 * @param instanceId obtained on the c++ side.
525 * @returns UIContext instance.
526 */
527function __getUIContext__(instanceId) {
528    return new UIContext(instanceId);
529}
530
531/**
532 * check regex valid
533 * @param pattern regex string
534 * @returns valid result
535 */
536function __checkRegexValid__(pattern) {
537    let result = true;
538    try {
539        new RegExp(pattern);
540    } catch (error) {
541        result = false;
542    } finally {
543        return result;
544    }
545}