Matteo Scandolo | bba1d78 | 2016-07-12 15:56:23 -0700 | [diff] [blame] | 1 | (function () { |
| 2 | angular.module('xos.dashboardManager') |
| 3 | .directive('userDashboards', function(){ |
| 4 | return { |
| 5 | restrict: 'E', |
| 6 | scope: {}, |
| 7 | bindToController: true, |
| 8 | controllerAs: 'vm', |
| 9 | templateUrl: 'templates/user-dashboards.tpl.html', |
| 10 | controller: function($q, _, UserDashboards, Dashboards){ |
| 11 | |
| 12 | // retrieving user list |
| 13 | $q.all([ |
| 14 | UserDashboards.query().$promise, |
| 15 | Dashboards.query({enabled: 'False'}).$promise, |
| 16 | ]) |
| 17 | .then((res) => { |
| 18 | let [dashboards, disabled] = res; |
| 19 | this.disabled = disabled; |
| 20 | this.list = { |
| 21 | enabled: _.filter(dashboards, {shown: true}), |
| 22 | disabled: _.filter(dashboards, {shown: false}) |
| 23 | }; |
| 24 | }) |
| 25 | .catch((e) => { |
| 26 | throw new Error(e); |
| 27 | }); |
| 28 | |
| 29 | this.removeFromList = (listName, itemId) => { |
| 30 | _.remove(this.list[listName], {id: itemId}); |
| 31 | }; |
| 32 | |
| 33 | this.addToList = (listName, item) => { |
| 34 | this.list[listName].push(item) |
Matteo Scandolo | 89634ce | 2016-08-03 17:27:32 -0700 | [diff] [blame] | 35 | location.reload(); |
Matteo Scandolo | bba1d78 | 2016-07-12 15:56:23 -0700 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | this.isInList = (listName, item) => { |
| 39 | return _.find(this.list[listName], item); |
| 40 | }; |
| 41 | |
| 42 | this.reorderList = (list, newPos, oldPos, item) => { |
| 43 | |
| 44 | let listToOrder = _.filter(list, i => { |
| 45 | // if is the item, skip it, it is already updated |
| 46 | if(i.id === item.id){ |
| 47 | return false; |
| 48 | } |
| 49 | if(i.order < oldPos){ |
| 50 | return true; |
| 51 | } |
| 52 | if(i.order >= newPos){ |
| 53 | return true; |
| 54 | } |
| 55 | return false; |
| 56 | }); |
| 57 | |
| 58 | listToOrder = listToOrder.map(i => { |
| 59 | i.order++; |
| 60 | return i; |
| 61 | }) |
| 62 | }; |
| 63 | |
| 64 | this.addedToList = (event, index, item, external, list) => { |
| 65 | let originalPosition; |
| 66 | |
| 67 | // load the item in the list |
| 68 | let itemInList = this.isInList(list, item); |
| 69 | if(itemInList){ |
| 70 | // if is in the list update it |
| 71 | originalPosition = item.order; |
| 72 | itemInList.order = index; |
| 73 | item.order = index; |
| 74 | } |
| 75 | else { |
| 76 | // create a new one |
| 77 | item.order = this.list[list].length; |
| 78 | item.shown = !item.shown; |
| 79 | } |
| 80 | |
| 81 | const otherList = list === 'enabled' ? 'disabled' : 'enabled'; |
| 82 | |
| 83 | UserDashboards.update(item).$promise |
| 84 | .then((item) => { |
| 85 | if(!itemInList){ |
| 86 | // if it is not in the list, update both lists |
| 87 | this.addToList(list, item); |
| 88 | this.removeFromList(otherList, item.id); |
| 89 | } |
| 90 | else { |
| 91 | // reorder |
| 92 | this.reorderList(this.list[list], index, originalPosition, item); |
| 93 | } |
| 94 | }); |
| 95 | } |
| 96 | } |
| 97 | }; |
| 98 | }); |
| 99 | })(); |