Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 |
Thomas Vachuska | 341310c | 2016-03-24 10:14:13 -0700 | [diff] [blame^] | 27 | var cache = {}, listeners = []; |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 28 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 29 | // returns the preference by the specified name |
| 30 | function getPrefs(name, defaults) { |
| 31 | return cache[name] || defaults; |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 34 | // converts string values to numbers for selected (or all) keys |
| 35 | function asNumbers(obj, keys) { |
| 36 | if (!obj) return null; |
| 37 | |
| 38 | if (!keys) { |
| 39 | // do them all |
| 40 | angular.forEach(obj, function (v, k) { |
| 41 | obj[k] = Number(obj[k]); |
| 42 | }); |
| 43 | } else { |
| 44 | keys.forEach(function (k) { |
| 45 | obj[k] = Number(obj[k]); |
| 46 | }); |
| 47 | } |
| 48 | return obj; |
| 49 | } |
| 50 | |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 51 | function setPrefs(name, obj) { |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 52 | // 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] | 53 | cache[name] = obj; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 54 | wss.sendEvent('updatePrefReq', { key: name, value: obj }); |
| 55 | } |
| 56 | |
| 57 | function updatePrefs(data) { |
| 58 | $log.info('User properties updated'); |
Thomas Vachuska | 341310c | 2016-03-24 10:14:13 -0700 | [diff] [blame^] | 59 | cache = data; |
| 60 | listeners.forEach(function (l) { l(); }); |
| 61 | } |
| 62 | |
| 63 | function addListener(listener) { |
| 64 | listeners.push(listener); |
| 65 | } |
| 66 | |
| 67 | function removeListener(listener) { |
| 68 | listeners = listeners.filter(function(obj) { return obj === listener; }); |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | angular.module('onosUtil') |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 72 | .factory('PrefsService', ['$log', 'FnService', 'WebSocketService', |
| 73 | function (_$log_, _fs_, _wss_) { |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 74 | $log = _$log_; |
Simon Hunt | 4deb0e8 | 2015-06-10 16:18:25 -0700 | [diff] [blame] | 75 | fs = _fs_; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 76 | wss = _wss_; |
| 77 | |
| 78 | cache = userPrefs; |
| 79 | |
| 80 | wss.bindHandlers({ |
| 81 | updatePrefs: updatePrefs |
| 82 | }); |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 83 | |
| 84 | return { |
| 85 | getPrefs: getPrefs, |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 86 | asNumbers: asNumbers, |
Thomas Vachuska | 341310c | 2016-03-24 10:14:13 -0700 | [diff] [blame^] | 87 | setPrefs: setPrefs, |
| 88 | addListener: addListener, |
| 89 | removeListener: removeListener |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 90 | }; |
| 91 | }]); |
| 92 | |
| 93 | }()); |