blob: f7f54faaced1102ba5a049880cfe325137fb50a6 [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 */
16import { Injectable } from '@angular/core';
17import { 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';
20import { WebSocketService } from '../remote/websocket.service';
21import * as d3 from 'd3';
22
23const id = 'loading-anim';
24const dir = 'data/img/loading/';
25const pfx = '/load-';
26const nImgs = 16;
27const speed = 100;
28const waitDelay = 500;
29
30
31/**
32 * ONOS GUI -- Layer -- Loading Service
33 *
34 * Provides a mechanism to start/stop the loading animation, center screen.
35 */
Sean Condon2bd11b72018-06-15 08:00:48 +010036@Injectable({
37 providedIn: 'root',
38})
Sean Condon83fc39f2018-04-19 18:56:13 +010039export class LoadingService {
40 images: any[] = [];
Sean Condon49e15be2018-05-16 16:58:29 +010041 idx = 0;
Sean Condon83fc39f2018-04-19 18:56:13 +010042 img: any;
43 theme: string;
44 task: any;
45 wait: number;
46
47 constructor(
48 private fs: FnService,
49 private log: LogService,
50 private ts: ThemeService,
51 private wss: WebSocketService
52 ) {
53 this.preloadImages();
54 this.log.debug('LoadingService constructed');
55 }
56
57 dbg(...args) {
58 this.fs.debug(this.constructor.name, args);
59 }
60
61 preloadImages() {
62 let idx: number;
63
64 this.dbg('preload images start...');
Sean Condon49e15be2018-05-16 16:58:29 +010065 for (idx = 1; idx <= nImgs ; idx++) {
Sean Condon83fc39f2018-04-19 18:56:13 +010066 this.addImg('light', idx);
67 this.addImg('dark', idx);
68 }
69 this.dbg('preload images DONE!', this.images);
70 }
71
72 addImg(theme: string, idx: number) {
Sean Condon49e15be2018-05-16 16:58:29 +010073 const img = new Image();
Sean Condon83fc39f2018-04-19 18:56:13 +010074 img.src = this.fname(idx, theme);
75 this.images.push(img);
76 }
77
78 fname(i: number, theme: string) {
79 const z = i > 9 ? '' : '0';
80 return dir + theme + pfx + z + i + '.png';
81 }
82
83 nextFrame() {
84 this.idx = this.idx === 16 ? 1 : this.idx + 1;
85 this.img.attr('src', this.fname(this.idx, this.theme));
86 }
87
88 // start displaying 'loading...' animation (idempotent)
89 startAnim() {
90 this.dbg('start ANIMATION');
91 this.theme = this.ts.getTheme();
Sean Condon49e15be2018-05-16 16:58:29 +010092 let div = d3.select('#' + id);
Sean Condon83fc39f2018-04-19 18:56:13 +010093 if (div.empty()) {
94 div = d3.select('body')
95 .append('div')
96 .attr('id', id);
97 this.img = div
98 .append('img')
99 .attr('src', this.fname(1, this.theme));
100 this.idx = 1;
101 this.task = setInterval(() => this.nextFrame(), speed);
102 }
103 }
104
105 // stop displaying 'loading...' animation (idempotent)
106 stopAnim() {
107 this.dbg('*stop* ANIMATION');
108 if (this.task) {
109 clearInterval(this.task);
110 this.task = null;
111 }
Sean Condon49e15be2018-05-16 16:58:29 +0100112 d3.select('#' + id).remove();
Sean Condon83fc39f2018-04-19 18:56:13 +0100113 }
114
115 // schedule function to start animation in the future
116 start() {
117 this.dbg('start (schedule)');
118 this.wait = setTimeout(this.startAnim(), waitDelay);
119 }
120
121 // cancel future start, if any; stop the animation
122 stop() {
123 if (this.wait) {
124 this.dbg('start CANCELED');
125 clearTimeout(this.wait);
126 this.wait = null;
127 }
128 this.stopAnim();
129 }
130
131 // return true if start() has been called but not stop()
Sean Condon2bd11b72018-06-15 08:00:48 +0100132 waiting(): boolean {
Sean Condon83fc39f2018-04-19 18:56:13 +0100133 return !!this.wait;
134 }
135
136
137}