blob: db037018302756f28069d574bb8ae600ae0173b2 [file] [log] [blame]
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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
Steven Burrows1c2a9682017-07-14 16:52:46 +010024 var $log, 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 = [],
Simon Huntf51bf462016-06-29 16:22:57 -070032 thidx;
33
Simon Hunta6ae1212017-03-18 17:58:53 -070034 // TODO: fine tune these colors
35 var spriteColors = {
36 gray1: {
37 fill: {
38 light: '#eeeeee',
Steven Burrows1c2a9682017-07-14 16:52:46 +010039 dark: '#222222',
Simon Hunta6ae1212017-03-18 17:58:53 -070040 },
41 stroke: {
42 light: '#cccccc',
Steven Burrows1c2a9682017-07-14 16:52:46 +010043 dark: '#333333',
44 },
Simon Hunta6ae1212017-03-18 17:58:53 -070045 },
46 gold1: {
47 fill: {
48 light: '#eeddaa',
Steven Burrows1c2a9682017-07-14 16:52:46 +010049 dark: '#544714',
Simon Hunta6ae1212017-03-18 17:58:53 -070050 },
51 stroke: {
52 light: '#ffddaa',
Steven Burrows1c2a9682017-07-14 16:52:46 +010053 dark: '#645724',
54 },
Simon Hunta6ae1212017-03-18 17:58:53 -070055 },
56 blue1: {
57 fill: {
58 light: '#a2b9ee',
Steven Burrows1c2a9682017-07-14 16:52:46 +010059 dark: '#273059',
Simon Hunta6ae1212017-03-18 17:58:53 -070060 },
61 stroke: {
62 light: '#92a9de',
Steven Burrows1c2a9682017-07-14 16:52:46 +010063 dark: '#273a63',
64 },
65 },
Simon Hunta6ae1212017-03-18 17:58:53 -070066 };
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080067
68 function init() {
Thomas Vachuska0af26912016-03-21 21:37:30 -070069 thidx = ps.getPrefs('theme', { idx: 0 }).idx;
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080070 updateBodyClass();
71 }
72
73 function getTheme() {
74 return themes[thidx];
75 }
76
Steven Burrows54462eb2017-04-12 15:50:16 -070077 function setTheme(t) {
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080078 var idx = themes.indexOf(t);
Steven Burrows54462eb2017-04-12 15:50:16 -070079 if (idx > -1 && idx !== thidx) {
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080080 thidx = idx;
Thomas Vachuska0af26912016-03-21 21:37:30 -070081 ps.setPrefs('theme', { idx: thidx });
Thomas Vachuska341310c2016-03-24 10:14:13 -070082 applyTheme();
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080083 }
84 }
85
86 function toggleTheme() {
87 var i = thidx + 1;
88 thidx = (i===themes.length) ? 0 : i;
Thomas Vachuska0af26912016-03-21 21:37:30 -070089 ps.setPrefs('theme', { idx: thidx });
Thomas Vachuska341310c2016-03-24 10:14:13 -070090 applyTheme('toggle');
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080091 return getTheme();
92 }
93
Thomas Vachuska341310c2016-03-24 10:14:13 -070094 function applyTheme(evt) {
95 thidx = ps.getPrefs('theme', { idx: thidx }).idx;
Steven Burrows54462eb2017-04-12 15:50:16 -070096 $log.info('Applying theme:', thidx);
97 updateBodyClass();
98 themeEvent(evt || 'set');
Thomas Vachuska341310c2016-03-24 10:14:13 -070099 }
100
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -0800101 function updateBodyClass() {
102 var body = d3.select('body');
103 body.classed(themeStr, false);
104 body.classed(getTheme(), true);
105 }
106
107 function themeEvent(w) {
Simon Hunt245a88e2015-02-02 13:26:04 -0800108 var t = getTheme(),
Simon Huntf51bf462016-06-29 16:22:57 -0700109 m = 'Theme-Change-(' + w + '): ' + t;
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -0800110 $log.debug(m);
Simon Huntf51bf462016-06-29 16:22:57 -0700111
Steven Burrows1c2a9682017-07-14 16:52:46 +0100112 listeners.forEach(function (lsnr) {
113 lsnr({ event: 'themeChange', value: t });
Simon Hunt245a88e2015-02-02 13:26:04 -0800114 });
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -0800115 }
116
Simon Huntf51bf462016-06-29 16:22:57 -0700117 function addListener(lsnr) {
118 listeners.push(lsnr);
Simon Hunt245a88e2015-02-02 13:26:04 -0800119 }
120
121 function removeListener(lsnr) {
Steven Burrows1c2a9682017-07-14 16:52:46 +0100122 listeners = listeners.filter(function (obj) { return obj !== lsnr; });
Simon Hunt245a88e2015-02-02 13:26:04 -0800123 }
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -0800124
Simon Hunta6ae1212017-03-18 17:58:53 -0700125 // color = logical color name
126 // what = fill or stroke
127 function spriteColor(color, what) {
128 var c = color || 'none',
129 w = what || 'stroke',
130 t = getTheme();
131
132 return c === 'none' ? c : spriteColors[c][w][t];
133 }
134
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -0800135 angular.module('onosUtil')
Steven Burrows1c2a9682017-07-14 16:52:46 +0100136 .factory('ThemeService', ['$log', 'PrefsService',
137 function (_$log_, _ps_) {
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -0800138 $log = _$log_;
Thomas Vachuska0af26912016-03-21 21:37:30 -0700139 ps = _ps_;
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -0800140
Thomas Vachuska341310c2016-03-24 10:14:13 -0700141 ps.addListener(applyTheme);
142
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -0800143 return {
144 init: init,
145 theme: function (x) {
146 if (x === undefined) {
147 return getTheme();
148 } else {
149 setTheme(x);
150 }
151 },
Simon Hunt245a88e2015-02-02 13:26:04 -0800152 toggleTheme: toggleTheme,
153 addListener: addListener,
Simon Hunta6ae1212017-03-18 17:58:53 -0700154 removeListener: removeListener,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100155 spriteColor: spriteColor,
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -0800156 };
157 }]);
158
159}());