UNPKG

6.33 kBJavaScriptView Raw
1// Require globals first so that snapshot takes __extends function.
2import '../globals';
3// Requires
4import * as bindableResources from '../ui/core/bindable/bindable-resources';
5import { CSSUtils } from '../css/system-classes';
6import { CoreTypes } from '../core-types';
7import { Trace } from '../trace';
8export * from './application-interfaces';
9export function hasLaunched() {
10 return global.NativeScriptGlobals && global.NativeScriptGlobals.launched;
11}
12export const launchEvent = 'launch';
13export const suspendEvent = 'suspend';
14export const displayedEvent = 'displayed';
15export const backgroundEvent = 'background';
16export const foregroundEvent = 'foreground';
17export const resumeEvent = 'resume';
18export const exitEvent = 'exit';
19export const lowMemoryEvent = 'lowMemory';
20export const uncaughtErrorEvent = 'uncaughtError';
21export const discardedErrorEvent = 'discardedError';
22export const orientationChangedEvent = 'orientationChanged';
23export const systemAppearanceChangedEvent = 'systemAppearanceChanged';
24export const fontScaleChangedEvent = 'fontScaleChanged';
25const ORIENTATION_CSS_CLASSES = [`${CSSUtils.CLASS_PREFIX}${CoreTypes.DeviceOrientation.portrait}`, `${CSSUtils.CLASS_PREFIX}${CoreTypes.DeviceOrientation.landscape}`, `${CSSUtils.CLASS_PREFIX}${CoreTypes.DeviceOrientation.unknown}`];
26const SYSTEM_APPEARANCE_CSS_CLASSES = [`${CSSUtils.CLASS_PREFIX}${CoreTypes.SystemAppearance.light}`, `${CSSUtils.CLASS_PREFIX}${CoreTypes.SystemAppearance.dark}`];
27let cssFile = './app.css';
28export function getResources() {
29 return bindableResources.get();
30}
31export function setResources(res) {
32 bindableResources.set(res);
33}
34export const android = undefined;
35export const ios = undefined;
36export const on = global.NativeScriptGlobals.events.on.bind(global.NativeScriptGlobals.events);
37export const off = global.NativeScriptGlobals.events.off.bind(global.NativeScriptGlobals.events);
38export const notify = global.NativeScriptGlobals.events.notify.bind(global.NativeScriptGlobals.events);
39export const hasListeners = global.NativeScriptGlobals.events.hasListeners.bind(global.NativeScriptGlobals.events);
40let app;
41export function setApplication(instance) {
42 app = instance;
43 // signal when the application instance is ready globally
44 global.NativeScriptGlobals.appInstanceReady = true;
45}
46export function livesync(rootView, context) {
47 global.NativeScriptGlobals.events.notify({ eventName: 'livesync', object: app });
48 const liveSyncCore = global.__onLiveSyncCore;
49 let reapplyAppStyles = false;
50 // ModuleContext is available only for Hot Module Replacement
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 // Handle application styles
58 if (rootView && reapplyAppStyles) {
59 rootView._onCssStateChange();
60 }
61 else if (liveSyncCore) {
62 liveSyncCore(context);
63 }
64}
65export function setCssFileName(cssFileName) {
66 cssFile = cssFileName;
67 global.NativeScriptGlobals.events.notify({
68 eventName: 'cssChanged',
69 object: app,
70 cssFile: cssFileName,
71 });
72}
73export function getCssFileName() {
74 return cssFile;
75}
76export 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}
90function addCssClass(rootView, cssClass) {
91 CSSUtils.pushToSystemCssClasses(cssClass);
92 rootView.cssClasses.add(cssClass);
93}
94function removeCssClass(rootView, cssClass) {
95 CSSUtils.removeSystemCssClass(cssClass);
96 rootView.cssClasses.delete(cssClass);
97}
98function increaseStyleScopeApplicationCssSelectorVersion(rootView) {
99 const styleScope = rootView._styleScope || (rootView.currentPage && rootView.currentPage._styleScope);
100 if (styleScope) {
101 styleScope._increaseApplicationCssSelectorVersion();
102 }
103}
104export 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}
112export 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}
123export let autoSystemAppearanceChanged = true;
124export function setAutoSystemAppearanceChanged(value) {
125 autoSystemAppearanceChanged = value;
126}
127export 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}
138global.__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};
147global.__onDiscardedError = function (error) {
148 global.NativeScriptGlobals.events.notify({
149 eventName: discardedErrorEvent,
150 object: app,
151 error: error,
152 });
153};
154//# sourceMappingURL=application-common.js.map
\No newline at end of file