blob: b5b00afacf7bce0cb5a185ecbc3e6496b5cbde44 [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandolobba1d782016-07-12 15:56:23 -070019(function () {
20 angular.module('xos.dashboardManager')
21 .directive('userDashboards', function(){
22 return {
23 restrict: 'E',
24 scope: {},
25 bindToController: true,
26 controllerAs: 'vm',
27 templateUrl: 'templates/user-dashboards.tpl.html',
28 controller: function($q, _, UserDashboards, Dashboards){
29
30 // retrieving user list
31 $q.all([
32 UserDashboards.query().$promise,
33 Dashboards.query({enabled: 'False'}).$promise,
34 ])
35 .then((res) => {
36 let [dashboards, disabled] = res;
37 this.disabled = disabled;
38 this.list = {
39 enabled: _.filter(dashboards, {shown: true}),
40 disabled: _.filter(dashboards, {shown: false})
41 };
42 })
43 .catch((e) => {
44 throw new Error(e);
45 });
46
47 this.removeFromList = (listName, itemId) => {
48 _.remove(this.list[listName], {id: itemId});
49 };
50
51 this.addToList = (listName, item) => {
52 this.list[listName].push(item)
Matteo Scandolo89634ce2016-08-03 17:27:32 -070053 location.reload();
Matteo Scandolobba1d782016-07-12 15:56:23 -070054 };
55
56 this.isInList = (listName, item) => {
57 return _.find(this.list[listName], item);
58 };
59
60 this.reorderList = (list, newPos, oldPos, item) => {
61
62 let listToOrder = _.filter(list, i => {
63 // if is the item, skip it, it is already updated
64 if(i.id === item.id){
65 return false;
66 }
67 if(i.order < oldPos){
68 return true;
69 }
70 if(i.order >= newPos){
71 return true;
72 }
73 return false;
74 });
75
76 listToOrder = listToOrder.map(i => {
77 i.order++;
78 return i;
79 })
80 };
81
82 this.addedToList = (event, index, item, external, list) => {
83 let originalPosition;
84
85 // load the item in the list
86 let itemInList = this.isInList(list, item);
87 if(itemInList){
88 // if is in the list update it
89 originalPosition = item.order;
90 itemInList.order = index;
91 item.order = index;
92 }
93 else {
94 // create a new one
95 item.order = this.list[list].length;
96 item.shown = !item.shown;
97 }
98
99 const otherList = list === 'enabled' ? 'disabled' : 'enabled';
100
101 UserDashboards.update(item).$promise
102 .then((item) => {
103 if(!itemInList){
104 // if it is not in the list, update both lists
105 this.addToList(list, item);
106 this.removeFromList(otherList, item.id);
107 }
108 else {
109 // reorder
110 this.reorderList(this.list[list], index, originalPosition, item);
111 }
112 });
113 }
114 }
115 };
116 });
117})();