From a5738e7afbba020d6caa1885b48de858c0073b6f Mon Sep 17 00:00:00 2001 From: Usman Nasir Date: Thu, 22 Oct 2020 10:00:54 +0500 Subject: [PATCH] codemirror: save file changes --- filemanager/filemanager.py | 8 ++- .../static/filemanager/js/codeMirror.js | 49 ++++++++++--------- .../templates/filemanager/editFile.html | 14 +++++- static/filemanager/js/codeMirror.js | 49 ++++++++++--------- 4 files changed, 74 insertions(+), 46 deletions(-) diff --git a/filemanager/filemanager.py b/filemanager/filemanager.py index 22c52feb7..9cbdfc1b0 100755 --- a/filemanager/filemanager.py +++ b/filemanager/filemanager.py @@ -11,7 +11,7 @@ from filemanager.models import Trash class FileManager: modes = {'php': 'application/x-httpd-php', 'javascript': 'javascript', 'python': 'text/x-python', - 'html': 'text/html', 'go': 'text/x-go', 'css': 'text/css'} + 'html': 'text/html', 'go': 'text/x-go', 'css': 'text/css', 'java': 'text/x-java'} def __init__(self, request, data): self.request = request @@ -31,6 +31,8 @@ class FileManager: return FileManager.modes['go'] elif fileName.endswith('.css') or fileName.endswith('.scss'): return FileManager.modes['css'] + elif fileName.endswith('.java'): + return FileManager.modes['java'] @staticmethod @@ -111,6 +113,10 @@ class FileManager: return """ """ + elif mode == FileManager.modes['java']: + return """ + +""" @staticmethod def findThemeFile(theme): diff --git a/filemanager/static/filemanager/js/codeMirror.js b/filemanager/static/filemanager/js/codeMirror.js index bf5958fdc..975ca1ff5 100755 --- a/filemanager/static/filemanager/js/codeMirror.js +++ b/filemanager/static/filemanager/js/codeMirror.js @@ -30,11 +30,9 @@ fileManager.controller('editFileCtrl', function ($scope, $http, $window) { var domainName = $("#domainNameInitial").text(); - $scope.editDisable = true; // html editor - $scope.errorMessageEditor = true; - $scope.htmlEditorLoading = true; - $scope.saveSuccess = true; + $scope.cyberPanelLoading = true; + var globalCM; var url = '/filemanager/controller'; @@ -67,15 +65,16 @@ fileManager.controller('editFileCtrl', function ($scope, $http, $window) { let params = new URLSearchParams(url.search); let python = params.get('python'); - if(python == null) { + if (python == null) { var cm = new CodeMirror.fromTextArea(document.getElementById("fileContent"), { lineNumbers: true, mode: $("#mode").text(), lineWrapping: false, theme: $("#theme").text() }); - }else{ - var mode = {name: $("#mode").text(), version:python}; + globalCM = cm; + } else { + var mode = {name: $("#mode").text(), version: python}; var cm = new CodeMirror.fromTextArea(document.getElementById("fileContent"), { lineNumbers: true, @@ -83,6 +82,7 @@ fileManager.controller('editFileCtrl', function ($scope, $http, $window) { lineWrapping: false, theme: $("#theme").text() }); + globalCM = cm; } cm.setValue(response.data.fileContents); @@ -151,17 +151,12 @@ fileManager.controller('editFileCtrl', function ($scope, $http, $window) { $scope.putFileContents = function () { - $scope.htmlEditorLoading = false; - $scope.saveSuccess = true; - $scope.errorMessageEditor = true; - - var completePathForFile = $scope.currentPath; + $scope.cyberPanelLoading = false; var data = { - fileName: completePathForFile, + fileName: $("#completeFilePath").text(), method: "writeFileContents", - fileContent: editor.getValue(), - domainRandomSeed: domainRandomSeed, + fileContent: globalCM.getValue(), domainName: domainName }; @@ -176,20 +171,30 @@ fileManager.controller('editFileCtrl', function ($scope, $http, $window) { $http.post(url, data, config).then(ListInitialDatas, cantLoadInitialDatas); function ListInitialDatas(response) { - - $scope.htmlEditorLoading = true; - + $scope.cyberPanelLoading = true; if (response.data.status === 1) { - $scope.htmlEditorLoading = true; - $scope.saveSuccess = false; + new PNotify({ + title: 'Success!', + text: 'File saved successfully.', + type: 'success' + }); } else { - $scope.errorMessageEditor = false; - $scope.error_message = response.data.error_message; + new PNotify({ + title: 'Operation Failed!', + text: response.data.error_message, + type: 'error' + }); } } function cantLoadInitialDatas(response) { + $scope.cyberPanelLoading = true; + new PNotify({ + title: 'Operation Failed!', + text: 'Could not connect to server, please refresh this page', + type: 'error' + }); } }; diff --git a/filemanager/templates/filemanager/editFile.html b/filemanager/templates/filemanager/editFile.html index bf7707e5f..c97e8c828 100755 --- a/filemanager/templates/filemanager/editFile.html +++ b/filemanager/templates/filemanager/editFile.html @@ -15,6 +15,7 @@ integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous"> + @@ -43,7 +44,7 @@ crossorigin="anonymous"/> {{ modeFiles | safe }} - + @@ -165,6 +166,17 @@ +
+
+
+

Editing {{ fileName }} + +

+
+
+
+ diff --git a/static/filemanager/js/codeMirror.js b/static/filemanager/js/codeMirror.js index bf5958fdc..975ca1ff5 100644 --- a/static/filemanager/js/codeMirror.js +++ b/static/filemanager/js/codeMirror.js @@ -30,11 +30,9 @@ fileManager.controller('editFileCtrl', function ($scope, $http, $window) { var domainName = $("#domainNameInitial").text(); - $scope.editDisable = true; // html editor - $scope.errorMessageEditor = true; - $scope.htmlEditorLoading = true; - $scope.saveSuccess = true; + $scope.cyberPanelLoading = true; + var globalCM; var url = '/filemanager/controller'; @@ -67,15 +65,16 @@ fileManager.controller('editFileCtrl', function ($scope, $http, $window) { let params = new URLSearchParams(url.search); let python = params.get('python'); - if(python == null) { + if (python == null) { var cm = new CodeMirror.fromTextArea(document.getElementById("fileContent"), { lineNumbers: true, mode: $("#mode").text(), lineWrapping: false, theme: $("#theme").text() }); - }else{ - var mode = {name: $("#mode").text(), version:python}; + globalCM = cm; + } else { + var mode = {name: $("#mode").text(), version: python}; var cm = new CodeMirror.fromTextArea(document.getElementById("fileContent"), { lineNumbers: true, @@ -83,6 +82,7 @@ fileManager.controller('editFileCtrl', function ($scope, $http, $window) { lineWrapping: false, theme: $("#theme").text() }); + globalCM = cm; } cm.setValue(response.data.fileContents); @@ -151,17 +151,12 @@ fileManager.controller('editFileCtrl', function ($scope, $http, $window) { $scope.putFileContents = function () { - $scope.htmlEditorLoading = false; - $scope.saveSuccess = true; - $scope.errorMessageEditor = true; - - var completePathForFile = $scope.currentPath; + $scope.cyberPanelLoading = false; var data = { - fileName: completePathForFile, + fileName: $("#completeFilePath").text(), method: "writeFileContents", - fileContent: editor.getValue(), - domainRandomSeed: domainRandomSeed, + fileContent: globalCM.getValue(), domainName: domainName }; @@ -176,20 +171,30 @@ fileManager.controller('editFileCtrl', function ($scope, $http, $window) { $http.post(url, data, config).then(ListInitialDatas, cantLoadInitialDatas); function ListInitialDatas(response) { - - $scope.htmlEditorLoading = true; - + $scope.cyberPanelLoading = true; if (response.data.status === 1) { - $scope.htmlEditorLoading = true; - $scope.saveSuccess = false; + new PNotify({ + title: 'Success!', + text: 'File saved successfully.', + type: 'success' + }); } else { - $scope.errorMessageEditor = false; - $scope.error_message = response.data.error_message; + new PNotify({ + title: 'Operation Failed!', + text: response.data.error_message, + type: 'error' + }); } } function cantLoadInitialDatas(response) { + $scope.cyberPanelLoading = true; + new PNotify({ + title: 'Operation Failed!', + text: 'Could not connect to server, please refresh this page', + type: 'error' + }); } };