Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | ce2a03d | 2017-08-03 19:21:03 -0700 | [diff] [blame^] | 2 | * Copyright 2014-present Open Networking Foundation |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 3 | * |
| 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 HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 19 | */ |
| 20 | (function () { |
| 21 | 'use strict'; |
| 22 | |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 23 | // injected refs |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 24 | var $log, fs, ps; |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 25 | |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 26 | // configuration |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 27 | var themes = ['light', 'dark'], |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 28 | themeStr = themes.join(' '); |
| 29 | |
| 30 | // internal state |
| 31 | var listeners = [], |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 32 | thidx; |
| 33 | |
Simon Hunt | a6ae121 | 2017-03-18 17:58:53 -0700 | [diff] [blame] | 34 | // TODO: fine tune these colors |
| 35 | var spriteColors = { |
| 36 | gray1: { |
| 37 | fill: { |
| 38 | light: '#eeeeee', |
| 39 | dark: '#222222' |
| 40 | }, |
| 41 | stroke: { |
| 42 | light: '#cccccc', |
| 43 | dark: '#333333' |
| 44 | } |
| 45 | }, |
| 46 | gold1: { |
| 47 | fill: { |
| 48 | light: '#eeddaa', |
| 49 | dark: '#544714' |
| 50 | }, |
| 51 | stroke: { |
| 52 | light: '#ffddaa', |
| 53 | dark: '#645724' |
| 54 | } |
| 55 | }, |
| 56 | blue1: { |
| 57 | fill: { |
| 58 | light: '#a2b9ee', |
| 59 | dark: '#273059' |
| 60 | }, |
| 61 | stroke: { |
| 62 | light: '#92a9de', |
| 63 | dark: '#273a63' |
| 64 | } |
| 65 | } |
| 66 | }; |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 67 | |
| 68 | function init() { |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 69 | thidx = ps.getPrefs('theme', { idx: 0 }).idx; |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 70 | updateBodyClass(); |
| 71 | } |
| 72 | |
| 73 | function getTheme() { |
| 74 | return themes[thidx]; |
| 75 | } |
| 76 | |
Steven Burrows | 54462eb | 2017-04-12 15:50:16 -0700 | [diff] [blame] | 77 | function setTheme(t) { |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 78 | var idx = themes.indexOf(t); |
Steven Burrows | 54462eb | 2017-04-12 15:50:16 -0700 | [diff] [blame] | 79 | if (idx > -1 && idx !== thidx) { |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 80 | thidx = idx; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 81 | ps.setPrefs('theme', { idx: thidx }); |
Thomas Vachuska | 341310c | 2016-03-24 10:14:13 -0700 | [diff] [blame] | 82 | applyTheme(); |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | function toggleTheme() { |
| 87 | var i = thidx + 1; |
| 88 | thidx = (i===themes.length) ? 0 : i; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 89 | ps.setPrefs('theme', { idx: thidx }); |
Thomas Vachuska | 341310c | 2016-03-24 10:14:13 -0700 | [diff] [blame] | 90 | applyTheme('toggle'); |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 91 | return getTheme(); |
| 92 | } |
| 93 | |
Thomas Vachuska | 341310c | 2016-03-24 10:14:13 -0700 | [diff] [blame] | 94 | function applyTheme(evt) { |
| 95 | thidx = ps.getPrefs('theme', { idx: thidx }).idx; |
Steven Burrows | 54462eb | 2017-04-12 15:50:16 -0700 | [diff] [blame] | 96 | $log.info('Applying theme:', thidx); |
| 97 | updateBodyClass(); |
| 98 | themeEvent(evt || 'set'); |
Thomas Vachuska | 341310c | 2016-03-24 10:14:13 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 101 | 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 Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 108 | var t = getTheme(), |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 109 | m = 'Theme-Change-(' + w + '): ' + t; |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 110 | $log.debug(m); |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 111 | |
| 112 | listeners.forEach(function (lsnr) { |
| 113 | lsnr({event: 'themeChange', value: t}); |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 114 | }); |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 117 | function addListener(lsnr) { |
| 118 | listeners.push(lsnr); |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | function removeListener(lsnr) { |
Steven Burrows | 54462eb | 2017-04-12 15:50:16 -0700 | [diff] [blame] | 122 | listeners = listeners.filter(function(obj) { return obj !== lsnr; }); |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 123 | } |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 124 | |
Simon Hunt | a6ae121 | 2017-03-18 17:58:53 -0700 | [diff] [blame] | 125 | // 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 HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 135 | angular.module('onosUtil') |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 136 | .factory('ThemeService', ['$log', 'FnService', 'PrefsService', |
| 137 | function (_$log_, _fs_, _ps_) { |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 138 | $log = _$log_; |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 139 | fs = _fs_; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 140 | ps = _ps_; |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 141 | |
Thomas Vachuska | 341310c | 2016-03-24 10:14:13 -0700 | [diff] [blame] | 142 | ps.addListener(applyTheme); |
| 143 | |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 144 | return { |
| 145 | init: init, |
| 146 | theme: function (x) { |
| 147 | if (x === undefined) { |
| 148 | return getTheme(); |
| 149 | } else { |
| 150 | setTheme(x); |
| 151 | } |
| 152 | }, |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 153 | toggleTheme: toggleTheme, |
| 154 | addListener: addListener, |
Simon Hunt | a6ae121 | 2017-03-18 17:58:53 -0700 | [diff] [blame] | 155 | removeListener: removeListener, |
| 156 | spriteColor: spriteColor |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 157 | }; |
| 158 | }]); |
| 159 | |
| 160 | }()); |