YangUI - Fixed "Export parameters" button`s action
[dlux.git] / modules / yangui-resources / src / main / resources / yangui / controllers / history / request-history.controller.js
1 define(['app/yangui/services/handle-file.services'], function () {
2     'use strict';
3
4     angular.module('app.yangui').controller('RequestHistoryCtrl', RequestHistoryCtrl);
5
6     RequestHistoryCtrl.$inject = ['$filter', '$scope', '$rootScope', 'PathUtilsService', 'HistoryService',
7         'HandleFileService', 'YangUtilsService', 'constants',
8         'MountPointsConnectorService', 'ParsingJsonService',
9     ];
10
11     // todo: comment the whole controller
12     function RequestHistoryCtrl($filter, $scope, $rootScope, PathUtilsService, HistoryService, HandleFileService,
13                                 YangUtilsService, constants, MountPointsConnectorService, ParsingJsonService) {
14
15         $scope.collectionBoxView = false;
16         $scope.collectionList = HistoryService.createEmptyCollectionList('collectionList', $scope.getApiCallback);
17         $scope.groupView = {};
18         $scope.parameterList = HistoryService.createEmptyParamList('parameterList');
19         $scope.parameterList.loadListFromStorage();
20         $scope.popupHistory = { show: false };
21         $scope.requestList = HistoryService.createEmptyHistoryList('requestList', $scope.getApiCallback);
22
23         $scope.addRequestToList = addRequestToList;
24         $scope.clearHistoryData = clearHistoryData;
25         $scope.checkParamIsNotInList = checkParamIsNotInList;
26         $scope.deleteRequestItem = deleteRequestItem;
27         $scope.executeCollectionRequest = executeCollectionRequest;
28         $scope.exportHistoryData = exportHistoryData;
29         $scope.getApiCallback = getApiCallback;
30         $scope.hideCollBox = hideCollBox;
31         $scope.readCollectionFromFile = readCollectionFromFile;
32         $scope.reqHistoryFunc = reqHistoryFunc;
33         $scope.saveElemToList = saveElemToList;
34         $scope.saveParamToList = saveParamToList;
35         $scope.setGroupView = setGroupView;
36         $scope.showCollBox = showCollBox;
37         $scope.toggleExpanded = toggleExpanded;
38
39         $scope.$on('GET_PARAMETER_LIST', getParametersList);
40         $scope.$on('LOAD_REQ_DATA', loadReqData);
41         $scope.$on('REFRESH_HISTORY_REQUEST_APIS', refreshHistoryApis);
42         $scope.$on('YUI_ADD_TO_HISTORY', addToHistory);
43
44         var mountPrefix = constants.MPPREFIX;
45
46
47         function getApiCallback(pathString) {
48             var snp = PathUtilsService.getStorageAndNormalizedPath(pathString),
49                 // if the path is for mountpoint then get the path to treedata structure
50                 mpSearchPath = MountPointsConnectorService.alterMpPath(pathString),
51                 apiIndexes = PathUtilsService.searchNodeByPath(mpSearchPath, $scope.treeApis, $scope.treeRows),
52                 selApi = apiIndexes ? $scope.apis[apiIndexes.indexApi] : null,
53                 selSubApi = selApi ? selApi.subApis[apiIndexes.indexSubApi] : null,
54                 copiedApi = selSubApi ?
55                     selSubApi.clone({ storage: snp.storage, withoutNode: true, clonePathArray: true }) :
56                     null;
57
58             if (copiedApi) {
59                 copiedApi.pathArray.forEach(function (p) {
60                     p.hover = false;
61                 });
62
63                 PathUtilsService.fillPath(copiedApi.pathArray, snp.normalizedPath);
64             }
65
66             var searchedModule = PathUtilsService.getModuleNameFromPath(pathString);
67
68             if (mpSearchPath.indexOf(mountPrefix) !== -1 && copiedApi){
69                 copiedApi = $scope.selSubApi &&
70                     searchedModule === $scope.selSubApi.pathArray[1].module ?
71                         copiedApi :
72                         null;
73             }
74
75             return copiedApi;
76         }
77
78         function refreshHistoryApis(event){
79             $scope.requestList.refresh();
80             $scope.collectionList.refresh();
81         }
82
83         function getParametersList(event, callback){
84             callback($scope.parameterList);
85         }
86
87         function addToHistory(event, status, data, requestData, operation, requestPath) {
88             $scope.addRequestToList(status, data, requestData, operation, requestPath);
89         }
90
91         function addRequestToList(status, receivedData, sentData, operation, path) {
92             if (typeof (Storage) !== 'undefined') {
93
94                 var rList = HistoryService.createEmptyHistoryList(),
95                     reqObj = HistoryService.createHistoryRequest(sentData, receivedData, path, null, operation,
96                         status, null, null, $scope.getApiCallback);
97
98                 reqObj.refresh($scope.getApiCallback);
99
100                 $scope.requestList.addRequestToList(reqObj);
101                 $scope.requestList.saveToStorage();
102             }
103         }
104
105         function reqHistoryFunc(){
106             $scope.popupHistory.show = !$scope.popupHistory.show;
107
108             $scope.requestList.loadListFromStorage();
109             $scope.collectionList.loadListFromStorage();
110             $scope.requestList.show = false;
111         }
112
113         function showCollBox(req, edit){
114             $scope.collectionBoxView = true;
115             $scope.$broadcast('COLL_CLEAR_VAL_SET_REQ', req, edit);
116         }
117
118         function hideCollBox(){
119             $scope.collectionBoxView = false;
120         }
121
122         function saveElemToList(elem) {
123             $scope.collectionList.addRequestToList(elem);
124             $scope.collectionList.saveToStorage();
125         }
126
127         function saveParamToList(elem, oldElem) {
128             $scope.parameterList.saveRequestToList(elem, oldElem);
129         }
130
131         function deleteRequestItem(elem, list){
132             $scope[list].deleteRequestItem(elem);
133             $scope[list].saveToStorage();
134         }
135
136         function clearHistoryData(list){
137             $scope[list].clear();
138             $scope[list].saveToStorage();
139         }
140
141         function clearFileInputValue() {
142             var el = document.getElementById('upload-collection');
143             el.value = '';
144         }
145
146         function exportHistoryData() {
147             var cListJSON = localStorage.getItem('collectionList');
148
149             HandleFileService.downloadFile('requestCollection.json', cListJSON, 'json', 'charset=utf-8', function () {
150                 $scope.setStatusMessage('success', 'EXPORT_COLLECTIONS_SUCCESS');
151             }, function (e){
152                     if(e == -1) {
153                         $scope.setStatusMessage('danger', 'EXPORT_COLLECTIONS_ERROR_BROWSER');
154                     }
155                     else {
156                         $scope.setStatusMessage('danger', 'EXPORT_COLLECTIONS_ERROR', e);
157                         // console.error('ExportCollection error:', e);
158                     }
159             });
160         }
161
162         function readCollectionFromFile($fileContent) {
163             var data = $fileContent,
164                 checkArray = [
165                     'sentData',
166                     'receivedData',
167                     'path',
168                     'group',
169                     'parametrizedPath',
170                     'method',
171                     'status',
172                     'name',
173                 ];
174
175             if (data && HistoryService.validateFile(data, checkArray)){
176                 try {
177                     $scope.collectionList.loadListFromFile(data);
178                     $scope.collectionList.saveToStorage();
179                     $scope.setStatusMessage('success', 'LOAD_COLLECTIONS_SUCCESS');
180                     clearFileInputValue();
181                 }
182                 catch (e) {
183                     clearFileInputValue();
184                     $scope.setStatusMessage('danger', 'PARSE_JSON_FILE_ERROR', e);
185                     // console.error('DataStorage error:', e);
186                 }
187             }
188             else {
189                 $scope.setStatusMessage('danger', 'PARSE_JSON_FILE_ERROR');
190                 clearFileInputValue();
191             }
192         }
193
194         function checkParamIsNotInList(req) {
195             var regexp = /<<([^>]+)>>/g;
196             var sParam = angular.toJson(req).match(regexp);
197
198             return sParam && sParam.some(function(parameter) {
199                 parameter = parameter.replace(/[<>]/g, '');
200
201                 return $filter('filter')($scope.parameterList.list, {'name': parameter}).length < 1;
202             });
203         }
204
205         function executeCollectionRequest(req, dataForView, showData) {
206             var sdata = dataForView ? ParsingJsonService.parseJson(dataForView) : req.sentData,
207                     path = req.parametrizedPath && showData ? req.parametrizedPath : req.api.buildApiRequestString(),
208                     paramResult = checkParamIsNotInList(sdata);
209
210             path = $scope.parameterizeData(path);
211             $scope.fillStandardApi(path, req.path);
212
213             if(sdata && !paramResult) {
214                 $scope.fillApiData(sdata);
215             }
216
217             var requestPath = req.api.parent.basePath + path;
218
219             if(!paramResult) {
220                 $scope.executeOperation(req.method, function(data){
221                     if ( !data &&  req.receivedData ){
222                         $scope.node.fill($scope.node.label,req.receivedData[$scope.node.label]);
223                     }
224                 }, requestPath);
225             }
226             else {
227                 $scope.setStatusMessage('danger', 'YANGUI_PARAMETER_MISSING_ERROR', e.message);
228             }
229         }
230
231         function setGroupView(key) {
232             $scope.groupView[key] = false;
233         }
234
235         function toggleExpanded(key) {
236             $scope.groupView[key] = !$scope.groupView[key];
237         }
238
239         function loadReqData(){
240             $scope.reqHistoryFunc();
241         }
242
243     }
244
245 });