blob: bb8a810bf19425274080432685ac2c67e58df1d8 [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){
87 this.setUserDetailsCookie(localPref).$promise.then((data)=>{
88 defer.resolve(data);
89 });
90 }
91 else{
92 defer.resolve(localPref.userData);
93 }
94 return {$promise: defer.promise};
95 };
96
97 /**
98 * @ngdoc method
99 * @name xos.helpers.XosUserPrefs#setDataUser
100 * @methodOf xos.helpers.XosUserPrefs
101 * @description
102 * Return all the user details from the endpoint (api/utility/me)
103 * @returns {object} The user details
104 **/
105
106 this.setDataUser = ()=>{
107 //var deff = $q.defer();
108 return Me.get().$promise;
109
110 };
111
112 /**
113 * @ngdoc method
114 * @name xos.helpers.XosUserPrefs#setUserDetailsCookie
115 * @methodOf xos.helpers.XosUserPrefs
116 * @description
117 * Save the user details in the cookie
118 * @param {object} details stored in cookie
119 * @param {objects} returns the user details as a promise
120 **/
121 this.setUserDetailsCookie = (localPref = localPref)=> {
122
123 var defer = $q.defer();
124 this.setDataUser().then((user)=>{
125 this.model = user;
126 defer.resolve(this.model);
127 }).then(() => {
128 localPref.userData = this.model.data;
129 this.setAll(localPref);
130 })
131 .catch ((e) => {
132 defer.reject(e);
133 throw new Error(e);
134 });
135 return {$promise: defer.promise};
136 }
137
138 /**
139 * @ngdoc method
140 * @name xos.helpers.XosUserPrefs#setSynchronizerNotificationStatus
141 * @methodOf xos.helpers.XosUserPrefs
142 * @description
143 * Update the notification status for a single synchronizer
144 * @param {string} name The synchronizer name
145 * @param {boolean} value The notification status (true means that it has been sent)
146 **/
147
148 this.setSynchronizerNotificationStatus = (name = false, value) => {
149 if(!name){
150 throw new Error('[XosUserPrefs] When updating a synchronizer is mandatory to provide a name.')
151 }
152
153 let cookies = this.getAll();
154
155 if(!cookies.synchronizers){
156 cookies.synchronizers = {
157 notification: {}
158 }
159 }
160 cookies.synchronizers.notification[name] = value;
161 this.setAll(cookies);
162 }
163 });
Matteo Scandoloe0afc4e2016-06-01 10:58:25 -0700164})();