Simon Hunt
Committed by Gerrit Code Review

Web UI -- added fs.debug(...) function for conditional debug messages to console

 - precache loading... images.

Change-Id: Ie93262b78e72f6c740ba851d8662b8170a91ab75
...@@ -23,12 +23,13 @@ ...@@ -23,12 +23,13 @@
23 'use strict'; 23 'use strict';
24 24
25 // injected references 25 // injected references
26 - var $log, $timeout, ts; 26 + var $log, $timeout, ts, fs;
27 27
28 // constants 28 // constants
29 var id = 'loading-anim', 29 var id = 'loading-anim',
30 dir = 'data/img/loading/', 30 dir = 'data/img/loading/',
31 pfx = '/load-', 31 pfx = '/load-',
32 + nImgs = 16,
32 speed = 100, 33 speed = 100,
33 waitDelay = 500; 34 waitDelay = 500;
34 35
...@@ -38,26 +39,51 @@ ...@@ -38,26 +39,51 @@
38 th, 39 th,
39 idx, 40 idx,
40 task, 41 task,
41 - wait; 42 + wait,
43 + images = [];
42 44
43 - function fname(i) { 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) {
44 var z = i > 9 ? '' : '0'; 69 var z = i > 9 ? '' : '0';
45 return dir + th + pfx + z + i + '.png'; 70 return dir + th + pfx + z + i + '.png';
46 } 71 }
47 72
48 function nextFrame() { 73 function nextFrame() {
49 idx = idx === 16 ? 1 : idx + 1; 74 idx = idx === 16 ? 1 : idx + 1;
50 - img.attr('src', fname(idx)); 75 + img.attr('src', fname(idx, th));
51 task = $timeout(nextFrame, speed); 76 task = $timeout(nextFrame, speed);
52 } 77 }
53 78
54 // start displaying 'loading...' animation (idempotent) 79 // start displaying 'loading...' animation (idempotent)
55 function startAnim() { 80 function startAnim() {
81 + dbg('start ANIMATION');
56 th = ts.theme(); 82 th = ts.theme();
57 div = d3.select('#'+id); 83 div = d3.select('#'+id);
58 if (div.empty()) { 84 if (div.empty()) {
59 div = d3.select('body').append('div').attr('id', id); 85 div = d3.select('body').append('div').attr('id', id);
60 - img = div.append('img').attr('src', fname(1)); 86 + img = div.append('img').attr('src', fname(1, th));
61 idx = 1; 87 idx = 1;
62 task = $timeout(nextFrame, speed); 88 task = $timeout(nextFrame, speed);
63 } 89 }
...@@ -65,6 +91,7 @@ ...@@ -65,6 +91,7 @@
65 91
66 // stop displaying 'loading...' animation (idempotent) 92 // stop displaying 'loading...' animation (idempotent)
67 function stopAnim() { 93 function stopAnim() {
94 + dbg('*stop* ANIMATION');
68 if (task) { 95 if (task) {
69 $timeout.cancel(task); 96 $timeout.cancel(task);
70 task = null; 97 task = null;
...@@ -74,12 +101,14 @@ ...@@ -74,12 +101,14 @@
74 101
75 // schedule function to start animation in the future 102 // schedule function to start animation in the future
76 function start() { 103 function start() {
104 + dbg('start (schedule)');
77 wait = $timeout(startAnim, waitDelay); 105 wait = $timeout(startAnim, waitDelay);
78 } 106 }
79 107
80 // cancel future start, if any; stop the animation 108 // cancel future start, if any; stop the animation
81 function stop() { 109 function stop() {
82 if (wait) { 110 if (wait) {
111 + dbg('start CANCELED');
83 $timeout.cancel(wait); 112 $timeout.cancel(wait);
84 wait = null; 113 wait = null;
85 } 114 }
...@@ -92,11 +121,16 @@ ...@@ -92,11 +121,16 @@
92 } 121 }
93 122
94 angular.module('onosLayer') 123 angular.module('onosLayer')
95 - .factory('LoadingService', ['$log', '$timeout', 'ThemeService', 124 + .factory('LoadingService',
96 - function (_$log_, _$timeout_, _ts_) { 125 + ['$log', '$timeout', 'ThemeService', 'FnService',
126 +
127 + function (_$log_, _$timeout_, _ts_, _fs_) {
97 $log = _$log_; 128 $log = _$log_;
98 $timeout = _$timeout_; 129 $timeout = _$timeout_;
99 ts = _ts_; 130 ts = _ts_;
131 + fs = _fs_;
132 +
133 + preloadImages();
100 134
101 return { 135 return {
102 start: start, 136 start: start,
......
...@@ -251,6 +251,17 @@ ...@@ -251,6 +251,17 @@
251 return debugFlags[tag]; 251 return debugFlags[tag];
252 } 252 }
253 253
254 + // output debug message to console, if debug tag set...
255 + // e.g. fs.debug('mytag', arg1, arg2, ...)
256 + function debug(tag) {
257 + var args;
258 + if (debugOn(tag)) {
259 + args = Array.prototype.slice.call(arguments, 1);
260 + args.unshift('['+tag+']');
261 + $log.debug.apply(this, args);
262 + }
263 + }
264 +
254 angular.module('onosUtil') 265 angular.module('onosUtil')
255 .factory('FnService', 266 .factory('FnService',
256 ['$window', '$location', '$log', function (_$window_, $loc, _$log_) { 267 ['$window', '$location', '$log', function (_$window_, $loc, _$log_) {
...@@ -273,6 +284,7 @@ ...@@ -273,6 +284,7 @@
273 isSafari: isSafari, 284 isSafari: isSafari,
274 isFirefox: isFirefox, 285 isFirefox: isFirefox,
275 debugOn: debugOn, 286 debugOn: debugOn,
287 + debug: debug,
276 find: find, 288 find: find,
277 inArray: inArray, 289 inArray: inArray,
278 removeFromArray: removeFromArray, 290 removeFromArray: removeFromArray,
......