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