blob: 5e59c7cae97529fdaa9ca24b6bf9b026e19c9ba3 [file] [log] [blame]
Arpit Agarwal711b1ec2016-06-27 13:28:54 -07001(function () {
2
3 angular.module('xos.helpers')
4
5 /**
6 * @ngdoc service
7 * @name xos.helpers.XosUserPrefs
8 * @description
9 * This service is used to store the user preferences in cookies, so that they survive to page changes.
10 * The structure of the user preference is:
11 * ```
12 * {
13 * synchronizers: {
14 * notification: {
15 * 'volt': boolean,
16 * 'openstack': boolean,
17 * ...
18 * }
19 * }
20 * userData: {
21 * current_user_site_id: Number,
22 * current_user_site_user_names: Array[1],
23 * ...
24 * }
25 * }
26 * ```
27 **/
28
29 .service('XosUserPrefs', function($cookies, Me, $q){
30
31 let userPrefs = $cookies.get('xosUserPrefs') ? angular.fromJson($cookies.get('xosUserPrefs')) : {};
32
33 /**
34 * @ngdoc method
35 * @name xos.helpers.XosUserPrefs#getAll
36 * @methodOf xos.helpers.XosUserPrefs
37 * @description
38 * Return all the user preferences stored in cookies
39 * @returns {object} The user preferences
40 **/
41 this.getAll = () => {
42 userPrefs = $cookies.get('xosUserPrefs') ? angular.fromJson($cookies.get('xosUserPrefs')) : {};
43 return userPrefs;
44 };
45
46 /**
47 * @ngdoc method
48 * @name xos.helpers.XosUserPrefs#setAll
49 * @methodOf xos.helpers.XosUserPrefs
50 * @description
51 * Override all user preferences
52 * @param {object} prefs The user preferences
53 **/
54 this.setAll = (prefs) => {
55 $cookies.put('xosUserPrefs', angular.toJson(prefs));
56 };
57
58 /**
59 * @ngdoc method
60 * @name xos.helpers.XosUserPrefs#getSynchronizerNotificationStatus
61 * @methodOf xos.helpers.XosUserPrefs
62 * @description
63 * Return the synchronizer notification status, if name is not provided return the status for all synchronizers
64 * @param {string=} prefs The synchronizer name
65 * @returns {object | string} The synchronizer status
66 **/
67 this.getSynchronizerNotificationStatus = (name = false) => {
68 if(name){
69 return this.getAll().synchronizers.notification[name];
70 }
71 return this.getAll().synchronizers.notification;
72 };
73
74
75 /**
76 * @ngdoc method
77 * @name xos.helpers.XosUserPrefs#getUserDetailsCookie
78 * @methodOf xos.helpers.XosUserPrefs
79 * @description
80 * Return all the user details stored in cookies or call the service
81 * @returns {object} The user details
82 **/
83 this.getUserDetailsCookie = () => {
84 var defer = $q.defer();
85 let localPref = this.getAll();
86 if(!localPref.userData){
Arpit Agarwalc7fee982016-07-07 16:55:45 -070087 this.setUserDetailsCookie().$promise.then((data)=>{
Arpit Agarwal711b1ec2016-06-27 13:28:54 -070088 defer.resolve(data);
89 });
90 }
91 else{
92 defer.resolve(localPref.userData);
93 }
94 return {$promise: defer.promise};
95 };
96
97 /**
98 * @ngdoc method
Arpit Agarwal711b1ec2016-06-27 13:28:54 -070099 * @name xos.helpers.XosUserPrefs#setUserDetailsCookie
100 * @methodOf xos.helpers.XosUserPrefs
101 * @description
102 * Save the user details in the cookie
103 * @param {object} details stored in cookie
104 * @param {objects} returns the user details as a promise
105 **/
Arpit Agarwalc7fee982016-07-07 16:55:45 -0700106 this.setUserDetailsCookie = (userData = null)=> {
Arpit Agarwal711b1ec2016-06-27 13:28:54 -0700107
108 var defer = $q.defer();
Arpit Agarwalc7fee982016-07-07 16:55:45 -0700109 let cookies = this.getAll();
110 if (!userData){
111 Me.get().then((user)=> {
112 cookies.userData = user;
113 this.setAll(cookies);
114 defer.resolve(user);
115 })
116 .catch ((e) => {
117 defer.reject(e);
118 });
119 }
120 else {
121 cookies.userData = userData;
122 this.setAll(cookies);
123 defer.resolve(userData);
124 }
Arpit Agarwal711b1ec2016-06-27 13:28:54 -0700125 return {$promise: defer.promise};
126 }
127
128 /**
129 * @ngdoc method
130 * @name xos.helpers.XosUserPrefs#setSynchronizerNotificationStatus
131 * @methodOf xos.helpers.XosUserPrefs
132 * @description
133 * Update the notification status for a single synchronizer
134 * @param {string} name The synchronizer name
135 * @param {boolean} value The notification status (true means that it has been sent)
136 **/
137
138 this.setSynchronizerNotificationStatus = (name = false, value) => {
139 if(!name){
140 throw new Error('[XosUserPrefs] When updating a synchronizer is mandatory to provide a name.')
141 }
142
143 let cookies = this.getAll();
144
145 if(!cookies.synchronizers){
146 cookies.synchronizers = {
147 notification: {}
148 }
149 }
150 cookies.synchronizers.notification[name] = value;
151 this.setAll(cookies);
152 }
153 });
Matteo Scandoloe0afc4e2016-06-01 10:58:25 -0700154})();