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 | */ |
| 36 | @Injectable() |
| 37 | export class LoadingService { |
| 38 | images: any[] = []; |
| 39 | idx: number = 0; |
| 40 | 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...'); |
| 63 | for (idx=1; idx<=nImgs; idx++) { |
| 64 | 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) { |
| 71 | let img = new Image(); |
| 72 | 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(); |
| 90 | let div = d3.select('#'+id); |
| 91 | 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 | } |
| 110 | d3.select('#'+id).remove(); |
| 111 | } |
| 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 | } |