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) {
69 var confirm = $mdDialog.confirm()
70 .title($filter('translate')('YANGMAN_DELETE_HISTORY_CONFIRM_TITLE'))
71 .textContent($filter('translate')('YANGMAN_DELETE_HISTORY_CONFIRM_TEXT'))
72 .ariaLabel($filter('translate')('YANGMAN_DELETE_HISTORY_CONFIRM_TITLE'))
74 .ok($filter('translate')('YANGMAN_OK'))
75 .cancel($filter('translate')('YANGMAN_CANCEL'));
77 $mdDialog.show(confirm).then(function (){
78 vm.requestList.clear();
79 vm.requestList.saveToStorage();
80 $scope.rootBroadcast('YANGMAN_REFRESH_HISTORY');
85 * Clear collections requests list and save to storage
87 function clearCollectionList(ev) {
88 var confirm = $mdDialog.confirm()
89 .title($filter('translate')('YANGMAN_DELETE_COLLECTION_CONFIRM_TITLE'))
90 .textContent($filter('translate')('YANGMAN_DELETE_COLLECTION_CONFIRM_TEXT'))
91 .ariaLabel($filter('translate')('YANGMAN_DELETE_COLLECTION_CONFIRM_TITLE'))
93 .ok($filter('translate')('YANGMAN_OK'))
94 .cancel($filter('translate')('YANGMAN_CANCEL'));
96 $mdDialog.show(confirm).then(function (){
97 vm.collectionList.clear();
98 vm.collectionList.saveToStorage();
99 $scope.rootBroadcast('YANGMAN_REFRESH_COLLECTIONS');
104 * Create history request from other ctrl
105 * @param broadcastEvent
108 function saveBcstedHistoryRequest(broadcastEvent, params) {
109 vm.requestList.addRequestToList(params.params);
110 vm.requestList.groupListByDate();
111 vm.requestList.saveToStorage();
112 loadHistoryRequests();
114 (params.cbk || angular.noop)();
118 * Clear value of input file used to import collection
120 function clearFileInputValue(){
121 angular.element(document).find('#importCollection').val('');
125 * Importing collection from a file
126 * todo: error handling - msgs for user
127 * @param $fileContent
129 function readCollectionFromFile($fileContent) {
130 var data = $fileContent,
141 if (data && YangmanService.validateFile(data, checkArray)){
143 vm.collectionList.loadListFromFile(data);
144 vm.collectionList.saveToStorage();
145 clearFileInputValue();
148 clearFileInputValue();
149 console.error('DataStorage error:', e);
153 clearFileInputValue();
157 function toggleCollectionsSort() {
158 vm.collectionsSortAsc = !vm.collectionsSortAsc;
162 * Export collection to json file
163 * @param {Collection} collection
165 function downloadCollection(collection) {
167 var cListJSON = vm.collectionList.getCollectionInJSON(collection.name);
169 HandleFileService.downloadFile(collection.name + '.json', cListJSON, 'json', 'charset=utf-8',
172 console.error('ExportCollection error:', e);
178 * Fill request form in right panel with request data
181 function showForm(reqObj) {
182 var data = reqObj.method === 'GET' ? reqObj.receivedData : reqObj.sentData;
184 $scope.rootBroadcast('YANGMAN_SET_ERROR_DATA', reqObj.receivedData.hasOwnProperty('errors') ? reqObj.receivedData : {});
186 $scope.rootBroadcast('YANGMAN_FILL_NODE_FROM_REQ', { requestUrl: reqObj.path, requestData: data },
188 $scope.setRightPanelSection('form');
189 $scope.rootBroadcast('YANGMAN_HEADER_INIT', {
191 method: reqObj.method,
193 status: reqObj.responseStatus,
194 statusText: reqObj.responseStatusText,
195 time: reqObj.responseTime,
201 YangmanService.fillNodeFromResponse($scope.node, data);
202 $scope.node.expanded = true;
211 * Force request header to execute request with data from reqObj
214 function executeRequest(reqObj) {
215 $scope.setRightPanelSection('req-data');
216 $scope.rootBroadcast(
217 'YANGMAN_HEADER_INIT',
218 { path: reqObj.path, method: reqObj.method },
220 $scope.rootBroadcast(
221 'YANGMAN_EXECUTE_WITH_DATA',
222 { data: reqObj.sentData },
223 function (historyReq){
224 showData(historyReq);
234 * Show current reqObj json data in right panel section
238 function showData(reqObj) {
240 $scope.setRightPanelSection('req-data');
241 $scope.setJsonView(true, reqObj.method !== 'GET');
243 $scope.rootBroadcast('YANGMAN_HEADER_INIT', {
245 method: reqObj.method,
247 status: reqObj.responseStatus,
248 statusText: reqObj.responseStatusText,
249 time: reqObj.responseTime,
253 $scope.rootBroadcast(
254 'YANGMAN_SET_ERROR_DATA',
255 reqObj.receivedData && reqObj.receivedData.hasOwnProperty('errors') ? reqObj.receivedData : {}
258 $scope.rootBroadcast(
259 'YANGMAN_SET_CODEMIRROR_DATA_SENT',
260 { data: reqObj.setDataForView(reqObj.sentData) }
262 $scope.rootBroadcast(
263 'YANGMAN_SET_CODEMIRROR_DATA_RECEIVED',
264 { data: reqObj.setDataForView(reqObj.receivedData) }
267 var data = reqObj.method === 'GET' ? reqObj.receivedData : reqObj.sentData;
269 $scope.rootBroadcast('YANGMAN_FILL_NODE_FROM_REQ', { requestUrl: reqObj.path, leftpanel: 0},
273 YangmanService.fillNodeFromResponse($scope.node, data);
274 $scope.node.expanded = true;
282 * Clear current ctrl search value
284 function clearFilter(){
289 * Dialog for deleting either selected requests or reqObj
294 function showDgDeleteRequests(ev, reqObj){
296 var confirm = $mdDialog.confirm()
297 .title($filter('translate')('YANGMAN_DELETE_REQ_CONFIRM_TITLE'))
298 .textContent($filter('translate')('YANGMAN_DELETE_REQ_CONFIRM_TEXT'))
299 .ariaLabel($filter('translate')('YANGMAN_DELETE_REQ_CONFIRM_TITLE'))
301 .ok($filter('translate')('YANGMAN_OK'))
302 .cancel($filter('translate')('YANGMAN_CANCEL'));
304 YangmanDesignService.disableMdMenuItem(ev);
306 $mdDialog.show(confirm).then(function (){
308 vm.mainList.deleteRequestItem(reqObj);
311 vm.mainList.selectedRequests.forEach(function (elem){
312 vm.mainList.deleteRequestItem(elem);
315 vm.mainList.saveToStorage();
316 $scope.rootBroadcast('YANGMAN_REFRESH_HISTORY');
318 YangmanDesignService.enableMdMenuItem(ev);
324 * Dialog for deleting collection and refreshing collections
328 function showDgDeleteCollection(ev, collObj){
329 var confirm = $mdDialog.confirm()
330 .title($filter('translate')('YANGMAN_DELETE_COL_CONFIRM_TITLE') + ' ' + collObj.name + '?')
331 .textContent($filter('translate')('YANGMAN_DELETE_COL_CONFIRM_TEXT'))
332 .ariaLabel($filter('translate')('YANGMAN_DELETE_COL_CONFIRM_TITLE'))
334 .ok($filter('translate')('YANGMAN_OK'))
335 .cancel($filter('translate')('YANGMAN_CANCEL'));
337 YangmanDesignService.disableMdMenuItem(ev);
339 $mdDialog.show(confirm).then(function (){
340 vm.collectionList.deleteCollection(collObj);
341 vm.collectionList.saveToStorage();
342 refreshCollections();
344 YangmanDesignService.enableMdMenuItem(ev);
349 * Check if reqObj matches current search value
353 function filterReq(reqObj){
354 return reqObj.path.indexOf(vm.search) !== -1;
358 * Check if collection name matches current search value or any collection req matches
361 function filterCol(colObj){
362 return filterColName(colObj) || colObj.data.some(filterReq);
366 * Get count of requests matching filter in collection colObj
370 function colMatchingReqsCount(colObj){
371 return colObj.data.filter(vm.filterReq).length;
375 * Check if collection name matches current filter
379 function filterColName(colObj){
380 return colObj.name.indexOf(vm.search) !== -1;
387 function fakeFilter(){
392 * Show dialog for saving reqObj to collection (used for duplicate req too)
397 function showDgSaveReq(ev, reqObj, duplicate){
400 controller: SaveReqDialogCtrl,
401 controllerAs: 'dialog',
402 templateUrl: $scope.globalViewPath + 'leftpanel/save-req-dialog.tpl.html',
403 parent: angular.element('#yangmanModule'),
405 clickOutsideToClose: true,
407 requests: reqObj ? [reqObj] : vm.requestList.selectedRequests,
408 collectionNames: vm.collectionList.getCollectionNames(),
409 duplicate: duplicate || false,
411 }).then(saveRequests);
415 * Add each request from requests array to collectionList and save
418 function saveRequests(requests){
419 requests.forEach(function (reqObj){
420 vm.collectionList.addRequestToList(reqObj);
421 vm.collectionList.saveToStorage();
422 refreshCollections();
428 * Dialog for editing collection name (used for duplicating collection too)
431 * @param {boolean} duplicate
433 function showDgEditCollection(ev, collection, duplicate){
435 controller: EditCollectionDialogCtrl,
436 controllerAs: 'dialog',
437 templateUrl: $scope.globalViewPath + 'leftpanel/edit-collection-dialog.tpl.html',
438 parent: angular.element('#yangmanModule'),
440 clickOutsideToClose: true,
442 collection: collection,
443 allCollections: vm.collectionList.collections,
444 duplicate: duplicate,
446 }).then(duplicate ? duplicateCollection : changeCollectionName);
451 * @param {array} names 0. element is old name, 1. element is new name
453 function changeCollectionName(names){
454 vm.collectionList.renameCollection(names[0], names[1]);
455 vm.collectionList.saveToStorage();
456 refreshCollections();
460 * Create collection duplicate, save and refresh collections
461 * @param {array} names 0. element is old name, 1. element is new name
463 function duplicateCollection(names){
464 vm.collectionList.duplicateCollection(names[0], names[1]);
465 vm.collectionList.saveToStorage();
466 refreshCollections();
470 function selectNewestRequest() {
471 vm.mainList.toggleReqSelection(true, vm.mainList.getNewestRequest());
476 * @param list collectionList or requestList object
481 if (list === vm.requestList){
482 // saving from request header after execution
483 $scope.$on('YANGMAN_SAVE_EXECUTED_REQUEST', saveBcstedHistoryRequest);
484 // saving from request header
485 $scope.$on('YANGMAN_SAVE_REQUEST_TO_COLLECTION', saveRequestFromExt);
486 // select newest request
487 $scope.$on('YANGMAN_SELECT_THE_NEWEST_REQUEST', selectNewestRequest);
490 // saving collections expanded status on refresh
491 $scope.$on('YANGMAN_REFRESH_AND_EXPAND_COLLECTIONS', function(event, params){
492 $scope.rootBroadcast('YANGMAN_REFRESH_COLLECTIONS');
493 (params.cbk || angular.noop)();
500 * Loading history request and grouping by date
502 function loadHistoryRequests(){
503 vm.requestList.loadListFromStorage();
504 vm.requestList.groupListByDate();
508 * Loading collections
510 function loadCollectionRequest(){
511 vm.collectionList.loadListFromStorage();
515 * Loading both history and collections reqs
517 function loadRequests(){
518 loadHistoryRequests();
519 loadCollectionRequest();
523 * Request in list selection
524 * For history reqs it is possible multiselect, thats why event.ctrlKey is used
528 function selectRequest(event, requestObj){
529 vm.mainList.toggleReqSelection(!event.ctrlKey, requestObj);
530 $scope.setHistoryReqsSelected(vm.requestList.selectedRequests.length > 0);
532 vm.showData(requestObj);
537 * Deselect history requests
539 function deselectAllRequests(){
540 vm.mainList.deselectReqs();
544 * Select history requests
546 function selectAllRequests(){
547 deselectAllRequests();
548 vm.mainList.dateGroups.forEach(function(group){
549 vm.mainList.selectReqs(group.requests);
554 * Refresh and expand collections
556 function refreshCollections(){
557 var collectionNames = vm.collectionList.getExpandedCollectionNames();
558 $scope.rootBroadcast('YANGMAN_REFRESH_AND_EXPAND_COLLECTIONS', null, function(){
559 vm.collectionList.expandCollectionByNames(collectionNames);