blob: 0d03ad66b1194bcdad91e398492bf1866d79a8a7 [file] [log] [blame]
Sean Condon83fc39f2018-04-19 18:56:13 +01001/*
2 * Copyright 2015-present Open Networking Foundation
3 *
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';
18import { LogService } from '../../log.service';
19import { 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 */
36@Injectable()
37export class LoadingService {
38 images: any[] = [];
Sean Condon49e15be2018-05-16 16:58:29 +010039 idx = 0;
Sean Condon83fc39f2018-04-19 18:56:13 +010040 img: any;
41 theme: string;
42 task: any;
43 wait: number;
44
45 constructor(
46 private fs: FnService,
47 private log: LogService,
48 private ts: ThemeService,
49 private wss: WebSocketService
50 ) {
51 this.preloadImages();
52 this.log.debug('LoadingService constructed');
53 }
54
55 dbg(...args) {
56 this.fs.debug(this.constructor.name, args);
57 }
58
59 preloadImages() {
60 let idx: number;
61
62 this.dbg('preload images start...');
Sean Condon49e15be2018-05-16 16:58:29 +010063 for (idx = 1; idx <= nImgs ; idx++) {
Sean Condon83fc39f2018-04-19 18:56:13 +010064 this.addImg('light', idx);
65 this.addImg('dark', idx);
66 }
67 this.dbg('preload images DONE!', this.images);
68 }
69
70 addImg(theme: string, idx: number) {
Sean Condon49e15be2018-05-16 16:58:29 +010071 const img = new Image();
Sean Condon83fc39f2018-04-19 18:56:13 +010072 img.src = this.fname(idx, theme);
73 this.images.push(img);
74 }
75
76 fname(i: number, theme: string) {
77 const z = i > 9 ? '' : '0';
78 return dir + theme + pfx + z + i + '.png';
79 }
80
81 nextFrame() {
82 this.idx = this.idx === 16 ? 1 : this.idx + 1;
83 this.img.attr('src', this.fname(this.idx, this.theme));
84 }
85
86 // start displaying 'loading...' animation (idempotent)
87 startAnim() {
88 this.dbg('start ANIMATION');
89 this.theme = this.ts.getTheme();
Sean Condon49e15be2018-05-16 16:58:29 +010090 let div = d3.select('#' + id);
Sean Condon83fc39f2018-04-19 18:56:13 +010091 if (div.empty()) {
92 div = d3.select('body')
93 .append('div')
94 .attr('id', id);
95 this.img = div
96 .append('img')
97 .attr('src', this.fname(1, this.theme));
98 this.idx = 1;
99 this.task = setInterval(() => this.nextFrame(), speed);
100 }
101 }
102
103 // stop displaying 'loading...' animation (idempotent)
104 stopAnim() {
105 this.dbg('*stop* ANIMATION');
106 if (this.task) {
107 clearInterval(this.task);
108 this.task = null;
109 }
Sean Condon49e15be2018-05-16 16:58:29 +0100110 d3.select('#' + id).remove();
Sean Condon83fc39f2018-04-19 18:56:13 +0100111 }
112
113 // schedule function to start animation in the future
114 start() {
115 this.dbg('start (schedule)');
116 this.wait = setTimeout(this.startAnim(), waitDelay);
117 }
118
119 // cancel future start, if any; stop the animation
120 stop() {
121 if (this.wait) {
122 this.dbg('start CANCELED');
123 clearTimeout(this.wait);
124 this.wait = null;
125 }
126 this.stopAnim();
127 }
128
129 // return true if start() has been called but not stop()
130 waiting() {
131 return !!this.wait;
132 }
133
134
135}