blob: a9e141eb02acc0d37abb8aac9c891c177d810729 [file] [log] [blame]
Simon Huntab34ecb2015-12-11 13:14:48 -08001/*
Simon Hunta50540f2016-01-25 11:25:11 -08002 * Copyright 2015,2016 Open Networking Laboratory
Simon Huntab34ecb2015-12-11 13:14:48 -08003 *
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
Simon Huntf90c18b2016-01-25 15:38:58 -080026 var $log, $timeout, ts, fs;
Simon Huntab34ecb2015-12-11 13:14:48 -080027
28 // constants
29 var id = 'loading-anim',
30 dir = 'data/img/loading/',
31 pfx = '/load-',
Simon Huntf90c18b2016-01-25 15:38:58 -080032 nImgs = 16,
Simon Hunta50540f2016-01-25 11:25:11 -080033 speed = 100,
34 waitDelay = 500;
Simon Huntab34ecb2015-12-11 13:14:48 -080035
36 // internal state
37 var div,
38 img,
39 th,
40 idx,
Simon Hunta50540f2016-01-25 11:25:11 -080041 task,
Simon Huntf90c18b2016-01-25 15:38:58 -080042 wait,
43 images = [];
Simon Huntab34ecb2015-12-11 13:14:48 -080044
Simon Huntf90c18b2016-01-25 15:38:58 -080045 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 Huntab34ecb2015-12-11 13:14:48 -080069 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 Huntf90c18b2016-01-25 15:38:58 -080075 img.attr('src', fname(idx, th));
Simon Huntab34ecb2015-12-11 13:14:48 -080076 task = $timeout(nextFrame, speed);
77 }
78
79 // start displaying 'loading...' animation (idempotent)
Simon Hunta50540f2016-01-25 11:25:11 -080080 function startAnim() {
Simon Huntf90c18b2016-01-25 15:38:58 -080081 dbg('start ANIMATION');
Simon Huntab34ecb2015-12-11 13:14:48 -080082 th = ts.theme();
83 div = d3.select('#'+id);
84 if (div.empty()) {
85 div = d3.select('body').append('div').attr('id', id);
Simon Huntf90c18b2016-01-25 15:38:58 -080086 img = div.append('img').attr('src', fname(1, th));
Simon Huntab34ecb2015-12-11 13:14:48 -080087 idx = 1;
88 task = $timeout(nextFrame, speed);
89 }
90 }
91
92 // stop displaying 'loading...' animation (idempotent)
Simon Hunta50540f2016-01-25 11:25:11 -080093 function stopAnim() {
Simon Huntf90c18b2016-01-25 15:38:58 -080094 dbg('*stop* ANIMATION');
Simon Huntab34ecb2015-12-11 13:14:48 -080095 if (task) {
96 $timeout.cancel(task);
97 task = null;
98 }
99 d3.select('#'+id).remove();
100 }
101
Simon Hunta50540f2016-01-25 11:25:11 -0800102 // schedule function to start animation in the future
103 function start() {
Simon Huntf90c18b2016-01-25 15:38:58 -0800104 dbg('start (schedule)');
Simon Hunta50540f2016-01-25 11:25:11 -0800105 wait = $timeout(startAnim, waitDelay);
106 }
107
108 // cancel future start, if any; stop the animation
109 function stop() {
110 if (wait) {
Simon Huntf90c18b2016-01-25 15:38:58 -0800111 dbg('start CANCELED');
Simon Hunta50540f2016-01-25 11:25:11 -0800112 $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 Huntab34ecb2015-12-11 13:14:48 -0800123 angular.module('onosLayer')
Simon Huntf90c18b2016-01-25 15:38:58 -0800124 .factory('LoadingService',
Thomas Vachuska0af26912016-03-21 21:37:30 -0700125 ['$log', '$timeout', 'ThemeService', 'FnService', 'WebSocketService',
Simon Huntf90c18b2016-01-25 15:38:58 -0800126
Thomas Vachuska0af26912016-03-21 21:37:30 -0700127 function (_$log_, _$timeout_, _ts_, _fs_, wss) {
Simon Huntab34ecb2015-12-11 13:14:48 -0800128 $log = _$log_;
129 $timeout = _$timeout_;
130 ts = _ts_;
Simon Huntf90c18b2016-01-25 15:38:58 -0800131 fs = _fs_;
132
133 preloadImages();
Simon Huntab34ecb2015-12-11 13:14:48 -0800134
Thomas Vachuska0af26912016-03-21 21:37:30 -0700135 var self = {
Simon Huntab34ecb2015-12-11 13:14:48 -0800136 start: start,
Simon Hunta50540f2016-01-25 11:25:11 -0800137 stop: stop,
138 waiting: waiting
Simon Huntab34ecb2015-12-11 13:14:48 -0800139 };
Thomas Vachuska0af26912016-03-21 21:37:30 -0700140 wss._setLoadingDelegate(self);
141 return self;
Simon Huntab34ecb2015-12-11 13:14:48 -0800142 }]);
143
144}());