blob: 02a23909f4c12080265f59399cb4d37a3acdde63 [file] [log] [blame]
Simon Huntc7ae7952015-04-08 18:59:27 -07001/*
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
Simon Hunt4deb0e82015-06-10 16:18:25 -070024 var $log, $cookies, fs;
Simon Huntc7ae7952015-04-08 18:59:27 -070025
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 Huntfcbde892015-04-16 12:05:28 -070040 // NOTE: prefs represented as simple name/value pairs
Simon Huntc7ae7952015-04-08 18:59:27 -070041 // => 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 Huntfcbde892015-04-16 12:05:28 -070063 obj[x[0]] = x[1];
Simon Huntc7ae7952015-04-08 18:59:27 -070064 });
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 Huntfcbde892015-04-16 12:05:28 -070074 // 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 Huntc7ae7952015-04-08 18:59:27 -070091 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);
Simon Hunt4deb0e82015-06-10 16:18:25 -0700109 if (fs.debugOn('prefs')) {
110 $log.debug('<<>> Wrote cookie <'+name+'>:', str);
111 }
Simon Huntc7ae7952015-04-08 18:59:27 -0700112 }
113
114 angular.module('onosUtil')
Simon Hunt4deb0e82015-06-10 16:18:25 -0700115 .factory('PrefsService', ['$log', '$cookies', 'FnService',
116 function (_$log_, _$cookies_, _fs_) {
Simon Huntc7ae7952015-04-08 18:59:27 -0700117 $log = _$log_;
118 $cookies = _$cookies_;
Simon Hunt4deb0e82015-06-10 16:18:25 -0700119 fs = _fs_;
Simon Huntc7ae7952015-04-08 18:59:27 -0700120
121 return {
122 getPrefs: getPrefs,
Simon Huntfcbde892015-04-16 12:05:28 -0700123 asNumbers: asNumbers,
Simon Huntc7ae7952015-04-08 18:59:27 -0700124 setPrefs: setPrefs
125 };
126 }]);
127
128}());