From 2fe70ce310814fe47ca42862999888e459de15f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20=C3=81ngel=20Gonz=C3=A1lez?= Date: Tue, 8 May 2018 16:07:51 +0200 Subject: [PATCH] First approach for the new dev tools section --- public/controllers/dev-tools.js | 126 +++++++++++++++------- public/templates/dev-tools/dev-tools.html | 9 +- 2 files changed, 90 insertions(+), 45 deletions(-) diff --git a/public/controllers/dev-tools.js b/public/controllers/dev-tools.js index 0e349c786..b3eb7bff3 100644 --- a/public/controllers/dev-tools.js +++ b/public/controllers/dev-tools.js @@ -11,58 +11,101 @@ */ import * as modules from 'ui/modules' import beautifier from 'plugins/wazuh/utils/json-beautifier' +import CodeMirror from 'plugins/wazuh/utils/codemirror/lib/codemirror' const app = modules.get('app/wazuh', []); // Logs controller -app.controller('devToolsController', function($scope, $rootScope, errorHandler, apiReq, $window) { - $scope.validJSON = true; - $scope.requestText = 'GET /agents' - $scope.output = beautifier.prettyPrint({}) - $scope.requestTextJson = JSON.stringify({ - limit: "1" - }, null, 4) - let oldCopy = JSON.parse($scope.requestTextJson); - const parseParams = req => { - const result = {}; - req.split('&').forEach(part => { - const item = part.split('='); - result[item[0]] = decodeURIComponent(item[1]); - }); - return result; +app.controller('devToolsController', function($scope, $rootScope, errorHandler, apiReq, $window, appState) { + + let groups = []; + const analyzeGroups = () => { + try{ + const groups = []; + const splitted = apiInputBox.getValue().toString().split(/[\r\n]+(?=(?:GET|PUT|POST|DELETE)\b)/gm) + let start = 0, end = 0; + let lastElement = null; + for(let i=0; i { - try { - $scope.validJSON = true; - const tmpCopy = JSON.parse($scope.requestTextJson); - if(oldCopy && JSON.stringify(oldCopy) === JSON.stringify(tmpCopy)) return; - if(typeof tmpCopy === 'object' && !Object.keys(tmpCopy).length) { - $scope.requestTextJson = '{\n\n}' - } else { - $scope.requestTextJson = JSON.stringify(tmpCopy, null, 4) - } - oldCopy = JSON.parse($scope.requestTextJson); - } catch (error) { - $scope.validJSON = false; - } + const apiInputBox = CodeMirror.fromTextArea(document.getElementById('api_input'),{ + lineNumbers : true, + matchBrackets: true, + mode: {name:"javascript",json:true}, + styleActiveLine: true, + theme:'ttcn', + foldGutter: true, + gutters: ["CodeMirror-foldgutter"] + }); + + apiInputBox.getDoc().setValue('GET /agents\n' + JSON.stringify({limit:1},null,2)); + let requestText = 'GET /agents' + let requestTextJson = '{ "limit": 1 }' + + const apiOutputBox = CodeMirror.fromTextArea(document.getElementById('api_output'),{ + lineNumbers : true, + matchBrackes: true, + mode: {name:"javascript",json:true}, + readOnly: true, + lineWrapping: true, + styleActiveLine: true, + theme:'ttcn', + foldGutter: true, + gutters: ["CodeMirror-foldgutter"] + }); + + apiInputBox.setSize('auto','100%') + apiOutputBox.setSize('auto','100%') + + + const calculateWhichGroup = () => { + const selection = apiInputBox.getCursor() + const desiredGroup = groups.filter(item => item.end >= selection.line && item.start <= selection.line); + return desiredGroup ? desiredGroup[0] : null; } $scope.send = async () => { try { - const method = $scope.requestText.startsWith('GET') ? + groups = analyzeGroups(); + const desiredGroup = calculateWhichGroup(); + + const method = desiredGroup.requestText.startsWith('GET') ? 'GET' : - $scope.requestText.startsWith('POST') ? + desiredGroup.requestText.startsWith('POST') ? 'POST' : - $scope.requestText.startsWith('PUT') ? + desiredGroup.requestText.startsWith('PUT') ? 'PUT' : - $scope.requestText.startsWith('DELETE') ? + desiredGroup.requestText.startsWith('DELETE') ? 'DELETE' : 'GET'; - const requestCopy = $scope.requestText.includes(method) ? - $scope.requestText.split(method)[1].trim() : - $scope.requestText; + const requestCopy = desiredGroup.requestText.includes(method) ? + desiredGroup.requestText.split(method)[1].trim() : + desiredGroup.requestText; const req = requestCopy ? requestCopy.startsWith('/') ? @@ -72,20 +115,20 @@ app.controller('devToolsController', function($scope, $rootScope, errorHandler, let validJSON = true, JSONraw = {}; try { - JSONraw = JSON.parse($scope.requestTextJson); + JSONraw = JSON.parse(desiredGroup.requestTextJson); } catch(error) { validJSON = false; } const path = req.includes('?') ? req.split('?')[0] : req; const params = req.includes('?') ? parseParams(req.split('?')[1]) : {} - + console.log(method, path, validJSON,!req.includes('?'),JSONraw, params) const output = await apiReq.request(method, path, validJSON && !req.includes('?') ? JSONraw : params) - - $scope.output = beautifier.prettyPrint(output.data.data); - if(!$scope.$$phase) $scope.$digest(); + + apiOutputBox.setValue(JSON.stringify(output.data.data,null,2)) } catch(error) { + console.log(error.message || error) $scope.output = beautifier.prettyPrint(error.data); if(!$scope.$$phase) $scope.$digest(); } @@ -97,4 +140,5 @@ app.controller('devToolsController', function($scope, $rootScope, errorHandler, } $scope.send(); + }); diff --git a/public/templates/dev-tools/dev-tools.html b/public/templates/dev-tools/dev-tools.html index a7119ebf0..2200c6248 100644 --- a/public/templates/dev-tools/dev-tools.html +++ b/public/templates/dev-tools/dev-tools.html @@ -1,3 +1,4 @@ +
@@ -5,13 +6,13 @@
- Console - - + Console + +
-
+