Matteo Scandolo | f0958b1 | 2016-06-01 10:58:25 -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 | * } |
| 21 | * ``` |
| 22 | **/ |
| 23 | |
| 24 | .service('XosUserPrefs', function($cookies){ |
| 25 | |
| 26 | let userPrefs = $cookies.get('xosUserPrefs') ? JSON.parse($cookies.get('xosUserPrefs')) : {}; |
| 27 | |
| 28 | /** |
| 29 | * @ngdoc method |
| 30 | * @name xos.helpers.XosUserPrefs#getAll |
| 31 | * @methodOf xos.helpers.XosUserPrefs |
| 32 | * @description |
| 33 | * Return all the user preferences stored in cookies |
| 34 | * @returns {object} The user preferences |
| 35 | **/ |
| 36 | this.getAll = () => { |
| 37 | userPrefs = $cookies.get('xosUserPrefs') ? JSON.parse($cookies.get('xosUserPrefs')) : {}; |
| 38 | return userPrefs; |
| 39 | }; |
| 40 | |
| 41 | /** |
| 42 | * @ngdoc method |
| 43 | * @name xos.helpers.XosUserPrefs#setAll |
| 44 | * @methodOf xos.helpers.XosUserPrefs |
| 45 | * @description |
| 46 | * Override all user preferences |
| 47 | * @param {object} prefs The user preferences |
| 48 | **/ |
| 49 | this.setAll = (prefs) => { |
| 50 | $cookies.put('xosUserPrefs', JSON.stringify(prefs)); |
| 51 | }; |
| 52 | |
| 53 | /** |
| 54 | * @ngdoc method |
| 55 | * @name xos.helpers.XosUserPrefs#getSynchronizerNotificationStatus |
| 56 | * @methodOf xos.helpers.XosUserPrefs |
| 57 | * @description |
| 58 | * Return the synchronizer notification status, if name is not provided return the status for all synchronizers |
| 59 | * @param {string=} prefs The synchronizer name |
| 60 | * @returns {object | string} The synchronizer status |
| 61 | **/ |
| 62 | this.getSynchronizerNotificationStatus = (name = false) => { |
| 63 | if(name){ |
| 64 | return this.getAll().synchronizers.notification[name]; |
| 65 | } |
| 66 | return this.getAll().synchronizers.notification; |
| 67 | }; |
| 68 | |
| 69 | /** |
| 70 | * @ngdoc method |
| 71 | * @name xos.helpers.XosUserPrefs#setSynchronizerNotificationStatus |
| 72 | * @methodOf xos.helpers.XosUserPrefs |
| 73 | * @description |
| 74 | * Update the notification status for a single synchronizer |
| 75 | * @param {string} name The synchronizer name |
| 76 | * @param {boolean} value The notification status (true means that it has been sent) |
| 77 | **/ |
| 78 | this.setSynchronizerNotificationStatus = (name = false, value) => { |
| 79 | if(!name){ |
| 80 | throw new Error('[XosUserPrefs] When updating a synchronizer is mandatory to provide a name.') |
| 81 | } |
| 82 | |
| 83 | let cookies = this.getAll(); |
| 84 | |
| 85 | if(!cookies.synchronizers){ |
| 86 | cookies.synchronizers = { |
| 87 | notification: {} |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | cookies.synchronizers.notification[name] = value; |
| 92 | this.setAll(cookies); |
| 93 | } |
| 94 | }); |
| 95 | })(); |