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) {
263 if ($scope.rightPanelSection === constants.DISPLAY_TYPE_FORM) {
269 $scope.rootBroadcast(constants.YANGMAN_EXECUTE_WITH_DATA,{ data: reqObj.sentData });
273 * Method for setup data into CM, Header, find api, subapi, node
277 function showData(reqObj, select){
280 method: reqObj.method,
283 // action select request
285 headerObj.statusObj = {
286 status: reqObj.responseStatus,
287 statusText: reqObj.responseStatusText,
288 time: reqObj.responseTime,
291 $scope.rootBroadcast(
292 constants.YANGMAN_SET_ERROR_DATA,
293 reqObj.receivedData && reqObj.receivedData.hasOwnProperty('errors') ? reqObj.receivedData : {}
297 $scope.setRightPanelSection(constants.DISPLAY_TYPE_REQ_DATA);
298 $scope.setJsonView(true, reqObj.method !== constants.OPERATION_GET);
300 $scope.rootBroadcast(constants.YANGMAN_HEADER_INIT, headerObj);
301 $scope.rootBroadcast(constants.YANGMAN_FILL_NODE_FROM_REQ, { requestUrl: reqObj.path });
303 $scope.rootBroadcast(
304 constants.YANGMAN_SET_CODEMIRROR_DATA_RECEIVED,
305 { data: reqObj.setDataForView(reqObj.receivedData) }
308 $scope.rootBroadcast(
309 constants.YANGMAN_SET_CODEMIRROR_DATA_SENT,
310 { data: reqObj.setDataForView(reqObj.sentData) }
315 * Clear current ctrl search value
317 function clearFilter(){
322 * Dialog for deleting either selected requests or reqObj
327 function showDgDeleteRequests(event, reqObj){
329 var confirm = $mdDialog.confirm()
330 .title($filter('translate')('YANGMAN_DELETE_REQ_CONFIRM_TITLE'))
331 .textContent($filter('translate')('YANGMAN_DELETE_REQ_CONFIRM_TEXT'))
332 .ariaLabel($filter('translate')('YANGMAN_DELETE_REQ_CONFIRM_TITLE'))
334 .ok($filter('translate')('YANGMAN_OK'))
335 .cancel($filter('translate')('YANGMAN_CANCEL'));
337 YangmanDesignService.disableMdMenuItem(event);
339 $mdDialog.show(confirm).then(function (){
341 vm.mainList.deleteRequestItem(reqObj);
344 vm.mainList.getSelectedItems(
345 vm.mainList === vm.collectionList ? filterCollReq : filterReq
346 ).forEach(function (elem){
347 vm.mainList.deleteRequestItem(elem);
350 vm.mainList.saveToStorage();
352 if (vm.mainList === vm.requestList) {
356 refreshCollectionsWithExpansion();
359 YangmanDesignService.enableMdMenuItem(event);
365 * Dialog for deleting collection and refreshing collections
369 function showDgDeleteCollection(ev, collObj){
370 var confirm = $mdDialog.confirm()
371 .title($filter('translate')('YANGMAN_DELETE_COL_CONFIRM_TITLE') + ' ' + collObj.name + '?')
372 .textContent($filter('translate')('YANGMAN_DELETE_COL_CONFIRM_TEXT'))
373 .ariaLabel($filter('translate')('YANGMAN_DELETE_COL_CONFIRM_TITLE'))
375 .ok($filter('translate')('YANGMAN_OK'))
376 .cancel($filter('translate')('YANGMAN_CANCEL'));
378 YangmanDesignService.disableMdMenuItem(ev);
380 $mdDialog.show(confirm).then(function (){
381 vm.collectionList.deleteCollection(collObj);
382 vm.collectionList.saveToStorage();
383 refreshCollectionsWithExpansion();
385 YangmanDesignService.enableMdMenuItem(ev);
390 * Check if reqObj matches current search value
394 function filterReq(reqObj){
395 var searchPhrase = vm.search.toLocaleLowerCase();
396 return reqObj.path.toLowerCase().indexOf(searchPhrase) > -1 ||
397 reqObj.collection.toLowerCase().indexOf(searchPhrase) > -1 ||
398 reqObj.method.toLowerCase() === searchPhrase;
402 * Check if collection name matches current search value or any collection req matches
405 function filterCol(colObj){
406 return filterColName(colObj) || colObj.data.some(filterReq);
410 * Get count of requests matching filter in collection colObj
414 function colMatchingReqsCount(colObj){
415 return colObj.data.filter(vm.filterReq).length;
419 * Check if collection name matches current filter
423 function filterColName(colObj){
424 return colObj.name.toLowerCase().indexOf(vm.search.toLowerCase()) > -1;
431 function fakeFilter(){
437 * Show dialog for saving reqObj to collection (used for duplicate req too)
442 function showDgSaveReq(ev, reqObj, duplicate){
445 controller: SaveReqDialogCtrl,
446 controllerAs: 'dialog',
447 templateUrl: $scope.globalViewPath + 'leftpanel/save-req-dialog.tpl.html',
448 parent: angular.element('#yangmanModule'),
450 clickOutsideToClose: true,
452 requests: reqObj ? [reqObj] : vm.mainList.getSelectedItems(
453 vm.mainList === vm.collectionList ? filterCollReq : filterReq
455 collectionNames: vm.collectionList.getCollectionNames(),
456 duplicate: duplicate || false,
458 }).then(saveRequests);
462 * Add each request from requests array to collectionList and save
465 function saveRequests(requests){
466 requests.forEach(function (reqObj){
467 vm.collectionList.addItemToList(RequestsService.clearUnnecessaryProperties(reqObj.clone()));
468 vm.collectionList.saveToStorage();
469 refreshCollectionsWithExpansion();
475 * Dialog for editing collection name (used for duplicating collection too)
478 * @param {boolean} duplicate
480 function showDgEditCollection(ev, collection, duplicate){
482 controller: EditCollectionDialogCtrl,
483 controllerAs: 'dialog',
484 templateUrl: $scope.globalViewPath + 'leftpanel/edit-collection-dialog.tpl.html',
485 parent: angular.element('#yangmanModule'),
487 clickOutsideToClose: true,
489 collection: collection,
490 allCollections: vm.collectionList.collections,
491 duplicate: duplicate,
493 }).then(duplicate ? duplicateCollection : changeCollectionName);
498 * @param {array} names 0. element is old name, 1. element is new name
500 function changeCollectionName(names){
501 vm.collectionList.renameCollection(names[0], names[1]);
502 vm.collectionList.saveToStorage();
503 refreshCollectionsWithExpansion();
507 * Create collection duplicate, save and refresh collections
508 * @param {array} names 0. element is old name, 1. element is new name
510 function duplicateCollection(names){
511 vm.collectionList.duplicateCollection(names[0], names[1]);
512 vm.collectionList.saveToStorage();
513 refreshCollectionsWithExpansion();
517 function selectNewestRequest() {
518 vm.mainList.toggleReqSelection(true, vm.mainList.getNewestRequest());
521 function loadCollectionsList() {
522 vm.collectionList.loadListFromStorage();
525 function loadHistoryList() {
526 vm.requestList.loadListFromStorage();
531 * @param mainList collectionList or requestList object
533 function init(mainList){
535 vm.collectionList = RequestsService.createEmptyCollectionList('yangman_collectionsList');
536 // collections are loaded for both history and collections tab
537 loadCollectionsList();
539 vm.requestList = RequestsService.createEmptyHistoryList('yangman_requestsList');
542 // if request was selected, deselect requests in all other instances of RequestsListCtrl
543 $scope.$on(constants.YANGMAN_DESELECT_REQUESTS, function (event, params) {
544 if (params.params.broadcastingCtrl !== vm) {
545 deselectAllRequests();
549 $scope.$on(constants.YANGMAN_REFRESH_COLLECTIONS, function (event, params){
550 loadCollectionsList();
551 (params.cbk || angular.noop)();
554 // list type dependend operations
555 if (mainList === 'history') {
557 vm.mainList = vm.requestList;
560 $scope.$on(constants.YANGMAN_REFRESH_HISTORY, loadHistoryList);
561 // saving from request header after execution
562 $scope.$on(constants.YANGMAN_SAVE_EXECUTED_REQUEST, saveBcstedHistoryRequest);
563 // select newest request
564 $scope.$on(constants.YANGMAN_SELECT_THE_NEWEST_REQUEST, selectNewestRequest);
567 vm.mainList = vm.collectionList;
568 // saving from request header
569 $scope.$on(constants.YANGMAN_SAVE_REQUEST_TO_COLLECTION, saveRequestFromExt);
570 // saving collections expanded status on refresh
571 $scope.$on(constants.YANGMAN_REFRESH_AND_EXPAND_COLLECTIONS, function(){
572 var expandedColNames = vm.collectionList.getExpandedCollectionNames();
573 $scope.rootBroadcast(constants.YANGMAN_REFRESH_COLLECTIONS, {}, function (){
574 vm.collectionList.expandCollectionByNames(expandedColNames);
583 * Request in list selection
584 * For history reqs it is possible multiselect, thats why event.ctrlKey is used
588 function selectRequest(event, requestObj){
589 $scope.rootBroadcast(constants.YANGMAN_DESELECT_REQUESTS, { broadcastingCtrl: vm });
590 vm.mainList.toggleReqSelection(!event.ctrlKey, requestObj);
592 if ($scope.rightPanelSection === constants.DISPLAY_TYPE_FORM) {
593 vm.showForm(requestObj, true);
596 vm.showData(requestObj, true);
602 * Mark only requestObj in current list as selected
603 * Used for example when user clicks on request submenu
606 function selectOnlyThisRequest(requestObj){
607 vm.mainList.toggleReqSelection(true, requestObj);
611 * Deselect history requests
613 function deselectAllFilteredReqs(){
614 vm.mainList.deselectAllFilteredItems(vm.mainList === vm.collectionList ? filterCollReq : vm.filterReq);
617 function deselectAllRequests() {
618 vm.mainList.deselectAllItems();
622 * Select history requests
624 function selectAllFilteredReqs(){
625 vm.mainList.selectAllFilteredItems(vm.mainList === vm.collectionList ? filterCollReq : vm.filterReq);
629 * Use when selecting filtered requests if they are saved to some collection
630 * Additional filter is if the collection of the request is expanded
632 * @returns {*|boolean}
634 function filterCollReq(request) {
635 return vm.collectionList.getCollection(request.collection).expanded && vm.filterReq(request);
639 * Refresh and expand collections
641 function refreshCollectionsWithExpansion(){
642 $scope.rootBroadcast(constants.YANGMAN_REFRESH_AND_EXPAND_COLLECTIONS);