1 |
|
2 | import '../globals';
|
3 |
|
4 | import * as bindableResources from '../ui/core/bindable/bindable-resources';
|
5 | import { CSSUtils } from '../css/system-classes';
|
6 | import { CoreTypes } from '../core-types';
|
7 | import { Trace } from '../trace';
|
8 | export * from './application-interfaces';
|
9 | export function hasLaunched() {
|
10 | return global.NativeScriptGlobals && global.NativeScriptGlobals.launched;
|
11 | }
|
12 | export const launchEvent = 'launch';
|
13 | export const suspendEvent = 'suspend';
|
14 | export const displayedEvent = 'displayed';
|
15 | export const backgroundEvent = 'background';
|
16 | export const foregroundEvent = 'foreground';
|
17 | export const resumeEvent = 'resume';
|
18 | export const exitEvent = 'exit';
|
19 | export const lowMemoryEvent = 'lowMemory';
|
20 | export const uncaughtErrorEvent = 'uncaughtError';
|
21 | export const discardedErrorEvent = 'discardedError';
|
22 | export const orientationChangedEvent = 'orientationChanged';
|
23 | export const systemAppearanceChangedEvent = 'systemAppearanceChanged';
|
24 | export const fontScaleChangedEvent = 'fontScaleChanged';
|
25 | const ORIENTATION_CSS_CLASSES = [`${CSSUtils.CLASS_PREFIX}${CoreTypes.DeviceOrientation.portrait}`, `${CSSUtils.CLASS_PREFIX}${CoreTypes.DeviceOrientation.landscape}`, `${CSSUtils.CLASS_PREFIX}${CoreTypes.DeviceOrientation.unknown}`];
|
26 | const SYSTEM_APPEARANCE_CSS_CLASSES = [`${CSSUtils.CLASS_PREFIX}${CoreTypes.SystemAppearance.light}`, `${CSSUtils.CLASS_PREFIX}${CoreTypes.SystemAppearance.dark}`];
|
27 | let cssFile = './app.css';
|
28 | export function getResources() {
|
29 | return bindableResources.get();
|
30 | }
|
31 | export function setResources(res) {
|
32 | bindableResources.set(res);
|
33 | }
|
34 | export const android = undefined;
|
35 | export const ios = undefined;
|
36 | export const on = global.NativeScriptGlobals.events.on.bind(global.NativeScriptGlobals.events);
|
37 | export const off = global.NativeScriptGlobals.events.off.bind(global.NativeScriptGlobals.events);
|
38 | export const notify = global.NativeScriptGlobals.events.notify.bind(global.NativeScriptGlobals.events);
|
39 | export const hasListeners = global.NativeScriptGlobals.events.hasListeners.bind(global.NativeScriptGlobals.events);
|
40 | let app;
|
41 | export function setApplication(instance) {
|
42 | app = instance;
|
43 |
|
44 | global.NativeScriptGlobals.appInstanceReady = true;
|
45 | }
|
46 | export function livesync(rootView, context) {
|
47 | global.NativeScriptGlobals.events.notify({ eventName: 'livesync', object: app });
|
48 | const liveSyncCore = global.__onLiveSyncCore;
|
49 | let reapplyAppStyles = false;
|
50 |
|
51 | if (context && context.path) {
|
52 | const styleExtensions = ['css', 'scss'];
|
53 | const appStylesFullFileName = getCssFileName();
|
54 | const appStylesFileName = appStylesFullFileName.substring(0, appStylesFullFileName.lastIndexOf('.') + 1);
|
55 | reapplyAppStyles = styleExtensions.some((ext) => context.path === appStylesFileName.concat(ext));
|
56 | }
|
57 |
|
58 | if (rootView && reapplyAppStyles) {
|
59 | rootView._onCssStateChange();
|
60 | }
|
61 | else if (liveSyncCore) {
|
62 | liveSyncCore(context);
|
63 | }
|
64 | }
|
65 | export function setCssFileName(cssFileName) {
|
66 | cssFile = cssFileName;
|
67 | global.NativeScriptGlobals.events.notify({
|
68 | eventName: 'cssChanged',
|
69 | object: app,
|
70 | cssFile: cssFileName,
|
71 | });
|
72 | }
|
73 | export function getCssFileName() {
|
74 | return cssFile;
|
75 | }
|
76 | export function loadAppCss() {
|
77 | try {
|
78 | global.NativeScriptGlobals.events.notify({
|
79 | eventName: 'loadAppCss',
|
80 | object: app,
|
81 | cssFile: getCssFileName(),
|
82 | });
|
83 | }
|
84 | catch (e) {
|
85 | if (Trace.isEnabled()) {
|
86 | Trace.write(`The app CSS file ${getCssFileName()} couldn't be loaded!`, Trace.categories.Style, Trace.messageType.warn);
|
87 | }
|
88 | }
|
89 | }
|
90 | function addCssClass(rootView, cssClass) {
|
91 | CSSUtils.pushToSystemCssClasses(cssClass);
|
92 | rootView.cssClasses.add(cssClass);
|
93 | }
|
94 | function removeCssClass(rootView, cssClass) {
|
95 | CSSUtils.removeSystemCssClass(cssClass);
|
96 | rootView.cssClasses.delete(cssClass);
|
97 | }
|
98 | function increaseStyleScopeApplicationCssSelectorVersion(rootView) {
|
99 | const styleScope = rootView._styleScope || (rootView.currentPage && rootView.currentPage._styleScope);
|
100 | if (styleScope) {
|
101 | styleScope._increaseApplicationCssSelectorVersion();
|
102 | }
|
103 | }
|
104 | export function applyCssClass(rootView, cssClasses, newCssClass) {
|
105 | if (!rootView.cssClasses.has(newCssClass)) {
|
106 | cssClasses.forEach((cssClass) => removeCssClass(rootView, cssClass));
|
107 | addCssClass(rootView, newCssClass);
|
108 | increaseStyleScopeApplicationCssSelectorVersion(rootView);
|
109 | rootView._onCssStateChange();
|
110 | }
|
111 | }
|
112 | export function orientationChanged(rootView, newOrientation) {
|
113 | if (!rootView) {
|
114 | return;
|
115 | }
|
116 | const newOrientationCssClass = `${CSSUtils.CLASS_PREFIX}${newOrientation}`;
|
117 | applyCssClass(rootView, ORIENTATION_CSS_CLASSES, newOrientationCssClass);
|
118 | const rootModalViews = rootView._getRootModalViews();
|
119 | rootModalViews.forEach((rootModalView) => {
|
120 | applyCssClass(rootModalView, ORIENTATION_CSS_CLASSES, newOrientationCssClass);
|
121 | });
|
122 | }
|
123 | export let autoSystemAppearanceChanged = true;
|
124 | export function setAutoSystemAppearanceChanged(value) {
|
125 | autoSystemAppearanceChanged = value;
|
126 | }
|
127 | export function systemAppearanceChanged(rootView, newSystemAppearance) {
|
128 | if (!rootView || !autoSystemAppearanceChanged) {
|
129 | return;
|
130 | }
|
131 | const newSystemAppearanceCssClass = `${CSSUtils.CLASS_PREFIX}${newSystemAppearance}`;
|
132 | applyCssClass(rootView, SYSTEM_APPEARANCE_CSS_CLASSES, newSystemAppearanceCssClass);
|
133 | const rootModalViews = rootView._getRootModalViews();
|
134 | rootModalViews.forEach((rootModalView) => {
|
135 | applyCssClass(rootModalView, SYSTEM_APPEARANCE_CSS_CLASSES, newSystemAppearanceCssClass);
|
136 | });
|
137 | }
|
138 | global.__onUncaughtError = function (error) {
|
139 | global.NativeScriptGlobals.events.notify({
|
140 | eventName: uncaughtErrorEvent,
|
141 | object: app,
|
142 | android: error,
|
143 | ios: error,
|
144 | error: error,
|
145 | });
|
146 | };
|
147 | global.__onDiscardedError = function (error) {
|
148 | global.NativeScriptGlobals.events.notify({
|
149 | eventName: discardedErrorEvent,
|
150 | object: app,
|
151 | error: error,
|
152 | });
|
153 | };
|
154 |
|
\ | No newline at end of file |