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',
14 function RequestsListCtrl($filter, $mdDialog, $scope, HandleFileService, PathUtilsService, RequestsService,
18 vm.collectionList = RequestsService.createEmptyCollectionList('yangman_collectionsList');
19 vm.collectionsSortAsc = true;
21 vm.requestList = RequestsService.createEmptyHistoryList('yangman_requestsList');
24 vm.clearFilter = clearFilter;
25 vm.clearHistoryList = clearHistoryList;
26 vm.clearCollectionList = clearCollectionList;
27 vm.colMatchingReqsCount = colMatchingReqsCount;
28 vm.deselectAllRequests = deselectAllRequests;
29 vm.downloadCollection = downloadCollection;
30 vm.executeRequest = executeRequest;
31 vm.fakeFilter = fakeFilter;
32 vm.filterCol = filterCol;
33 vm.filterColName = filterColName;
34 vm.filterReq = filterReq;
36 vm.loadRequests = loadRequests;
37 vm.readCollectionFromFile = readCollectionFromFile;
38 vm.refreshCollections = refreshCollections;
39 vm.selectAllRequests = selectAllRequests;
40 vm.selectRequest = selectRequest;
41 vm.showData = showData;
42 vm.showDgDeleteCollection = showDgDeleteCollection;
43 vm.showDgDeleteRequests = showDgDeleteRequests;
44 vm.showDgEditCollection = showDgEditCollection;
45 vm.showDgSaveReq = showDgSaveReq;
46 vm.toggleCollectionsSort = toggleCollectionsSort;
47 vm.showForm = showForm;
49 $scope.$on('YANGMAN_REFRESH_COLLECTIONS', loadCollectionRequest);
50 $scope.$on('YANGMAN_REFRESH_HISTORY', loadHistoryRequests);
55 * Save request obje to collection from other controller
58 function saveRequestFromExt(event, args) {
59 vm.showDgSaveReq(args.params.event, args.params.reqObj, false);
64 * Clear history requests list and save to storage
66 function clearHistoryList(ev) {
67 var confirm = $mdDialog.confirm()
68 .title($filter('translate')('YANGMAN_DELETE_HISTORY_CONFIRM_TITLE'))
69 .textContent($filter('translate')('YANGMAN_DELETE_HISTORY_CONFIRM_TEXT'))
70 .ariaLabel($filter('translate')('YANGMAN_DELETE_HISTORY_CONFIRM_TITLE'))
72 .ok($filter('translate')('YANGMAN_OK'))
73 .cancel($filter('translate')('YANGMAN_CANCEL'));
75 $mdDialog.show(confirm).then(function (){
76 vm.requestList.clear();
77 vm.requestList.saveToStorage();
78 $scope.rootBroadcast('YANGMAN_REFRESH_HISTORY');
83 * Clear collections requests list and save to storage
85 function clearCollectionList(ev) {
86 var confirm = $mdDialog.confirm()
87 .title($filter('translate')('YANGMAN_DELETE_COLLECTION_CONFIRM_TITLE'))
88 .textContent($filter('translate')('YANGMAN_DELETE_COLLECTION_CONFIRM_TEXT'))
89 .ariaLabel($filter('translate')('YANGMAN_DELETE_COLLECTION_CONFIRM_TITLE'))
91 .ok($filter('translate')('YANGMAN_OK'))
92 .cancel($filter('translate')('YANGMAN_CANCEL'));
94 $mdDialog.show(confirm).then(function (){
95 vm.collectionList.clear();
96 vm.collectionList.saveToStorage();
97 $scope.rootBroadcast('YANGMAN_REFRESH_COLLECTIONS');
102 * Create history request from other ctrl
103 * @param broadcastEvent
106 function saveBcstedHistoryRequest(broadcastEvent, params) {
107 vm.requestList.addRequestToList(params.params);
108 vm.requestList.groupListByDate();
109 vm.requestList.saveToStorage();
110 loadHistoryRequests();
114 * Clear value of input file used to import collection
116 function clearFileInputValue(){
117 angular.element(document).find('#importCollection').val('');
121 * Importing collection from a file
122 * todo: error handling - msgs for user
123 * @param $fileContent
125 function readCollectionFromFile($fileContent) {
126 var data = $fileContent,
137 if (data && YangmanService.validateFile(data, checkArray)){
139 vm.collectionList.loadListFromFile(data);
140 vm.collectionList.saveToStorage();
141 clearFileInputValue();
144 clearFileInputValue();
145 console.error('DataStorage error:', e);
149 clearFileInputValue();
153 function toggleCollectionsSort() {
154 vm.collectionsSortAsc = !vm.collectionsSortAsc;
158 * Export collection to json file
159 * @param {Collection} collection
161 function downloadCollection(collection) {
163 var cListJSON = vm.collectionList.getCollectionInJSON(collection.name);
165 HandleFileService.downloadFile(collection.name + '.json', cListJSON, 'json', 'charset=utf-8',
168 console.error('ExportCollection error:', e);
174 * Fill request form in right panel with request data
177 function showForm(reqObj) {
178 var data = reqObj.method === 'GET' ? reqObj.receivedData : reqObj.sentData;
180 $scope.rootBroadcast('YANGMAN_FILL_NODE_FROM_REQ', { requestUrl: reqObj.path, requestData: data },
182 $scope.setRightPanelSection('form');
183 $scope.rootBroadcast('YANGMAN_HEADER_INIT', { path: reqObj.path, method: reqObj.method });
187 YangmanService.fillNodeFromResponse($scope.node, data);
188 $scope.node.expanded = true;
197 * Force request header to execute request with data from reqObj
200 function executeRequest(reqObj) {
201 $scope.rootBroadcast(
202 'YANGMAN_HEADER_INIT',
203 { path: reqObj.path, method: reqObj.method },
205 $scope.rootBroadcast(
206 'YANGMAN_EXECUTE_WITH_DATA',
207 { data: reqObj.sentData },
208 function (historyReq){
209 showData(historyReq);
219 * Show current reqObj json data in right panel section
223 function showData(reqObj) {
225 $scope.setRightPanelSection('req-data');
226 $scope.setJsonView(true, reqObj.method !== 'GET');
228 $scope.rootBroadcast('YANGMAN_HEADER_INIT', { path: reqObj.path, method: reqObj.method });
230 $scope.rootBroadcast(
231 'YANGMAN_SET_CODEMIRROR_DATA_SENT',
232 { data: reqObj.setDataForView(reqObj.sentData) }
234 $scope.rootBroadcast(
235 'YANGMAN_SET_CODEMIRROR_DATA_RECEIVED',
236 { data: reqObj.setDataForView(reqObj.receivedData) }
239 var data = reqObj.method === 'GET' ? reqObj.receivedData : reqObj.sentData;
241 $scope.rootBroadcast('YANGMAN_FILL_NODE_FROM_REQ', { requestUrl: reqObj.path, leftpanel: 0},
245 YangmanService.fillNodeFromResponse($scope.node, data);
246 $scope.node.expanded = true;
254 * Clear current ctrl search value
256 function clearFilter(){
261 * Dialog for deleting either selected requests or reqObj
266 function showDgDeleteRequests(ev, reqObj){
267 var confirm = $mdDialog.confirm()
268 .title($filter('translate')('YANGMAN_DELETE_REQ_CONFIRM_TITLE'))
269 .textContent($filter('translate')('YANGMAN_DELETE_REQ_CONFIRM_TEXT'))
270 .ariaLabel($filter('translate')('YANGMAN_DELETE_REQ_CONFIRM_TITLE'))
272 .ok($filter('translate')('YANGMAN_OK'))
273 .cancel($filter('translate')('YANGMAN_CANCEL'));
275 $mdDialog.show(confirm).then(function (){
277 vm.mainList.deleteRequestItem(reqObj);
280 vm.mainList.selectedRequests.forEach(function (elem){
281 vm.mainList.deleteRequestItem(elem);
284 vm.mainList.saveToStorage();
285 $scope.rootBroadcast('YANGMAN_REFRESH_HISTORY');
291 * Dialog for deleting collection and refreshing collections
295 function showDgDeleteCollection(ev, collObj){
296 var confirm = $mdDialog.confirm()
297 .title($filter('translate')('YANGMAN_DELETE_COL_CONFIRM_TITLE') + ' ' + collObj.name + '?')
298 .textContent($filter('translate')('YANGMAN_DELETE_COL_CONFIRM_TEXT'))
299 .ariaLabel($filter('translate')('YANGMAN_DELETE_COL_CONFIRM_TITLE'))
301 .ok($filter('translate')('YANGMAN_OK'))
302 .cancel($filter('translate')('YANGMAN_CANCEL'));
304 $mdDialog.show(confirm).then(function (){
305 ev.stopPropagation();
306 vm.collectionList.deleteCollection(collObj);
307 vm.collectionList.saveToStorage();
308 refreshCollections();
313 * Check if reqObj matches current search value
317 function filterReq(reqObj){
318 return reqObj.path.indexOf(vm.search) !== -1;
322 * Check if collection name matches current search value or any collection req matches
325 function filterCol(colObj){
326 return filterColName(colObj) || colObj.data.some(filterReq);
330 * Get count of requests matching filter in collection colObj
334 function colMatchingReqsCount(colObj){
335 return colObj.data.filter(vm.filterReq).length;
339 * Check if collection name matches current filter
343 function filterColName(colObj){
344 return colObj.name.indexOf(vm.search) !== -1;
351 function fakeFilter(){
356 * Show dialog for saving reqObj to collection (used for duplicate req too)
361 function showDgSaveReq(ev, reqObj, duplicate){
364 controller: SaveReqDialogCtrl,
365 controllerAs: 'dialog',
366 templateUrl: $scope.globalViewPath + 'leftpanel/save-req-dialog.tpl.html',
367 parent: angular.element('#yangmanModule'),
369 clickOutsideToClose: true,
371 requests: reqObj ? [reqObj] : vm.requestList.selectedRequests,
372 collectionNames: vm.collectionList.getCollectionNames(),
373 duplicate: duplicate || false,
375 }).then(saveRequests);
379 * Add each request from requests array to collectionList and save
382 function saveRequests(requests){
383 requests.forEach(function (reqObj){
384 vm.collectionList.addRequestToList(reqObj);
385 vm.collectionList.saveToStorage();
386 $scope.rootBroadcast('YANGMAN_REFRESH_COLLECTIONS');
392 * Dialog for editing collection name (used for duplicating collection too)
395 * @param {boolean} duplicate
397 function showDgEditCollection(ev, collection, duplicate){
399 controller: EditCollectionDialogCtrl,
400 controllerAs: 'dialog',
401 templateUrl: $scope.globalViewPath + 'leftpanel/edit-collection-dialog.tpl.html',
402 parent: angular.element('#yangmanModule'),
404 clickOutsideToClose: true,
406 collection: collection,
407 allCollections: vm.collectionList.collections,
408 duplicate: duplicate,
410 }).then(duplicate ? duplicateCollection : changeCollectionName);
415 * @param {array} names 0. element is old name, 1. element is new name
417 function changeCollectionName(names){
418 vm.collectionList.renameCollection(names[0], names[1]);
419 vm.collectionList.saveToStorage();
420 refreshCollections();
424 * Create collection duplicate, save and refresh collections
425 * @param {array} names 0. element is old name, 1. element is new name
427 function duplicateCollection(names){
428 vm.collectionList.duplicateCollection(names[0], names[1]);
429 vm.collectionList.saveToStorage();
430 refreshCollections();
435 * @param list collectionList or requestList object
440 if (list === vm.requestList){
441 // saving from request header after execution
442 $scope.$on('YANGMAN_SAVE_EXECUTED_REQUEST', saveBcstedHistoryRequest);
443 // saving from request header
444 $scope.$on('YANGMAN_SAVE_REQUEST_TO_COLLECTION', saveRequestFromExt);
446 // saving collections expanded status on refresh
447 $scope.$on('YANGMAN_REFRESH_AND_EXPAND_COLLECTIONS', function(event, params){
448 $scope.rootBroadcast('YANGMAN_REFRESH_COLLECTIONS');
449 (params.cbk || angular.noop)();
456 * Loading history request and grouping by date
458 function loadHistoryRequests(){
459 vm.requestList.loadListFromStorage();
460 vm.requestList.groupListByDate();
464 * Loading collections
466 function loadCollectionRequest(){
467 vm.collectionList.loadListFromStorage();
471 * Loading both history and collections reqs
473 function loadRequests(){
474 loadHistoryRequests();
475 loadCollectionRequest();
479 * Request in list selection
480 * For history reqs it is possible multiselect, thats why event.ctrlKey is used
484 function selectRequest(event, requestObj){
485 vm.mainList.toggleReqSelection(!event.ctrlKey, requestObj);
486 $scope.setHistoryReqsSelected(vm.requestList.selectedRequests.length > 0);
490 * Deselect history requests
492 function deselectAllRequests(){
493 vm.mainList.deselectReqs();
497 * Select history requests
499 function selectAllRequests(){
500 deselectAllRequests();
501 vm.mainList.dateGroups.forEach(function(group){
502 vm.mainList.selectReqs(group.requests);
507 * Refresh and expand collections
509 function refreshCollections(){
510 var collectionNames = vm.collectionList.getExpandedCollectionNames();
511 $scope.rootBroadcast('YANGMAN_REFRESH_AND_EXPAND_COLLECTIONS', null, function(){
512 vm.collectionList.expandCollectionByNames(collectionNames);