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', 'YangmanService',
12 'YangmanDesignService',
15 function RequestsListCtrl($filter, $mdDialog, $scope, HandleFileService, PathUtilsService, RequestsService,
16 YangmanService, YangmanDesignService) {
19 vm.collectionList = RequestsService.createEmptyCollectionList('yangman_collectionsList');
20 vm.collectionsSortAsc = true;
22 vm.requestList = RequestsService.createEmptyHistoryList('yangman_requestsList');
26 vm.clearFilter = clearFilter;
27 vm.clearHistoryList = clearHistoryList;
28 vm.clearCollectionList = clearCollectionList;
29 vm.colMatchingReqsCount = colMatchingReqsCount;
30 vm.deselectAllRequests = deselectAllRequests;
31 vm.downloadCollection = downloadCollection;
32 vm.executeRequest = executeRequest;
33 vm.fakeFilter = fakeFilter;
34 vm.filterCol = filterCol;
35 vm.filterColName = filterColName;
36 vm.filterReq = filterReq;
38 vm.loadRequests = loadRequests;
39 vm.readCollectionFromFile = readCollectionFromFile;
40 vm.refreshCollections = refreshCollections;
41 vm.selectAllRequests = selectAllRequests;
42 vm.selectRequest = selectRequest;
43 vm.showData = showData;
44 vm.showDgDeleteCollection = showDgDeleteCollection;
45 vm.showDgDeleteRequests = showDgDeleteRequests;
46 vm.showDgEditCollection = showDgEditCollection;
47 vm.showDgSaveReq = showDgSaveReq;
48 vm.toggleCollectionsSort = toggleCollectionsSort;
49 vm.showForm = showForm;
51 $scope.$on('YANGMAN_REFRESH_COLLECTIONS', loadCollectionRequest);
52 $scope.$on('YANGMAN_REFRESH_HISTORY', loadHistoryRequests);
57 * Save request obje to collection from other controller
60 function saveRequestFromExt(event, args) {
61 vm.showDgSaveReq(args.params.event, args.params.reqObj, false);
66 * Clear history requests list and save to storage
68 function clearHistoryList(ev) {
70 YangmanDesignService.disableMdMenuItem(ev);
72 var confirm = $mdDialog.confirm()
73 .title($filter('translate')('YANGMAN_DELETE_HISTORY_CONFIRM_TITLE'))
74 .textContent($filter('translate')('YANGMAN_DELETE_HISTORY_CONFIRM_TEXT'))
75 .ariaLabel($filter('translate')('YANGMAN_DELETE_HISTORY_CONFIRM_TITLE'))
77 .ok($filter('translate')('YANGMAN_OK'))
78 .cancel($filter('translate')('YANGMAN_CANCEL'));
80 $mdDialog.show(confirm).then(function (){
81 vm.requestList.clear();
82 vm.requestList.saveToStorage();
83 $scope.rootBroadcast('YANGMAN_REFRESH_HISTORY');
84 YangmanDesignService.enableMdMenuItem(ev);
86 YangmanDesignService.enableMdMenuItem(ev);
91 * Clear collections requests list and save to storage
93 function clearCollectionList(ev) {
94 var confirm = $mdDialog.confirm()
95 .title($filter('translate')('YANGMAN_DELETE_COLLECTION_CONFIRM_TITLE'))
96 .textContent($filter('translate')('YANGMAN_DELETE_COLLECTION_CONFIRM_TEXT'))
97 .ariaLabel($filter('translate')('YANGMAN_DELETE_COLLECTION_CONFIRM_TITLE'))
99 .ok($filter('translate')('YANGMAN_OK'))
100 .cancel($filter('translate')('YANGMAN_CANCEL'));
102 YangmanDesignService.disableMdMenuItem(ev);
104 $mdDialog.show(confirm).then(function (){
105 vm.collectionList.clear();
106 vm.collectionList.saveToStorage();
107 $scope.rootBroadcast('YANGMAN_REFRESH_COLLECTIONS');
108 YangmanDesignService.enableMdMenuItem(ev);
110 YangmanDesignService.enableMdMenuItem(ev);
115 * Create history request from other ctrl
116 * @param broadcastEvent
119 function saveBcstedHistoryRequest(broadcastEvent, params) {
120 vm.requestList.addRequestToList(params.params);
121 vm.requestList.groupListByDate();
122 vm.requestList.saveToStorage();
123 loadHistoryRequests();
125 (params.cbk || angular.noop)();
129 * Clear value of input file used to import collection
131 function clearFileInputValue(){
132 angular.element(document).find('#importCollection').val('');
136 * Importing collection from a file
137 * todo: error handling - msgs for user
138 * @param $fileContent
140 function readCollectionFromFile($fileContent) {
141 var data = $fileContent,
152 if (data && YangmanService.validateFile(data, checkArray)){
154 vm.collectionList.loadListFromFile(data);
155 vm.collectionList.saveToStorage();
156 clearFileInputValue();
159 clearFileInputValue();
160 console.error('DataStorage error:', e);
164 clearFileInputValue();
168 function toggleCollectionsSort() {
169 vm.collectionsSortAsc = !vm.collectionsSortAsc;
173 * Export collection to json file
174 * @param {Collection} collection
176 function downloadCollection(collection) {
178 var cListJSON = vm.collectionList.getCollectionInJSON(collection.name);
180 HandleFileService.downloadFile(collection.name + '.json', cListJSON, 'json', 'charset=utf-8',
183 console.error('ExportCollection error:', e);
189 * Fill request form in right panel with request data
192 function showForm(reqObj) {
193 var data = reqObj.method === 'GET' ? reqObj.receivedData : reqObj.sentData;
195 $scope.rootBroadcast('YANGMAN_SET_ERROR_DATA',
196 reqObj.receivedData && reqObj.receivedData.hasOwnProperty('errors') ? reqObj.receivedData : {});
198 $scope.rootBroadcast('YANGMAN_FILL_NODE_FROM_REQ', { requestUrl: reqObj.path, requestData: data },
200 $scope.setRightPanelSection('form');
201 $scope.rootBroadcast('YANGMAN_HEADER_INIT', {
203 method: reqObj.method,
205 status: reqObj.responseStatus,
206 statusText: reqObj.responseStatusText,
207 time: reqObj.responseTime,
213 YangmanService.fillNodeFromResponse($scope.node, data);
214 $scope.node.expanded = true;
223 * Force request header to execute request with data from reqObj
226 function executeRequest(reqObj) {
227 $scope.setRightPanelSection('req-data');
228 $scope.rootBroadcast(
229 'YANGMAN_HEADER_INIT',
230 { path: reqObj.path, method: reqObj.method },
232 $scope.rootBroadcast(
233 'YANGMAN_EXECUTE_WITH_DATA',
234 { data: reqObj.sentData },
235 function (historyReq){
236 showData(historyReq);
246 * Show current reqObj json data in right panel section
250 function showData(reqObj) {
252 $scope.setRightPanelSection('req-data');
253 $scope.setJsonView(true, reqObj.method !== 'GET');
255 $scope.rootBroadcast('YANGMAN_HEADER_INIT', {
257 method: reqObj.method,
259 status: reqObj.responseStatus,
260 statusText: reqObj.responseStatusText,
261 time: reqObj.responseTime,
265 $scope.rootBroadcast(
266 'YANGMAN_SET_ERROR_DATA',
267 reqObj.receivedData && reqObj.receivedData.hasOwnProperty('errors') ? reqObj.receivedData : {}
270 $scope.rootBroadcast(
271 'YANGMAN_SET_CODEMIRROR_DATA_SENT',
272 { data: reqObj.setDataForView(reqObj.sentData) }
274 $scope.rootBroadcast(
275 'YANGMAN_SET_CODEMIRROR_DATA_RECEIVED',
276 { data: reqObj.setDataForView(reqObj.receivedData) }
279 var data = reqObj.method === 'GET' ? reqObj.receivedData : reqObj.sentData;
281 $scope.rootBroadcast('YANGMAN_FILL_NODE_FROM_REQ', { requestUrl: reqObj.path, leftpanel: 0},
285 YangmanService.fillNodeFromResponse($scope.node, data);
286 $scope.node.expanded = true;
294 * Clear current ctrl search value
296 function clearFilter(){
301 * Dialog for deleting either selected requests or reqObj
306 function showDgDeleteRequests(ev, reqObj){
308 var confirm = $mdDialog.confirm()
309 .title($filter('translate')('YANGMAN_DELETE_REQ_CONFIRM_TITLE'))
310 .textContent($filter('translate')('YANGMAN_DELETE_REQ_CONFIRM_TEXT'))
311 .ariaLabel($filter('translate')('YANGMAN_DELETE_REQ_CONFIRM_TITLE'))
313 .ok($filter('translate')('YANGMAN_OK'))
314 .cancel($filter('translate')('YANGMAN_CANCEL'));
316 YangmanDesignService.disableMdMenuItem(ev);
318 $mdDialog.show(confirm).then(function (){
320 vm.mainList.deleteRequestItem(reqObj);
323 vm.mainList.selectedRequests.forEach(function (elem){
324 vm.mainList.deleteRequestItem(elem);
327 vm.mainList.saveToStorage();
328 $scope.rootBroadcast('YANGMAN_REFRESH_HISTORY');
330 YangmanDesignService.enableMdMenuItem(ev);
336 * Dialog for deleting collection and refreshing collections
340 function showDgDeleteCollection(ev, collObj){
341 var confirm = $mdDialog.confirm()
342 .title($filter('translate')('YANGMAN_DELETE_COL_CONFIRM_TITLE') + ' ' + collObj.name + '?')
343 .textContent($filter('translate')('YANGMAN_DELETE_COL_CONFIRM_TEXT'))
344 .ariaLabel($filter('translate')('YANGMAN_DELETE_COL_CONFIRM_TITLE'))
346 .ok($filter('translate')('YANGMAN_OK'))
347 .cancel($filter('translate')('YANGMAN_CANCEL'));
349 YangmanDesignService.disableMdMenuItem(ev);
351 $mdDialog.show(confirm).then(function (){
352 vm.collectionList.deleteCollection(collObj);
353 vm.collectionList.saveToStorage();
354 refreshCollections();
356 YangmanDesignService.enableMdMenuItem(ev);
361 * Check if reqObj matches current search value
365 function filterReq(reqObj){
366 return reqObj.path.toLowerCase().indexOf(vm.search.toLowerCase()) > -1;
370 * Check if collection name matches current search value or any collection req matches
373 function filterCol(colObj){
374 return filterColName(colObj) || colObj.data.some(filterReq);
378 * Get count of requests matching filter in collection colObj
382 function colMatchingReqsCount(colObj){
383 return colObj.data.filter(vm.filterReq).length;
387 * Check if collection name matches current filter
391 function filterColName(colObj){
392 return colObj.name.toLowerCase().indexOf(vm.search.toLowerCase()) > -1;
399 function fakeFilter(){
404 * Show dialog for saving reqObj to collection (used for duplicate req too)
409 function showDgSaveReq(ev, reqObj, duplicate){
412 controller: SaveReqDialogCtrl,
413 controllerAs: 'dialog',
414 templateUrl: $scope.globalViewPath + 'leftpanel/save-req-dialog.tpl.html',
415 parent: angular.element('#yangmanModule'),
417 clickOutsideToClose: true,
419 requests: reqObj ? [reqObj] : vm.requestList.selectedRequests,
420 collectionNames: vm.collectionList.getCollectionNames(),
421 duplicate: duplicate || false,
423 }).then(saveRequests);
427 * Add each request from requests array to collectionList and save
430 function saveRequests(requests){
431 requests.forEach(function (reqObj){
432 vm.collectionList.addRequestToList(reqObj);
433 vm.collectionList.saveToStorage();
434 refreshCollections();
440 * Dialog for editing collection name (used for duplicating collection too)
443 * @param {boolean} duplicate
445 function showDgEditCollection(ev, collection, duplicate){
447 controller: EditCollectionDialogCtrl,
448 controllerAs: 'dialog',
449 templateUrl: $scope.globalViewPath + 'leftpanel/edit-collection-dialog.tpl.html',
450 parent: angular.element('#yangmanModule'),
452 clickOutsideToClose: true,
454 collection: collection,
455 allCollections: vm.collectionList.collections,
456 duplicate: duplicate,
458 }).then(duplicate ? duplicateCollection : changeCollectionName);
463 * @param {array} names 0. element is old name, 1. element is new name
465 function changeCollectionName(names){
466 vm.collectionList.renameCollection(names[0], names[1]);
467 vm.collectionList.saveToStorage();
468 refreshCollections();
472 * Create collection duplicate, save and refresh collections
473 * @param {array} names 0. element is old name, 1. element is new name
475 function duplicateCollection(names){
476 vm.collectionList.duplicateCollection(names[0], names[1]);
477 vm.collectionList.saveToStorage();
478 refreshCollections();
482 function selectNewestRequest() {
483 vm.mainList.toggleReqSelection(true, vm.mainList.getNewestRequest());
488 * @param list collectionList or requestList object
493 if (list === vm.requestList){
494 // saving from request header after execution
495 $scope.$on('YANGMAN_SAVE_EXECUTED_REQUEST', saveBcstedHistoryRequest);
496 // saving from request header
497 $scope.$on('YANGMAN_SAVE_REQUEST_TO_COLLECTION', saveRequestFromExt);
498 // select newest request
499 $scope.$on('YANGMAN_SELECT_THE_NEWEST_REQUEST', selectNewestRequest);
502 // saving collections expanded status on refresh
503 $scope.$on('YANGMAN_REFRESH_AND_EXPAND_COLLECTIONS', function(event, params){
504 $scope.rootBroadcast('YANGMAN_REFRESH_COLLECTIONS');
505 (params.cbk || angular.noop)();
512 * Loading history request and grouping by date
514 function loadHistoryRequests(){
515 vm.requestList.loadListFromStorage();
516 vm.requestList.groupListByDate();
520 * Loading collections
522 function loadCollectionRequest(){
523 vm.collectionList.loadListFromStorage();
527 * Loading both history and collections reqs
529 function loadRequests(){
530 loadHistoryRequests();
531 loadCollectionRequest();
535 * Request in list selection
536 * For history reqs it is possible multiselect, thats why event.ctrlKey is used
540 function selectRequest(event, requestObj){
541 vm.mainList.toggleReqSelection(!event.ctrlKey, requestObj);
542 $scope.setHistoryReqsSelected(vm.requestList.selectedRequests.length > 0);
544 vm.showData(requestObj);
549 * Deselect history requests
551 function deselectAllRequests(){
552 vm.mainList.deselectReqs();
556 * Select history requests
558 function selectAllRequests(){
559 deselectAllRequests();
560 vm.mainList.dateGroups.forEach(function(group){
561 vm.mainList.selectReqs(group.requests);
566 * Refresh and expand collections
568 function refreshCollections(){
569 var collectionNames = vm.collectionList.getExpandedCollectionNames();
570 $scope.rootBroadcast('YANGMAN_REFRESH_AND_EXPAND_COLLECTIONS', null, function(){
571 vm.collectionList.expandCollectionByNames(collectionNames);