blob: 0e0ee6497c970b5df61cff95eaeeddf29c44d01f [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
Simon Hunt245a88e2015-02-02 13:26:04 -080023 var $log, fs;
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080024
25 var themes = ['light', 'dark'],
26 themeStr = themes.join(' '),
Simon Hunt245a88e2015-02-02 13:26:04 -080027 thidx,
28 listeners = {},
29 nextListenerId = 1;
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080030
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 Hunt245a88e2015-02-02 13:26:04 -080064 var t = getTheme(),
65 m = 'Theme-Change-('+w+'): ' + t;
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080066 $log.debug(m);
Simon Hunt245a88e2015-02-02 13:26:04 -080067 angular.forEach(listeners, function(value) {
68 value.cb(
69 {
70 event: 'themeChange',
71 value: t
72 }
73 );
74 });
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080075 }
76
Simon Hunt245a88e2015-02-02 13:26:04 -080077 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 HIGUCHI4f39bcd2014-12-18 20:46:14 -080098
99 angular.module('onosUtil')
Simon Hunt245a88e2015-02-02 13:26:04 -0800100 .factory('ThemeService', ['$log', 'FnService',
101 function (_$log_, _fs_) {
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -0800102 $log = _$log_;
Simon Hunt245a88e2015-02-02 13:26:04 -0800103 fs = _fs_;
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -0800104 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 Hunt245a88e2015-02-02 13:26:04 -0800115 toggleTheme: toggleTheme,
116 addListener: addListener,
117 removeListener: removeListener
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -0800118 };
119 }]);
120
121}());