blob: 81bdaf45b1345b56068ce6db3ad1281283931d33 [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
26 var $log, $timeout, ts;
27
28 // constants
29 var id = 'loading-anim',
30 dir = 'data/img/loading/',
31 pfx = '/load-',
Simon Hunta50540f2016-01-25 11:25:11 -080032 speed = 100,
33 waitDelay = 500;
Simon Huntab34ecb2015-12-11 13:14:48 -080034
35 // internal state
36 var div,
37 img,
38 th,
39 idx,
Simon Hunta50540f2016-01-25 11:25:11 -080040 task,
41 wait;
Simon Huntab34ecb2015-12-11 13:14:48 -080042
43 function fname(i) {
44 var z = i > 9 ? '' : '0';
45 return dir + th + pfx + z + i + '.png';
46 }
47
48 function nextFrame() {
49 idx = idx === 16 ? 1 : idx + 1;
50 img.attr('src', fname(idx));
51 task = $timeout(nextFrame, speed);
52 }
53
54 // start displaying 'loading...' animation (idempotent)
Simon Hunta50540f2016-01-25 11:25:11 -080055 function startAnim() {
Simon Huntab34ecb2015-12-11 13:14:48 -080056 th = ts.theme();
57 div = d3.select('#'+id);
58 if (div.empty()) {
59 div = d3.select('body').append('div').attr('id', id);
60 img = div.append('img').attr('src', fname(1));
61 idx = 1;
62 task = $timeout(nextFrame, speed);
63 }
64 }
65
66 // stop displaying 'loading...' animation (idempotent)
Simon Hunta50540f2016-01-25 11:25:11 -080067 function stopAnim() {
Simon Huntab34ecb2015-12-11 13:14:48 -080068 if (task) {
69 $timeout.cancel(task);
70 task = null;
71 }
72 d3.select('#'+id).remove();
73 }
74
Simon Hunta50540f2016-01-25 11:25:11 -080075 // schedule function to start animation in the future
76 function start() {
77 wait = $timeout(startAnim, waitDelay);
78 }
79
80 // cancel future start, if any; stop the animation
81 function stop() {
82 if (wait) {
83 $timeout.cancel(wait);
84 wait = null;
85 }
86 stopAnim();
87 }
88
89 // return true if start() has been called but not stop()
90 function waiting() {
91 return !!wait;
92 }
93
Simon Huntab34ecb2015-12-11 13:14:48 -080094 angular.module('onosLayer')
95 .factory('LoadingService', ['$log', '$timeout', 'ThemeService',
96 function (_$log_, _$timeout_, _ts_) {
97 $log = _$log_;
98 $timeout = _$timeout_;
99 ts = _ts_;
100
101 return {
102 start: start,
Simon Hunta50540f2016-01-25 11:25:11 -0800103 stop: stop,
104 waiting: waiting
Simon Huntab34ecb2015-12-11 13:14:48 -0800105 };
106 }]);
107
108}());