Yangman - hide show previous item icon if there are no data
[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             if (!$scope.node.listData.length) {
62                 yangList.currentDisplayIndex = 1;
63             }
64         }
65
66         /**
67          * Add element into list
68          */
69         function addListElem() {
70             $scope.showListFilter = false;
71             $scope.showModal = false;
72             ListFilteringService.removeEmptyFilters($scope.node);
73             $scope.node.addListElem();
74         }
75
76         // TODO :: do method description
77         function removeListElem(elemIndex, fromFilter) {
78             $scope.node.removeListElem(elemIndex, fromFilter);
79             // $scope.preview();
80             yangList.currentDisplayIndex =
81                 Math.max(Math.min(yangList.currentDisplayIndex, $scope.node.listData.length - 2), 1);
82             if ($scope.node.listData.length === 0) {
83                 $scope.$broadcast('hideInfoBox');
84             }
85         }
86
87         // TODO :: do method description
88         function toggleExpanded() {
89             $scope.node.expanded = !$scope.node.expanded;
90         }
91
92         // TODO :: do method description
93         function shiftDisplayNext(typeListData) {
94             yangList.currentDisplayIndex = Math.min(yangList.currentDisplayIndex + 3, $scope.node[typeListData].length - 2);
95         }
96
97         // TODO :: do method description
98         function shiftDisplayPrev() {
99             yangList.currentDisplayIndex = Math.max(yangList.currentDisplayIndex - 3, 1);
100         }
101
102         // TODO :: do method description
103         function showPrevButton() {
104             return yangList.currentDisplayIndex > 1;
105         }
106
107         // TODO :: do method description
108         function showNextButton(typeListData) {
109             // node is selected after view is loaded
110             return $scope.node[typeListData] && yangList.currentDisplayIndex < $scope.node[typeListData].length - 2;
111         }
112
113         // TODO :: do method description
114         function showModalWin() {
115             $scope.showModal = !$scope.showModal;
116             if ($scope.showListFilter){
117                 $scope.showListFilter = !$scope.showListFilter;
118             }
119         }
120
121         // TODO :: do method description
122         function showListFilterWin() {
123             $scope.showListFilter = !$scope.showListFilter;
124             if ($scope.showModal){
125                 $scope.showModal = !$scope.showModal;
126             }
127             ListFilteringService.showListFilterWin($scope.filterRootNode,$scope.node);
128         }
129
130         // TODO :: do method description
131         function getFilterData() {
132             ListFilteringService.getFilterData($scope.node);
133         }
134
135         // TODO :: do method description
136         function switchFilter(showedFilter) {
137             ListFilteringService.switchFilter($scope.node, showedFilter);
138         }
139
140         // TODO :: do method description
141         function createNewFilter() {
142             ListFilteringService.createNewFilter($scope.node);
143         }
144
145         // TODO :: do method description
146         function applyFilter() {
147             ListFilteringService.applyFilter($scope.node);
148             $scope.showListFilter = !$scope.showListFilter;
149             yangList.currentDisplayIndex = 1;
150             if ($scope.node.filteredListData.length){
151                 $scope.node.doubleKeyIndexes =
152                     NodeWrapperService.checkKeyDuplicity($scope.node.filteredListData, $scope.node.refKey);
153             } else {
154                 $scope.node.doubleKeyIndexes =
155                     NodeWrapperService.checkKeyDuplicity($scope.node.listData, $scope.node.refKey);
156             }
157         }
158
159         // TODO :: do method description
160         function clearFilterData(changeAct, filterForClear, removeFilters) {
161             ListFilteringService.clearFilterData($scope.node, changeAct, filterForClear, removeFilters);
162             if (changeAct){
163                 $scope.showListFilter = !$scope.showListFilter;
164             }
165             $scope.node.doubleKeyIndexes =
166                 NodeWrapperService.checkKeyDuplicity($scope.node.listData, $scope.node.refKey);
167         }
168
169         // TODO :: do method description
170         function activeFilter(filter) {
171             if (filter.active === 1){
172                 filter.active = 2;
173             } else {
174                 filter.active = 1;
175             }
176         }
177
178         // TODO :: do method description
179         function getListName(offset, config) {
180             var createdListItemName = $scope.node.createListName(yangList.currentDisplayIndex + offset);
181
182             if ( createdListItemName.length > 33 ) {
183                 return {
184                     name: createdListItemName.substring(0, 30) + '...',
185                     tooltip: createdListItemName,
186                 };
187             } else {
188                 return {
189                     name: config ? createdListItemName || '[' + (yangList.currentDisplayIndex + offset) + ']' : createdListItemName,
190                     tooltip: '',
191                 };
192             }
193         }
194
195         /**
196          * Show hide action menu
197          * @returns {boolean|*}
198          */
199         function isActionMenu() {
200             return true;
201         }
202     }
203 });