2 'app/yangman/controllers/save-req-dialog.controller',
3 'app/yangman/controllers/edit-collection-dialog.controller',
4 'app/yangman/services/handle-file.services',
5 ], function (SaveReqDialogCtrl, EditCollectionDialogCtrl) {
8 angular.module('app.yangman').controller('RequestsListCtrl', RequestsListCtrl);
10 RequestsListCtrl.$inject = [
11 '$filter', '$mdDialog', '$scope', 'HandleFileService', 'PathUtilsService', 'RequestsService',
14 function RequestsListCtrl($filter, $mdDialog, $scope, HandleFileService, PathUtilsService, RequestsService) {
17 vm.collectionList = RequestsService.createEmptyCollectionList('yangman_collectionsList', vm.getApiCallback);
19 vm.requestList = RequestsService.createEmptyHistoryList('yangman_requestsList', vm.getApiCallback);
21 vm.collectionsSortAsc = true;
23 vm.clearFilter = clearFilter;
24 vm.filterReq = filterReq;
25 vm.filterCol = filterCol;
26 vm.filterColName = filterColName;
27 vm.colMatchingReqsCount = colMatchingReqsCount;
28 vm.downloadCollection = downloadCollection;
29 vm.fakeFilter = fakeFilter;
30 vm.getApiCallback = getApiCallback;
31 vm.loadRequests = loadRequests;
32 vm.toggleCollectionsSort = toggleCollectionsSort;
33 vm.selectRequest = selectRequest;
35 vm.showDgDeleteCollection = showDgDeleteCollection;
36 vm.showDgDeleteRequests = showDgDeleteRequests;
37 vm.showDgEditCollection = showDgEditCollection;
38 vm.readCollectionFromFile = readCollectionFromFile;
39 vm.showDgSaveReq = showDgSaveReq;
41 vm.showData = showData;
42 vm.useAsMainList = useAsMainList;
44 $scope.$on('YANGMAN_REFRESH_HISTORY', loadHistoryRequests);
45 $scope.$on('YANGMAN_REFRESH_COLLECTIONS', loadCollectionRequest);
50 * Clear value of input file used to import collection
52 function clearFileInputValue(){
53 angular.element(document).find('#importCollection').val('');
57 * Importing collection from a file
58 * todo: error handling - msgs for user
61 function readCollectionFromFile($fileContent) {
62 var data = $fileContent,
63 checkArray = ['sentData',
73 if (data && RequestsService.validateFile(data, checkArray)){
75 vm.collectionList.loadListFromFile(data);
76 vm.collectionList.saveToStorage();
77 clearFileInputValue();
80 clearFileInputValue();
81 console.error('DataStorage error:', e);
85 clearFileInputValue();
89 function toggleCollectionsSort() {
90 vm.collectionsSortAsc = !vm.collectionsSortAsc;
94 * Export collection to json file
95 * @param {Collection} collection
97 function downloadCollection(collection) {
99 var cListJSON = vm.collectionList.getCollectionInJSON(collection.name);
101 HandleFileService.downloadFile(collection.name + '.json', cListJSON, 'json', 'charset=utf-8',
104 console.error('ExportCollection error:', e);
111 * Show current reqObj sent data in right panel section
115 function showData(reqObj, dataType) {
116 $scope.setRequestToShow(reqObj, dataType);
117 $scope.setRightPanelSection('req-data');
118 $scope.broadcastFromRoot('YANGMAN_REFRESH_CM_DATA');
122 * Clear current ctrl search value
124 function clearFilter(){
129 * Dialog for deleting either selected requests or reqObj
134 function showDgDeleteRequests(ev, reqObj){
135 var confirm = $mdDialog.confirm()
136 .title($filter('translate')('YANGMAN_DELETE_REQ_CONFIRM_TITLE'))
137 .textContent($filter('translate')('YANGMAN_DELETE_REQ_CONFIRM_TEXT'))
138 .ariaLabel($filter('translate')('YANGMAN_DELETE_REQ_CONFIRM_TITLE'))
140 .ok($filter('translate')('YANGMAN_OK'))
141 .cancel($filter('translate')('YANGMAN_CANCEL'));
143 $mdDialog.show(confirm).then(function (){
145 vm.mainList.deleteRequestItem(reqObj);
148 vm.mainList.selectedRequests.forEach(function (elem){
149 vm.mainList.deleteRequestItem(elem);
152 vm.mainList.saveToStorage();
159 * Dialog for delete collection
163 function showDgDeleteCollection(ev, collObj){
164 var confirm = $mdDialog.confirm()
165 .title($filter('translate')('YANGMAN_DELETE_COL_CONFIRM_TITLE'))
166 .textContent($filter('translate')('YANGMAN_DELETE_COL_CONFIRM_TEXT'))
167 .ariaLabel($filter('translate')('YANGMAN_DELETE_COL_CONFIRM_TITLE'))
169 .ok($filter('translate')('YANGMAN_OK'))
170 .cancel($filter('translate')('YANGMAN_CANCEL'));
172 $mdDialog.show(confirm).then(function (){
173 vm.collectionList.deleteCollection(collObj);
174 vm.collectionList.saveToStorage();
175 $scope.broadcastFromRoot('YANGMAN_REFRESH_COLLECTIONS');
180 * Check if reqObj matches current search value
184 function filterReq(reqObj){
185 return reqObj.path.indexOf(vm.search) !== -1;
189 * Check if collection name matches current search value or any collection req matches
192 function filterCol(colObj){
193 return filterColName(colObj) || colObj.data.some(filterReq);
197 * Get count of requests matching filter in collection colObj
201 function colMatchingReqsCount(colObj){
202 return colObj.data.filter(vm.filterReq).length;
206 * Check if collection name matches current filter
210 function filterColName(colObj){
211 return colObj.name.indexOf(vm.search) !== -1;
218 function fakeFilter(){
223 * Show dialog for saving reqObj to collection (used for duplicate req too)
228 function showDgSaveReq(ev, reqObj, duplicate){
231 controller: SaveReqDialogCtrl,
232 controllerAs: 'dialog',
233 templateUrl: $scope.globalViewPath + 'leftpanel/save-req-dialog.tpl.html',
234 parent: angular.element('#yangmanModule'),
236 clickOutsideToClose: true,
238 requests: reqObj ? [reqObj] : vm.requestList.selectedRequests,
239 collectionNames: vm.collectionList.getCollectionNames(),
240 duplicate: duplicate || false,
242 }).then(saveRequests);
246 * Add each request from requests array to collectionList and save
249 function saveRequests(requests){
250 requests.forEach(function (reqObj){
251 vm.collectionList.addRequestToList(reqObj);
252 vm.collectionList.saveToStorage();
253 $scope.broadcastFromRoot('YANGMAN_REFRESH_COLLECTIONS');
259 * Dialog for editing collection name (used for duplicating collection too)
262 * @param {boolean} duplicate
264 function showDgEditCollection(ev, collection, duplicate){
266 controller: EditCollectionDialogCtrl,
267 controllerAs: 'dialog',
268 templateUrl: $scope.globalViewPath + 'leftpanel/edit-collection-dialog.tpl.html',
269 parent: angular.element('#yangmanModule'),
271 clickOutsideToClose: true,
273 collection: collection,
274 allCollections: vm.collectionList.collections,
275 duplicate: duplicate,
277 }).then(duplicate ? duplicateCollection : changeCollectionName);
282 * @param {array} names 0. element is old name, 1. element is new name
284 function changeCollectionName(names){
285 vm.collectionList.renameCollection(names[0], names[1]);
286 vm.collectionList.saveToStorage();
287 $scope.broadcastFromRoot('YANGMAN_REFRESH_COLLECTIONS');
291 * Create collection duplicate and save
292 * @param {array} names 0. element is old name, 1. element is new name
294 function duplicateCollection(names){
295 vm.collectionList.duplicateCollection(names[0], names[1]);
296 vm.collectionList.saveToStorage();
297 $scope.broadcastFromRoot('YANGMAN_REFRESH_COLLECTIONS');
304 * @param list collectionList or requestList object
306 function useAsMainList(list){
311 * Loading history request and grouping by date
313 function loadHistoryRequests(){
314 vm.requestList.loadListFromStorage();
315 vm.requestList.groupListByDate();
319 * Loading collections
321 function loadCollectionRequest(){
322 vm.collectionList.loadListFromStorage();
326 * Loading both history and collections reqs
328 function loadRequests(){
329 loadHistoryRequests();
330 loadCollectionRequest();
334 * Request in list selection
335 * For history reqs it is possible multiselect, thats why event.ctrlKey is used
339 function selectRequest(event, requestObj){
340 vm.mainList.toggleReqSelection(!event.ctrlKey, requestObj);
341 $scope.setHistoryReqsSelected(vm.requestList.selectedRequests.length > 0);
349 function getApiCallback(pathString) {
350 var snp = PathUtilsService.getStorageAndNormalizedPath(pathString),
351 // if the path is for mountpoint then get the path to treedata structure
352 mpSearchPath = MountPointsConnectorService.alterMpPath(pathString),
353 apiIndexes = PathUtilsService.searchNodeByPath(mpSearchPath, $scope.treeApis, $scope.treeRows),
354 selApi = apiIndexes ? $scope.apis[apiIndexes.indexApi] : null,
355 selSubApi = selApi ? selApi.subApis[apiIndexes.indexSubApi] : null,
356 copiedApi = selSubApi ?
357 selSubApi.clone({ storage: snp.storage, withoutNode: true, clonePathArray: true }) :
361 copiedApi.pathArray.forEach(function (p) {
365 PathUtilsService.fillPath(copiedApi.pathArray, snp.normalizedPath);
368 var searchedModule = PathUtilsService.getModuleNameFromPath(pathString);
370 if (mpSearchPath.indexOf(mountPrefix) !== -1 && copiedApi){
371 copiedApi = $scope.selSubApi &&
372 searchedModule === $scope.selSubApi.pathArray[1].module ?