blob: 7691f09c9e5a8f9317ace9909b31157a05dcab98 [file] [log] [blame]
Simon Huntab34ecb2015-12-11 13:14:48 -08001/*
2 * Copyright 2015 Open Networking Laboratory
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
26 var $log, $timeout, ts;
27
28 // constants
29 var id = 'loading-anim',
30 dir = 'data/img/loading/',
31 pfx = '/load-',
32 speed = 100;
33
34 // internal state
35 var div,
36 img,
37 th,
38 idx,
39 task;
40
41 function fname(i) {
42 var z = i > 9 ? '' : '0';
43 return dir + th + pfx + z + i + '.png';
44 }
45
46 function nextFrame() {
47 idx = idx === 16 ? 1 : idx + 1;
48 img.attr('src', fname(idx));
49 task = $timeout(nextFrame, speed);
50 }
51
52 // start displaying 'loading...' animation (idempotent)
53 function start() {
54 th = ts.theme();
55 div = d3.select('#'+id);
56 if (div.empty()) {
57 div = d3.select('body').append('div').attr('id', id);
58 img = div.append('img').attr('src', fname(1));
59 idx = 1;
60 task = $timeout(nextFrame, speed);
61 }
62 }
63
64 // stop displaying 'loading...' animation (idempotent)
65 function stop() {
66 if (task) {
67 $timeout.cancel(task);
68 task = null;
69 }
70 d3.select('#'+id).remove();
71 }
72
73 angular.module('onosLayer')
74 .factory('LoadingService', ['$log', '$timeout', 'ThemeService',
75 function (_$log_, _$timeout_, _ts_) {
76 $log = _$log_;
77 $timeout = _$timeout_;
78 ts = _ts_;
79
80 return {
81 start: start,
82 stop: stop
83 };
84 }]);
85
86}());