blob: 0a430ae6a67ded752467d7d86cf9b30a25f06074 [file] [log] [blame]
Matteo Scandolo686547a2017-08-08 13:05:25 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandoloa5d03d52016-07-21 11:35:46 -070019(function () {
20
21 angular.module('xos.helpers')
22
23 /**
24 * @ngdoc service
25 * @name xos.helpers.XosUserPrefs
26 * @description
27 * This service is used to store the user preferences in cookies, so that they survive to page changes.
28 * The structure of the user preference is:
29 * ```
30 * {
31 * synchronizers: {
32 * notification: {
33 * 'volt': boolean,
34 * 'openstack': boolean,
35 * ...
36 * }
37 * }
38 * userData: {
39 * current_user_site_id: Number,
40 * current_user_site_user_names: Array[1],
41 * ...
42 * }
43 * }
44 * ```
45 **/
46
47 .service('XosUserPrefs', function($cookies, Me, $q){
48
49 let userPrefs = $cookies.get('xosUserPrefs') ? angular.fromJson($cookies.get('xosUserPrefs')) : {};
50
51 /**
52 * @ngdoc method
53 * @name xos.helpers.XosUserPrefs#getAll
54 * @methodOf xos.helpers.XosUserPrefs
55 * @description
56 * Return all the user preferences stored in cookies
57 * @returns {object} The user preferences
58 **/
59 this.getAll = () => {
60 userPrefs = $cookies.get('xosUserPrefs') ? angular.fromJson($cookies.get('xosUserPrefs')) : {};
61 return userPrefs;
62 };
63
64 /**
65 * @ngdoc method
66 * @name xos.helpers.XosUserPrefs#setAll
67 * @methodOf xos.helpers.XosUserPrefs
68 * @description
69 * Override all user preferences
70 * @param {object} prefs The user preferences
71 **/
72 this.setAll = (prefs) => {
73 $cookies.put('xosUserPrefs', angular.toJson(prefs));
74 };
75
76 /**
77 * @ngdoc method
78 * @name xos.helpers.XosUserPrefs#getSynchronizerNotificationStatus
79 * @methodOf xos.helpers.XosUserPrefs
80 * @description
81 * Return the synchronizer notification status, if name is not provided return the status for all synchronizers
82 * @param {string=} prefs The synchronizer name
83 * @returns {object | string} The synchronizer status
84 **/
85 this.getSynchronizerNotificationStatus = (name = false) => {
86 if(name){
87 return this.getAll().synchronizers.notification[name];
88 }
89 return this.getAll().synchronizers.notification;
90 };
91
92
93 /**
94 * @ngdoc method
95 * @name xos.helpers.XosUserPrefs#getUserDetailsCookie
96 * @methodOf xos.helpers.XosUserPrefs
97 * @description
98 * Return all the user details stored in cookies or call the service
99 * @returns {object} The user details
100 **/
101 this.getUserDetailsCookie = () => {
102 const defer = $q.defer();
103 let localPref = this.getAll();
104 if(!localPref.userData){
105 this.setUserDetailsCookie().$promise.then((data)=>{
106 defer.resolve(data);
107 });
108 }
109 else{
110 defer.resolve(localPref.userData);
111 }
112 return {$promise: defer.promise};
113 };
114
115 /**
116 * @ngdoc method
117 * @name xos.helpers.XosUserPrefs#setUserDetailsCookie
118 * @methodOf xos.helpers.XosUserPrefs
119 * @description
120 * Save the user details in the cookie
121 * @param {object} details stored in cookie
122 * @param {objects} returns the user details as a promise
123 **/
124 this.setUserDetailsCookie = (userData = null)=> {
125
126 const defer = $q.defer();
127 let cookies = this.getAll();
128 if (!userData){
129 Me.get().then((user)=> {
130 cookies.userData = user;
131 this.setAll(cookies);
132 defer.resolve(user);
133 })
134 .catch ((e) => {
135 defer.reject(e);
136 });
137 }
138 else {
139 cookies.userData = userData;
140 this.setAll(cookies);
141 defer.resolve(userData);
142 }
143 return {$promise: defer.promise};
144 }
145
146 /**
147 * @ngdoc method
148 * @name xos.helpers.XosUserPrefs#setSynchronizerNotificationStatus
149 * @methodOf xos.helpers.XosUserPrefs
150 * @description
151 * Update the notification status for a single synchronizer
152 * @param {string} name The synchronizer name
153 * @param {boolean} value The notification status (true means that it has been sent)
154 **/
155
156 this.setSynchronizerNotificationStatus = (name = false, value) => {
157 if(!name){
158 throw new Error('[XosUserPrefs] When updating a synchronizer is mandatory to provide a name.')
159 }
160
161 let cookies = this.getAll();
162
163 if(!cookies.synchronizers){
164 cookies.synchronizers = {
165 notification: {}
166 }
167 }
168 cookies.synchronizers.notification[name] = value;
169 this.setAll(cookies);
170 }
171 });
172})();