blob: 66e1c419c4dfcd804015672721e7fc9c7ca0318e [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
Simon Hunt5c3ed732017-07-20 19:03:28 +000024 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 = [],
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',
Simon Hunt5c3ed732017-07-20 19:03:28 +000039 dark: '#222222'
Simon Hunta6ae1212017-03-18 17:58:53 -070040 },
41 stroke: {
42 light: '#cccccc',
Simon Hunt5c3ed732017-07-20 19:03:28 +000043 dark: '#333333'
44 }
Simon Hunta6ae1212017-03-18 17:58:53 -070045 },
46 gold1: {
47 fill: {
48 light: '#eeddaa',
Simon Hunt5c3ed732017-07-20 19:03:28 +000049 dark: '#544714'
Simon Hunta6ae1212017-03-18 17:58:53 -070050 },
51 stroke: {
52 light: '#ffddaa',
Simon Hunt5c3ed732017-07-20 19:03:28 +000053 dark: '#645724'
54 }
Simon Hunta6ae1212017-03-18 17:58:53 -070055 },
56 blue1: {
57 fill: {
58 light: '#a2b9ee',
Simon Hunt5c3ed732017-07-20 19:03:28 +000059 dark: '#273059'
Simon Hunta6ae1212017-03-18 17:58:53 -070060 },
61 stroke: {
62 light: '#92a9de',
Simon Hunt5c3ed732017-07-20 19:03:28 +000063 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
Simon Hunt5c3ed732017-07-20 19:03:28 +0000112 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) {
Simon Hunt5c3ed732017-07-20 19:03:28 +0000122 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')
Simon Hunt5c3ed732017-07-20 19:03:28 +0000136 .factory('ThemeService', ['$log', 'FnService', 'PrefsService',
137 function (_$log_, _fs_, _ps_) {
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -0800138 $log = _$log_;
Simon Hunt5c3ed732017-07-20 19:03:28 +0000139 fs = _fs_;
Thomas Vachuska0af26912016-03-21 21:37:30 -0700140 ps = _ps_;
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -0800141
Thomas Vachuska341310c2016-03-24 10:14:13 -0700142 ps.addListener(applyTheme);
143
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -0800144 return {
145 init: init,
146 theme: function (x) {
147 if (x === undefined) {
148 return getTheme();
149 } else {
150 setTheme(x);
151 }
152 },
Simon Hunt245a88e2015-02-02 13:26:04 -0800153 toggleTheme: toggleTheme,
154 addListener: addListener,
Simon Hunta6ae1212017-03-18 17:58:53 -0700155 removeListener: removeListener,
Simon Hunt5c3ed732017-07-20 19:03:28 +0000156 spriteColor: spriteColor
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -0800157 };
158 }]);
159
160}());