Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 1 | /* |
Simon Hunt | 8ead3a2 | 2015-01-06 11:00:15 -0800 | [diff] [blame] | 2 | * Copyright 2014,2015 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 |
| 19 | |
| 20 | @author Simon Hunt |
| 21 | */ |
| 22 | (function () { |
| 23 | 'use strict'; |
| 24 | |
| 25 | var $log; |
| 26 | |
| 27 | var themes = ['light', 'dark'], |
| 28 | themeStr = themes.join(' '), |
| 29 | thidx; |
| 30 | |
| 31 | function init() { |
| 32 | thidx = 0; |
| 33 | updateBodyClass(); |
| 34 | } |
| 35 | |
| 36 | function getTheme() { |
| 37 | return themes[thidx]; |
| 38 | } |
| 39 | |
| 40 | function setTheme(t) { |
| 41 | var idx = themes.indexOf(t); |
| 42 | if (idx > -1 && idx !== thidx) { |
| 43 | thidx = idx; |
| 44 | updateBodyClass(); |
| 45 | themeEvent('set'); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | function toggleTheme() { |
| 50 | var i = thidx + 1; |
| 51 | thidx = (i===themes.length) ? 0 : i; |
| 52 | updateBodyClass(); |
| 53 | themeEvent('toggle'); |
| 54 | return getTheme(); |
| 55 | } |
| 56 | |
| 57 | function updateBodyClass() { |
| 58 | var body = d3.select('body'); |
| 59 | body.classed(themeStr, false); |
| 60 | body.classed(getTheme(), true); |
| 61 | } |
| 62 | |
| 63 | function themeEvent(w) { |
| 64 | // TODO: emit a theme-changed event |
| 65 | var m = 'Theme-Change-('+w+'): ' + getTheme(); |
| 66 | $log.debug(m); |
| 67 | } |
| 68 | |
| 69 | // TODO: add hook for theme-change listener |
| 70 | |
| 71 | angular.module('onosUtil') |
| 72 | .factory('ThemeService', ['$log', function (_$log_) { |
| 73 | $log = _$log_; |
| 74 | thidx = 0; |
| 75 | |
| 76 | return { |
| 77 | init: init, |
| 78 | theme: function (x) { |
| 79 | if (x === undefined) { |
| 80 | return getTheme(); |
| 81 | } else { |
| 82 | setTheme(x); |
| 83 | } |
| 84 | }, |
| 85 | toggleTheme: toggleTheme |
| 86 | }; |
| 87 | }]); |
| 88 | |
| 89 | }()); |