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', reqObj.receivedData.hasOwnProperty('errors') ? reqObj.receivedData : {});
197 $scope.rootBroadcast('YANGMAN_FILL_NODE_FROM_REQ', { requestUrl: reqObj.path, requestData: data },
199 $scope.setRightPanelSection('form');
200 $scope.rootBroadcast('YANGMAN_HEADER_INIT', {
202 method: reqObj.method,
204 status: reqObj.responseStatus,
205 statusText: reqObj.responseStatusText,
206 time: reqObj.responseTime,
212 YangmanService.fillNodeFromResponse($scope.node, data);
213 $scope.node.expanded = true;
222 * Force request header to execute request with data from reqObj
225 function executeRequest(reqObj) {
226 $scope.setRightPanelSection('req-data');
227 $scope.rootBroadcast(
228 'YANGMAN_HEADER_INIT',
229 { path: reqObj.path, method: reqObj.method },
231 $scope.rootBroadcast(
232 'YANGMAN_EXECUTE_WITH_DATA',
233 { data: reqObj.sentData },
234 function (historyReq){
235 showData(historyReq);
245 * Show current reqObj json data in right panel section
249 function showData(reqObj) {
251 $scope.setRightPanelSection('req-data');
252 $scope.setJsonView(true, reqObj.method !== 'GET');
254 $scope.rootBroadcast('YANGMAN_HEADER_INIT', {
256 method: reqObj.method,
258 status: reqObj.responseStatus,
259 statusText: reqObj.responseStatusText,
260 time: reqObj.responseTime,
264 $scope.rootBroadcast(
265 'YANGMAN_SET_ERROR_DATA',
266 reqObj.receivedData && reqObj.receivedData.hasOwnProperty('errors') ? reqObj.receivedData : {}
269 $scope.rootBroadcast(
270 'YANGMAN_SET_CODEMIRROR_DATA_SENT',
271 { data: reqObj.setDataForView(reqObj.sentData) }
273 $scope.rootBroadcast(
274 'YANGMAN_SET_CODEMIRROR_DATA_RECEIVED',
275 { data: reqObj.setDataForView(reqObj.receivedData) }
278 var data = reqObj.method === 'GET' ? reqObj.receivedData : reqObj.sentData;
280 $scope.rootBroadcast('YANGMAN_FILL_NODE_FROM_REQ', { requestUrl: reqObj.path, leftpanel: 0},
284 YangmanService.fillNodeFromResponse($scope.node, data);
285 $scope.node.expanded = true;
293 * Clear current ctrl search value
295 function clearFilter(){
300 * Dialog for deleting either selected requests or reqObj
305 function showDgDeleteRequests(ev, reqObj){
307 var confirm = $mdDialog.confirm()
308 .title($filter('translate')('YANGMAN_DELETE_REQ_CONFIRM_TITLE'))
309 .textContent($filter('translate')('YANGMAN_DELETE_REQ_CONFIRM_TEXT'))
310 .ariaLabel($filter('translate')('YANGMAN_DELETE_REQ_CONFIRM_TITLE'))
312 .ok($filter('translate')('YANGMAN_OK'))
313 .cancel($filter('translate')('YANGMAN_CANCEL'));
315 YangmanDesignService.disableMdMenuItem(ev);
317 $mdDialog.show(confirm).then(function (){
319 vm.mainList.deleteRequestItem(reqObj);
322 vm.mainList.selectedRequests.forEach(function (elem){
323 vm.mainList.deleteRequestItem(elem);
326 vm.mainList.saveToStorage();
327 $scope.rootBroadcast('YANGMAN_REFRESH_HISTORY');
329 YangmanDesignService.enableMdMenuItem(ev);
335 * Dialog for deleting collection and refreshing collections
339 function showDgDeleteCollection(ev, collObj){
340 var confirm = $mdDialog.confirm()
341 .title($filter('translate')('YANGMAN_DELETE_COL_CONFIRM_TITLE') + ' ' + collObj.name + '?')
342 .textContent($filter('translate')('YANGMAN_DELETE_COL_CONFIRM_TEXT'))
343 .ariaLabel($filter('translate')('YANGMAN_DELETE_COL_CONFIRM_TITLE'))
345 .ok($filter('translate')('YANGMAN_OK'))
346 .cancel($filter('translate')('YANGMAN_CANCEL'));
348 YangmanDesignService.disableMdMenuItem(ev);
350 $mdDialog.show(confirm).then(function (){
351 vm.collectionList.deleteCollection(collObj);
352 vm.collectionList.saveToStorage();
353 refreshCollections();
355 YangmanDesignService.enableMdMenuItem(ev);
360 * Check if reqObj matches current search value
364 function filterReq(reqObj){
365 return reqObj.path.indexOf(vm.search) !== -1;
369 * Check if collection name matches current search value or any collection req matches
372 function filterCol(colObj){
373 return filterColName(colObj) || colObj.data.some(filterReq);
377 * Get count of requests matching filter in collection colObj
381 function colMatchingReqsCount(colObj){
382 return colObj.data.filter(vm.filterReq).length;
386 * Check if collection name matches current filter
390 function filterColName(colObj){
391 return colObj.name.indexOf(vm.search) !== -1;
398 function fakeFilter(){
403 * Show dialog for saving reqObj to collection (used for duplicate req too)
408 function showDgSaveReq(ev, reqObj, duplicate){
411 controller: SaveReqDialogCtrl,
412 controllerAs: 'dialog',
413 templateUrl: $scope.globalViewPath + 'leftpanel/save-req-dialog.tpl.html',
414 parent: angular.element('#yangmanModule'),
416 clickOutsideToClose: true,
418 requests: reqObj ? [reqObj] : vm.requestList.selectedRequests,
419 collectionNames: vm.collectionList.getCollectionNames(),
420 duplicate: duplicate || false,
422 }).then(saveRequests);
426 * Add each request from requests array to collectionList and save
429 function saveRequests(requests){
430 requests.forEach(function (reqObj){
431 vm.collectionList.addRequestToList(reqObj);
432 vm.collectionList.saveToStorage();
433 refreshCollections();
439 * Dialog for editing collection name (used for duplicating collection too)
442 * @param {boolean} duplicate
444 function showDgEditCollection(ev, collection, duplicate){
446 controller: EditCollectionDialogCtrl,
447 controllerAs: 'dialog',
448 templateUrl: $scope.globalViewPath + 'leftpanel/edit-collection-dialog.tpl.html',
449 parent: angular.element('#yangmanModule'),
451 clickOutsideToClose: true,
453 collection: collection,
454 allCollections: vm.collectionList.collections,
455 duplicate: duplicate,
457 }).then(duplicate ? duplicateCollection : changeCollectionName);
462 * @param {array} names 0. element is old name, 1. element is new name
464 function changeCollectionName(names){
465 vm.collectionList.renameCollection(names[0], names[1]);
466 vm.collectionList.saveToStorage();
467 refreshCollections();
471 * Create collection duplicate, save and refresh collections
472 * @param {array} names 0. element is old name, 1. element is new name
474 function duplicateCollection(names){
475 vm.collectionList.duplicateCollection(names[0], names[1]);
476 vm.collectionList.saveToStorage();
477 refreshCollections();
481 function selectNewestRequest() {
482 vm.mainList.toggleReqSelection(true, vm.mainList.getNewestRequest());
487 * @param list collectionList or requestList object
492 if (list === vm.requestList){
493 // saving from request header after execution
494 $scope.$on('YANGMAN_SAVE_EXECUTED_REQUEST', saveBcstedHistoryRequest);
495 // saving from request header
496 $scope.$on('YANGMAN_SAVE_REQUEST_TO_COLLECTION', saveRequestFromExt);
497 // select newest request
498 $scope.$on('YANGMAN_SELECT_THE_NEWEST_REQUEST', selectNewestRequest);
501 // saving collections expanded status on refresh
502 $scope.$on('YANGMAN_REFRESH_AND_EXPAND_COLLECTIONS', function(event, params){
503 $scope.rootBroadcast('YANGMAN_REFRESH_COLLECTIONS');
504 (params.cbk || angular.noop)();
511 * Loading history request and grouping by date
513 function loadHistoryRequests(){
514 vm.requestList.loadListFromStorage();
515 vm.requestList.groupListByDate();
519 * Loading collections
521 function loadCollectionRequest(){
522 vm.collectionList.loadListFromStorage();
526 * Loading both history and collections reqs
528 function loadRequests(){
529 loadHistoryRequests();
530 loadCollectionRequest();
534 * Request in list selection
535 * For history reqs it is possible multiselect, thats why event.ctrlKey is used
539 function selectRequest(event, requestObj){
540 vm.mainList.toggleReqSelection(!event.ctrlKey, requestObj);
541 $scope.setHistoryReqsSelected(vm.requestList.selectedRequests.length > 0);
543 vm.showData(requestObj);
548 * Deselect history requests
550 function deselectAllRequests(){
551 vm.mainList.deselectReqs();
555 * Select history requests
557 function selectAllRequests(){
558 deselectAllRequests();
559 vm.mainList.dateGroups.forEach(function(group){
560 vm.mainList.selectReqs(group.requests);
565 * Refresh and expand collections
567 function refreshCollections(){
568 var collectionNames = vm.collectionList.getExpandedCollectionNames();
569 $scope.rootBroadcast('YANGMAN_REFRESH_AND_EXPAND_COLLECTIONS', null, function(){
570 vm.collectionList.expandCollectionByNames(collectionNames);