blob: 01391f159eb8fc1ecc7063b327bf208008512218 [file] [log] [blame]
Sean Condon83fc39f2018-04-19 18:56:13 +01001/*
Sean Condon5ca00262018-09-06 17:55:25 +01002 * Copyright 2018-present Open Networking Foundation
Sean Condon83fc39f2018-04-19 18:56:13 +01003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Sean Condon7d275162018-11-02 16:29:06 +000016import {Inject, Injectable} from '@angular/core';
Sean Condon83fc39f2018-04-19 18:56:13 +010017import { FnService } from '../util/fn.service';
Sean Condon5ca00262018-09-06 17:55:25 +010018import { LogService } from '../log.service';
Sean Condon83fc39f2018-04-19 18:56:13 +010019import { ThemeService } from '../util/theme.service';
Sean Condon83fc39f2018-04-19 18:56:13 +010020import * as d3 from 'd3';
21
22const id = 'loading-anim';
23const dir = 'data/img/loading/';
24const pfx = '/load-';
25const nImgs = 16;
26const speed = 100;
27const waitDelay = 500;
28
29
30/**
31 * ONOS GUI -- Layer -- Loading Service
32 *
33 * Provides a mechanism to start/stop the loading animation, center screen.
34 */
Sean Condon2bd11b72018-06-15 08:00:48 +010035@Injectable({
36 providedIn: 'root',
37})
Sean Condon83fc39f2018-04-19 18:56:13 +010038export class LoadingService {
39 images: any[] = [];
Sean Condon49e15be2018-05-16 16:58:29 +010040 idx = 0;
Sean Condon83fc39f2018-04-19 18:56:13 +010041 img: any;
42 theme: string;
43 task: any;
44 wait: number;
45
46 constructor(
47 private fs: FnService,
48 private log: LogService,
49 private ts: ThemeService,
Sean Condon7d275162018-11-02 16:29:06 +000050 @Inject('Window') private w: any
Sean Condon83fc39f2018-04-19 18:56:13 +010051 ) {
52 this.preloadImages();
53 this.log.debug('LoadingService constructed');
54 }
55
56 dbg(...args) {
57 this.fs.debug(this.constructor.name, args);
58 }
59
60 preloadImages() {
61 let idx: number;
62
63 this.dbg('preload images start...');
Sean Condon49e15be2018-05-16 16:58:29 +010064 for (idx = 1; idx <= nImgs ; idx++) {
Sean Condon83fc39f2018-04-19 18:56:13 +010065 this.addImg('light', idx);
66 this.addImg('dark', idx);
67 }
68 this.dbg('preload images DONE!', this.images);
69 }
70
71 addImg(theme: string, idx: number) {
Sean Condon49e15be2018-05-16 16:58:29 +010072 const img = new Image();
Sean Condon83fc39f2018-04-19 18:56:13 +010073 img.src = this.fname(idx, theme);
74 this.images.push(img);
75 }
76
77 fname(i: number, theme: string) {
78 const z = i > 9 ? '' : '0';
79 return dir + theme + pfx + z + i + '.png';
80 }
81
82 nextFrame() {
83 this.idx = this.idx === 16 ? 1 : this.idx + 1;
84 this.img.attr('src', this.fname(this.idx, this.theme));
85 }
86
87 // start displaying 'loading...' animation (idempotent)
88 startAnim() {
89 this.dbg('start ANIMATION');
90 this.theme = this.ts.getTheme();
Sean Condon49e15be2018-05-16 16:58:29 +010091 let div = d3.select('#' + id);
Sean Condon83fc39f2018-04-19 18:56:13 +010092 if (div.empty()) {
93 div = d3.select('body')
94 .append('div')
95 .attr('id', id);
96 this.img = div
97 .append('img')
98 .attr('src', this.fname(1, this.theme));
99 this.idx = 1;
100 this.task = setInterval(() => this.nextFrame(), speed);
101 }
102 }
103
104 // stop displaying 'loading...' animation (idempotent)
105 stopAnim() {
106 this.dbg('*stop* ANIMATION');
107 if (this.task) {
108 clearInterval(this.task);
109 this.task = null;
110 }
Sean Condon49e15be2018-05-16 16:58:29 +0100111 d3.select('#' + id).remove();
Sean Condon83fc39f2018-04-19 18:56:13 +0100112 }
113
114 // schedule function to start animation in the future
115 start() {
116 this.dbg('start (schedule)');
Sean Condon7d275162018-11-02 16:29:06 +0000117 this.wait = this.w.setTimeout(this.startAnim(), waitDelay);
Sean Condon83fc39f2018-04-19 18:56:13 +0100118 }
119
120 // cancel future start, if any; stop the animation
121 stop() {
122 if (this.wait) {
123 this.dbg('start CANCELED');
124 clearTimeout(this.wait);
125 this.wait = null;
126 }
127 this.stopAnim();
128 }
129
130 // return true if start() has been called but not stop()
Sean Condon2bd11b72018-06-15 08:00:48 +0100131 waiting(): boolean {
Sean Condon83fc39f2018-04-19 18:56:13 +0100132 return !!this.wait;
133 }
134
135
136}