Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
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 = [], |
Thomas Vachuska | 341310c | 2016-03-24 10:14:13 -0700 | [diff] [blame] | 32 | currentTheme, |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 33 | thidx; |
| 34 | |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 35 | |
| 36 | function init() { |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 37 | thidx = ps.getPrefs('theme', { idx: 0 }).idx; |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 38 | updateBodyClass(); |
| 39 | } |
| 40 | |
| 41 | function getTheme() { |
| 42 | return themes[thidx]; |
| 43 | } |
| 44 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 45 | function setTheme(t, force) { |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 46 | var idx = themes.indexOf(t); |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 47 | if (force || idx > -1 && idx !== thidx) { |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 48 | thidx = idx; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 49 | ps.setPrefs('theme', { idx: thidx }); |
Thomas Vachuska | 341310c | 2016-03-24 10:14:13 -0700 | [diff] [blame] | 50 | applyTheme(); |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | function toggleTheme() { |
| 55 | var i = thidx + 1; |
| 56 | thidx = (i===themes.length) ? 0 : i; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 57 | ps.setPrefs('theme', { idx: thidx }); |
Thomas Vachuska | 341310c | 2016-03-24 10:14:13 -0700 | [diff] [blame] | 58 | applyTheme('toggle'); |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 59 | return getTheme(); |
| 60 | } |
| 61 | |
Thomas Vachuska | 341310c | 2016-03-24 10:14:13 -0700 | [diff] [blame] | 62 | 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 HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 71 | function updateBodyClass() { |
| 72 | var body = d3.select('body'); |
| 73 | body.classed(themeStr, false); |
| 74 | body.classed(getTheme(), true); |
Thomas Vachuska | 341310c | 2016-03-24 10:14:13 -0700 | [diff] [blame] | 75 | currentTheme = thidx; |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | function themeEvent(w) { |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 79 | var t = getTheme(), |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 80 | m = 'Theme-Change-(' + w + '): ' + t; |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 81 | $log.debug(m); |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 82 | |
| 83 | listeners.forEach(function (lsnr) { |
| 84 | lsnr({event: 'themeChange', value: t}); |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 85 | }); |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 88 | function addListener(lsnr) { |
| 89 | listeners.push(lsnr); |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | function removeListener(lsnr) { |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 93 | listeners = listeners.filter(function(obj) { return obj === lsnr; }); |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 94 | } |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 95 | |
| 96 | angular.module('onosUtil') |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 97 | .factory('ThemeService', ['$log', 'FnService', 'PrefsService', |
| 98 | function (_$log_, _fs_, _ps_) { |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 99 | $log = _$log_; |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 100 | fs = _fs_; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 101 | ps = _ps_; |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 102 | |
Thomas Vachuska | 341310c | 2016-03-24 10:14:13 -0700 | [diff] [blame] | 103 | ps.addListener(applyTheme); |
| 104 | |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 105 | return { |
| 106 | init: init, |
| 107 | theme: function (x) { |
| 108 | if (x === undefined) { |
| 109 | return getTheme(); |
| 110 | } else { |
| 111 | setTheme(x); |
| 112 | } |
| 113 | }, |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 114 | toggleTheme: toggleTheme, |
| 115 | addListener: addListener, |
| 116 | removeListener: removeListener |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 117 | }; |
| 118 | }]); |
| 119 | |
| 120 | }()); |