Simon Hunt | ab34ecb | 2015-12-11 13:14:48 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Simon Hunt | ab34ecb | 2015-12-11 13:14:48 -0800 | [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 | */ |
| 16 | |
| 17 | /* |
| 18 | ONOS GUI -- Layer -- Loading Service |
| 19 | |
| 20 | Provides a mechanism to start/stop the loading animation, center screen. |
| 21 | */ |
| 22 | (function () { |
| 23 | 'use strict'; |
| 24 | |
| 25 | // injected references |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 26 | var $timeout, ts, fs; |
Simon Hunt | ab34ecb | 2015-12-11 13:14:48 -0800 | [diff] [blame] | 27 | |
| 28 | // constants |
| 29 | var id = 'loading-anim', |
| 30 | dir = 'data/img/loading/', |
| 31 | pfx = '/load-', |
Simon Hunt | f90c18b | 2016-01-25 15:38:58 -0800 | [diff] [blame] | 32 | nImgs = 16, |
Simon Hunt | a50540f | 2016-01-25 11:25:11 -0800 | [diff] [blame] | 33 | speed = 100, |
| 34 | waitDelay = 500; |
Simon Hunt | ab34ecb | 2015-12-11 13:14:48 -0800 | [diff] [blame] | 35 | |
| 36 | // internal state |
| 37 | var div, |
| 38 | img, |
| 39 | th, |
| 40 | idx, |
Simon Hunt | a50540f | 2016-01-25 11:25:11 -0800 | [diff] [blame] | 41 | task, |
Simon Hunt | f90c18b | 2016-01-25 15:38:58 -0800 | [diff] [blame] | 42 | wait, |
| 43 | images = []; |
Simon Hunt | ab34ecb | 2015-12-11 13:14:48 -0800 | [diff] [blame] | 44 | |
Simon Hunt | f90c18b | 2016-01-25 15:38:58 -0800 | [diff] [blame] | 45 | function dbg() { |
| 46 | var args = Array.prototype.slice.call(arguments); |
| 47 | args.unshift('loading'); |
| 48 | fs.debug.apply(this, args); |
| 49 | } |
| 50 | |
| 51 | function preloadImages() { |
| 52 | var idx; |
| 53 | |
| 54 | function addImg(th) { |
| 55 | var img = new Image(); |
| 56 | img.src = fname(idx, th); |
| 57 | images.push(img); |
| 58 | } |
| 59 | |
| 60 | dbg('preload images start...'); |
| 61 | for (idx=1; idx<=nImgs; idx++) { |
| 62 | addImg('light'); |
| 63 | addImg('dark'); |
| 64 | } |
| 65 | dbg('preload images DONE!', images); |
| 66 | } |
| 67 | |
| 68 | function fname(i, th) { |
Simon Hunt | ab34ecb | 2015-12-11 13:14:48 -0800 | [diff] [blame] | 69 | var z = i > 9 ? '' : '0'; |
| 70 | return dir + th + pfx + z + i + '.png'; |
| 71 | } |
| 72 | |
| 73 | function nextFrame() { |
| 74 | idx = idx === 16 ? 1 : idx + 1; |
Simon Hunt | f90c18b | 2016-01-25 15:38:58 -0800 | [diff] [blame] | 75 | img.attr('src', fname(idx, th)); |
Simon Hunt | ab34ecb | 2015-12-11 13:14:48 -0800 | [diff] [blame] | 76 | task = $timeout(nextFrame, speed); |
| 77 | } |
| 78 | |
| 79 | // start displaying 'loading...' animation (idempotent) |
Simon Hunt | a50540f | 2016-01-25 11:25:11 -0800 | [diff] [blame] | 80 | function startAnim() { |
Simon Hunt | f90c18b | 2016-01-25 15:38:58 -0800 | [diff] [blame] | 81 | dbg('start ANIMATION'); |
Simon Hunt | ab34ecb | 2015-12-11 13:14:48 -0800 | [diff] [blame] | 82 | th = ts.theme(); |
| 83 | div = d3.select('#'+id); |
| 84 | if (div.empty()) { |
| 85 | div = d3.select('body').append('div').attr('id', id); |
Simon Hunt | f90c18b | 2016-01-25 15:38:58 -0800 | [diff] [blame] | 86 | img = div.append('img').attr('src', fname(1, th)); |
Simon Hunt | ab34ecb | 2015-12-11 13:14:48 -0800 | [diff] [blame] | 87 | idx = 1; |
| 88 | task = $timeout(nextFrame, speed); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // stop displaying 'loading...' animation (idempotent) |
Simon Hunt | a50540f | 2016-01-25 11:25:11 -0800 | [diff] [blame] | 93 | function stopAnim() { |
Simon Hunt | f90c18b | 2016-01-25 15:38:58 -0800 | [diff] [blame] | 94 | dbg('*stop* ANIMATION'); |
Simon Hunt | ab34ecb | 2015-12-11 13:14:48 -0800 | [diff] [blame] | 95 | if (task) { |
| 96 | $timeout.cancel(task); |
| 97 | task = null; |
| 98 | } |
| 99 | d3.select('#'+id).remove(); |
| 100 | } |
| 101 | |
Simon Hunt | a50540f | 2016-01-25 11:25:11 -0800 | [diff] [blame] | 102 | // schedule function to start animation in the future |
| 103 | function start() { |
Simon Hunt | f90c18b | 2016-01-25 15:38:58 -0800 | [diff] [blame] | 104 | dbg('start (schedule)'); |
Simon Hunt | a50540f | 2016-01-25 11:25:11 -0800 | [diff] [blame] | 105 | wait = $timeout(startAnim, waitDelay); |
| 106 | } |
| 107 | |
| 108 | // cancel future start, if any; stop the animation |
| 109 | function stop() { |
| 110 | if (wait) { |
Simon Hunt | f90c18b | 2016-01-25 15:38:58 -0800 | [diff] [blame] | 111 | dbg('start CANCELED'); |
Simon Hunt | a50540f | 2016-01-25 11:25:11 -0800 | [diff] [blame] | 112 | $timeout.cancel(wait); |
| 113 | wait = null; |
| 114 | } |
| 115 | stopAnim(); |
| 116 | } |
| 117 | |
| 118 | // return true if start() has been called but not stop() |
| 119 | function waiting() { |
| 120 | return !!wait; |
| 121 | } |
| 122 | |
Simon Hunt | ab34ecb | 2015-12-11 13:14:48 -0800 | [diff] [blame] | 123 | angular.module('onosLayer') |
Simon Hunt | f90c18b | 2016-01-25 15:38:58 -0800 | [diff] [blame] | 124 | .factory('LoadingService', |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 125 | ['$timeout', 'ThemeService', 'FnService', 'WebSocketService', |
Simon Hunt | f90c18b | 2016-01-25 15:38:58 -0800 | [diff] [blame] | 126 | |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 127 | function (_$timeout_, _ts_, _fs_, wss) { |
Simon Hunt | ab34ecb | 2015-12-11 13:14:48 -0800 | [diff] [blame] | 128 | $timeout = _$timeout_; |
| 129 | ts = _ts_; |
Simon Hunt | f90c18b | 2016-01-25 15:38:58 -0800 | [diff] [blame] | 130 | fs = _fs_; |
| 131 | |
| 132 | preloadImages(); |
Simon Hunt | ab34ecb | 2015-12-11 13:14:48 -0800 | [diff] [blame] | 133 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 134 | var self = { |
Simon Hunt | ab34ecb | 2015-12-11 13:14:48 -0800 | [diff] [blame] | 135 | start: start, |
Simon Hunt | a50540f | 2016-01-25 11:25:11 -0800 | [diff] [blame] | 136 | stop: stop, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 137 | waiting: waiting, |
Simon Hunt | ab34ecb | 2015-12-11 13:14:48 -0800 | [diff] [blame] | 138 | }; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 139 | wss._setLoadingDelegate(self); |
| 140 | return self; |
Simon Hunt | ab34ecb | 2015-12-11 13:14:48 -0800 | [diff] [blame] | 141 | }]); |
| 142 | |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 143 | }()); |