From ffb9447352eea9a28db838154c15ebca36bda11e Mon Sep 17 00:00:00 2001 From: Marco Dickert Date: Tue, 4 Jul 2017 16:02:45 +0200 Subject: [PATCH] some organizational code changes, prepare for JS templates --- ifm.php | 693 ++++++++++++++++++++++++++++++++++----------------- src/ifm.js | 671 +++++++++++++++++++++++++++++++++---------------- src/main.php | 21 +- 3 files changed, 933 insertions(+), 452 deletions(-) diff --git a/ifm.php b/ifm.php index 78d5c1e..9ad20c5 100644 --- a/ifm.php +++ b/ifm.php @@ -186,14 +186,21 @@ ini_set( 'display_errors', 'OFF' ); class IFM { const VERSION = '2.4.0'; - public function __construct() { + private $config = array(); + + public function __construct( $config ) { session_start(); + if( ! is_array( $config ) ) { + trigger_error( "IFM: could not load config" ); + exit( 1 ); + } else { + $this->config = $config; + } } - /* - this function contains the client-side application + /** + * This function contains the client-side application */ - public function getApplication() { print ' @@ -488,19 +495,49 @@ this.activeTarget=b,this.clear();var c=this.selector+'[data-target="'+b+'"],'+th //# sourceMappingURL=ekko-lightbox.min.js.map - + @@ -1443,6 +1681,9 @@ ifm.init(); $this->getFiles( $_REQUEST["dir"] ); else $this->getFiles( "" ); + } + elseif( $_REQUEST["api"] == "getConfig" ) { + echo json_encode( IFMConfig::getConstants() ); } else { if( isset( $_REQUEST["dir"] ) && $this->isPathValid( $_REQUEST["dir"] ) ) { switch( $_REQUEST["api"] ) { @@ -2262,5 +2503,5 @@ ifm.init(); start program */ -$ifm = new IFM(); +$ifm = new IFM( IFMConfig::getConstants() ); $ifm->run(); diff --git a/src/ifm.js b/src/ifm.js index 71498a9..aafe3e2 100644 --- a/src/ifm.js +++ b/src/ifm.js @@ -1,16 +1,44 @@ -// IFM - js app - -function IFM() { +/** + * IFM constructor + */ +function IFM( params ) { var self = this; // reference to ourself, because "this" does not work within callbacks - this.IFM_SCFN = ""; - this.config = jQuery.parseJSON(''); // serialize the PHP config array, so we can use it in JS too + // set the backend for the application + if( ! params.api ) { + throw new Error( "IFM: no backend configured" ); + } else { + 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 - // modal functions + /** + * Shows a bootstrap modal + * + * @param string content - content of the modal + * @param object options - options for the modal + */ this.showModal = function( content, options = {} ) { var modal = $( document.createElement( 'div' ) ) .addClass( "modal fade" ) @@ -36,24 +64,38 @@ function IFM() { modal.modal('show'); }; + /** + * Hides a bootstrap modal + */ this.hideModal = function() { $('#ifmmodal').modal('hide'); }; + /** + * Reloads the file table + */ this.refreshFileTable = function () { - var id=self.generateGuid(); - self.task_add("Refresh", id); + var id = self.generateGuid(); + self.task_add( "Refresh", id ); $.ajax({ - url: self.IFM_SCFN, + url: self.api, type: "POST", - data: "api=getFiles&dir=" + self.currentDir, + data: { + api: "getFiles", + dir: self.currentDir + }, dataType: "json", success: self.rebuildFileTable, - error: function(response) { ifm.showMessage("General error occured: No or broken response", "e"); }, + error: function( response ) { self.showMessage( "General error occured: No or broken response", "e" ); }, complete: function() { self.task_done( id ); } }); }; + /** + * Rebuilds the file table with fetched items + * + * @param object data - object with items + */ this.rebuildFileTable = function( data ) { var newTBody = $(document.createElement('tbody')); for( var i=0; i < data.length; i++ ) { @@ -142,12 +184,18 @@ function IFM() { }); }; + /** + * Changes the current directory + * + * @param string newdir - target directory + * @param object options - options for changing the directory + */ this.changeDirectory = function( newdir, options={} ) { config = { absolute: false, pushState: true }; jQuery.extend( config, options ); if( ! config.absolute ) newdir = self.pathCombine( self.currentDir, newdir ); $.ajax({ - url: self.IFM_SCFN, + url: self.api, type: "POST", data: ({ api: "getRealpath", @@ -164,6 +212,9 @@ function IFM() { }); }; + /** + * Shows a file, either a new file or an existing + */ this.showFileForm = function () { var filename = arguments.length > 0 ? arguments[0] : "newfile.txt"; var content = arguments.length > 1 ? arguments[1] : ""; @@ -211,6 +262,9 @@ function IFM() { }); }; + /** + * Shows the create directory dialog + */ this.createDirForm = function() { self.showModal( '
\