From cc18187ce3f5207484df56270a7fea7084e9697c Mon Sep 17 00:00:00 2001 From: Marco Dickert Date: Wed, 5 Jul 2017 16:44:57 +0200 Subject: [PATCH] added templates, restructured JS part --- ifm.php | 920 +++++++++++------- src/config.php | 1 + src/ifm.js | 524 +++++----- src/ifmzip.php | 2 +- src/main.php | 132 +-- src/templates/app.html | 80 ++ src/templates/misc.savequestion.html | 5 + src/templates/modal.ajaxrequest.html | 16 + src/templates/modal.copymove.html | 13 + src/templates/modal.createdir.html | 12 + src/templates/modal.deletefile.html | 9 + src/templates/modal.extractfile.html | 23 + .../{modal.fileform.html => modal.file.html} | 10 +- src/templates/modal.multidelete.html | 9 + src/templates/modal.remoteupload.html | 16 + src/templates/modal.renamefile.html | 10 + src/templates/modal.uploadfile.html | 14 + 17 files changed, 1125 insertions(+), 671 deletions(-) create mode 100644 src/templates/app.html create mode 100644 src/templates/misc.savequestion.html create mode 100644 src/templates/modal.ajaxrequest.html create mode 100644 src/templates/modal.copymove.html create mode 100644 src/templates/modal.createdir.html create mode 100644 src/templates/modal.deletefile.html create mode 100644 src/templates/modal.extractfile.html rename src/templates/{modal.fileform.html => modal.file.html} (58%) create mode 100644 src/templates/modal.multidelete.html create mode 100644 src/templates/modal.remoteupload.html create mode 100644 src/templates/modal.renamefile.html create mode 100644 src/templates/modal.uploadfile.html diff --git a/ifm.php b/ifm.php index c71c347..5291e07 100644 --- a/ifm.php +++ b/ifm.php @@ -87,6 +87,7 @@ class IFMConfig { // development tools const ajaxrequest = 1; // formular to perform an ajax request + const debug = true; static function getConstants() { $oClass = new ReflectionClass(__CLASS__); @@ -156,7 +157,7 @@ class IFMZip { /** * Unzip a zip file */ - public function extract( $file, $destination="./" ) { + public static function extract( $file, $destination="./" ) { $zip = new ZipArchive; $res = $zip->open( $file ); if( $res === true ) { @@ -330,8 +331,6 @@ class IFM { .icon-file-code:before { content: '\f1c9'; } /* '' */ .icon-sliders:before { content: '\f1de'; } /* '' */ .icon-trash:before { content: '\f1f8'; } /* '' */ - - -
- - - - '; - if( $this->config['download'] == 1 ) print ''; - if( $this->config['showlastmodified'] == 1 ) print ''; - if( $this->config['showfilesize'] == 1 ) print ''; - if( $this->config['showpermissions'] > 0 ) print ''; - if( $this->config['showowner'] == 1 && function_exists( "posix_getpwuid" ) ) print ''; - if( $this->config['showgroup'] == 1 && function_exists( "posix_getgrgid" ) ) print ''; - if( in_array( 1, array( $this->config['edit'], $this->config['rename'], $this->config['delete'], $this->config['zipnload'], $this->config['extract'] ) ) ) print ''; - print ' - - - -
Filenamelast modifiedsize
-
-
- -
+
@@ -1705,7 +1655,9 @@ ifm.init(); } elseif( $_REQUEST["api"] == "getConfig" ) { echo json_encode( $this->config ); - } else { + } elseif( $_REQUEST["api"] == "getTemplates" ) { + echo json_encode( $this->getTemplates() ); + } else { if( isset( $_REQUEST["dir"] ) && $this->isPathValid( $_REQUEST["dir"] ) ) { switch( $_REQUEST["api"] ) { case "createDir": $this->createDir( $_REQUEST["dir"], $_REQUEST["dirname"] ); break; @@ -2026,7 +1978,7 @@ ifm.init(); echo json_encode( array( "status" => "ERROR", "message" => "Not allowed to download hidden files" ) ); else { $this->chDirIfNecessary( $d["dir"] ); - $this->file_download( $d['filename'] ); + $this->fileDownload( $d['filename'] ); } } @@ -2149,7 +2101,7 @@ ifm.init(); else $d['filename'] = basename( getcwd() ); } - $this->file_download( $dfile, $d['filename'] . ".zip" ); + $this->fileDownload( $dfile, $d['filename'] . ".zip" ); } catch ( Exception $e ) { echo "An error occured: " . $e->getMessage(); } finally { @@ -2499,7 +2451,7 @@ ifm.init(); else return true; } - private function file_download( $file, $name="" ) { + private function fileDownload( $file, $name="" ) { header( 'Content-Description: File Transfer' ); header( 'Content-Type: application/octet-stream' ); header( 'Content-Disposition: attachment; filename="' . ( trim( $name ) == "" ? basename( $file ) : $name ) . '"' ); @@ -2517,12 +2469,310 @@ ifm.init(); fclose($stdout_stream); } - ///helper + private function getTemplates() { + $templates = array(); + $templates['app'] = <<<'f00bar' + +
+ + + + + {{#config.download}} + + {{/config.download}} + {{#config.showlastmodified}} + + {{/config.showlastmodified}} + {{#config.showfilesize}} + + {{/config.showfilesize}} + {{#config.showpermissions}} + + {{/config.showpermissions}} + {{#config.showowner}} + + {{/config.showowner}} + {{#config.showgroup}} + + {{/config.showgroup}} + {{#ftbuttons}} + + {{/ftbuttons}} + + + + +
Filenamelast modifiedsizepermissionsownergroup
+
+
+ +
+ +f00bar; + $templates['file'] = <<<'f00bar' +
+ + +
+ +f00bar; + $templates['createdir'] = <<<'f00bar' +
+ + +
+ +f00bar; + $templates['ajaxrequest'] = <<<'f00bar' +
+
+ +f00bar; + $templates['copymove'] = <<<'f00bar' +
+
+ + +
+
+ +f00bar; + $templates['createdir'] = <<<'f00bar' +
+ + +
+ +f00bar; + $templates['deletefile'] = <<<'f00bar' +
+ + +
+ +f00bar; + $templates['extractfile'] = <<<'f00bar' +
+
+ + +
+
+ +f00bar; + $templates['file'] = <<<'f00bar' +
+ + +
+ +f00bar; + $templates['multidelete'] = <<<'f00bar' +
+ + +
+ +f00bar; + $templates['remoteupload'] = <<<'f00bar' +
+ + +
+ +f00bar; + $templates['renamefile'] = <<<'f00bar' + + +f00bar; + $templates['uploadfile'] = <<<'f00bar' +
+ + +
+ +f00bar; + + return $templates; + } } -/* - start program +/** + * start IFM */ - $ifm = new IFM( IFMConfig::getConstants() ); $ifm->run(); diff --git a/src/config.php b/src/config.php index 3ade0b1..55d8ea6 100644 --- a/src/config.php +++ b/src/config.php @@ -87,6 +87,7 @@ class IFMConfig { // development tools const ajaxrequest = 1; // formular to perform an ajax request + const debug = true; static function getConstants() { $oClass = new ReflectionClass(__CLASS__); diff --git a/src/ifm.js b/src/ifm.js index 35d17cd..dc90995 100644 --- a/src/ifm.js +++ b/src/ifm.js @@ -11,27 +11,13 @@ function IFM( params ) { self.api = params.api; } - // load the configuration from the backend - $.ajax({ - url: self.api, - type: "POST", - data: { - api: "getConfig" - }, - dataType: "json", - success: function(d) { - self.config = d; - self.log( "configuration loaded" ); - }, - error: function() { - throw new Error( "IFM: could not load configuration" ); - } - }); + this.isDocroot = ; this.editor = null; // global ace editor this.fileChanged = false; // flag for check if file was changed already this.currentDir = ""; // this is the global variable for the current directory; it is used for AJAX requests + this.rootElement = undefined; /** * Shows a bootstrap modal @@ -149,18 +135,18 @@ function IFM( params ) { if( self.inArray( 1, [self.config.edit, self.config.rename, self.config.delete, self.config.extract, self.config.copymove] ) ) { newRow += ''; if( data[i].name.toLowerCase().substr(-4) == ".zip" && self.config.extract == 1 ) { - newRow += ''; + newRow += ''; } else if( self.config.edit == 1 && data[i].type != "dir" ) { newRow += ''; } if( data[i].name != ".." && data[i].name != "." ) { if( self.config.copymove == 1 ) { - newRow += ''; + newRow += ''; } if( self.config.rename == 1 ) newRow += ''; if( self.config.delete == 1 ) - newRow += ''; + newRow += ''; } newRow += ''; } else { @@ -213,22 +199,26 @@ function IFM( params ) { /** * Shows a file, either a new file or an existing */ - this.showFileForm = function () { + this.showFileDialog = function () { var filename = arguments.length > 0 ? arguments[0] : "newfile.txt"; var content = arguments.length > 1 ? arguments[1] : ""; - var overlay = '
' + - '' + - '
'; - self.showModal( overlay, { large: true } ); - $('#editoroptions').popover({ + self.showModal( Mustache.render( self.templates.file, { filename: filename } ), { large: true } ); + var form = $('#formFile'); + form.find('input[name="filename"]').on( 'keypress', self.preventEnter ); + form.find('#buttonSave').on( 'click', function() { + self.saveFile( form.find('input[name=filename]').val(), self.editor.getValue() ); + self.hideModal(); + return false; + }); + form.find('#buttonSaveNotClose').on( 'click', function() { + self.saveFile( form.find('input[name=filename]').val(), self.editor.getValue() ); + return false; + }); + form.find('#buttonClose').on( 'click', function() { + self.hideModal(); + return false; + }); + form.find('#editoroptions').popover({ html: true, title: function() { return $('#editoroptions-head').html(); }, content: function() { @@ -247,7 +237,7 @@ function IFM( params ) { return content; } }); - $('#ifmmodal').on( 'remove', function () { self.editor = null; self.fileChanged = false; }); + form.on( 'remove', function () { self.editor = null; self.fileChanged = false; }); // Start ACE self.editor = ace.edit("content"); self.editor.$blockScrolling = 'Infinity'; @@ -260,63 +250,18 @@ function IFM( params ) { }); }; - /** - * Shows the create directory dialog - */ - this.createDirForm = function() { - self.showModal( '
\ - \ - \ -
' ); - }; - - /** - * Create a directory - */ - this.createDir = function() { - $.ajax({ - url: self.api, - type: "POST", - data: ({ - api: "createDir", - dir: self.currentDir, - dirname: $("#createDir input[name^=dirname]").val() - }), - dataType: "json", - success: function(data){ - if(data.status == "OK") { - self.showMessage("Directory sucessfully created.", "s"); - self.refreshFileTable(); - } - else { - self.showMessage("Directory could not be created: "+data.message, "e"); - } - }, - error: function() { self.showMessage("General error occured.", "e"); } - }); - }; - - /** * Saves a file */ - this.saveFile = function() { + this.saveFile = function( filename, content ) { $.ajax({ url: self.api, type: "POST", data: ({ api: "saveFile", dir: self.currentDir, - filename: $("#showFile input[name^=filename]").val(), - content: ifm.editor.getValue() + filename: filename, + content: content }), dataType: "json", success: function( data ) { @@ -347,7 +292,7 @@ function IFM( params ) { }), success: function( data ) { if( data.status == "OK" && data.data.content != null ) { - self.showFileForm( data.data.filename, data.data.content ); + self.showFileDialog( data.data.filename, data.data.content ); } else if( data.status == "OK" && data.data.content == null ) { self.showMessage( "The content of this file cannot be fetched.", "e" ); @@ -358,20 +303,68 @@ function IFM( params ) { }); }; + /** + * Shows the create directory dialog + */ + this.showCreateDirDialog = function() { + self.showModal( self.templates.createdir ); + var form = $( '#formCreateDir' ); + form.find( 'input[name=dirname]' ).on( 'keypress', self.preventEnter ); + form.find( '#buttonSave' ).on( 'click', function() { + self.createDir( form.find( 'input[name=dirname] ').val() ); + self.hideModal(); + return false; + }); + form.find( '#buttonCancel' ).on( 'click', function() { + self.hideModal(); + return false; + }); + }; + + /** + * Create a directory + */ + this.createDir = function( dirname ) { + $.ajax({ + url: self.api, + type: "POST", + data: ({ + api: "createDir", + dir: self.currentDir, + dirname: dirname + }), + dataType: "json", + success: function( data ){ + if( data.status == "OK" ) { + self.showMessage( "Directory sucessfully created.", "s" ); + self.refreshFileTable(); + } + else { + self.showMessage( "Directory could not be created: "+data.message, "e" ); + } + }, + error: function() { self.showMessage( "General error occured.", "e" ); } + }); + }; + + /** * Shows the delete file dialog * * @param string name - name of the file */ - this.deleteFileDialog = function( name ) { - self.showModal( '
\ - \ -
' ); + this.showDeleteFileDialog = function( filename ) { + self.showModal( Mustache.render( self.templates.deletefile, { filename: name } ) ); + var form = $( '#formDeleteFile' ); + form.find( '#buttonYes' ).on( 'click', function() { + self.deleteFile( self.JSEncode( filename ) ); + self.hideModal(); + return false; + }); + form.find( '#buttonNo' ).on( 'click', function() { + self.hideModal(); + return false; + }); }; /** @@ -379,14 +372,14 @@ function IFM( params ) { * * @params string name - name of the file */ - this.deleteFile = function( name ) { + this.deleteFile = function( filename ) { $.ajax({ url: self.api, type: "POST", data: ({ api: "deleteFile", dir: self.currentDir, - filename: name + filename: filename }), dataType: "json", success: function(data) { @@ -404,16 +397,19 @@ function IFM( params ) { * * @params string name - name of the file */ - this.renameFileDialog = function( name ) { - self.showModal( '' ); + this.showRenameFileDialog = function( filename ) { + self.showModal( Mustache.render( self.templates.renamefile, { filename: filename } ) ); + var form = $( '#formRenameFile' ); + form.find( 'input[name=newname]' ).on( 'keypress', self.preventEnter ); + form.find( '#buttonRename' ).on( 'click', function() { + self.renameFile( filename, form.find( 'input[name=newname]' ).val() ); + self.hideModal(); + return false; + }); + form.find( '#buttonCancel' ).on( 'click', function() { + self.hideModal(); + return false; + }); }; /** @@ -421,15 +417,15 @@ function IFM( params ) { * * @params string name - name of the file */ - this.renameFile = function( name ) { + this.renameFile = function( filename, newname ) { $.ajax({ url: ifm.api, type: "POST", data: ({ api: "renameFile", dir: ifm.currentDir, - filename: name, - newname: $("#renameFile input[name^=newname]").val() + filename: filename, + newname: newname }), dataType: "json", success: function(data) { @@ -447,42 +443,32 @@ function IFM( params ) { * * @params string name - name of the file */ - this.copyMoveDialog = function( name ) { - self.showModal( '
\ - \ - \ -
'); + this.showCopyMoveDialog = function( filename ) { + self.showModal( self.templates.copymove ); $.ajax({ - url: ifm.api, + url: self.api, type: "POST", data: ({ api: "getFolderTree", - dir: ifm.currentDir + dir: self.currentDir }), dataType: "json", - success: function(data) { - $('#copyMoveTree').treeview({data: data, levels: 0, expandIcon: "icon icon-folder-empty", collapseIcon: "icon icon-folder-open-empty"}); + success: function( data ) { + $( '#copyMoveTree' ).treeview( { data: data, levels: 0, expandIcon: "icon icon-folder-empty", collapseIcon: "icon icon-folder-open-empty" } ); }, error: function() { self.hideModal(); self.showMessage( "Error while fetching the folder tree.", "e" ) } }); - $('#copyButton').on( 'click', function(e) { + $( '#copyButton' ).on( 'click', function() { self.copyMove( name, $('#copyMoveTree .node-selected').data('path'), 'copy' ); self.hideModal(); return false; }); - $('#moveButton').on( 'click', function(e) { + $( '#moveButton' ).on( 'click', function() { self.copyMove( name, $('#copyMoveTree .node-selected').data('path'), 'move' ); self.hideModal(); return false; }); - $('#cancelButton').on( 'click', function(e) { + $( '#cancelButton' ).on( 'click', function() { self.hideModal(); return false; }); @@ -494,7 +480,7 @@ function IFM( params ) { * @params string name - name of the file */ this.copyMove = function( source, destination, action ) { - var id=self.generateGuid(); + var id = self.generateGuid(); self.task_add( action.charAt(0).toUpperCase() + action.slice(1) + " " + source + " to " + destination, id ); $.ajax({ url: self.api, @@ -529,35 +515,25 @@ function IFM( params ) { * * @param string name - name of the file */ - this.extractFileDialog = function( name ) { + this.showExtractFileDialog = function( filename ) { var targetDirSuggestion = ""; - if( name.lastIndexOf( "." ) > 1 ) - targetDirSuggestion = name.substr( 0, name.length - 4 ); - else targetDirSuggestion = name; - self.showModal( '
\ - \ - \ -
'); - $('#extractFileButton').on( 'click', function() { - var t = $('input[name=extractTargetLocation]:checked').val(); - if( t == "custom" ) t = $('#extractCustomLocation').val(); - self.extractFile( self.JSEncode( name ), t ); + if( filename.lastIndexOf( "." ) > 1 ) + targetDirSuggestion = filename.substr( 0, name.length - 4 ); + else targetDirSuggestion = filename; + self.showModal( Mustache.render( self.templates.extractfile, { filename: filename, targetDirSuggestion: targetDirSuggestion } ) ); + var form = $('#formExtractFile'); + form.find('#buttonExtract').on( 'click', function() { + var t = form.find('input[name=extractTargetLocation]:checked').val(); + if( t == "custom" ) t = form.find('#extractCustomLocation').val(); + self.extractFile( self.JSEncode( filename ), t ); self.hideModal(); return false; }); - $('#extractCancelButton').on( 'click', function() { + form.find('#buttonCancel').on( 'click', function() { self.hideModal(); return false; }); - $('#extractCustomLocation').on( 'click', function(e) { + form.find('#extractCustomLocation').on( 'click', function(e) { $(e.target).prev().children().first().prop( 'checked', true ); }); }; @@ -565,18 +541,18 @@ function IFM( params ) { /** * Extracts a file * - * @param string name - name of the file - * @param string t - name of the target directory + * @param string filename - name of the file + * @param string destination - name of the target directory */ - this.extractFile = function( name, t ) { + this.extractFile = function( filename, destination ) { $.ajax({ url: self.api, type: "POST", data: { api: "extractFile", dir: self.currentDir, - filename: name, - targetdir: t + filename: filename, + targetdir: destination }, dataType: "json", success: function( data ) { @@ -592,20 +568,19 @@ function IFM( params ) { /** * Shows the upload file dialog */ - this.uploadFileDialog = function() { - self.showModal( '
\ - \ -
' ); + this.showUploadFileDialog = function() { + self.showModal( self.templates.uploadfile ); + var form = $('#formUploadFile'); + form.find( 'input[name=newfilename]' ).on( 'keypress', self.preventEnter ); + form.find( '#buttonUpload' ).on( 'click', function() { + self.uploadFile(); + self.hideModal(); + return false; + }); + form.find( '#buttonCancel' ).on( 'click', function() { + self.hideModal(); + return false; + }); }; /** @@ -614,7 +589,7 @@ function IFM( params ) { this.uploadFile = function() { var ufile = document.getElementById( 'ufile' ).files[0]; var data = new FormData(); - var newfilename = $("#uploadFile input[name^=newfilename]").val(); + var newfilename = $("#formUploadFile input[name^=newfilename]").val(); data.append('api', 'uploadFile'); data.append('dir', self.currentDir); data.append('file', ufile); @@ -634,11 +609,11 @@ function IFM( params ) { return xhr ; }, success: function(data) { - if(data.status == "OK") { - self.showMessage("File successfully uploaded", "s"); - if(data.cd == self.currentDir) self.refreshFileTable(); - } else self.showMessage("File could not be uploaded: "+data.message, "e"); - }, + if(data.status == "OK") { + self.showMessage("File successfully uploaded", "s"); + if(data.cd == self.currentDir) self.refreshFileTable(); + } else self.showMessage("File could not be uploaded: "+data.message, "e"); + }, error: function() { self.showMessage("General error occured", "e"); }, complete: function() { self.task_done(id); } }); @@ -659,7 +634,7 @@ function IFM( params ) { data: ({ api: "changePermissions", dir: self.currentDir, - filename: name, + filename: filename, chmod: e.target.value }), dataType: "json", @@ -679,35 +654,33 @@ function IFM( params ) { /** * Show the remote upload dialog */ - this.remoteUploadDialog = function() { - self.showModal( '
\ -