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();
142 (params.cbk || angular.noop)();
146 * Clear value of input file used to import collection
147 * todo: move to design utils
149 function clearFileInputValue(){
150 angular.element(document).find('#importCollection').val('');
154 * Importing collection from a file
155 * todo: error handling - msgs for user
156 * @param $fileContent
158 function readCollectionFromFile($fileContent) {
159 var data = $fileContent;
161 if (data && YangmanService.validateFile(data, constants.COLLECTION_CHECK_ARRAY)){
163 vm.collectionList.loadListFromFile(data);
164 vm.collectionList.saveToStorage();
165 $scope.rootBroadcast(constants.YANGMAN_REFRESH_COLLECTIONS);
166 clearFileInputValue();
169 clearFileInputValue();
170 console.error('DataStorage error:', e);
174 clearFileInputValue();
176 removeButtonBackground();
178 function removeButtonBackground() {
179 $('#importCollection').next().css({ 'background': 'transparent' });
183 function toggleCollectionsSort() {
184 vm.collectionsSortAsc = !vm.collectionsSortAsc;
188 * Export collection to json file
189 * @param {Collection} collection
191 function downloadCollection(collection) {
193 var cListJSON = vm.collectionList.getCollectionInRawJSON(collection.name);
195 YMHandleFileService.downloadFile(collection.name + '.json', cListJSON, 'json', 'charset=utf-8',
198 console.error('ExportCollection error:', e);
204 * Fill request form in right panel with request data
207 function showForm(reqObj) {
208 var data = reqObj.sentData;
210 // exception for get meth
211 if ( reqObj.method === constants.OPERATION_GET ) {
212 data = reqObj.receivedData;
215 $scope.rootBroadcast(
216 constants.YANGMAN_SET_CODEMIRROR_DATA_RECEIVED, { data: reqObj.setDataForView(reqObj.receivedData) }
218 $scope.rootBroadcast(
219 constants.YANGMAN_SET_CODEMIRROR_DATA_SENT, { data: reqObj.setDataForView(reqObj.sentData) }
222 $scope.rootBroadcast(constants.YANGMAN_SET_ERROR_DATA,
223 reqObj.receivedData && reqObj.receivedData.hasOwnProperty('errors') ? reqObj.receivedData : {});
225 $scope.rootBroadcast(constants.YANGMAN_FILL_NODE_FROM_REQ, { requestUrl: reqObj.path, requestData: data },
227 $scope.setRightPanelSection(constants.DISPLAY_TYPE_FORM);
228 $scope.rootBroadcast(constants.YANGMAN_HEADER_INIT, {
230 method: reqObj.method,
232 status: reqObj.responseStatus,
233 statusText: reqObj.responseStatusText,
234 time: reqObj.responseTime,
239 // prepare data for filling form
240 data = $scope.node.type === constants.NODE_RPC ?
241 YangmanService.prepareReceivedData(
246 constants.DISPLAY_TYPE_FORM
250 YangmanService.fillNodeFromResponse($scope.node, data);
251 $scope.node.expanded = true;
260 * Force request header to execute request with data from reqObj
263 function executeRequest(reqObj) {
265 $scope.rootBroadcast(constants.YANGMAN_EXECUTE_WITH_DATA,{ data: reqObj.sentData });
269 * Method for setup data into CM, Header, find api, subapi, node
273 function showData(reqObj, select){
276 method: reqObj.method,
279 // action select request
281 headerObj.statusObj = {
282 status: reqObj.responseStatus,
283 statusText: reqObj.responseStatusText,
284 time: reqObj.responseTime,
287 $scope.rootBroadcast(
288 constants.YANGMAN_SET_ERROR_DATA,
289 reqObj.receivedData && reqObj.receivedData.hasOwnProperty('errors') ? reqObj.receivedData : {}
293 $scope.setRightPanelSection(constants.DISPLAY_TYPE_REQ_DATA);
294 $scope.setJsonView(true, reqObj.method !== constants.OPERATION_GET);
296 $scope.rootBroadcast(constants.YANGMAN_HEADER_INIT, headerObj);
297 $scope.rootBroadcast(constants.YANGMAN_FILL_NODE_FROM_REQ, { requestUrl: reqObj.path });
299 $scope.rootBroadcast(
300 constants.YANGMAN_SET_CODEMIRROR_DATA_RECEIVED,
301 { data: reqObj.setDataForView(reqObj.receivedData) }
304 $scope.rootBroadcast(
305 constants.YANGMAN_SET_CODEMIRROR_DATA_SENT,
306 { data: reqObj.setDataForView(reqObj.sentData) }
311 * Clear current ctrl search value
313 function clearFilter(){
318 * Dialog for deleting either selected requests or reqObj
323 function showDgDeleteRequests(event, reqObj){
325 var confirm = $mdDialog.confirm()
326 .title($filter('translate')('YANGMAN_DELETE_REQ_CONFIRM_TITLE'))
327 .textContent($filter('translate')('YANGMAN_DELETE_REQ_CONFIRM_TEXT'))
328 .ariaLabel($filter('translate')('YANGMAN_DELETE_REQ_CONFIRM_TITLE'))
330 .ok($filter('translate')('YANGMAN_OK'))
331 .cancel($filter('translate')('YANGMAN_CANCEL'));
333 YangmanDesignService.disableMdMenuItem(event);
335 $mdDialog.show(confirm).then(function (){
337 vm.mainList.deleteRequestItem(reqObj);
340 vm.mainList.getSelectedItems(
341 vm.mainList === vm.collectionList ? filterCollReq : filterReq
342 ).forEach(function (elem){
343 vm.mainList.deleteRequestItem(elem);
346 vm.mainList.saveToStorage();
348 if (vm.mainList === vm.requestList) {
352 refreshCollectionsWithExpansion();
355 YangmanDesignService.enableMdMenuItem(event);
361 * Dialog for deleting collection and refreshing collections
365 function showDgDeleteCollection(ev, collObj){
366 var confirm = $mdDialog.confirm()
367 .title($filter('translate')('YANGMAN_DELETE_COL_CONFIRM_TITLE') + ' ' + collObj.name + '?')
368 .textContent($filter('translate')('YANGMAN_DELETE_COL_CONFIRM_TEXT'))
369 .ariaLabel($filter('translate')('YANGMAN_DELETE_COL_CONFIRM_TITLE'))
371 .ok($filter('translate')('YANGMAN_OK'))
372 .cancel($filter('translate')('YANGMAN_CANCEL'));
374 YangmanDesignService.disableMdMenuItem(ev);
376 $mdDialog.show(confirm).then(function (){
377 vm.collectionList.deleteCollection(collObj);
378 vm.collectionList.saveToStorage();
379 refreshCollectionsWithExpansion();
381 YangmanDesignService.enableMdMenuItem(ev);
386 * Check if reqObj matches current search value
390 function filterReq(reqObj){
391 var searchPhrase = vm.search.toLocaleLowerCase();
392 return reqObj.path.toLowerCase().indexOf(searchPhrase) > -1 ||
393 reqObj.collection.toLowerCase().indexOf(searchPhrase) > -1 ||
394 reqObj.method.toLowerCase() === searchPhrase;
398 * Check if collection name matches current search value or any collection req matches
401 function filterCol(colObj){
402 return filterColName(colObj) || colObj.data.some(filterReq);
406 * Get count of requests matching filter in collection colObj
410 function colMatchingReqsCount(colObj){
411 return colObj.data.filter(vm.filterReq).length;
415 * Check if collection name matches current filter
419 function filterColName(colObj){
420 return colObj.name.toLowerCase().indexOf(vm.search.toLowerCase()) > -1;
427 function fakeFilter(){
433 * Show dialog for saving reqObj to collection (used for duplicate req too)
438 function showDgSaveReq(ev, reqObj, duplicate){
441 controller: SaveReqDialogCtrl,
442 controllerAs: 'dialog',
443 templateUrl: $scope.globalViewPath + 'leftpanel/save-req-dialog.tpl.html',
444 parent: angular.element('#yangmanModule'),
446 clickOutsideToClose: true,
448 requests: reqObj ? [reqObj] : vm.mainList.getSelectedItems(
449 vm.mainList === vm.collectionList ? filterCollReq : filterReq
451 collectionNames: vm.collectionList.getCollectionNames(),
452 duplicate: duplicate || false,
454 }).then(saveRequests);
458 * Add each request from requests array to collectionList and save
461 function saveRequests(requests){
462 requests.forEach(function (reqObj){
463 vm.collectionList.addItemToList(RequestsService.clearUnnecessaryProperties(reqObj.clone()));
464 vm.collectionList.saveToStorage();
465 refreshCollectionsWithExpansion();
471 * Dialog for editing collection name (used for duplicating collection too)
474 * @param {boolean} duplicate
476 function showDgEditCollection(ev, collection, duplicate){
478 controller: EditCollectionDialogCtrl,
479 controllerAs: 'dialog',
480 templateUrl: $scope.globalViewPath + 'leftpanel/edit-collection-dialog.tpl.html',
481 parent: angular.element('#yangmanModule'),
483 clickOutsideToClose: true,
485 collection: collection,
486 allCollections: vm.collectionList.collections,
487 duplicate: duplicate,
489 }).then(duplicate ? duplicateCollection : changeCollectionName);
494 * @param {array} names 0. element is old name, 1. element is new name
496 function changeCollectionName(names){
497 vm.collectionList.renameCollection(names[0], names[1]);
498 vm.collectionList.saveToStorage();
499 refreshCollectionsWithExpansion();
503 * Create collection duplicate, save and refresh collections
504 * @param {array} names 0. element is old name, 1. element is new name
506 function duplicateCollection(names){
507 vm.collectionList.duplicateCollection(names[0], names[1]);
508 vm.collectionList.saveToStorage();
509 refreshCollectionsWithExpansion();
513 function selectNewestRequest() {
514 vm.mainList.toggleReqSelection(true, vm.mainList.getNewestRequest());
517 function loadCollectionsList() {
518 vm.collectionList.loadListFromStorage();
521 function loadHistoryList() {
522 vm.requestList.loadListFromStorage();
527 * @param mainList collectionList or requestList object
529 function init(mainList){
531 vm.collectionList = RequestsService.createEmptyCollectionList('yangman_collectionsList');
532 // collections are loaded for both history and collections tab
533 loadCollectionsList();
535 vm.requestList = RequestsService.createEmptyHistoryList('yangman_requestsList');
538 // if request was selected, deselect requests in all other instances of RequestsListCtrl
539 $scope.$on(constants.YANGMAN_DESELECT_REQUESTS, function (event, params) {
540 if (params.params.broadcastingCtrl !== vm) {
541 deselectAllRequests();
545 $scope.$on(constants.YANGMAN_REFRESH_COLLECTIONS, function (event, params){
546 loadCollectionsList();
547 (params.cbk || angular.noop)();
550 // list type dependend operations
551 if (mainList === 'history') {
553 vm.mainList = vm.requestList;
556 $scope.$on(constants.YANGMAN_REFRESH_HISTORY, loadHistoryList);
557 // saving from request header after execution
558 $scope.$on(constants.YANGMAN_SAVE_EXECUTED_REQUEST, saveBcstedHistoryRequest);
559 // select newest request
560 $scope.$on(constants.YANGMAN_SELECT_THE_NEWEST_REQUEST, selectNewestRequest);
563 vm.mainList = vm.collectionList;
564 // saving from request header
565 $scope.$on(constants.YANGMAN_SAVE_REQUEST_TO_COLLECTION, saveRequestFromExt);
566 // saving collections expanded status on refresh
567 $scope.$on(constants.YANGMAN_REFRESH_AND_EXPAND_COLLECTIONS, function(){
568 var expandedColNames = vm.collectionList.getExpandedCollectionNames();
569 $scope.rootBroadcast(constants.YANGMAN_REFRESH_COLLECTIONS, {}, function (){
570 vm.collectionList.expandCollectionByNames(expandedColNames);
579 * Request in list selection
580 * For history reqs it is possible multiselect, thats why event.ctrlKey is used
584 function selectRequest(event, requestObj){
585 $scope.rootBroadcast(constants.YANGMAN_DESELECT_REQUESTS, { broadcastingCtrl: vm });
586 vm.mainList.toggleReqSelection(!event.ctrlKey, requestObj);
588 vm.showData(requestObj, true);
593 * Mark only requestObj in current list as selected
594 * Used for example when user clicks on request submenu
597 function selectOnlyThisRequest(requestObj){
598 vm.mainList.toggleReqSelection(true, requestObj);
602 * Deselect history requests
604 function deselectAllFilteredReqs(){
605 vm.mainList.deselectAllFilteredItems(vm.mainList === vm.collectionList ? filterCollReq : vm.filterReq);
608 function deselectAllRequests() {
609 vm.mainList.deselectAllItems();
613 * Select history requests
615 function selectAllFilteredReqs(){
616 vm.mainList.selectAllFilteredItems(vm.mainList === vm.collectionList ? filterCollReq : vm.filterReq);
620 * Use when selecting filtered requests if they are saved to some collection
621 * Additional filter is if the collection of the request is expanded
623 * @returns {*|boolean}
625 function filterCollReq(request) {
626 return vm.collectionList.getCollection(request.collection).expanded && vm.filterReq(request);
630 * Refresh and expand collections
632 function refreshCollectionsWithExpansion(){
633 $scope.rootBroadcast(constants.YANGMAN_REFRESH_AND_EXPAND_COLLECTIONS);