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 |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 19 | */ |
| 20 | (function () { |
| 21 | 'use strict'; |
| 22 | |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 23 | var $log, fs; |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 24 | |
| 25 | var themes = ['light', 'dark'], |
| 26 | themeStr = themes.join(' '), |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 27 | thidx, |
| 28 | listeners = {}, |
| 29 | nextListenerId = 1; |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 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) { |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 64 | var t = getTheme(), |
| 65 | m = 'Theme-Change-('+w+'): ' + t; |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 66 | $log.debug(m); |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 67 | angular.forEach(listeners, function(value) { |
| 68 | value.cb( |
| 69 | { |
| 70 | event: 'themeChange', |
| 71 | value: t |
| 72 | } |
| 73 | ); |
| 74 | }); |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 77 | function addListener(callback) { |
| 78 | var id = nextListenerId++, |
| 79 | cb = fs.isF(callback), |
| 80 | o = { id: id, cb: cb }; |
| 81 | |
| 82 | if (cb) { |
| 83 | listeners[id] = o; |
| 84 | } else { |
| 85 | $log.error('ThemeService.addListener(): callback not a function'); |
| 86 | o.error = 'No callback defined'; |
| 87 | } |
| 88 | return o; |
| 89 | } |
| 90 | |
| 91 | function removeListener(lsnr) { |
| 92 | var id = lsnr && lsnr.id, |
| 93 | o = listeners[id]; |
| 94 | if (o) { |
| 95 | delete listeners[id]; |
| 96 | } |
| 97 | } |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 98 | |
| 99 | angular.module('onosUtil') |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 100 | .factory('ThemeService', ['$log', 'FnService', |
| 101 | function (_$log_, _fs_) { |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 102 | $log = _$log_; |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 103 | fs = _fs_; |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 104 | thidx = 0; |
| 105 | |
| 106 | return { |
| 107 | init: init, |
| 108 | theme: function (x) { |
| 109 | if (x === undefined) { |
| 110 | return getTheme(); |
| 111 | } else { |
| 112 | setTheme(x); |
| 113 | } |
| 114 | }, |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 115 | toggleTheme: toggleTheme, |
| 116 | addListener: addListener, |
| 117 | removeListener: removeListener |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 118 | }; |
| 119 | }]); |
| 120 | |
| 121 | }()); |