blob: baa13c18d993ded4f77636fb53716919c6438973 [file] [log] [blame]
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -08001/*
Simon Hunt8ead3a22015-01-06 11:00:15 -08002 * Copyright 2014,2015 Open Networking Laboratory
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -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 -- Util -- Theme Service
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080019 */
20(function () {
21 'use strict';
22
Thomas Vachuska0af26912016-03-21 21:37:30 -070023 var $log, fs, ps;
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080024
25 var themes = ['light', 'dark'],
26 themeStr = themes.join(' '),
Thomas Vachuska341310c2016-03-24 10:14:13 -070027 currentTheme,
Simon Hunt245a88e2015-02-02 13:26:04 -080028 thidx,
29 listeners = {},
30 nextListenerId = 1;
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080031
32 function init() {
Thomas Vachuska0af26912016-03-21 21:37:30 -070033 thidx = ps.getPrefs('theme', { idx: 0 }).idx;
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080034 updateBodyClass();
35 }
36
37 function getTheme() {
38 return themes[thidx];
39 }
40
Thomas Vachuska0af26912016-03-21 21:37:30 -070041 function setTheme(t, force) {
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080042 var idx = themes.indexOf(t);
Thomas Vachuska0af26912016-03-21 21:37:30 -070043 if (force || idx > -1 && idx !== thidx) {
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080044 thidx = idx;
Thomas Vachuska0af26912016-03-21 21:37:30 -070045 ps.setPrefs('theme', { idx: thidx });
Thomas Vachuska341310c2016-03-24 10:14:13 -070046 applyTheme();
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080047 }
48 }
49
50 function toggleTheme() {
51 var i = thidx + 1;
52 thidx = (i===themes.length) ? 0 : i;
Thomas Vachuska0af26912016-03-21 21:37:30 -070053 ps.setPrefs('theme', { idx: thidx });
Thomas Vachuska341310c2016-03-24 10:14:13 -070054 applyTheme('toggle');
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080055 return getTheme();
56 }
57
Thomas Vachuska341310c2016-03-24 10:14:13 -070058 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
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080067 function updateBodyClass() {
68 var body = d3.select('body');
69 body.classed(themeStr, false);
70 body.classed(getTheme(), true);
Thomas Vachuska341310c2016-03-24 10:14:13 -070071 currentTheme = thidx;
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080072 }
73
74 function themeEvent(w) {
Simon Hunt245a88e2015-02-02 13:26:04 -080075 var t = getTheme(),
76 m = 'Theme-Change-('+w+'): ' + t;
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080077 $log.debug(m);
Simon Hunt245a88e2015-02-02 13:26:04 -080078 angular.forEach(listeners, function(value) {
Thomas Vachuska341310c2016-03-24 10:14:13 -070079 value.cb({
80 event: 'themeChange',
Simon Hunt245a88e2015-02-02 13:26:04 -080081 value: t
82 }
83 );
84 });
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080085 }
86
Simon Hunt245a88e2015-02-02 13:26:04 -080087 function addListener(callback) {
88 var id = nextListenerId++,
89 cb = fs.isF(callback),
90 o = { id: id, cb: cb };
91
92 if (cb) {
93 listeners[id] = o;
94 } else {
95 $log.error('ThemeService.addListener(): callback not a function');
96 o.error = 'No callback defined';
97 }
98 return o;
99 }
100
101 function removeListener(lsnr) {
102 var id = lsnr && lsnr.id,
103 o = listeners[id];
104 if (o) {
105 delete listeners[id];
106 }
107 }
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -0800108
109 angular.module('onosUtil')
Thomas Vachuska0af26912016-03-21 21:37:30 -0700110 .factory('ThemeService', ['$log', 'FnService', 'PrefsService',
111 function (_$log_, _fs_, _ps_) {
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -0800112 $log = _$log_;
Simon Hunt245a88e2015-02-02 13:26:04 -0800113 fs = _fs_;
Thomas Vachuska0af26912016-03-21 21:37:30 -0700114 ps = _ps_;
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -0800115
Thomas Vachuska341310c2016-03-24 10:14:13 -0700116 ps.addListener(applyTheme);
117
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -0800118 return {
119 init: init,
120 theme: function (x) {
121 if (x === undefined) {
122 return getTheme();
123 } else {
124 setTheme(x);
125 }
126 },
Simon Hunt245a88e2015-02-02 13:26:04 -0800127 toggleTheme: toggleTheme,
128 addListener: addListener,
129 removeListener: removeListener
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -0800130 };
131 }]);
132
133}());