Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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 |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 24 | var $log, fs, wss; |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 25 | |
| 26 | // internal state |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 27 | var cache = {}, |
| 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 |
| 46 | function asNumbers(obj, keys) { |
| 47 | if (!obj) return null; |
| 48 | |
| 49 | if (!keys) { |
| 50 | // do them all |
| 51 | angular.forEach(obj, function (v, k) { |
| 52 | obj[k] = Number(obj[k]); |
| 53 | }); |
| 54 | } else { |
| 55 | keys.forEach(function (k) { |
| 56 | obj[k] = Number(obj[k]); |
| 57 | }); |
| 58 | } |
| 59 | return obj; |
| 60 | } |
| 61 | |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 62 | function setPrefs(name, obj) { |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 63 | // 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] | 64 | cache[name] = obj; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 65 | wss.sendEvent('updatePrefReq', { key: name, value: obj }); |
| 66 | } |
| 67 | |
| 68 | function updatePrefs(data) { |
| 69 | $log.info('User properties updated'); |
Thomas Vachuska | 341310c | 2016-03-24 10:14:13 -0700 | [diff] [blame] | 70 | cache = data; |
Simon Hunt | 8a119b0 | 2016-03-29 16:00:32 -0700 | [diff] [blame] | 71 | listeners.forEach(function (lsnr) { lsnr(); }); |
Thomas Vachuska | 341310c | 2016-03-24 10:14:13 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | function addListener(listener) { |
| 75 | listeners.push(listener); |
| 76 | } |
| 77 | |
| 78 | function removeListener(listener) { |
| 79 | listeners = listeners.filter(function(obj) { return obj === listener; }); |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | angular.module('onosUtil') |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 83 | .factory('PrefsService', ['$log', 'FnService', 'WebSocketService', |
| 84 | function (_$log_, _fs_, _wss_) { |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 85 | $log = _$log_; |
Simon Hunt | 4deb0e8 | 2015-06-10 16:18:25 -0700 | [diff] [blame] | 86 | fs = _fs_; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 87 | wss = _wss_; |
| 88 | |
Matteo Scandolo | cf06077 | 2016-05-13 15:54:44 -0700 | [diff] [blame] | 89 | try { |
| 90 | cache = angular.isDefined(userPrefs) ? userPrefs : {}; |
| 91 | } |
| 92 | catch(e){ |
| 93 | // browser throws error for non-existing globals |
| 94 | cache = {} |
| 95 | } |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 96 | |
| 97 | wss.bindHandlers({ |
| 98 | updatePrefs: updatePrefs |
| 99 | }); |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 100 | |
| 101 | return { |
| 102 | getPrefs: getPrefs, |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 103 | asNumbers: asNumbers, |
Thomas Vachuska | 341310c | 2016-03-24 10:14:13 -0700 | [diff] [blame] | 104 | setPrefs: setPrefs, |
| 105 | addListener: addListener, |
| 106 | removeListener: removeListener |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 107 | }; |
| 108 | }]); |
| 109 | |
| 110 | }()); |