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 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 23 | var $log, fs, ps; |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 24 | |
| 25 | var themes = ['light', 'dark'], |
| 26 | themeStr = themes.join(' '), |
Thomas Vachuska | 341310c | 2016-03-24 10:14:13 -0700 | [diff] [blame] | 27 | currentTheme, |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 28 | thidx, |
| 29 | listeners = {}, |
| 30 | nextListenerId = 1; |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 31 | |
| 32 | function init() { |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 33 | thidx = ps.getPrefs('theme', { idx: 0 }).idx; |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 34 | updateBodyClass(); |
| 35 | } |
| 36 | |
| 37 | function getTheme() { |
| 38 | return themes[thidx]; |
| 39 | } |
| 40 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 41 | function setTheme(t, force) { |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 42 | var idx = themes.indexOf(t); |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 43 | if (force || idx > -1 && idx !== thidx) { |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 44 | thidx = idx; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 45 | ps.setPrefs('theme', { idx: thidx }); |
Thomas Vachuska | 341310c | 2016-03-24 10:14:13 -0700 | [diff] [blame] | 46 | applyTheme(); |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | |
| 50 | function toggleTheme() { |
| 51 | var i = thidx + 1; |
| 52 | thidx = (i===themes.length) ? 0 : i; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 53 | ps.setPrefs('theme', { idx: thidx }); |
Thomas Vachuska | 341310c | 2016-03-24 10:14:13 -0700 | [diff] [blame] | 54 | applyTheme('toggle'); |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 55 | return getTheme(); |
| 56 | } |
| 57 | |
Thomas Vachuska | 341310c | 2016-03-24 10:14:13 -0700 | [diff] [blame] | 58 | function applyTheme(evt) { |
| 59 | thidx = ps.getPrefs('theme', { idx: thidx }).idx; |
| 60 | if (currentTheme != thidx) { |
| 61 | $log.info('Applying theme:', thidx); |
| 62 | updateBodyClass(); |
| 63 | themeEvent(evt || 'set'); |
| 64 | } |
| 65 | } |
| 66 | |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 67 | function updateBodyClass() { |
| 68 | var body = d3.select('body'); |
| 69 | body.classed(themeStr, false); |
| 70 | body.classed(getTheme(), true); |
Thomas Vachuska | 341310c | 2016-03-24 10:14:13 -0700 | [diff] [blame] | 71 | currentTheme = thidx; |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | function themeEvent(w) { |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 75 | var t = getTheme(), |
| 76 | m = 'Theme-Change-('+w+'): ' + t; |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 77 | $log.debug(m); |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 78 | angular.forEach(listeners, function(value) { |
Thomas Vachuska | 341310c | 2016-03-24 10:14:13 -0700 | [diff] [blame] | 79 | value.cb({ |
| 80 | event: 'themeChange', |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 81 | value: t |
| 82 | } |
| 83 | ); |
| 84 | }); |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 87 | function addListener(callback) { |
| 88 | var id = nextListenerId++, |
| 89 | cb = fs.isF(callback), |
| 90 | o = { id: id, cb: cb }; |
| 91 | |
| 92 | if (cb) { |
| 93 | listeners[id] = o; |
| 94 | } else { |
| 95 | $log.error('ThemeService.addListener(): callback not a function'); |
| 96 | o.error = 'No callback defined'; |
| 97 | } |
| 98 | return o; |
| 99 | } |
| 100 | |
| 101 | function removeListener(lsnr) { |
| 102 | var id = lsnr && lsnr.id, |
| 103 | o = listeners[id]; |
| 104 | if (o) { |
| 105 | delete listeners[id]; |
| 106 | } |
| 107 | } |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 108 | |
| 109 | angular.module('onosUtil') |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 110 | .factory('ThemeService', ['$log', 'FnService', 'PrefsService', |
| 111 | function (_$log_, _fs_, _ps_) { |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 112 | $log = _$log_; |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 113 | fs = _fs_; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 114 | ps = _ps_; |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 115 | |
Thomas Vachuska | 341310c | 2016-03-24 10:14:13 -0700 | [diff] [blame] | 116 | ps.addListener(applyTheme); |
| 117 | |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 118 | return { |
| 119 | init: init, |
| 120 | theme: function (x) { |
| 121 | if (x === undefined) { |
| 122 | return getTheme(); |
| 123 | } else { |
| 124 | setTheme(x); |
| 125 | } |
| 126 | }, |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 127 | toggleTheme: toggleTheme, |
| 128 | addListener: addListener, |
| 129 | removeListener: removeListener |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 130 | }; |
| 131 | }]); |
| 132 | |
| 133 | }()); |