YangUI - quickfix operational list form
[dlux.git] / modules / yangui-resources / src / main / resources / yangui / controllers / api / list.controller.js
1 define([], function () {
2     'use strict';
3     angular.module('app.yangui').controller('ListCtrl', ListCtrl);
4
5     ListCtrl.$inject = ['$scope', 'ListFilteringService', 'NodeWrapperService'];
6
7     // todo: comment the whole controller
8     function ListCtrl($scope, ListFilteringService, NodeWrapperService) {
9
10         $scope.actElement = null;
11         $scope.augModalView = false;
12         $scope.currentDisplayIndex = 1;
13         $scope.displayOffsets = [-1, 0, 1];
14         $scope.filterListHover = 0;
15         $scope.showListFilter = false;
16         $scope.showModal = false;
17
18         $scope.activeFilter = activeFilter;
19         $scope.addListElem = addListElem;
20         $scope.applyFilter = applyFilter;
21         $scope.clearFilterData = clearFilterData;
22         $scope.createNewFilter = createNewFilter;
23         $scope.existsActiveFilter = existsActiveFilter;
24         $scope.getFilterData = getFilterData;
25         $scope.getListName = getListName;
26         $scope.removeListElem = removeListElem;
27         $scope.shiftDisplayNext = shiftDisplayNext;
28         $scope.shiftDisplayPrev = shiftDisplayPrev;
29         $scope.showListFilterWin = showListFilterWin;
30         $scope.showModalWin = showModalWin;
31         $scope.showNextButton = showNextButton;
32         $scope.showPrevButton = showPrevButton;
33         $scope.switchFilter = switchFilter;
34         $scope.toggleExpanded = toggleExpanded;
35         $scope.toggleExpandedAugModal = toggleExpandedAugModal;
36
37         $scope.$on('EV_REFRESH_LIST_INDEX', refreshListIndex);
38
39         $scope.$on('EV_DISABLE_ADDING_LIST_ELEMENT', function() {
40             $scope.init();
41         });
42
43         /**
44          * Disable adding more then one element
45          */
46         $scope.init = function() {
47             $scope.disableAddingListElement = $scope.checkAddingListElement($scope.node);
48
49             if($scope.disableAddingListElement) {
50                 $scope.addListElem();
51             }
52         };
53
54         function toggleExpandedAugModal(){
55             $scope.augModalView = !$scope.augModalView;
56         }
57
58         function refreshListIndex() {
59             $scope.currentDisplayIndex = 1;
60         }
61
62         function addListElem() {
63             $scope.showListFilter = false;
64             $scope.showModal = false;
65             ListFilteringService.removeEmptyFilters($scope.node);
66             $scope.node.addListElem();
67         }
68
69         function removeListElem(elemIndex, fromFilter) {
70             $scope.node.removeListElem(elemIndex, fromFilter);
71             $scope.preview();
72             $scope.currentDisplayIndex = Math.max(
73                 Math.min($scope.currentDisplayIndex, $scope.node.listData.length - 2), 1
74             );
75         }
76
77         function toggleExpanded() {
78             $scope.node.expanded = !$scope.node.expanded;
79         }
80
81         function shiftDisplayNext(typeListData) {
82             $scope.currentDisplayIndex = Math.min($scope.currentDisplayIndex + 3, $scope.node[typeListData].length - 2);
83         }
84
85         function shiftDisplayPrev() {
86             $scope.currentDisplayIndex = Math.max($scope.currentDisplayIndex - 3, 1);
87         }
88
89         function showPrevButton() {
90             return $scope.currentDisplayIndex > 1;
91         }
92
93         function showNextButton(typeListData) {
94             return $scope.node[typeListData] &&
95                 // node is selected after view is loaded
96                 $scope.currentDisplayIndex < $scope.node[typeListData].length - 2;
97         }
98
99         function showModalWin() {
100             $scope.showModal = !$scope.showModal;
101             if ($scope.showListFilter){
102                 $scope.showListFilter = !$scope.showListFilter;
103             }
104         }
105
106         function showListFilterWin() {
107             $scope.showListFilter = !$scope.showListFilter;
108             if ($scope.showModal){
109                 $scope.showModal = !$scope.showModal;
110             }
111             ListFilteringService.showListFilterWin($scope.filterRootNode, $scope.node);
112         }
113
114         function getFilterData() {
115             ListFilteringService.getFilterData($scope.node);
116         }
117
118         function switchFilter(showedFilter) {
119             ListFilteringService.switchFilter($scope.node, showedFilter);
120         }
121
122         function createNewFilter() {
123             ListFilteringService.createNewFilter($scope.node);
124         }
125
126         function existsActiveFilter() {
127             return $scope.node.filters.some(function(filter) {
128                 return filter.active == 1;
129             });
130         }
131
132         function applyFilter() {
133             ListFilteringService.applyFilter($scope.node);
134             $scope.showListFilter = !$scope.showListFilter;
135             $scope.currentDisplayIndex = 1;
136             if ($scope.node.filteredListData.length){
137                 $scope.node.doubleKeyIndexes = NodeWrapperService.checkKeyDuplicity($scope.node.filteredListData, $scope.node.refKey);
138                 $scope.setStatusMessage('success', 'YANGUI_FILTER_MATCH_SUCCESS', e.message);
139             }
140             else {
141                 $scope.node.doubleKeyIndexes = NodeWrapperService.checkKeyDuplicity(
142                     $scope.node.listData,
143                     $scope.node.refKey
144                 );
145
146                 if($scope.existsActiveFilter()) {
147                     $scope.setStatusMessage('danger', 'YANGUI_FILTER_MATCH_ERROR', e.message);
148                 }
149             }
150         }
151
152         function clearFilterData(changeAct, filterForClear, removeFilters) {
153             ListFilteringService.clearFilterData($scope.node, changeAct, filterForClear, removeFilters);
154             if (changeAct) {
155                 $scope.showListFilter = !$scope.showListFilter;
156             }
157             $scope.node.doubleKeyIndexes = NodeWrapperService.checkKeyDuplicity(
158                 $scope.node.listData,
159                 $scope.node.refKey
160             );
161         }
162
163         function activeFilter(filter) {
164             if (filter.active === 1) {
165                 filter.active = 2;
166             }
167             else {
168                 filter.active = 1;
169             }
170         }
171
172         function getListName(offset, config) {
173             var createdListItemName = $scope.node.createListName($scope.currentDisplayIndex + offset);
174
175             if ( createdListItemName.length > 33 ) {
176                 return {
177                     name: createdListItemName.substring(0, 30) + '...',
178                     tooltip: createdListItemName,
179                 };
180             } else {
181                 return {
182                     name: config ?
183                         createdListItemName || '[' + ($scope.currentDisplayIndex + offset) + ']' :
184                         createdListItemName,
185                     tooltip: '',
186                 };
187             }
188         }
189
190     }
191
192 });