2017-11-24 15:13:27 +01:00
|
|
|
// (C) Copyright 2015 Martin Dougiamas
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2018-04-16 12:27:05 +02:00
|
|
|
import { Component, Input, OnInit, OnChanges, SimpleChange, ViewChild, ElementRef } from '@angular/core';
|
2017-11-24 15:13:27 +01:00
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
2018-03-01 16:55:49 +01:00
|
|
|
import { coreShowHideAnimation } from '@classes/animations';
|
2017-11-24 15:13:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Component to show a loading spinner and message while data is being loaded.
|
|
|
|
*
|
|
|
|
* It will show a spinner with a message and hide all the content until 'dataLoaded' variable is set to true.
|
|
|
|
* If 'message' and 'dynMessage' attributes aren't set, default message "Loading" is shown.
|
2017-12-08 15:53:27 +01:00
|
|
|
* 'message' attribute accepts hardcoded strings, variables, filters, etc. E.g. message="'core.loading' | translate".
|
2017-11-24 15:13:27 +01:00
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
* <core-loading [message]="loadingMessage" [hideUntil]="dataLoaded">
|
|
|
|
* <!-- CONTENT TO HIDE UNTIL LOADED -->
|
|
|
|
* </core-loading>
|
2018-01-19 15:41:14 +01:00
|
|
|
*
|
|
|
|
* IMPORTANT: Due to how ng-content works in Angular, the content of core-loading will be executed as soon as your view
|
|
|
|
* is loaded, even if the content hidden. So if you have the following code:
|
|
|
|
* <core-loading [hideUntil]="dataLoaded"><my-component></my-component></core-loading>
|
|
|
|
*
|
|
|
|
* The component "my-component" will be initialized immediately, even if dataLoaded is false, but it will be hidden. If you want
|
|
|
|
* your component to be initialized only if dataLoaded is true, then you should use ngIf:
|
|
|
|
* <core-loading [hideUntil]="dataLoaded"><my-component *ngIf="dataLoaded"></my-component></core-loading>
|
2017-11-24 15:13:27 +01:00
|
|
|
*/
|
|
|
|
@Component({
|
|
|
|
selector: 'core-loading',
|
2018-06-12 08:41:17 +02:00
|
|
|
templateUrl: 'core-loading.html',
|
2018-02-14 17:19:09 +01:00
|
|
|
animations: [coreShowHideAnimation]
|
2017-11-24 15:13:27 +01:00
|
|
|
})
|
2018-04-16 12:27:05 +02:00
|
|
|
export class CoreLoadingComponent implements OnInit, OnChanges {
|
2017-11-24 15:13:27 +01:00
|
|
|
@Input() hideUntil: boolean; // Determine when should the contents be shown.
|
|
|
|
@Input() message?: string; // Message to show while loading.
|
2018-04-16 12:27:05 +02:00
|
|
|
@ViewChild('content') content: ElementRef;
|
|
|
|
protected element: HTMLElement; // Current element.
|
2017-11-24 15:13:27 +01:00
|
|
|
|
2018-04-16 12:27:05 +02:00
|
|
|
constructor(private translate: TranslateService, element: ElementRef) {
|
|
|
|
this.element = element.nativeElement;
|
|
|
|
}
|
2017-11-24 15:13:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Component being initialized.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
ngOnInit(): void {
|
2017-11-24 15:13:27 +01:00
|
|
|
if (!this.message) {
|
|
|
|
// Default loading message.
|
2017-12-08 15:53:27 +01:00
|
|
|
this.message = this.translate.instant('core.loading');
|
2017-11-24 15:13:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-16 12:27:05 +02:00
|
|
|
ngOnChanges(changes: { [name: string]: SimpleChange }): void {
|
|
|
|
if (changes.hideUntil.currentValue === true) {
|
|
|
|
setTimeout(() => {
|
|
|
|
// Content is loaded so, center the spinner on the content itself.
|
|
|
|
this.element.classList.add('core-loading-loaded');
|
|
|
|
setTimeout(() => {
|
|
|
|
// Change CSS to force calculate height.
|
|
|
|
this.content.nativeElement.classList.add('core-loading-content');
|
|
|
|
}, 500);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-24 15:13:27 +01:00
|
|
|
}
|