2 'app/yangman/controllers/save-req-dialog.controller',
3 'app/yangman/controllers/edit-collection-dialog.controller',
4 'app/yangman/controllers/history-settings.controller',
5 'app/yangman/services/handle-file.services',
6 ], function (SaveReqDialogCtrl, EditCollectionDialogCtrl, HistorySettingsCtrl) {
9 angular.module('app.yangman').controller('RequestsListCtrl', RequestsListCtrl);
11 RequestsListCtrl.$inject = [
12 '$filter', '$mdDialog', '$scope', 'YMHandleFileService', 'PathUtilsService', 'RequestsService', 'YangmanService',
13 'YangmanDesignService', 'constants',
17 * Controller for requests lists, means History requests and Collections requests
21 * @param YMHandleFileService
22 * @param PathUtilsService
23 * @param RequestsService
24 * @param YangmanService
25 * @param YangmanDesignService
28 function RequestsListCtrl($filter, $mdDialog, $scope, YMHandleFileService, PathUtilsService, RequestsService,
29 YangmanService, YangmanDesignService, constants) {
32 vm.collectionList = null;
33 vm.constants = constants;
35 vm.requestList = null;
37 vm.collectionsSortAsc = true;
40 vm.clearCollectionList = clearCollectionList;
41 vm.clearFilter = clearFilter;
42 vm.clearHistoryList = clearHistoryList;
43 vm.colMatchingReqsCount = colMatchingReqsCount;
44 vm.deselectAllFilteredRequests = deselectAllFilteredReqs;
45 vm.downloadCollection = downloadCollection;
46 vm.executeRequest = executeRequest;
47 vm.fakeFilter = fakeFilter;
48 vm.filterCol = filterCol;
49 vm.filterColName = filterColName;
50 vm.filterReq = filterReq;
52 vm.readCollectionFromFile = readCollectionFromFile;
53 vm.selectAllFilteredRequests = selectAllFilteredReqs;
54 vm.selectRequest = selectRequest;
55 vm.showData = showData;
56 vm.showDgDeleteCollection = showDgDeleteCollection;
57 vm.showDgDeleteRequests = showDgDeleteRequests;
58 vm.showDgEditCollection = showDgEditCollection;
59 vm.showDgSaveReq = showDgSaveReq;
60 vm.showHistorySettings = showHistorySettings;
61 vm.showForm = showForm;
62 vm.toggleCollectionsSort = toggleCollectionsSort;
63 vm.selectOnlyThisRequest = selectOnlyThisRequest;
64 vm.deselectAllRequests = deselectAllRequests;
65 vm.filterCollReq = filterCollReq;
68 * Save request obje to collection from other controller
71 function saveRequestFromExt(event, args) {
72 vm.showDgSaveReq(args.params.event, args.params.reqObj, false);
77 * Clear history requests list and save to storage
79 function clearHistoryList(event) {
81 YangmanDesignService.disableMdMenuItem(event);
83 var confirm = $mdDialog.confirm()
84 .title($filter('translate')('YANGMAN_DELETE_HISTORY_CONFIRM_TITLE'))
85 .textContent($filter('translate')('YANGMAN_DELETE_HISTORY_CONFIRM_TEXT'))
86 .ariaLabel($filter('translate')('YANGMAN_DELETE_HISTORY_CONFIRM_TITLE'))
88 .ok($filter('translate')('YANGMAN_OK'))
89 .cancel($filter('translate')('YANGMAN_CANCEL'));
91 $mdDialog.show(confirm).then(function (){
92 vm.requestList.clear();
93 vm.requestList.saveToStorage();
95 YangmanDesignService.enableMdMenuItem(event);
97 YangmanDesignService.enableMdMenuItem(event);
102 * Clear collections requests list and save to storage
104 function clearCollectionList(event) {
105 var confirm = $mdDialog.confirm()
106 .title($filter('translate')('YANGMAN_DELETE_COLLECTION_CONFIRM_TITLE'))
107 .textContent($filter('translate')('YANGMAN_DELETE_COLLECTION_CONFIRM_TEXT'))
108 .ariaLabel($filter('translate')('YANGMAN_DELETE_COLLECTION_CONFIRM_TITLE'))
110 .ok($filter('translate')('YANGMAN_OK'))
111 .cancel($filter('translate')('YANGMAN_CANCEL'));
113 YangmanDesignService.disableMdMenuItem(event);
115 $mdDialog.show(confirm).then(function (){
116 vm.collectionList.clear();
117 vm.collectionList.saveToStorage();
118 $scope.rootBroadcast(constants.YANGMAN_REFRESH_COLLECTIONS);
119 YangmanDesignService.enableMdMenuItem(event);
121 YangmanDesignService.enableMdMenuItem(event);
126 * Create history request from other ctrl
127 * @param broadcastEvent
130 function saveBcstedHistoryRequest(broadcastEvent, params) {
131 vm.requestList.addItemToList(params.params);
132 vm.requestList.saveToStorage();
133 (params.cbk || angular.noop)();
137 * Clear value of input file used to import collection
139 function clearFileInputValue(){
140 angular.element(document).find('#importCollection').val('');
144 * Importing collection from a file
145 * @param $fileContent
147 function readCollectionFromFile($fileContent) {
148 var data = $fileContent;
150 if (data && YangmanService.validateFile(data, constants.COLLECTION_CHECK_ARRAY)){
152 vm.collectionList.loadListFromFile(data);
153 vm.collectionList.saveToStorage();
154 $scope.rootBroadcast(constants.YANGMAN_REFRESH_COLLECTIONS);
155 clearFileInputValue();
158 clearFileInputValue();
159 console.error('DataStorage error:', e);
163 clearFileInputValue();
165 removeButtonBackground();
167 function removeButtonBackground() {
168 $('#importCollection').next().css({ 'background': 'transparent' });
172 function toggleCollectionsSort() {
173 vm.collectionsSortAsc = !vm.collectionsSortAsc;
177 * Export collection to json file
178 * @param {Collection} collection
180 function downloadCollection(collection) {
182 var cListJSON = vm.collectionList.getCollectionInRawJSON(collection.name);
184 YMHandleFileService.downloadFile(collection.name + '.json', cListJSON, 'json', 'charset=utf-8',
187 console.error('ExportCollection error:', e);
193 * Fill request form in right panel with request data
196 function showForm(reqObj) {
197 var data = reqObj.sentData;
199 if ($scope.historySettings.data.fillWithReceived) {
200 data = reqObj.receivedData;
203 $scope.rootBroadcast(
204 constants.YANGMAN_SET_CODEMIRROR_DATA_RECEIVED, { data: reqObj.setDataForView(reqObj.receivedData) }
206 $scope.rootBroadcast(
207 constants.YANGMAN_SET_CODEMIRROR_DATA_SENT, { data: reqObj.setDataForView(reqObj.sentData) }
210 $scope.rootBroadcast(constants.YANGMAN_SET_ERROR_DATA,
211 reqObj.receivedData && reqObj.receivedData.hasOwnProperty('errors') ? reqObj.receivedData : {});
213 $scope.rootBroadcast(constants.YANGMAN_FILL_NODE_FROM_REQ, { requestUrl: reqObj.path, requestData: data },
215 $scope.setRightPanelSection(constants.DISPLAY_TYPE_FORM);
216 $scope.rootBroadcast(constants.YANGMAN_HEADER_INIT, {
218 method: reqObj.method,
220 status: reqObj.responseStatus,
221 statusText: reqObj.responseStatusText,
222 time: reqObj.responseTime,
227 // prepare data for filling form
228 if ($scope.node.type === constants.NODE_RPC && $scope.historySettings.data.fillWithReceived) {
229 data = YangmanService.prepareReceivedData(
234 constants.DISPLAY_TYPE_FORM
239 YangmanService.fillNodeFromResponse($scope.node, data);
240 $scope.node.expanded = true;
249 * Force request header to execute request with data from reqObj
252 function executeRequest(reqObj) {
253 if ($scope.rightPanelSection === constants.DISPLAY_TYPE_FORM) {
259 $scope.rootBroadcast(constants.YANGMAN_EXECUTE_WITH_DATA,{ data: reqObj.sentData });
263 * Method for setup data into CM, Header, find api, subapi, node
267 function showData(reqObj, select){
270 method: reqObj.method,
273 // action select request
275 headerObj.statusObj = {
276 status: reqObj.responseStatus,
277 statusText: reqObj.responseStatusText,
278 time: reqObj.responseTime,
281 $scope.rootBroadcast(
282 constants.YANGMAN_SET_ERROR_DATA,
283 reqObj.receivedData && reqObj.receivedData.hasOwnProperty('errors') ? reqObj.receivedData : {}
287 $scope.setRightPanelSection(constants.DISPLAY_TYPE_REQ_DATA);
288 $scope.setJsonView(true, reqObj.method !== constants.OPERATION_GET);
290 $scope.rootBroadcast(constants.YANGMAN_HEADER_INIT, headerObj);
291 $scope.rootBroadcast(constants.YANGMAN_FILL_NODE_FROM_REQ, { requestUrl: reqObj.path });
293 $scope.rootBroadcast(
294 constants.YANGMAN_SET_CODEMIRROR_DATA_RECEIVED,
295 { data: reqObj.setDataForView(reqObj.receivedData) }
298 $scope.rootBroadcast(
299 constants.YANGMAN_SET_CODEMIRROR_DATA_SENT,
300 { data: reqObj.setDataForView(reqObj.sentData) }
305 * Clear current ctrl search value
307 function clearFilter(){
312 * Dialog for deleting either selected requests or reqObj
317 function showDgDeleteRequests(event, reqObj){
319 var confirm = $mdDialog.confirm()
320 .title($filter('translate')('YANGMAN_DELETE_REQ_CONFIRM_TITLE'))
321 .textContent($filter('translate')('YANGMAN_DELETE_REQ_CONFIRM_TEXT'))
322 .ariaLabel($filter('translate')('YANGMAN_DELETE_REQ_CONFIRM_TITLE'))
324 .ok($filter('translate')('YANGMAN_OK'))
325 .cancel($filter('translate')('YANGMAN_CANCEL'));
327 YangmanDesignService.disableMdMenuItem(event);
329 $mdDialog.show(confirm).then(function (){
331 vm.mainList.deleteRequestItem(reqObj);
334 vm.mainList.getSelectedItems(
335 vm.mainList === vm.collectionList ? filterCollReq : filterReq
336 ).forEach(function (elem){
337 vm.mainList.deleteRequestItem(elem);
340 vm.mainList.saveToStorage();
342 if (vm.mainList === vm.requestList) {
346 refreshCollectionsWithExpansion();
349 YangmanDesignService.enableMdMenuItem(event);
355 * Dialog for deleting collection and refreshing collections
359 function showDgDeleteCollection(ev, collObj){
360 var confirm = $mdDialog.confirm()
361 .title($filter('translate')('YANGMAN_DELETE_COL_CONFIRM_TITLE') + ' ' + collObj.name + '?')
362 .textContent($filter('translate')('YANGMAN_DELETE_COL_CONFIRM_TEXT'))
363 .ariaLabel($filter('translate')('YANGMAN_DELETE_COL_CONFIRM_TITLE'))
365 .ok($filter('translate')('YANGMAN_OK'))
366 .cancel($filter('translate')('YANGMAN_CANCEL'));
368 YangmanDesignService.disableMdMenuItem(ev);
370 $mdDialog.show(confirm).then(function (){
371 vm.collectionList.deleteCollection(collObj);
372 vm.collectionList.saveToStorage();
373 refreshCollectionsWithExpansion();
375 YangmanDesignService.enableMdMenuItem(ev);
380 * Check if reqObj matches current search value
384 function filterReq(reqObj){
385 var searchPhrase = vm.search.toLocaleLowerCase();
386 return reqObj.path.toLowerCase().indexOf(searchPhrase) > -1 ||
387 reqObj.collection.toLowerCase().indexOf(searchPhrase) > -1 ||
388 reqObj.method.toLowerCase() === searchPhrase;
392 * Check if collection name matches current search value or any collection req matches
395 function filterCol(colObj){
396 return filterColName(colObj) || colObj.data.some(filterReq);
400 * Get count of requests matching filter in collection colObj
404 function colMatchingReqsCount(colObj){
405 return colObj.data.filter(vm.filterReq).length;
409 * Check if collection name matches current filter
413 function filterColName(colObj){
414 return colObj.name.toLowerCase().indexOf(vm.search.toLowerCase()) > -1;
421 function fakeFilter(){
427 * Show dialog for saving reqObj to collection (used for duplicate req too)
432 function showDgSaveReq(ev, reqObj, duplicate){
435 controller: SaveReqDialogCtrl,
436 controllerAs: 'dialog',
437 templateUrl: $scope.globalViewPath + 'leftpanel/save-req-dialog.tpl.html',
438 parent: angular.element('#yangmanModule'),
440 clickOutsideToClose: true,
442 requests: reqObj ? [reqObj] : vm.mainList.getSelectedItems(
443 vm.mainList === vm.collectionList ? filterCollReq : filterReq
445 collectionNames: vm.collectionList.getCollectionNames(),
446 duplicate: duplicate || false,
448 }).then(saveRequests);
452 * Show popup window for history requests settings
455 function showHistorySettings(ev){
458 controller: HistorySettingsCtrl,
459 controllerAs: 'settingsCtrl',
460 templateUrl: $scope.globalViewPath + 'leftpanel/history-settings.tpl.html',
461 parent: angular.element('#yangmanModule'),
463 clickOutsideToClose: true,
465 settingsObj: $scope.historySettings,
467 }).then(function (changedSettings){
468 $scope.historySettings.setData(changedSettings.data);
469 $scope.rootBroadcast(constants.YANGMAN_RESET_HISTORY_SETTINGS);
474 * Add each request from requests array to collectionList and save
477 function saveRequests(requests){
478 requests.forEach(function (reqObj){
479 vm.collectionList.addItemToList(RequestsService.clearUnnecessaryProperties(reqObj.clone()));
480 vm.collectionList.saveToStorage();
481 refreshCollectionsWithExpansion();
487 * Dialog for editing collection name (used for duplicating collection too)
490 * @param {boolean} duplicate
492 function showDgEditCollection(ev, collection, duplicate){
494 controller: EditCollectionDialogCtrl,
495 controllerAs: 'dialog',
496 templateUrl: $scope.globalViewPath + 'leftpanel/edit-collection-dialog.tpl.html',
497 parent: angular.element('#yangmanModule'),
499 clickOutsideToClose: true,
501 collection: collection,
502 allCollections: vm.collectionList.collections,
503 duplicate: duplicate,
505 }).then(duplicate ? duplicateCollection : changeCollectionName);
510 * @param {array} names 0. element is old name, 1. element is new name
512 function changeCollectionName(names){
513 vm.collectionList.renameCollection(names[0], names[1]);
514 vm.collectionList.saveToStorage();
515 refreshCollectionsWithExpansion();
519 * Create collection duplicate, save and refresh collections
520 * @param {array} names 0. element is old name, 1. element is new name
522 function duplicateCollection(names){
523 vm.collectionList.duplicateCollection(names[0], names[1]);
524 vm.collectionList.saveToStorage();
525 refreshCollectionsWithExpansion();
529 function selectNewestRequest() {
530 vm.mainList.toggleReqSelection(true, vm.mainList.getNewestRequest());
533 function loadCollectionsList() {
534 vm.collectionList.loadListFromStorage();
537 function loadHistoryList() {
538 vm.requestList.loadListFromStorage();
543 * @param mainList collectionList or requestList object
545 function init(mainList){
547 vm.collectionList = RequestsService.createEmptyCollectionList('yangman_collectionsList');
548 // collections are loaded for both history and collections tab
549 loadCollectionsList();
551 vm.requestList = RequestsService.createEmptyHistoryList('yangman_requestsList', $scope.historySettings);
553 $scope.$on(constants.YANGMAN_RESET_HISTORY_SETTINGS, function () {
554 vm.requestList.setSettings($scope.historySettings);
557 // if request was selected, deselect requests in all other instances of RequestsListCtrl
558 $scope.$on(constants.YANGMAN_DESELECT_REQUESTS, function (event, params) {
559 if (params.params.broadcastingCtrl !== vm) {
560 deselectAllRequests();
564 $scope.$on(constants.YANGMAN_REFRESH_COLLECTIONS, function (event, params){
565 loadCollectionsList();
566 (params.cbk || angular.noop)();
569 // list type dependend operations
570 if (mainList === 'history') {
572 vm.mainList = vm.requestList;
575 $scope.$on(constants.YANGMAN_REFRESH_HISTORY, loadHistoryList);
576 // saving from request header after execution
577 $scope.$on(constants.YANGMAN_SAVE_EXECUTED_REQUEST, saveBcstedHistoryRequest);
578 // select newest request
579 $scope.$on(constants.YANGMAN_SELECT_THE_NEWEST_REQUEST, selectNewestRequest);
582 vm.mainList = vm.collectionList;
583 // saving from request header
584 $scope.$on(constants.YANGMAN_SAVE_REQUEST_TO_COLLECTION, saveRequestFromExt);
585 // saving collections expanded status on refresh
586 $scope.$on(constants.YANGMAN_REFRESH_AND_EXPAND_COLLECTIONS, function(){
587 var expandedColNames = vm.collectionList.getExpandedCollectionNames();
588 $scope.rootBroadcast(constants.YANGMAN_REFRESH_COLLECTIONS, {}, function (){
589 vm.collectionList.expandCollectionByNames(expandedColNames);
598 * Request in list selection
599 * For history reqs it is possible multiselect, thats why event.ctrlKey is used
603 function selectRequest(event, requestObj){
604 $scope.rootBroadcast(constants.YANGMAN_DESELECT_REQUESTS, { broadcastingCtrl: vm });
605 vm.mainList.toggleReqSelection(!event.ctrlKey, requestObj);
607 if ($scope.rightPanelSection === constants.DISPLAY_TYPE_FORM) {
608 vm.showForm(requestObj, true);
611 vm.showData(requestObj, true);
617 * Mark only requestObj in current list as selected
618 * Used for example when user clicks on request submenu
621 function selectOnlyThisRequest(requestObj){
622 vm.mainList.toggleReqSelection(true, requestObj);
626 * Deselect history requests
628 function deselectAllFilteredReqs(){
629 vm.mainList.deselectAllFilteredItems(vm.mainList === vm.collectionList ? filterCollReq : vm.filterReq);
632 function deselectAllRequests() {
633 vm.mainList.deselectAllItems();
637 * Select history requests
639 function selectAllFilteredReqs(){
640 vm.mainList.selectAllFilteredItems(vm.mainList === vm.collectionList ? filterCollReq : vm.filterReq);
644 * Use when selecting filtered requests if they are saved to some collection
645 * Additional filter is if the collection of the request is expanded
647 * @returns {*|boolean}
649 function filterCollReq(request) {
650 return vm.collectionList.getCollection(request.collection).expanded && vm.filterReq(request);
654 * Refresh and expand collections
656 function refreshCollectionsWithExpansion(){
657 $scope.rootBroadcast(constants.YANGMAN_REFRESH_AND_EXPAND_COLLECTIONS);