Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [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 -- User Preference Service |
| 19 | */ |
| 20 | (function () { |
| 21 | 'use strict'; |
| 22 | |
| 23 | // injected refs |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 24 | var fs, wss; |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 25 | |
| 26 | // internal state |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 27 | var cache = {}, |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 28 | listeners = []; |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 29 | |
Simon Hunt | 8a119b0 | 2016-03-29 16:00:32 -0700 | [diff] [blame] | 30 | // 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 Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 45 | // converts string values to numbers for selected (or all) keys |
Simon Hunt | fc5c584 | 2017-02-01 23:32:18 -0800 | [diff] [blame] | 46 | // 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 Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 50 | if (!obj) return null; |
| 51 | |
Simon Hunt | fc5c584 | 2017-02-01 23:32:18 -0800 | [diff] [blame] | 52 | var skip = {}; |
| 53 | if (not) { |
| 54 | keys.forEach(function (k) { |
| 55 | skip[k] = 1; |
| 56 | }); |
| 57 | } |
| 58 | |
| 59 | if (!keys || not) { |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 60 | // do them all |
| 61 | angular.forEach(obj, function (v, k) { |
Simon Hunt | fc5c584 | 2017-02-01 23:32:18 -0800 | [diff] [blame] | 62 | if (!not || !skip[k]) { |
| 63 | obj[k] = Number(obj[k]); |
| 64 | } |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 65 | }); |
| 66 | } else { |
Simon Hunt | fc5c584 | 2017-02-01 23:32:18 -0800 | [diff] [blame] | 67 | // do the explicitly named keys |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 68 | keys.forEach(function (k) { |
| 69 | obj[k] = Number(obj[k]); |
| 70 | }); |
| 71 | } |
| 72 | return obj; |
| 73 | } |
| 74 | |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 75 | function setPrefs(name, obj) { |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 76 | // keep a cached copy of the object and send an update to server |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 77 | cache[name] = obj; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 78 | wss.sendEvent('updatePrefReq', { key: name, value: obj }); |
| 79 | } |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 80 | |
| 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 Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 91 | function updatePrefs(data) { |
Thomas Vachuska | 341310c | 2016-03-24 10:14:13 -0700 | [diff] [blame] | 92 | cache = data; |
Simon Hunt | 8a119b0 | 2016-03-29 16:00:32 -0700 | [diff] [blame] | 93 | listeners.forEach(function (lsnr) { lsnr(); }); |
Thomas Vachuska | 341310c | 2016-03-24 10:14:13 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | function addListener(listener) { |
| 97 | listeners.push(listener); |
| 98 | } |
| 99 | |
| 100 | function removeListener(listener) { |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 101 | listeners = listeners.filter(function (obj) { return obj === listener; }); |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | angular.module('onosUtil') |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 105 | .factory('PrefsService', ['FnService', 'WebSocketService', |
| 106 | function (_fs_, _wss_) { |
Simon Hunt | 4deb0e8 | 2015-06-10 16:18:25 -0700 | [diff] [blame] | 107 | fs = _fs_; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 108 | wss = _wss_; |
| 109 | |
Matteo Scandolo | cf06077 | 2016-05-13 15:54:44 -0700 | [diff] [blame] | 110 | try { |
| 111 | cache = angular.isDefined(userPrefs) ? userPrefs : {}; |
| 112 | } |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 113 | catch (e) { |
Matteo Scandolo | cf06077 | 2016-05-13 15:54:44 -0700 | [diff] [blame] | 114 | // browser throws error for non-existing globals |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 115 | cache = {}; |
Matteo Scandolo | cf06077 | 2016-05-13 15:54:44 -0700 | [diff] [blame] | 116 | } |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 117 | |
| 118 | wss.bindHandlers({ |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 119 | updatePrefs: updatePrefs, |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 120 | }); |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 121 | |
| 122 | return { |
| 123 | getPrefs: getPrefs, |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 124 | asNumbers: asNumbers, |
Thomas Vachuska | 341310c | 2016-03-24 10:14:13 -0700 | [diff] [blame] | 125 | setPrefs: setPrefs, |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 126 | mergePrefs: mergePrefs, |
Thomas Vachuska | 341310c | 2016-03-24 10:14:13 -0700 | [diff] [blame] | 127 | addListener: addListener, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 128 | removeListener: removeListener, |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 129 | }; |
| 130 | }]); |
| 131 | |
| 132 | }()); |