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.
|
|
|
|
|
2020-10-15 09:41:42 +00:00
|
|
|
import { Component, OnInit } from '@angular/core';
|
2020-10-22 11:04:57 +00:00
|
|
|
import { NavController } from '@ionic/angular';
|
|
|
|
|
2020-10-15 09:41:42 +00:00
|
|
|
import { CoreLangProvider } from '@services/lang';
|
2020-11-12 14:13:47 +00:00
|
|
|
import { CoreLoginHelperProvider } from '@core/login/services/login.helper';
|
2020-10-30 11:37:00 +00:00
|
|
|
import { CoreEvents, CoreEventSessionExpiredData } from '@singletons/events';
|
2020-11-05 13:37:53 +00:00
|
|
|
import { Network, NgZone, Platform } from '@singletons/core.singletons';
|
|
|
|
import { CoreApp } from '@services/app';
|
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
|
|
|
})
|
2020-10-15 09:41:42 +00:00
|
|
|
export class AppComponent implements OnInit {
|
|
|
|
|
|
|
|
constructor(
|
2020-10-22 11:04:57 +00:00
|
|
|
protected langProvider: CoreLangProvider,
|
|
|
|
protected navCtrl: NavController,
|
2020-10-30 11:37:00 +00:00
|
|
|
protected loginHelper: CoreLoginHelperProvider,
|
2020-10-15 09:41:42 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Component being initialized.
|
|
|
|
*/
|
|
|
|
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.
|
2020-10-22 11:04:57 +00:00
|
|
|
this.navCtrl.navigateRoot('/login/sites');
|
2020-10-15 09:41:42 +00:00
|
|
|
|
|
|
|
// Unload lang custom strings.
|
|
|
|
this.langProvider.clearCustomStrings();
|
|
|
|
|
|
|
|
// 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) => {
|
|
|
|
this.loginHelper.sessionExpired(data);
|
|
|
|
});
|
2020-11-05 13:37:53 +00:00
|
|
|
|
|
|
|
this.onPlatformReady();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async onPlatformReady(): Promise<void> {
|
|
|
|
await Platform.instance.ready();
|
|
|
|
|
|
|
|
// Refresh online status when changes.
|
|
|
|
Network.instance.onChange().subscribe(() => {
|
|
|
|
// Execute the callback in the Angular zone, so change detection doesn't stop working.
|
|
|
|
NgZone.instance.run(() => {
|
|
|
|
const isOnline = CoreApp.instance.isOnline();
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
|
|
|
}
|