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 |
| 24 | var $log, $cookies; |
| 25 | |
| 26 | // internal state |
| 27 | var cache = {}; |
| 28 | |
| 29 | // NOTE: in Angular 1.3.5, $cookies is just a simple object, and |
| 30 | // cookie values are just strings. From the 1.3.5 docs: |
| 31 | // |
| 32 | // "Only a simple Object is exposed and by adding or removing |
| 33 | // properties to/from this object, new cookies are created/deleted |
| 34 | // at the end of current $eval. The object's properties can only |
| 35 | // be strings." |
| 36 | // |
| 37 | // We may want to upgrade the version of Angular sometime soon |
| 38 | // since later version support objects as cookie values. |
| 39 | |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame^] | 40 | // NOTE: prefs represented as simple name/value pairs |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 41 | // => a temporary restriction while we are encoding into cookies |
| 42 | /* |
| 43 | { |
| 44 | foo: 1, |
| 45 | bar: 0, |
| 46 | goo: 2 |
| 47 | } |
| 48 | |
| 49 | stored as "foo:1,bar:0,goo:2" |
| 50 | */ |
| 51 | |
| 52 | // reads cookie with given name and returns an object rep of its value |
| 53 | // or null if no such cookie is set |
| 54 | function getPrefs(name) { |
| 55 | var cook = $cookies[name], |
| 56 | bits, |
| 57 | obj = {}; |
| 58 | |
| 59 | if (cook) { |
| 60 | bits = cook.split(','); |
| 61 | bits.forEach(function (value) { |
| 62 | var x = value.split(':'); |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame^] | 63 | obj[x[0]] = x[1]; |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 64 | }); |
| 65 | |
| 66 | // update the cache |
| 67 | cache[name] = obj; |
| 68 | return obj; |
| 69 | } |
| 70 | // perhaps we have a cached copy.. |
| 71 | return cache[name]; |
| 72 | } |
| 73 | |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame^] | 74 | // converts string values to numbers for selected (or all) keys |
| 75 | function asNumbers(obj, keys) { |
| 76 | if (!obj) return null; |
| 77 | |
| 78 | if (!keys) { |
| 79 | // do them all |
| 80 | angular.forEach(obj, function (v, k) { |
| 81 | obj[k] = Number(obj[k]); |
| 82 | }); |
| 83 | } else { |
| 84 | keys.forEach(function (k) { |
| 85 | obj[k] = Number(obj[k]); |
| 86 | }); |
| 87 | } |
| 88 | return obj; |
| 89 | } |
| 90 | |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 91 | function setPrefs(name, obj) { |
| 92 | var bits = [], |
| 93 | str; |
| 94 | |
| 95 | angular.forEach(obj, function (value, key) { |
| 96 | bits.push(key + ':' + value); |
| 97 | }); |
| 98 | str = bits.join(','); |
| 99 | |
| 100 | // keep a cached copy of the object |
| 101 | cache[name] = obj; |
| 102 | |
| 103 | // The angular way of doing this... |
| 104 | // $cookies[name] = str; |
| 105 | // ...but it appears that this gets delayed, and doesn't 'stick' ?? |
| 106 | |
| 107 | // FORCE cookie to be set by writing directly to document.cookie... |
| 108 | document.cookie = name + '=' + encodeURIComponent(str); |
| 109 | $log.debug('<<>> Wrote cookie <'+name+'>:', str); |
| 110 | } |
| 111 | |
| 112 | angular.module('onosUtil') |
| 113 | .factory('PrefsService', ['$log', '$cookies', |
| 114 | function (_$log_, _$cookies_) { |
| 115 | $log = _$log_; |
| 116 | $cookies = _$cookies_; |
| 117 | |
| 118 | return { |
| 119 | getPrefs: getPrefs, |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame^] | 120 | asNumbers: asNumbers, |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 121 | setPrefs: setPrefs |
| 122 | }; |
| 123 | }]); |
| 124 | |
| 125 | }()); |