1 define(['app/yangui/services/handle-file.services'], function () {
4 angular.module('app.yangui').controller('RequestHistoryCtrl', RequestHistoryCtrl);
6 RequestHistoryCtrl.$inject = ['$filter', '$scope', '$rootScope', 'PathUtilsService', 'HistoryService',
7 'HandleFileService', 'YangUtilsService', 'constants',
8 'MountPointsConnectorService', 'ParsingJsonService',
11 // todo: comment the whole controller
12 function RequestHistoryCtrl($filter, $scope, $rootScope, PathUtilsService, HistoryService, HandleFileService,
13 YangUtilsService, constants, MountPointsConnectorService, ParsingJsonService) {
15 $scope.collectionBoxView = false;
16 $scope.collectionList = HistoryService.createEmptyCollectionList('collectionList', $scope.getApiCallback);
17 $scope.groupView = {};
18 $scope.parameterList = HistoryService.createEmptyParamList('parameterList');
19 $scope.parameterList.loadListFromStorage();
20 $scope.popupHistory = { show: false };
21 $scope.requestList = HistoryService.createEmptyHistoryList('requestList', $scope.getApiCallback);
23 $scope.addRequestToList = addRequestToList;
24 $scope.clearHistoryData = clearHistoryData;
25 $scope.checkParamIsNotInList = checkParamIsNotInList;
26 $scope.deleteRequestItem = deleteRequestItem;
27 $scope.executeCollectionRequest = executeCollectionRequest;
28 $scope.exportHistoryData = exportHistoryData;
29 $scope.getApiCallback = getApiCallback;
30 $scope.hideCollBox = hideCollBox;
31 $scope.readCollectionFromFile = readCollectionFromFile;
32 $scope.reqHistoryFunc = reqHistoryFunc;
33 $scope.saveElemToList = saveElemToList;
34 $scope.saveParamToList = saveParamToList;
35 $scope.setGroupView = setGroupView;
36 $scope.showCollBox = showCollBox;
37 $scope.toggleExpanded = toggleExpanded;
39 $scope.$on('GET_PARAMETER_LIST', getParametersList);
40 $scope.$on('LOAD_REQ_DATA', loadReqData);
41 $scope.$on('REFRESH_HISTORY_REQUEST_APIS', refreshHistoryApis);
42 $scope.$on('YUI_ADD_TO_HISTORY', addToHistory);
44 var mountPrefix = constants.MPPREFIX;
47 function getApiCallback(pathString) {
48 var snp = PathUtilsService.getStorageAndNormalizedPath(pathString),
49 // if the path is for mountpoint then get the path to treedata structure
50 mpSearchPath = MountPointsConnectorService.alterMpPath(pathString),
51 apiIndexes = PathUtilsService.searchNodeByPath(mpSearchPath, $scope.treeApis, $scope.treeRows),
52 selApi = apiIndexes ? $scope.apis[apiIndexes.indexApi] : null,
53 selSubApi = selApi ? selApi.subApis[apiIndexes.indexSubApi] : null,
54 copiedApi = selSubApi ?
55 selSubApi.clone({ storage: snp.storage, withoutNode: true, clonePathArray: true }) :
59 copiedApi.pathArray.forEach(function (p) {
63 PathUtilsService.fillPath(copiedApi.pathArray, snp.normalizedPath);
66 var searchedModule = PathUtilsService.getModuleNameFromPath(pathString);
68 if (mpSearchPath.indexOf(mountPrefix) !== -1 && copiedApi){
69 copiedApi = $scope.selSubApi &&
70 searchedModule === $scope.selSubApi.pathArray[1].module ?
78 function refreshHistoryApis(event){
79 $scope.requestList.refresh();
80 $scope.collectionList.refresh();
83 function getParametersList(event, callback){
84 callback($scope.parameterList);
87 function addToHistory(event, status, data, requestData, operation, requestPath) {
88 $scope.addRequestToList(status, data, requestData, operation, requestPath);
91 function addRequestToList(status, receivedData, sentData, operation, path) {
92 if (typeof (Storage) !== 'undefined') {
94 var rList = HistoryService.createEmptyHistoryList(),
95 reqObj = HistoryService.createHistoryRequest(sentData, receivedData, path, null, operation,
96 status, null, null, $scope.getApiCallback);
98 reqObj.refresh($scope.getApiCallback);
100 $scope.requestList.addRequestToList(reqObj);
101 $scope.requestList.saveToStorage();
105 function reqHistoryFunc(){
106 $scope.popupHistory.show = !$scope.popupHistory.show;
108 $scope.requestList.loadListFromStorage();
109 $scope.collectionList.loadListFromStorage();
110 $scope.requestList.show = false;
113 function showCollBox(req, edit){
114 $scope.collectionBoxView = true;
115 $scope.$broadcast('COLL_CLEAR_VAL_SET_REQ', req, edit);
118 function hideCollBox(){
119 $scope.collectionBoxView = false;
122 function saveElemToList(elem) {
123 $scope.collectionList.addRequestToList(elem);
124 $scope.collectionList.saveToStorage();
127 function saveParamToList(elem, oldElem) {
128 $scope.parameterList.saveRequestToList(elem, oldElem);
131 function deleteRequestItem(elem, list){
132 $scope[list].deleteRequestItem(elem);
133 $scope[list].saveToStorage();
136 function clearHistoryData(list){
137 $scope[list].clear();
138 $scope[list].saveToStorage();
141 function clearFileInputValue() {
142 var el = document.getElementById('upload-collection');
146 function exportHistoryData() {
147 var cListJSON = localStorage.getItem('collectionList');
149 HandleFileService.downloadFile('requestCollection.json', cListJSON, 'json', 'charset=utf-8', function () {
150 $scope.setStatusMessage('success', 'EXPORT_COLLECTIONS_SUCCESS');
153 $scope.setStatusMessage('danger', 'EXPORT_COLLECTIONS_ERROR_BROWSER');
156 $scope.setStatusMessage('danger', 'EXPORT_COLLECTIONS_ERROR', e);
157 // console.error('ExportCollection error:', e);
162 function readCollectionFromFile($fileContent) {
163 var data = $fileContent,
175 if (data && HistoryService.validateFile(data, checkArray)){
177 $scope.collectionList.loadListFromFile(data);
178 $scope.collectionList.saveToStorage();
179 $scope.setStatusMessage('success', 'LOAD_COLLECTIONS_SUCCESS');
180 clearFileInputValue();
183 clearFileInputValue();
184 $scope.setStatusMessage('danger', 'PARSE_JSON_FILE_ERROR', e);
185 // console.error('DataStorage error:', e);
189 $scope.setStatusMessage('danger', 'PARSE_JSON_FILE_ERROR');
190 clearFileInputValue();
194 function checkParamIsNotInList(req) {
195 var regexp = /<<([^>]+)>>/g;
196 var sParam = angular.toJson(req).match(regexp);
198 return sParam && sParam.some(function(parameter) {
199 parameter = parameter.replace(/[<>]/g, '');
201 return $filter('filter')($scope.parameterList.list, {'name': parameter}).length < 1;
205 function executeCollectionRequest(req, dataForView, showData) {
206 var sdata = dataForView ? ParsingJsonService.parseJson(dataForView) : req.sentData,
207 path = req.parametrizedPath && showData ? req.parametrizedPath : req.api.buildApiRequestString(),
208 paramResult = checkParamIsNotInList(sdata);
210 path = $scope.parameterizeData(path);
211 $scope.fillStandardApi(path, req.path);
213 if(sdata && !paramResult) {
214 $scope.fillApiData(sdata);
217 var requestPath = req.api.parent.basePath + path;
220 $scope.executeOperation(req.method, function(data){
221 if ( !data && req.receivedData ){
222 $scope.node.fill($scope.node.label,req.receivedData[$scope.node.label]);
227 $scope.setStatusMessage('danger', 'YANGUI_PARAMETER_MISSING_ERROR', e.message);
231 function setGroupView(key) {
232 $scope.groupView[key] = false;
235 function toggleExpanded(key) {
236 $scope.groupView[key] = !$scope.groupView[key];
239 function loadReqData(){
240 $scope.reqHistoryFunc();