Thomas Vachuska

Adding a listener mechanism for updated preferences.

Theme updates are now propagated to all user sessions.

Change-Id: If0e3f417294ee503c1186710c614d813b7cbd88e
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
24 var $log, fs, wss; 24 var $log, fs, wss;
25 25
26 // internal state 26 // internal state
27 - var cache = {}; 27 + var cache = {}, listeners = [];
28 28
29 // returns the preference by the specified name 29 // returns the preference by the specified name
30 function getPrefs(name, defaults) { 30 function getPrefs(name, defaults) {
...@@ -56,7 +56,16 @@ ...@@ -56,7 +56,16 @@
56 56
57 function updatePrefs(data) { 57 function updatePrefs(data) {
58 $log.info('User properties updated'); 58 $log.info('User properties updated');
59 - cache[data.key] = data.value; 59 + cache = data;
60 + listeners.forEach(function (l) { l(); });
61 + }
62 +
63 + function addListener(listener) {
64 + listeners.push(listener);
65 + }
66 +
67 + function removeListener(listener) {
68 + listeners = listeners.filter(function(obj) { return obj === listener; });
60 } 69 }
61 70
62 angular.module('onosUtil') 71 angular.module('onosUtil')
...@@ -75,7 +84,9 @@ ...@@ -75,7 +84,9 @@
75 return { 84 return {
76 getPrefs: getPrefs, 85 getPrefs: getPrefs,
77 asNumbers: asNumbers, 86 asNumbers: asNumbers,
78 - setPrefs: setPrefs 87 + setPrefs: setPrefs,
88 + addListener: addListener,
89 + removeListener: removeListener
79 }; 90 };
80 }]); 91 }]);
81 92
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
24 24
25 var themes = ['light', 'dark'], 25 var themes = ['light', 'dark'],
26 themeStr = themes.join(' '), 26 themeStr = themes.join(' '),
27 + currentTheme,
27 thidx, 28 thidx,
28 listeners = {}, 29 listeners = {},
29 nextListenerId = 1; 30 nextListenerId = 1;
...@@ -42,8 +43,7 @@ ...@@ -42,8 +43,7 @@
42 if (force || idx > -1 && idx !== thidx) { 43 if (force || idx > -1 && idx !== thidx) {
43 thidx = idx; 44 thidx = idx;
44 ps.setPrefs('theme', { idx: thidx }); 45 ps.setPrefs('theme', { idx: thidx });
45 - updateBodyClass(); 46 + applyTheme();
46 - themeEvent('set');
47 } 47 }
48 } 48 }
49 49
...@@ -51,15 +51,24 @@ ...@@ -51,15 +51,24 @@
51 var i = thidx + 1; 51 var i = thidx + 1;
52 thidx = (i===themes.length) ? 0 : i; 52 thidx = (i===themes.length) ? 0 : i;
53 ps.setPrefs('theme', { idx: thidx }); 53 ps.setPrefs('theme', { idx: thidx });
54 - updateBodyClass(); 54 + applyTheme('toggle');
55 - themeEvent('toggle');
56 return getTheme(); 55 return getTheme();
57 } 56 }
58 57
58 + function applyTheme(evt) {
59 + thidx = ps.getPrefs('theme', { idx: thidx }).idx;
60 + if (currentTheme != thidx) {
61 + $log.info('Applying theme:', thidx);
62 + updateBodyClass();
63 + themeEvent(evt || 'set');
64 + }
65 + }
66 +
59 function updateBodyClass() { 67 function updateBodyClass() {
60 var body = d3.select('body'); 68 var body = d3.select('body');
61 body.classed(themeStr, false); 69 body.classed(themeStr, false);
62 body.classed(getTheme(), true); 70 body.classed(getTheme(), true);
71 + currentTheme = thidx;
63 } 72 }
64 73
65 function themeEvent(w) { 74 function themeEvent(w) {
...@@ -67,8 +76,7 @@ ...@@ -67,8 +76,7 @@
67 m = 'Theme-Change-('+w+'): ' + t; 76 m = 'Theme-Change-('+w+'): ' + t;
68 $log.debug(m); 77 $log.debug(m);
69 angular.forEach(listeners, function(value) { 78 angular.forEach(listeners, function(value) {
70 - value.cb( 79 + value.cb({
71 - {
72 event: 'themeChange', 80 event: 'themeChange',
73 value: t 81 value: t
74 } 82 }
...@@ -105,6 +113,8 @@ ...@@ -105,6 +113,8 @@
105 fs = _fs_; 113 fs = _fs_;
106 ps = _ps_; 114 ps = _ps_;
107 115
116 + ps.addListener(applyTheme);
117 +
108 return { 118 return {
109 init: init, 119 init: init,
110 theme: function (x) { 120 theme: function (x) {
......