UNPKG

2.26 kBJavaScriptView Raw
1import angular from 'angular';
2
3import loaderNg from '../loader-ng/loader-ng';
4import styles from '../loader-screen/loader-screen.css';
5
6/**
7 * @name Loader Screen Ng
8 */
9
10const angularModule = angular.module('Ring.loader-screen', [loaderNg]);
11
12angularModule.service('loaderScreen', function service($timeout, $rootScope) {
13 // TODO in CSS Modules version put constant to global.css
14 const ordinaryLoadingTTL = 100;
15
16 let initialLoading = false;
17 let loadingFailed = false;
18 let showLoader;
19 let showLoaderPromise;
20
21 this.startLoading = (ttl = ordinaryLoadingTTL) => {
22 if (showLoaderPromise) {
23 return; // already scheduled to show
24 }
25
26 showLoaderPromise = $timeout(() => {
27 this.setVisible(true);
28 }, ttl);
29 };
30
31 this.stopLoading = () => {
32 if (showLoaderPromise) {
33 $timeout.cancel(showLoaderPromise);
34 showLoaderPromise = null;
35 }
36
37 this.setVisible(false);
38 };
39
40 this.startInitialLoading = () => {
41 initialLoading = true;
42 this.setVisible(true);
43 };
44
45 this.stopInitialLoading = () => {
46 initialLoading = false;
47 this.setVisible(false);
48 };
49
50 $rootScope.isInitialLoading = () => initialLoading;
51 $rootScope.isLoaderVisible = () => showLoader;
52
53 $rootScope.isLoadingFailed = () => loadingFailed;
54
55 this.failInitialLoading = error => {
56 this.stopInitialLoading();
57 loadingFailed = true;
58 $rootScope.error = error;
59 };
60
61 this.setVisible = visible => {
62 showLoader = visible;
63 };
64
65 /* eslint-disable angular/on-watch */
66 $rootScope.$on('$routeChangeSuccess', () => {
67 this.stopInitialLoading();
68 });
69
70 $rootScope.$on('$routeChangeError', (event, current, previous, rejection) => {
71 if (!rejection || !(rejection.silent || rejection.authRedirect)) {
72 this.failInitialLoading(rejection);
73 }
74 });
75 /* eslint-enable angular/on-watch */
76});
77
78angularModule.directive('rgLoaderScreen', function rgLoaderScreenDirective() {
79 return {
80 restrict: 'A',
81
82 scope: {
83 message: '@rgLoaderScreen'
84 },
85
86 template: `
87<div class="${styles.loaderScreen}" ng-if="$root.isLoaderVisible()">
88 <rg-loader class="${styles.loader}"
89 message="{{$root.isInitialLoading() ? message : ''}}"></rg-loader>
90</div>
91 `
92 };
93});
94
95export default angularModule.name;