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', 'YMHandleFileService', 'PathUtilsService', 'RequestsService', 'YangmanService',
12 'YangmanDesignService', 'constants',
16 * Controller for requests lists, means History requests and Collections requests
20 * @param YMHandleFileService
21 * @param PathUtilsService
22 * @param RequestsService
23 * @param YangmanService
24 * @param YangmanDesignService
27 function RequestsListCtrl($filter, $mdDialog, $scope, YMHandleFileService, PathUtilsService, RequestsService,
28 YangmanService, YangmanDesignService, constants) {
32 * List of all collections containing requests, loads even for history controller to use collection names
33 * in saving requests dialog
34 * @type {*|CollectionList}
36 vm.collectionList = null;
37 vm.constants = constants;
41 * @type {*|HistoryList}
43 vm.requestList = null;
45 vm.collectionsSortAsc = true;
49 vm.clearCollectionList = clearCollectionList;
50 vm.clearFilter = clearFilter;
51 vm.clearHistoryList = clearHistoryList;
52 vm.colMatchingReqsCount = colMatchingReqsCount;
53 vm.deselectAllFilteredRequests = deselectAllFilteredReqs;
54 vm.downloadCollection = downloadCollection;
55 vm.executeRequest = executeRequest;
56 vm.fakeFilter = fakeFilter;
57 vm.filterCol = filterCol;
58 vm.filterColName = filterColName;
59 vm.filterReq = filterReq;
61 vm.readCollectionFromFile = readCollectionFromFile;
62 vm.selectAllFilteredRequests = selectAllFilteredReqs;
63 vm.selectRequest = selectRequest;
64 vm.showData = showData;
65 vm.showDgDeleteCollection = showDgDeleteCollection;
66 vm.showDgDeleteRequests = showDgDeleteRequests;
67 vm.showDgEditCollection = showDgEditCollection;
68 vm.showDgSaveReq = showDgSaveReq;
69 vm.showForm = showForm;
70 vm.toggleCollectionsSort = toggleCollectionsSort;
71 vm.selectOnlyThisRequest = selectOnlyThisRequest;
72 vm.deselectAllRequests = deselectAllRequests;
73 vm.filterCollReq = filterCollReq;
76 * Save request obje to collection from other controller
79 function saveRequestFromExt(event, args) {
80 vm.showDgSaveReq(args.params.event, args.params.reqObj, false);
85 * Clear history requests list and save to storage
87 function clearHistoryList(event) {
89 YangmanDesignService.disableMdMenuItem(event);
91 var confirm = $mdDialog.confirm()
92 .title($filter('translate')('YANGMAN_DELETE_HISTORY_CONFIRM_TITLE'))
93 .textContent($filter('translate')('YANGMAN_DELETE_HISTORY_CONFIRM_TEXT'))
94 .ariaLabel($filter('translate')('YANGMAN_DELETE_HISTORY_CONFIRM_TITLE'))
96 .ok($filter('translate')('YANGMAN_OK'))
97 .cancel($filter('translate')('YANGMAN_CANCEL'));
99 $mdDialog.show(confirm).then(function (){
100 vm.requestList.clear();
101 vm.requestList.saveToStorage();
103 YangmanDesignService.enableMdMenuItem(event);
105 YangmanDesignService.enableMdMenuItem(event);
110 * Clear collections requests list and save to storage
112 function clearCollectionList(event) {
113 var confirm = $mdDialog.confirm()
114 .title($filter('translate')('YANGMAN_DELETE_COLLECTION_CONFIRM_TITLE'))
115 .textContent($filter('translate')('YANGMAN_DELETE_COLLECTION_CONFIRM_TEXT'))
116 .ariaLabel($filter('translate')('YANGMAN_DELETE_COLLECTION_CONFIRM_TITLE'))
118 .ok($filter('translate')('YANGMAN_OK'))
119 .cancel($filter('translate')('YANGMAN_CANCEL'));
121 YangmanDesignService.disableMdMenuItem(event);
123 $mdDialog.show(confirm).then(function (){
124 vm.collectionList.clear();
125 vm.collectionList.saveToStorage();
126 $scope.rootBroadcast(constants.YANGMAN_REFRESH_COLLECTIONS);
127 YangmanDesignService.enableMdMenuItem(event);
129 YangmanDesignService.enableMdMenuItem(event);
134 * Create history request from other ctrl
135 * @param broadcastEvent
138 function saveBcstedHistoryRequest(broadcastEvent, params) {
139 vm.requestList.addItemToList(params.params);
140 vm.requestList.saveToStorage();
141 (params.cbk || angular.noop)();
145 * Clear value of input file used to import collection
146 * todo: move to design utils
148 function clearFileInputValue(){
149 angular.element(document).find('#importCollection').val('');
153 * Importing collection from a file
154 * todo: error handling - msgs for user
155 * @param $fileContent
157 function readCollectionFromFile($fileContent) {
158 var data = $fileContent;
160 if (data && YangmanService.validateFile(data, constants.COLLECTION_CHECK_ARRAY)){
162 vm.collectionList.loadListFromFile(data);
163 vm.collectionList.saveToStorage();
164 $scope.rootBroadcast(constants.YANGMAN_REFRESH_COLLECTIONS);
165 clearFileInputValue();
168 clearFileInputValue();
169 console.error('DataStorage error:', e);
173 clearFileInputValue();
175 removeButtonBackground();
177 function removeButtonBackground() {
178 $('#importCollection').next().css({ 'background': 'transparent' });
182 function toggleCollectionsSort() {
183 vm.collectionsSortAsc = !vm.collectionsSortAsc;
187 * Export collection to json file
188 * @param {Collection} collection
190 function downloadCollection(collection) {
192 var cListJSON = vm.collectionList.getCollectionInRawJSON(collection.name);
194 YMHandleFileService.downloadFile(collection.name + '.json', cListJSON, 'json', 'charset=utf-8',
197 console.error('ExportCollection error:', e);
203 * Fill request form in right panel with request data
206 function showForm(reqObj) {
207 var data = reqObj.sentData;
209 // exception for get meth
210 if ( reqObj.method === constants.OPERATION_GET ) {
211 data = reqObj.receivedData;
214 $scope.rootBroadcast(
215 constants.YANGMAN_SET_CODEMIRROR_DATA_RECEIVED, { data: reqObj.setDataForView(reqObj.receivedData) }
217 $scope.rootBroadcast(
218 constants.YANGMAN_SET_CODEMIRROR_DATA_SENT, { data: reqObj.setDataForView(reqObj.sentData) }
221 $scope.rootBroadcast(constants.YANGMAN_SET_ERROR_DATA,
222 reqObj.receivedData && reqObj.receivedData.hasOwnProperty('errors') ? reqObj.receivedData : {});
224 $scope.rootBroadcast(constants.YANGMAN_FILL_NODE_FROM_REQ, { requestUrl: reqObj.path, requestData: data },
226 $scope.setRightPanelSection(constants.DISPLAY_TYPE_FORM);
227 $scope.rootBroadcast(constants.YANGMAN_HEADER_INIT, {
229 method: reqObj.method,
231 status: reqObj.responseStatus,
232 statusText: reqObj.responseStatusText,
233 time: reqObj.responseTime,
238 // prepare data for filling form
239 data = $scope.node.type === constants.NODE_RPC ?
240 YangmanService.prepareReceivedData(
245 constants.DISPLAY_TYPE_FORM
249 YangmanService.fillNodeFromResponse($scope.node, data);
250 $scope.node.expanded = true;
259 * Force request header to execute request with data from reqObj
262 function executeRequest(reqObj) {
264 $scope.rootBroadcast(constants.YANGMAN_EXECUTE_WITH_DATA,{ data: reqObj.sentData });
268 * Method for setup data into CM, Header, find api, subapi, node
272 function showData(reqObj, select){
275 method: reqObj.method,
278 // action select request
280 headerObj.statusObj = {
281 status: reqObj.responseStatus,
282 statusText: reqObj.responseStatusText,
283 time: reqObj.responseTime,
286 $scope.rootBroadcast(
287 constants.YANGMAN_SET_ERROR_DATA,
288 reqObj.receivedData && reqObj.receivedData.hasOwnProperty('errors') ? reqObj.receivedData : {}
292 $scope.setRightPanelSection(constants.DISPLAY_TYPE_REQ_DATA);
293 $scope.setJsonView(true, reqObj.method !== constants.OPERATION_GET);
295 $scope.rootBroadcast(constants.YANGMAN_HEADER_INIT, headerObj);
296 $scope.rootBroadcast(constants.YANGMAN_FILL_NODE_FROM_REQ, { requestUrl: reqObj.path });
298 $scope.rootBroadcast(
299 constants.YANGMAN_SET_CODEMIRROR_DATA_RECEIVED,
300 { data: reqObj.setDataForView(reqObj.receivedData) }
303 $scope.rootBroadcast(
304 constants.YANGMAN_SET_CODEMIRROR_DATA_SENT,
305 { data: reqObj.setDataForView(reqObj.sentData) }
310 * Clear current ctrl search value
312 function clearFilter(){
317 * Dialog for deleting either selected requests or reqObj
322 function showDgDeleteRequests(event, reqObj){
324 var confirm = $mdDialog.confirm()
325 .title($filter('translate')('YANGMAN_DELETE_REQ_CONFIRM_TITLE'))
326 .textContent($filter('translate')('YANGMAN_DELETE_REQ_CONFIRM_TEXT'))
327 .ariaLabel($filter('translate')('YANGMAN_DELETE_REQ_CONFIRM_TITLE'))
329 .ok($filter('translate')('YANGMAN_OK'))
330 .cancel($filter('translate')('YANGMAN_CANCEL'));
332 YangmanDesignService.disableMdMenuItem(event);
334 $mdDialog.show(confirm).then(function (){
336 vm.mainList.deleteRequestItem(reqObj);
339 vm.mainList.getSelectedItems(
340 vm.mainList === vm.collectionList ? filterCollReq : filterReq
341 ).forEach(function (elem){
342 vm.mainList.deleteRequestItem(elem);
345 vm.mainList.saveToStorage();
347 if (vm.mainList === vm.requestList) {
351 refreshCollectionsWithExpansion();
354 YangmanDesignService.enableMdMenuItem(event);
360 * Dialog for deleting collection and refreshing collections
364 function showDgDeleteCollection(ev, collObj){
365 var confirm = $mdDialog.confirm()
366 .title($filter('translate')('YANGMAN_DELETE_COL_CONFIRM_TITLE') + ' ' + collObj.name + '?')
367 .textContent($filter('translate')('YANGMAN_DELETE_COL_CONFIRM_TEXT'))
368 .ariaLabel($filter('translate')('YANGMAN_DELETE_COL_CONFIRM_TITLE'))
370 .ok($filter('translate')('YANGMAN_OK'))
371 .cancel($filter('translate')('YANGMAN_CANCEL'));
373 YangmanDesignService.disableMdMenuItem(ev);
375 $mdDialog.show(confirm).then(function (){
376 vm.collectionList.deleteCollection(collObj);
377 vm.collectionList.saveToStorage();
378 refreshCollectionsWithExpansion();
380 YangmanDesignService.enableMdMenuItem(ev);
385 * Check if reqObj matches current search value
389 function filterReq(reqObj){
390 var searchPhrase = vm.search.toLocaleLowerCase();
391 return reqObj.path.toLowerCase().indexOf(searchPhrase) > -1 ||
392 reqObj.collection.toLowerCase().indexOf(searchPhrase) > -1 ||
393 reqObj.method.toLowerCase() === searchPhrase;
397 * Check if collection name matches current search value or any collection req matches
400 function filterCol(colObj){
401 return filterColName(colObj) || colObj.data.some(filterReq);
405 * Get count of requests matching filter in collection colObj
409 function colMatchingReqsCount(colObj){
410 return colObj.data.filter(vm.filterReq).length;
414 * Check if collection name matches current filter
418 function filterColName(colObj){
419 return colObj.name.toLowerCase().indexOf(vm.search.toLowerCase()) > -1;
426 function fakeFilter(){
432 * Show dialog for saving reqObj to collection (used for duplicate req too)
437 function showDgSaveReq(ev, reqObj, duplicate){
440 controller: SaveReqDialogCtrl,
441 controllerAs: 'dialog',
442 templateUrl: $scope.globalViewPath + 'leftpanel/save-req-dialog.tpl.html',
443 parent: angular.element('#yangmanModule'),
445 clickOutsideToClose: true,
447 requests: reqObj ? [reqObj] : vm.mainList.getSelectedItems(
448 vm.mainList === vm.collectionList ? filterCollReq : filterReq
450 collectionNames: vm.collectionList.getCollectionNames(),
451 duplicate: duplicate || false,
453 }).then(saveRequests);
457 * Add each request from requests array to collectionList and save
460 function saveRequests(requests){
461 requests.forEach(function (reqObj){
462 vm.collectionList.addItemToList(RequestsService.clearUnnecessaryProperties(reqObj.clone()));
463 vm.collectionList.saveToStorage();
464 refreshCollectionsWithExpansion();
470 * Dialog for editing collection name (used for duplicating collection too)
473 * @param {boolean} duplicate
475 function showDgEditCollection(ev, collection, duplicate){
477 controller: EditCollectionDialogCtrl,
478 controllerAs: 'dialog',
479 templateUrl: $scope.globalViewPath + 'leftpanel/edit-collection-dialog.tpl.html',
480 parent: angular.element('#yangmanModule'),
482 clickOutsideToClose: true,
484 collection: collection,
485 allCollections: vm.collectionList.collections,
486 duplicate: duplicate,
488 }).then(duplicate ? duplicateCollection : changeCollectionName);
493 * @param {array} names 0. element is old name, 1. element is new name
495 function changeCollectionName(names){
496 vm.collectionList.renameCollection(names[0], names[1]);
497 vm.collectionList.saveToStorage();
498 refreshCollectionsWithExpansion();
502 * Create collection duplicate, save and refresh collections
503 * @param {array} names 0. element is old name, 1. element is new name
505 function duplicateCollection(names){
506 vm.collectionList.duplicateCollection(names[0], names[1]);
507 vm.collectionList.saveToStorage();
508 refreshCollectionsWithExpansion();
512 function selectNewestRequest() {
513 vm.mainList.toggleReqSelection(true, vm.mainList.getNewestRequest());
516 function loadCollectionsList() {
517 vm.collectionList.loadListFromStorage();
520 function loadHistoryList() {
521 vm.requestList.loadListFromStorage();
526 * @param mainList collectionList or requestList object
528 function init(mainList){
530 vm.collectionList = RequestsService.createEmptyCollectionList('yangman_collectionsList');
531 // collections are loaded for both history and collections tab
532 loadCollectionsList();
534 vm.requestList = RequestsService.createEmptyHistoryList('yangman_requestsList');
537 // if request was selected, deselect requests in all other instances of RequestsListCtrl
538 $scope.$on(constants.YANGMAN_DESELECT_REQUESTS, function (event, params) {
539 if (params.params.broadcastingCtrl !== vm) {
540 deselectAllRequests();
544 $scope.$on(constants.YANGMAN_REFRESH_COLLECTIONS, function (event, params){
545 loadCollectionsList();
546 (params.cbk || angular.noop)();
549 // list type dependend operations
550 if (mainList === 'history') {
552 vm.mainList = vm.requestList;
555 $scope.$on(constants.YANGMAN_REFRESH_HISTORY, loadHistoryList);
556 // saving from request header after execution
557 $scope.$on(constants.YANGMAN_SAVE_EXECUTED_REQUEST, saveBcstedHistoryRequest);
558 // select newest request
559 $scope.$on(constants.YANGMAN_SELECT_THE_NEWEST_REQUEST, selectNewestRequest);
562 vm.mainList = vm.collectionList;
563 // saving from request header
564 $scope.$on(constants.YANGMAN_SAVE_REQUEST_TO_COLLECTION, saveRequestFromExt);
565 // saving collections expanded status on refresh
566 $scope.$on(constants.YANGMAN_REFRESH_AND_EXPAND_COLLECTIONS, function(){
567 var expandedColNames = vm.collectionList.getExpandedCollectionNames();
568 $scope.rootBroadcast(constants.YANGMAN_REFRESH_COLLECTIONS, {}, function (){
569 vm.collectionList.expandCollectionByNames(expandedColNames);
578 * Request in list selection
579 * For history reqs it is possible multiselect, thats why event.ctrlKey is used
583 function selectRequest(event, requestObj){
584 $scope.rootBroadcast(constants.YANGMAN_DESELECT_REQUESTS, { broadcastingCtrl: vm });
585 vm.mainList.toggleReqSelection(!event.ctrlKey, requestObj);
587 if ($scope.rightPanelSection === constants.DISPLAY_TYPE_FORM) {
588 vm.showForm(requestObj, true);
591 vm.showData(requestObj, true);
597 * Mark only requestObj in current list as selected
598 * Used for example when user clicks on request submenu
601 function selectOnlyThisRequest(requestObj){
602 vm.mainList.toggleReqSelection(true, requestObj);
606 * Deselect history requests
608 function deselectAllFilteredReqs(){
609 vm.mainList.deselectAllFilteredItems(vm.mainList === vm.collectionList ? filterCollReq : vm.filterReq);
612 function deselectAllRequests() {
613 vm.mainList.deselectAllItems();
617 * Select history requests
619 function selectAllFilteredReqs(){
620 vm.mainList.selectAllFilteredItems(vm.mainList === vm.collectionList ? filterCollReq : vm.filterReq);
624 * Use when selecting filtered requests if they are saved to some collection
625 * Additional filter is if the collection of the request is expanded
627 * @returns {*|boolean}
629 function filterCollReq(request) {
630 return vm.collectionList.getCollection(request.collection).expanded && vm.filterReq(request);
634 * Refresh and expand collections
636 function refreshCollectionsWithExpansion(){
637 $scope.rootBroadcast(constants.YANGMAN_REFRESH_AND_EXPAND_COLLECTIONS);