Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | import { Injectable } from '@angular/core'; |
| 17 | import { FnService } from '../util/fn.service'; |
| 18 | import { LogService } from '../../log.service'; |
| 19 | import { ThemeService } from '../util/theme.service'; |
| 20 | import { WebSocketService } from '../remote/websocket.service'; |
| 21 | import * as d3 from 'd3'; |
| 22 | |
| 23 | const id = 'loading-anim'; |
| 24 | const dir = 'data/img/loading/'; |
| 25 | const pfx = '/load-'; |
| 26 | const nImgs = 16; |
| 27 | const speed = 100; |
| 28 | const 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 Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame^] | 36 | @Injectable({ |
| 37 | providedIn: 'root', |
| 38 | }) |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 39 | export class LoadingService { |
| 40 | images: any[] = []; |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 41 | idx = 0; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 42 | 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 Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 65 | for (idx = 1; idx <= nImgs ; idx++) { |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 66 | 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 Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 73 | const img = new Image(); |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 74 | 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 Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 92 | let div = d3.select('#' + id); |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 93 | 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 Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 112 | d3.select('#' + id).remove(); |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 113 | } |
| 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 Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame^] | 132 | waiting(): boolean { |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 133 | return !!this.wait; |
| 134 | } |
| 135 | |
| 136 | |
| 137 | } |