blob: d85609b049d1836403554f5574915376e700f5de [file] [log] [blame]
Simon Huntc7ae7952015-04-08 18:59:27 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Simon Huntc7ae7952015-04-08 18:59:27 -07003 *
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 -- User Preference Service
19 */
20(function () {
21 'use strict';
22
23 // injected refs
Thomas Vachuska0af26912016-03-21 21:37:30 -070024 var $log, fs, wss;
Simon Huntc7ae7952015-04-08 18:59:27 -070025
26 // internal state
Thomas Vachuska341310c2016-03-24 10:14:13 -070027 var cache = {}, listeners = [];
Simon Huntc7ae7952015-04-08 18:59:27 -070028
Simon Hunt8a119b02016-03-29 16:00:32 -070029 // returns the preference settings for the specified key
30 function getPrefs(name, defaults, qparams) {
31 var obj = angular.extend({}, defaults || {}, cache[name] || {});
32
33 // if query params are specified, they override...
34 if (fs.isO(qparams)) {
35 angular.forEach(obj, function (v, k) {
36 if (qparams.hasOwnProperty(k)) {
37 obj[k] = qparams[k];
38 }
39 });
40 }
41 return obj;
Simon Huntc7ae7952015-04-08 18:59:27 -070042 }
43
Simon Huntfcbde892015-04-16 12:05:28 -070044 // converts string values to numbers for selected (or all) keys
45 function asNumbers(obj, keys) {
46 if (!obj) return null;
47
48 if (!keys) {
49 // do them all
50 angular.forEach(obj, function (v, k) {
51 obj[k] = Number(obj[k]);
52 });
53 } else {
54 keys.forEach(function (k) {
55 obj[k] = Number(obj[k]);
56 });
57 }
58 return obj;
59 }
60
Simon Huntc7ae7952015-04-08 18:59:27 -070061 function setPrefs(name, obj) {
Thomas Vachuska0af26912016-03-21 21:37:30 -070062 // keep a cached copy of the object and send an update to server
Simon Huntc7ae7952015-04-08 18:59:27 -070063 cache[name] = obj;
Thomas Vachuska0af26912016-03-21 21:37:30 -070064 wss.sendEvent('updatePrefReq', { key: name, value: obj });
65 }
66
67 function updatePrefs(data) {
68 $log.info('User properties updated');
Thomas Vachuska341310c2016-03-24 10:14:13 -070069 cache = data;
Simon Hunt8a119b02016-03-29 16:00:32 -070070 listeners.forEach(function (lsnr) { lsnr(); });
Thomas Vachuska341310c2016-03-24 10:14:13 -070071 }
72
73 function addListener(listener) {
74 listeners.push(listener);
75 }
76
77 function removeListener(listener) {
78 listeners = listeners.filter(function(obj) { return obj === listener; });
Simon Huntc7ae7952015-04-08 18:59:27 -070079 }
80
81 angular.module('onosUtil')
Thomas Vachuska0af26912016-03-21 21:37:30 -070082 .factory('PrefsService', ['$log', 'FnService', 'WebSocketService',
83 function (_$log_, _fs_, _wss_) {
Simon Huntc7ae7952015-04-08 18:59:27 -070084 $log = _$log_;
Simon Hunt4deb0e82015-06-10 16:18:25 -070085 fs = _fs_;
Thomas Vachuska0af26912016-03-21 21:37:30 -070086 wss = _wss_;
87
Matteo Scandolocf060772016-05-13 15:54:44 -070088 try {
89 cache = angular.isDefined(userPrefs) ? userPrefs : {};
90 }
91 catch(e){
92 // browser throws error for non-existing globals
93 cache = {}
94 }
Thomas Vachuska0af26912016-03-21 21:37:30 -070095
96 wss.bindHandlers({
97 updatePrefs: updatePrefs
98 });
Simon Huntc7ae7952015-04-08 18:59:27 -070099
100 return {
101 getPrefs: getPrefs,
Simon Huntfcbde892015-04-16 12:05:28 -0700102 asNumbers: asNumbers,
Thomas Vachuska341310c2016-03-24 10:14:13 -0700103 setPrefs: setPrefs,
104 addListener: addListener,
105 removeListener: removeListener
Simon Huntc7ae7952015-04-08 18:59:27 -0700106 };
107 }]);
108
109}());