blob: 8424850b51a89414b9f0f791bf1e412674ddb5c8 [file] [log] [blame]
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present 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
Simon Huntf51bf462016-06-29 16:22:57 -070023 // injected refs
Thomas Vachuska0af26912016-03-21 21:37:30 -070024 var $log, fs, ps;
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080025
Simon Huntf51bf462016-06-29 16:22:57 -070026 // configuration
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080027 var themes = ['light', 'dark'],
Simon Huntf51bf462016-06-29 16:22:57 -070028 themeStr = themes.join(' ');
29
30 // internal state
31 var listeners = [],
Thomas Vachuska341310c2016-03-24 10:14:13 -070032 currentTheme,
Simon Huntf51bf462016-06-29 16:22:57 -070033 thidx;
34
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080035
36 function init() {
Thomas Vachuska0af26912016-03-21 21:37:30 -070037 thidx = ps.getPrefs('theme', { idx: 0 }).idx;
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080038 updateBodyClass();
39 }
40
41 function getTheme() {
42 return themes[thidx];
43 }
44
Thomas Vachuska0af26912016-03-21 21:37:30 -070045 function setTheme(t, force) {
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080046 var idx = themes.indexOf(t);
Thomas Vachuska0af26912016-03-21 21:37:30 -070047 if (force || idx > -1 && idx !== thidx) {
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080048 thidx = idx;
Thomas Vachuska0af26912016-03-21 21:37:30 -070049 ps.setPrefs('theme', { idx: thidx });
Thomas Vachuska341310c2016-03-24 10:14:13 -070050 applyTheme();
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080051 }
52 }
53
54 function toggleTheme() {
55 var i = thidx + 1;
56 thidx = (i===themes.length) ? 0 : i;
Thomas Vachuska0af26912016-03-21 21:37:30 -070057 ps.setPrefs('theme', { idx: thidx });
Thomas Vachuska341310c2016-03-24 10:14:13 -070058 applyTheme('toggle');
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080059 return getTheme();
60 }
61
Thomas Vachuska341310c2016-03-24 10:14:13 -070062 function applyTheme(evt) {
63 thidx = ps.getPrefs('theme', { idx: thidx }).idx;
64 if (currentTheme != thidx) {
65 $log.info('Applying theme:', thidx);
66 updateBodyClass();
67 themeEvent(evt || 'set');
68 }
69 }
70
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080071 function updateBodyClass() {
72 var body = d3.select('body');
73 body.classed(themeStr, false);
74 body.classed(getTheme(), true);
Thomas Vachuska341310c2016-03-24 10:14:13 -070075 currentTheme = thidx;
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080076 }
77
78 function themeEvent(w) {
Simon Hunt245a88e2015-02-02 13:26:04 -080079 var t = getTheme(),
Simon Huntf51bf462016-06-29 16:22:57 -070080 m = 'Theme-Change-(' + w + '): ' + t;
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080081 $log.debug(m);
Simon Huntf51bf462016-06-29 16:22:57 -070082
83 listeners.forEach(function (lsnr) {
84 lsnr({event: 'themeChange', value: t});
Simon Hunt245a88e2015-02-02 13:26:04 -080085 });
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080086 }
87
Simon Huntf51bf462016-06-29 16:22:57 -070088 function addListener(lsnr) {
89 listeners.push(lsnr);
Simon Hunt245a88e2015-02-02 13:26:04 -080090 }
91
92 function removeListener(lsnr) {
Simon Huntf51bf462016-06-29 16:22:57 -070093 listeners = listeners.filter(function(obj) { return obj === lsnr; });
Simon Hunt245a88e2015-02-02 13:26:04 -080094 }
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080095
96 angular.module('onosUtil')
Thomas Vachuska0af26912016-03-21 21:37:30 -070097 .factory('ThemeService', ['$log', 'FnService', 'PrefsService',
98 function (_$log_, _fs_, _ps_) {
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080099 $log = _$log_;
Simon Hunt245a88e2015-02-02 13:26:04 -0800100 fs = _fs_;
Thomas Vachuska0af26912016-03-21 21:37:30 -0700101 ps = _ps_;
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -0800102
Thomas Vachuska341310c2016-03-24 10:14:13 -0700103 ps.addListener(applyTheme);
104
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -0800105 return {
106 init: init,
107 theme: function (x) {
108 if (x === undefined) {
109 return getTheme();
110 } else {
111 setTheme(x);
112 }
113 },
Simon Hunt245a88e2015-02-02 13:26:04 -0800114 toggleTheme: toggleTheme,
115 addListener: addListener,
116 removeListener: removeListener
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -0800117 };
118 }]);
119
120}());