Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 1 | /* |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 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 | */ |
Sean Condon | 7d27516 | 2018-11-02 16:29:06 +0000 | [diff] [blame] | 16 | import {Inject, Injectable} from '@angular/core'; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 17 | import { FnService } from '../util/fn.service'; |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 18 | import { LogService } from '../log.service'; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 19 | import { ThemeService } from '../util/theme.service'; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 20 | import * as d3 from 'd3'; |
| 21 | |
| 22 | const id = 'loading-anim'; |
| 23 | const dir = 'data/img/loading/'; |
| 24 | const pfx = '/load-'; |
| 25 | const nImgs = 16; |
| 26 | const speed = 100; |
| 27 | const 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 Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 35 | @Injectable({ |
| 36 | providedIn: 'root', |
| 37 | }) |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 38 | export class LoadingService { |
| 39 | images: any[] = []; |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 40 | idx = 0; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 41 | 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 Condon | 7d27516 | 2018-11-02 16:29:06 +0000 | [diff] [blame] | 50 | @Inject('Window') private w: any |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 51 | ) { |
| 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 Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 64 | for (idx = 1; idx <= nImgs ; idx++) { |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 65 | 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 Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 72 | const img = new Image(); |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 73 | 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 Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 91 | let div = d3.select('#' + id); |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 92 | 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 Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 111 | d3.select('#' + id).remove(); |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // schedule function to start animation in the future |
| 115 | start() { |
| 116 | this.dbg('start (schedule)'); |
Sean Condon | 7d27516 | 2018-11-02 16:29:06 +0000 | [diff] [blame] | 117 | this.wait = this.w.setTimeout(this.startAnim(), waitDelay); |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 118 | } |
| 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 Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 131 | waiting(): boolean { |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 132 | return !!this.wait; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | } |