2020-10-05 12:56:27 +00:00
|
|
|
// (C) Copyright 2015 Moodle Pty Ltd.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-02-02 17:41:58 +00:00
|
|
|
import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core';
|
|
|
|
import { IonRouterOutlet } from '@ionic/angular';
|
2020-10-22 11:04:57 +00:00
|
|
|
|
2021-02-02 17:41:58 +00:00
|
|
|
import { CoreLang } from '@services/lang';
|
|
|
|
import { CoreLoginHelper } from '@features/login/services/login-helper';
|
2020-11-23 11:29:56 +00:00
|
|
|
import {
|
|
|
|
CoreEvents,
|
|
|
|
CoreEventSessionExpiredData,
|
|
|
|
CoreEventSiteAddedData,
|
|
|
|
CoreEventSiteData,
|
|
|
|
CoreEventSiteUpdatedData,
|
|
|
|
} from '@singletons/events';
|
2021-02-02 17:41:58 +00:00
|
|
|
import { Network, NgZone, Platform, SplashScreen } from '@singletons';
|
2020-11-05 13:37:53 +00:00
|
|
|
import { CoreApp } from '@services/app';
|
2020-11-23 11:29:56 +00:00
|
|
|
import { CoreSites } from '@services/sites';
|
2021-01-21 14:24:45 +00:00
|
|
|
import { CoreNavigator } from '@services/navigator';
|
2021-02-02 17:41:58 +00:00
|
|
|
import { CoreSubscriptions } from '@singletons/subscriptions';
|
2020-10-05 12:56:27 +00:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-root',
|
|
|
|
templateUrl: 'app.component.html',
|
2020-10-06 10:47:16 +00:00
|
|
|
styleUrls: ['app.component.scss'],
|
2020-10-05 12:56:27 +00:00
|
|
|
})
|
2021-02-02 17:41:58 +00:00
|
|
|
export class AppComponent implements OnInit, AfterViewInit {
|
2020-10-15 09:41:42 +00:00
|
|
|
|
2021-02-02 17:41:58 +00:00
|
|
|
@ViewChild(IonRouterOutlet) outlet?: IonRouterOutlet;
|
2020-10-15 09:41:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Component being initialized.
|
2020-11-23 11:29:56 +00:00
|
|
|
*
|
|
|
|
* @todo Review all old code to see if something is missing:
|
|
|
|
* - IAB events listening.
|
|
|
|
* - Platform pause/resume subscriptions.
|
|
|
|
* - handleOpenURL and openWindowSafely.
|
|
|
|
* - Screen orientation events (probably it can be removed).
|
|
|
|
* - Back button registering to close modal first.
|
|
|
|
* - Note: HideKeyboardFormAccessoryBar has been moved to config.xml.
|
2020-10-15 09:41:42 +00:00
|
|
|
*/
|
|
|
|
ngOnInit(): void {
|
2020-10-22 10:48:23 +00:00
|
|
|
CoreEvents.on(CoreEvents.LOGOUT, () => {
|
2020-10-15 09:41:42 +00:00
|
|
|
// Go to sites page when user is logged out.
|
2021-03-02 10:41:04 +00:00
|
|
|
CoreNavigator.navigate('/login/sites', { reset: true });
|
2020-10-15 09:41:42 +00:00
|
|
|
|
|
|
|
// Unload lang custom strings.
|
2021-03-02 10:41:04 +00:00
|
|
|
CoreLang.clearCustomStrings();
|
2020-10-15 09:41:42 +00:00
|
|
|
|
|
|
|
// Remove version classes from body.
|
2020-11-05 13:37:53 +00:00
|
|
|
this.removeVersionClass();
|
2020-10-15 09:41:42 +00:00
|
|
|
});
|
2020-10-30 11:37:00 +00:00
|
|
|
|
|
|
|
// Listen for session expired events.
|
|
|
|
CoreEvents.on(CoreEvents.SESSION_EXPIRED, (data: CoreEventSessionExpiredData) => {
|
2021-03-02 10:41:04 +00:00
|
|
|
CoreLoginHelper.sessionExpired(data);
|
2020-10-30 11:37:00 +00:00
|
|
|
});
|
2020-11-05 13:37:53 +00:00
|
|
|
|
2020-11-23 11:29:56 +00:00
|
|
|
// Listen for passwordchange and usernotfullysetup events to open InAppBrowser.
|
|
|
|
CoreEvents.on(CoreEvents.PASSWORD_CHANGE_FORCED, (data: CoreEventSiteData) => {
|
2021-03-02 10:41:04 +00:00
|
|
|
CoreLoginHelper.passwordChangeForced(data.siteId!);
|
2020-11-23 11:29:56 +00:00
|
|
|
});
|
|
|
|
CoreEvents.on(CoreEvents.USER_NOT_FULLY_SETUP, (data: CoreEventSiteData) => {
|
2021-03-02 10:41:04 +00:00
|
|
|
CoreLoginHelper.openInAppForEdit(data.siteId!, '/user/edit.php', 'core.usernotfullysetup');
|
2020-11-23 11:29:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Listen for sitepolicynotagreed event to accept the site policy.
|
|
|
|
CoreEvents.on(CoreEvents.SITE_POLICY_NOT_AGREED, (data: CoreEventSiteData) => {
|
2021-03-02 10:41:04 +00:00
|
|
|
CoreLoginHelper.sitePolicyNotAgreed(data.siteId);
|
2020-11-23 11:29:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
CoreEvents.on(CoreEvents.LOGIN, async (data: CoreEventSiteData) => {
|
|
|
|
if (data.siteId) {
|
2021-03-02 10:41:04 +00:00
|
|
|
const site = await CoreSites.getSite(data.siteId);
|
2020-11-23 11:29:56 +00:00
|
|
|
const info = site.getInfo();
|
|
|
|
if (info) {
|
|
|
|
// Add version classes to body.
|
|
|
|
this.removeVersionClass();
|
2021-03-02 10:41:04 +00:00
|
|
|
this.addVersionClass(CoreSites.getReleaseNumber(info.release || ''));
|
2020-11-23 11:29:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.loadCustomStrings();
|
|
|
|
});
|
|
|
|
|
|
|
|
CoreEvents.on(CoreEvents.SITE_UPDATED, (data: CoreEventSiteUpdatedData) => {
|
2021-03-02 10:41:04 +00:00
|
|
|
if (data.siteId == CoreSites.getCurrentSiteId()) {
|
2020-11-23 11:29:56 +00:00
|
|
|
this.loadCustomStrings();
|
|
|
|
|
|
|
|
// Add version classes to body.
|
|
|
|
this.removeVersionClass();
|
2021-03-02 10:41:04 +00:00
|
|
|
this.addVersionClass(CoreSites.getReleaseNumber(data.release || ''));
|
2020-11-23 11:29:56 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
CoreEvents.on(CoreEvents.SITE_ADDED, (data: CoreEventSiteAddedData) => {
|
2021-03-02 10:41:04 +00:00
|
|
|
if (data.siteId == CoreSites.getCurrentSiteId()) {
|
2020-11-23 11:29:56 +00:00
|
|
|
this.loadCustomStrings();
|
|
|
|
|
|
|
|
// Add version classes to body.
|
|
|
|
this.removeVersionClass();
|
2021-03-02 10:41:04 +00:00
|
|
|
this.addVersionClass(CoreSites.getReleaseNumber(data.release || ''));
|
2020-11-23 11:29:56 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-11-05 13:37:53 +00:00
|
|
|
this.onPlatformReady();
|
|
|
|
}
|
|
|
|
|
2021-02-02 17:41:58 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ngAfterViewInit(): void {
|
|
|
|
if (!this.outlet) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-02 10:41:04 +00:00
|
|
|
CoreSubscriptions.once(this.outlet.activateEvents, () => SplashScreen.hide());
|
2021-02-02 17:41:58 +00:00
|
|
|
}
|
|
|
|
|
2020-11-23 11:29:56 +00:00
|
|
|
/**
|
|
|
|
* Async init function on platform ready.
|
|
|
|
*/
|
2020-11-05 13:37:53 +00:00
|
|
|
protected async onPlatformReady(): Promise<void> {
|
2021-03-02 10:41:04 +00:00
|
|
|
await Platform.ready();
|
2020-11-05 13:37:53 +00:00
|
|
|
|
|
|
|
// Refresh online status when changes.
|
2021-03-02 10:41:04 +00:00
|
|
|
Network.onChange().subscribe(() => {
|
2020-11-05 13:37:53 +00:00
|
|
|
// Execute the callback in the Angular zone, so change detection doesn't stop working.
|
2021-03-02 10:41:04 +00:00
|
|
|
NgZone.run(() => {
|
|
|
|
const isOnline = CoreApp.isOnline();
|
2020-11-05 13:37:53 +00:00
|
|
|
const hadOfflineMessage = document.body.classList.contains('core-offline');
|
|
|
|
|
|
|
|
document.body.classList.toggle('core-offline', !isOnline);
|
|
|
|
|
|
|
|
if (isOnline && hadOfflineMessage) {
|
|
|
|
document.body.classList.add('core-online');
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
document.body.classList.remove('core-online');
|
|
|
|
}, 3000);
|
|
|
|
} else if (!isOnline) {
|
|
|
|
document.body.classList.remove('core-online');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2020-11-23 11:30:30 +00:00
|
|
|
|
|
|
|
// Set StatusBar properties.
|
2021-03-02 10:41:04 +00:00
|
|
|
CoreApp.setStatusBarColor();
|
2020-11-05 13:37:53 +00:00
|
|
|
}
|
|
|
|
|
2020-11-23 11:29:56 +00:00
|
|
|
/**
|
|
|
|
* Load custom lang strings. This cannot be done inside the lang provider because it causes circular dependencies.
|
|
|
|
*/
|
|
|
|
protected loadCustomStrings(): void {
|
2021-03-02 10:41:04 +00:00
|
|
|
const currentSite = CoreSites.getCurrentSite();
|
2021-02-02 17:41:58 +00:00
|
|
|
|
2020-11-23 11:29:56 +00:00
|
|
|
if (currentSite) {
|
2021-03-02 10:41:04 +00:00
|
|
|
CoreLang.loadCustomStringsFromSite(currentSite);
|
2020-11-23 11:29:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 13:37:53 +00:00
|
|
|
/**
|
|
|
|
* Convenience function to add version to body classes.
|
|
|
|
*
|
|
|
|
* @param release Current release number of the site.
|
|
|
|
*/
|
|
|
|
protected addVersionClass(release: string): void {
|
|
|
|
const parts = release.split('.', 3);
|
|
|
|
|
|
|
|
parts[1] = parts[1] || '0';
|
|
|
|
parts[2] = parts[2] || '0';
|
|
|
|
|
2020-11-06 08:42:45 +00:00
|
|
|
document.body.classList.add(
|
|
|
|
'version-' + parts[0],
|
2020-11-05 13:37:53 +00:00
|
|
|
'version-' + parts[0] + '-' + parts[1],
|
2020-11-06 08:42:45 +00:00
|
|
|
'version-' + parts[0] + '-' + parts[1] + '-' + parts[2],
|
|
|
|
);
|
2020-11-05 13:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience function to remove all version classes form body.
|
|
|
|
*/
|
|
|
|
protected removeVersionClass(): void {
|
|
|
|
const remove: string[] = [];
|
|
|
|
|
|
|
|
Array.from(document.body.classList).forEach((tempClass) => {
|
|
|
|
if (tempClass.substring(0, 8) == 'version-') {
|
|
|
|
remove.push(tempClass);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
remove.forEach((tempClass) => {
|
|
|
|
document.body.classList.remove(tempClass);
|
|
|
|
});
|
2020-10-15 09:41:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|