blob: fe4201d0fd476213533c77a5c21d45438785d147 [file] [log] [blame]
Simon Huntc7ae7952015-04-08 18:59:27 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
Steven Burrows1c2a9682017-07-14 16:52:46 +010024 var fs, wss;
Simon Huntc7ae7952015-04-08 18:59:27 -070025
26 // internal state
Steven Burrows1c2a9682017-07-14 16:52:46 +010027 var cache = {},
Simon Huntf51bf462016-06-29 16:22:57 -070028 listeners = [];
Simon Huntc7ae7952015-04-08 18:59:27 -070029
Simon Hunt8a119b02016-03-29 16:00:32 -070030 // returns the preference settings for the specified key
31 function getPrefs(name, defaults, qparams) {
32 var obj = angular.extend({}, defaults || {}, cache[name] || {});
33
34 // if query params are specified, they override...
35 if (fs.isO(qparams)) {
36 angular.forEach(obj, function (v, k) {
37 if (qparams.hasOwnProperty(k)) {
38 obj[k] = qparams[k];
39 }
40 });
41 }
42 return obj;
Simon Huntc7ae7952015-04-08 18:59:27 -070043 }
44
Simon Huntfcbde892015-04-16 12:05:28 -070045 // converts string values to numbers for selected (or all) keys
Simon Huntfc5c5842017-02-01 23:32:18 -080046 // asNumbers(obj, ['a', 'b']) <-- convert keys .a, .b to numbers
47 // asNumbers(obj, ['a', 'b'], true) <-- convert ALL BUT keys .a, .b to numbers
48
49 function asNumbers(obj, keys, not) {
Simon Huntfcbde892015-04-16 12:05:28 -070050 if (!obj) return null;
51
Simon Huntfc5c5842017-02-01 23:32:18 -080052 var skip = {};
53 if (not) {
54 keys.forEach(function (k) {
55 skip[k] = 1;
56 });
57 }
58
59 if (!keys || not) {
Simon Huntfcbde892015-04-16 12:05:28 -070060 // do them all
61 angular.forEach(obj, function (v, k) {
Simon Huntfc5c5842017-02-01 23:32:18 -080062 if (!not || !skip[k]) {
63 obj[k] = Number(obj[k]);
64 }
Simon Huntfcbde892015-04-16 12:05:28 -070065 });
66 } else {
Simon Huntfc5c5842017-02-01 23:32:18 -080067 // do the explicitly named keys
Simon Huntfcbde892015-04-16 12:05:28 -070068 keys.forEach(function (k) {
69 obj[k] = Number(obj[k]);
70 });
71 }
72 return obj;
73 }
74
Simon Huntc7ae7952015-04-08 18:59:27 -070075 function setPrefs(name, obj) {
Thomas Vachuska0af26912016-03-21 21:37:30 -070076 // keep a cached copy of the object and send an update to server
Simon Huntc7ae7952015-04-08 18:59:27 -070077 cache[name] = obj;
Thomas Vachuska0af26912016-03-21 21:37:30 -070078 wss.sendEvent('updatePrefReq', { key: name, value: obj });
79 }
Simon Hunt95f4b422017-03-03 13:49:05 -080080
81 // merge preferences:
82 // The assumption here is that obj is a sparse object, and that the
83 // defined keys should overwrite the corresponding values, but any
84 // existing keys that are NOT explicitly defined here should be left
85 // alone (not deleted).
86 function mergePrefs(name, obj) {
87 var merged = cache[name] || {};
88 setPrefs(name, angular.extend(merged, obj));
89 }
90
Thomas Vachuska0af26912016-03-21 21:37:30 -070091 function updatePrefs(data) {
Thomas Vachuska341310c2016-03-24 10:14:13 -070092 cache = data;
Simon Hunt8a119b02016-03-29 16:00:32 -070093 listeners.forEach(function (lsnr) { lsnr(); });
Thomas Vachuska341310c2016-03-24 10:14:13 -070094 }
95
96 function addListener(listener) {
97 listeners.push(listener);
98 }
99
100 function removeListener(listener) {
Steven Burrows1c2a9682017-07-14 16:52:46 +0100101 listeners = listeners.filter(function (obj) { return obj === listener; });
Simon Huntc7ae7952015-04-08 18:59:27 -0700102 }
103
104 angular.module('onosUtil')
Steven Burrows1c2a9682017-07-14 16:52:46 +0100105 .factory('PrefsService', ['FnService', 'WebSocketService',
106 function (_fs_, _wss_) {
Simon Hunt4deb0e82015-06-10 16:18:25 -0700107 fs = _fs_;
Thomas Vachuska0af26912016-03-21 21:37:30 -0700108 wss = _wss_;
109
Matteo Scandolocf060772016-05-13 15:54:44 -0700110 try {
111 cache = angular.isDefined(userPrefs) ? userPrefs : {};
112 }
Steven Burrows1c2a9682017-07-14 16:52:46 +0100113 catch (e) {
Matteo Scandolocf060772016-05-13 15:54:44 -0700114 // browser throws error for non-existing globals
Steven Burrows1c2a9682017-07-14 16:52:46 +0100115 cache = {};
Matteo Scandolocf060772016-05-13 15:54:44 -0700116 }
Thomas Vachuska0af26912016-03-21 21:37:30 -0700117
118 wss.bindHandlers({
Steven Burrows1c2a9682017-07-14 16:52:46 +0100119 updatePrefs: updatePrefs,
Thomas Vachuska0af26912016-03-21 21:37:30 -0700120 });
Simon Huntc7ae7952015-04-08 18:59:27 -0700121
122 return {
123 getPrefs: getPrefs,
Simon Huntfcbde892015-04-16 12:05:28 -0700124 asNumbers: asNumbers,
Thomas Vachuska341310c2016-03-24 10:14:13 -0700125 setPrefs: setPrefs,
Simon Hunt95f4b422017-03-03 13:49:05 -0800126 mergePrefs: mergePrefs,
Thomas Vachuska341310c2016-03-24 10:14:13 -0700127 addListener: addListener,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100128 removeListener: removeListener,
Simon Huntc7ae7952015-04-08 18:59:27 -0700129 };
130 }]);
131
132}());