Arpit Agarwal | 711b1ec | 2016-06-27 13:28:54 -0700 | [diff] [blame] | 1 | (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 Agarwal | c7fee98 | 2016-07-07 16:55:45 -0700 | [diff] [blame] | 87 | this.setUserDetailsCookie().$promise.then((data)=>{
|
Arpit Agarwal | 711b1ec | 2016-06-27 13:28:54 -0700 | [diff] [blame] | 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
|
Arpit Agarwal | 711b1ec | 2016-06-27 13:28:54 -0700 | [diff] [blame] | 99 | * @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 Agarwal | c7fee98 | 2016-07-07 16:55:45 -0700 | [diff] [blame] | 106 | this.setUserDetailsCookie = (userData = null)=> {
|
Arpit Agarwal | 711b1ec | 2016-06-27 13:28:54 -0700 | [diff] [blame] | 107 |
|
| 108 | var defer = $q.defer();
|
Arpit Agarwal | c7fee98 | 2016-07-07 16:55:45 -0700 | [diff] [blame] | 109 | 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 Agarwal | 711b1ec | 2016-06-27 13:28:54 -0700 | [diff] [blame] | 125 | 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 Scandolo | e0afc4e | 2016-06-01 10:58:25 -0700 | [diff] [blame] | 154 | })(); |