Commit 0e51eb3e authored by Christopher Mark Fullarton's avatar Christopher Mark Fullarton
Browse files

Adds checks if the app is running in cordova environment

parent 7a53a8b1
......@@ -37,7 +37,7 @@ export class HeaderComponent implements OnInit, OnDestroy {
public isCheckingForUpdates: boolean;
public readonly version;
public readonly isCordovaApp: boolean = isPlatformServer(this.platformId) || !!((window as any)._cordovaNative);
public readonly isCordovaApp: boolean = !isPlatformServer(this.platformId) && Boolean((window as any)._cordovaNative);
get storage(): StorageEstimate {
return this._storage;
......
......@@ -10,34 +10,33 @@ export const AppInitializerFactory = (
settingsService: SettingsService,
injector: Injector
) => () => {
if (isPlatformServer(platformId)) {
return;
}
return new Promise(resolve => {
return settingsService.initServerSideLoadedSettings().toPromise().finally(() => {
if (isPlatformServer(platformId)) {
resolve(null);
return;
}
console.log('APP INIT - SETTINGS RECEIVED');
return settingsService.initServerSideLoadedSettings().toPromise().finally(() => {
console.log('APP INIT - SETTINGS RECEIVED');
const dom = ɵgetDOM();
const styles = Array.prototype.slice.apply(
dom.getDefaultDocument().querySelectorAll('style[ng-transition]'),
);
styles.forEach(el => {
// Remove ng-transition attribute to prevent Angular appInitializerFactory
// to remove server styles before preboot complete
el.removeAttribute('ng-transition');
});
dom.getDefaultDocument().addEventListener('PrebootComplete', () => {
// After preboot complete, remove the server scripts
styles.forEach(el => dom.remove(el));
});
return new Promise(resolve => {
const dom = ɵgetDOM();
const styles = Array.prototype.slice.apply(
dom.getDefaultDocument().querySelectorAll('style[ng-transition]'),
);
styles.forEach(el => {
// Remove ng-transition attribute to prevent Angular appInitializerFactory
// to remove server styles before preboot complete
el.removeAttribute('ng-transition');
});
dom.getDefaultDocument().addEventListener('PrebootComplete', () => {
// After preboot complete, remove the server scripts
styles.forEach(el => dom.remove(el));
});
const locationInitialized = injector.get(LOCATION_INITIALIZED, Promise.resolve(null));
return locationInitialized.then(locationInittializedHandler(translate, resolve));
}).catch(err => {
console.log('APP INIT - fetching setting threw error which is awkward since they are cached locally', err);
});
const locationInitialized = injector.get(LOCATION_INITIALIZED, Promise.resolve(null));
return locationInitialized.then(locationInittializedHandler(translate, resolve));
});
}).catch(err => {
console.log('APP INIT - fetching setting threw error which is awkward since they are cached locally', err);
});
};
......
......@@ -28,7 +28,7 @@ export class LivePreviewComponent implements OnInit, OnDestroy {
private readonly _destroy = new Subject();
private _questionIndex: number;
public readonly isCordovaApp: boolean = isPlatformServer(this.platformId) || !!((window as any)._cordovaNative);
public readonly isCordovaApp: boolean = !isPlatformServer(this.platformId) && Boolean((window as any)._cordovaNative);
public readonly ENVIRONMENT_TYPE = LivePreviewEnvironment;
public dataSource: Array<string>;
......
......@@ -154,7 +154,7 @@ export class HomeComponent implements OnInit, OnDestroy {
public ngOnInit(): void {
this.activatedRoute.data.pipe(takeUntil(this._destroy)).subscribe(data => {
this.twitterService.twitterEnabled = this.settingsService.frontEnv?.twitterEnabled && !data.disableTwitter;
this.twitterService.twitterEnabled = (this.settingsService.frontEnv?.twitterEnabled && !data.disableTwitter) ?? false;
this.disableStatistics = data.disableStatistics;
});
......
......@@ -84,6 +84,10 @@ export class RootComponent implements OnInit, AfterViewInit {
if (isPlatformBrowser(this.platformId)) {
this.updateCheckService.checkForUpdates();
if ('cordova' in window) {
this.cookieConsentService.setGrantedConsents(['base']);
}
let theme: QuizTheme;
this.themeService.themeChanged.pipe(
filter(t => !!t),
......@@ -105,15 +109,6 @@ export class RootComponent implements OnInit, AfterViewInit {
this.initializeCookieConsent();
});
if ('cordova' in window) {
this.cookieConsentService.getGrantedConsents().pipe(
take(1),
filter(consents => consents === null || consents.length === 0)
).subscribe(() => {
this.cookieConsentService.setGrantedConsents(['base']);
});
}
this.cookieConsentService.getGrantedConsents().pipe(
filter(consents => !consents?.includes('base')),
takeUntil(this._destroy),
......
......@@ -17,7 +17,7 @@ import { SharedService } from '../shared/shared.service';
providedIn: 'root',
})
export class ConnectionService {
private _serverAvailable: boolean;
private _serverAvailable = false;
private _websocketAvailable = false;
private _serverStatusEmitter = new ReplaySubject<boolean>(1);
private _websocketStatusEmitter = new ReplaySubject<boolean>(1);
......@@ -157,7 +157,7 @@ export class ConnectionService {
private initConnection(overrideCurrentState?: boolean): Promise<any> {
if (isPlatformServer(this.platformId)) {
return new Promise<any>(resolve => resolve());
return new Promise<void>(resolve => resolve());
}
if (isPlatformBrowser(this.platformId)) {
this.motdService.getAllMotds().subscribe((resp) => {
......@@ -165,7 +165,7 @@ export class ConnectionService {
// this.motdService.showMotdModal();
});
}
return new Promise(async (resolve) => {
return new Promise<{ activeQuizzes: Array<string> } | void>(async (resolve) => {
if ((
this.pending || this.serverAvailable
) && !overrideCurrentState) {
......@@ -173,7 +173,7 @@ export class ConnectionService {
return;
}
this.pending = true;
const data = await new Promise(resolve2 => {
const data = await new Promise<{ activeQuizzes: Array<string> } | void>(resolve2 => {
this.statisticsApiService.getBaseStatistics().subscribe(httpData => {
this.pending = false;
this.serverAvailable = true;
......
......@@ -66,14 +66,17 @@ export function app(): Express {
server.get('*', (req, res) => {
const reqUrlMatch = req.url.match(/\/preview\/([a-z\-A-Z]*)\/.*/);
const theme = (reqUrlMatch ? reqUrlMatch[1] : req.cookies.theme) ?? themes[0].id;
const hash = themeHashMap.find(value => value.theme === theme)?.hash ?? themeHashMap.find(value => value.theme === themes[0].id);
const href = `theme-${theme}${hash ? '-' : ''}${hash}.css`;
const hash = themeHashMap.find(value => value.theme === theme)?.hash;
const href = `theme-${theme}${hash ? ('-' + hash) : ''}.css`;
const logoHref = `theme/${theme}/logo_s32x32.png`;
const indexHtmlContent = readFileSync(join(distFolder, indexHtml), {encoding: 'utf-8'});
let updatedIndexHtml = indexHtmlContent.replace(/theme-default.css/g, href).replace(/logo.svg/, logoHref);
if (!req.get('user-agent').includes('arsnova.click-cordova')) {
updatedIndexHtml = updatedIndexHtml.replace(/="\.\//g, '="/').replace('href="."', 'href="/"');
updatedIndexHtml = updatedIndexHtml.replace(/="\.\//g, '="/')
.replace('href="."', 'href="/"')
.replace('\'./', '\'/')
.replace('<script type="text/javascript" src="./cordova.js"></script>', '');
}
res.render(indexHtml, {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment