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