From 0cf5797fd51a9a272bb0f033b96866fa83b27a91 Mon Sep 17 00:00:00 2001 From: xevidos Date: Mon, 6 Jan 2020 21:35:50 -0500 Subject: [PATCH] Continued work on updated filemanager for update --- components/active/init.js | 6 +- components/filemanager/init.js | 1912 ++++++++------------------------ 2 files changed, 466 insertions(+), 1452 deletions(-) diff --git a/components/active/init.js b/components/active/init.js index 7da344f..2927643 100755 --- a/components/active/init.js +++ b/components/active/init.js @@ -557,9 +557,11 @@ ////////////////////////////////////////////////////////////////// remove: function( path ) { + + console.log( "remove file", this.isOpen( path ), session.listThumb.hasClass( 'changed' ), closeFile ); if( !this.isOpen( path ) ) return; - var session = this.sessions[path]; - var closeFile = true; + let session = this.sessions[path]; + let closeFile = true; if( session.listThumb.hasClass( 'changed' ) ) { codiad.modal.load( 450, 'components/active/dialog.php?action=confirm&path=' + encodeURIComponent( path ) ); closeFile = false; diff --git a/components/filemanager/init.js b/components/filemanager/init.js index 896c5b2..dd991d6 100755 --- a/components/filemanager/init.js +++ b/components/filemanager/init.js @@ -18,31 +18,24 @@ clipboard: '', controller: 'components/filemanager/controller.php', dialog: 'components/filemanager/dialog.php', - post_max_size: ( 1024*1024 ), - preview: null, - refresh_interval: null, - - init: async function() { + filelist: { - this.noAudio = [ - //Audio + audio: [ 'aac', 'aif', 'mp3', 'mp4', 'wav', 'ogg', - ]; - this.noFiles = [ - //Files + ], + files: [ 'exe', 'pdf', 'zip', 'tar', 'tar.gz', - ]; - this.noImages = [ - //Images + ], + image: [ 'ico', 'icon', 'jpg', @@ -50,22 +43,25 @@ 'png', 'gif', 'bmp', - ]; + ], + }, + files: [], + opened_folders: [], + post_max_size: ( 1024*1024 ), + preview: null, + refresh_interval: null, + selected: [], + + init: async function() { + let _this = this; - this.noOpen = this.noAudio.concat( this.noFiles, this.noImages ), - this.noBrowser = this.noAudio.concat( this.noImages ), - - // Initialize node listener - this.nodeListener(); - this.auto_reload = ( await codiad.settings.get_option( "codiad.filemanager.autoReloadPreview" ) == "true" ); - + /* Reload the page when saving auto reload preview */ amplify.subscribe( 'settings.save', async function() { let option = ( await codiad.settings.get_option( "codiad.filemanager.autoReloadPreview" ) == "true" ); if( option != codiad.filemanager.auto_reload ) { - //codiad.auto_save.reload_interval(); window.location.reload( true ); } }); @@ -73,7 +69,6 @@ /* Subscribe to know when a file become active. */ amplify.subscribe( 'active.onFocus', async function( path ) { - let _this = codiad.filemanager; let editor = codiad.editor.getActive(); if( _this.auto_reload && editor !== null ) { @@ -82,147 +77,14 @@ } }); - // Load uploader - $.loadScript( "components/filemanager/upload_scripts/jquery.ui.widget.js", true ); - $.loadScript( "components/filemanager/upload_scripts/jquery.iframe-transport.js", true ); + /* + maybe we should have this calcualted as the file is being uploaded. + this may allow for a more dynamic upload speed for faster + connections and a more stable upload for slower connections. + */ + _this.calculate_upload_variables(); - $( document ).on( 'dragenter', function( e ) { - - $( '.drop-overlay' ).css( 'display', 'block' ); - $( document ).on( 'keypress', codiad.filemanager.upload_overlay_off ); - }); - - $( '.drop-overlay' ).on( 'drag dragstart dragend dragover dragenter dragleave drop', function( e ) { - - e.preventDefault(); - e.stopPropagation(); - }) - .on( 'dragover dragenter', function() { - - - }) - .on( 'dragleave dragend drop', function() { - - //$( '.drop-overlay' ).css( 'display', 'none' ); - }) - .on( 'drop', function( e ) { - - e.preventDefault(); - e.stopPropagation(); - codiad.filemanager.upload_drop( e ); - }); - - - ( async function( global, $ ) { - - let _this = codiad.filemanager; - let result = await codiad.system.get_ini_setting( 'post_max_size' ); - result = result.toLowerCase() - - console.log( result, result.includes( 'g' ), result.includes( 'm' ), result.includes( 'k' ) ); - - if( result.includes( 'g' ) ) { - - let integer = result.replace( /\D/g, '' ); - - console.log( integer, 1024*1024*1024*integer ); - result = 1024*1024*1024*integer; - } else if( result.includes( 'm' ) ) { - - let integer = result.replace( /^\D+/g, '' ); - console.log( integer, 1024*1024*integer ); - result = 1024*1024*integer; - } else if( result.includes( 'k' ) ) { - - let integer = result.replace( /^\D+/g, '' ); - console.log( integer, 1024*integer ); - result = 1024*integer; - } - - _this.post_max_size = result; - console.log( _this.post_max_size ); - })( this, jQuery ); - }, - - ////////////////////////////////////////////////////////////////// - // Context Menu - ////////////////////////////////////////////////////////////////// - - contextMenuShow: function( e, path, type, name ) { - let _this = this; - - $( '#context-menu a, #context-menu hr' ).hide(); - // Selective options - switch ( type ) { - case 'directory': - $( '#context-menu .directory-only, #context-menu .non-root, #context-menu .both' ).show(); - break; - case 'file': - $( '#context-menu .file-only, #context-menu .non-root, #context-menu .both' ).show(); - break; - case 'root': - $( '#context-menu .directory-only, #context-menu .root-only' ).show(); - $( '#context-menu .non-root' ).hide(); - break; - case 'editor': - $( '#context-menu .editor-only' ).show(); - break; - } - - if( codiad.project.isAbsPath( $( '#file-manager a[data-type="root"]' ).attr( 'data-path' ) ) ) { - $( '#context-menu .no-external' ).hide(); - } else if( type == "editor" ) { - $( '#context-menu .no-external' ).hide(); - } else { - $( '#context-menu .no-external' ).show(); - } - // Show menu - let top = e.pageY; - if( top > $( window ).height() - $( '#context-menu' ).height() ) { - top -= $( '#context-menu' ).height(); - } - if( top < 10 ) { - top = 10; - } - let max = $( window ).height() - top - 10; - - $( '#context-menu' ) - .css( { - 'top': top + 'px', - 'left': e.pageX + 'px', - 'max-height': max + 'px' - }) - .fadeIn( 200 ) - .attr( 'data-path', path ) - .attr( 'data-type', type ) - .attr( 'data-name', name ); - // Show faded 'paste' if nothing in clipboard - if( this.clipboard === '' ) { - $( '#context-menu a[content="Paste"]' ) - .addClass( 'disabled' ); - } else { - $( '#context-menu a[data-action="paste"]' ) - .removeClass( 'disabled' ); - } - // Hide menu - /** - * make sure that the user has moved their mouse far enough - * away from the context menu to warrant a close. - */ - $( '#file-manager, #editor-region' ).on( 'mousemove', codiad.filemanager.contextCheckMouse ); - $( '#context-menu, #editor-region' ).on( 'paste', codiad.editor.paste ); - - /* Notify listeners. */ - amplify.publish( 'context-menu.onShow', { - e: e, - path: path, - type: type - }); - // Hide on click - $( '#context-menu a' ) - .click( function() { - _this.contextMenuHide(); - }); + _this.node_listeners(); }, archive: function( path ) { @@ -240,23 +102,39 @@ }); }, - unarchive: function( path ) { + calculate_upload_variables: async function() { - let _this = this; + let _this = codiad.filemanager; + let result = await codiad.system.get_ini_setting( 'post_max_size' ); + result = result.toLowerCase() - $.get( _this.controller + '?action=unarchive&path=' + encodeURIComponent( path ), function( data ) { + console.log( result, result.includes( 'g' ), result.includes( 'm' ), result.includes( 'k' ) ); + + if( result.includes( 'g' ) ) { - console.log( data ); - let response = codiad.jsend.parse( data ); - console.log( response ); - parent = path.split( '/' ); - parent.pop(); - _this.rescan( parent.join( '/' ) ); - }); + let integer = result.replace( /^\D+/g, '' ); + + console.log( integer, 1024*1024*1024*integer ); + result = 1024*1024*1024*integer; + } else if( result.includes( 'm' ) ) { + + let integer = result.replace( /^\D+/g, '' ); + console.log( integer, 1024*1024*integer ); + result = 1024*1024*integer; + } else if( result.includes( 'k' ) ) { + + let integer = result.replace( /^\D+/g, '' ); + console.log( integer, 1024*integer ); + result = 1024*integer; + } + + _this.post_max_size = result; + console.log( _this.post_max_size ); }, - contextCheckMouse: function( e ) { + context_menu_track_mouse: function() { + let _this = codiad.filemanager; let offset = $( '#context-menu' ).offset(); let bottom = offset.top + $( '#context-menu' ).outerHeight( true ) + 20; let left = offset.left - 20; @@ -265,64 +143,54 @@ if( ( e.clientX > right || e.clientX < left ) || ( e.clientY > bottom || e.clientY < top ) ) { - $( '#file-manager, #editor-region' ).off( 'mousemove', codiad.filemanager.contextCheckMouse ); + $( '#file-manager, #editor-region' ).off( 'mousemove', codiad.filemanager.context_menu_track_mouse ); $( '#context-menu, #editor-region' ).off( 'paste', codiad.editor.paste ); - codiad.filemanager.contextMenuHide(); + _this.hide_context_menu(); } }, - contextMenuHide: function() { - $( '#context-menu' ) - .fadeOut( 200 ); - $( '#file-manager a' ) - .removeClass( 'context-menu-active' ); - /* Notify listeners. */ - amplify.publish( 'context-menu.onHide' ); - }, - ////////////////////////////////////////////////////////////////// // Copy to Clipboard ////////////////////////////////////////////////////////////////// - copyNode: function( path ) { + copy_node: function( path ) { + this.clipboard = path; codiad.message.success( i18n( 'Copied to Clipboard' ) ); }, - ////////////////////////////////////////////////////////////////// - // Create Object - ////////////////////////////////////////////////////////////////// - - createNode: function( path, type ) { + create_node: function( path, type ) { + codiad.modal.load( 250, this.dialog, { action: 'create', type: type, path: path }); $( '#modal-content form' ) - .live( 'submit', function( e ) { + .on( 'submit', function( e ) { + e.preventDefault(); - let shortName = $( '#modal-content form input[name="object_name"]' ) - .val(); - let path = $( '#modal-content form input[name="path"]' ) - .val(); - let type = $( '#modal-content form input[name="type"]' ) - .val(); + let shortName = $( '#modal-content form input[name="object_name"]' ).val(); + let path = $( '#modal-content form input[name="path"]' ).val(); + let type = $( '#modal-content form input[name="type"]' ).val(); let createPath = path + '/' + shortName; + $.get( codiad.filemanager.controller + '?action=create&path=' + encodeURIComponent( createPath ) + '&type=' + type, function( data ) { + let createResponse = codiad.jsend.parse( data ); if( createResponse != 'error' ) { + codiad.message.success( type.charAt( 0 ) .toUpperCase() + type.slice( 1 ) + ' Created' ); codiad.modal.unload(); - // Add new element to filemanager screen - codiad.filemanager.createObject( path, createPath, type ); - if( type == 'file' ) { - codiad.filemanager.openFile( createPath, true ); - } codiad.filemanager.rescan( path ); + if( type == 'file' ) { + + codiad.filemanager.openFile( createPath, true ); + } + /* Notify listeners. */ amplify.publish( 'filemanager.onCreate', { createPath: createPath, @@ -335,46 +203,8 @@ }); }, - ////////////////////////////////////////////////////////////////// - // Create node in file tree - ////////////////////////////////////////////////////////////////// - - createObject: function( parent, path, type ) { - // NODE FORMAT:
  • {short_name}
  • - let parentNode = $( '#file-manager a[data-path="' + parent + '"]' ); - let appendage = null; - if( !$( '#file-manager a[data-path="' + path + '"]' ) - .length ) { // Doesn't already exist - if( parentNode.hasClass( 'open' ) && parentNode.hasClass( 'directory' ) ) { // Only append node if parent is open (and a directory) - let shortName = this.getShortName( path ); - if( type == 'directory' ) { - appendage = '
  • ' + shortName + '
  • '; - } else { - appendage = '
  • ' + shortName + '
  • '; - } - if( parentNode.siblings( 'ul' ) - .length ) { // UL exists, other children to play with - parentNode.siblings( 'ul' ) - .append( appendage ); - } else { - $( '' ) - .insertAfter( parentNode ); - } - } else { - parentNode.parent().children( 'span' ).removeClass( 'none' ); - parentNode.parent().children( 'span' ).addClass( 'plus' ); - } - } - }, - - ////////////////////////////////////////////////////////////////// - // Delete - ////////////////////////////////////////////////////////////////// - - deleteNode: function( path ) { + delete_node: function( path ) { + let _this = this; codiad.modal.load( 400, this.dialog, { action: 'delete', @@ -382,25 +212,31 @@ }); $( '#modal-content form' ) .live( 'submit', function( e ) { + e.preventDefault(); $.get( _this.controller + '?action=delete&path=' + encodeURIComponent( path ), function( data ) { - console.log( data ); - let deleteResponse = codiad.jsend.parse( data ); - if( deleteResponse != 'error' ) { + let response = codiad.jsend.parse( data ); + if( response != 'error' ) { + let node = $( '#file-manager a[data-path="' + path + '"]' ); - let parent_path = node.parent().parent().prev().attr( 'data-path' ); + let parent_path = node.parent().parent().children( 'a' ).attr( 'data-path' ); node.parent( 'li' ).remove(); + // Close any active files - $( '#active-files a' ) - .each( function() { - let curPath = $( this ) - .attr( 'data-path' ); + $( '#active-files a' ).each( function() { + + let curPath = $( this ).attr( 'data-path' ); + + console.log( curPath, curPath.indexOf( path ) ); + if( curPath.indexOf( path ) == 0 ) { + codiad.active.remove( curPath ); } }); + /* Notify listeners. */ amplify.publish( 'filemanager.onDelete', { deletePath: path, @@ -412,41 +248,42 @@ }); }, - deleteInnerNode: function( path ) { + delete_children_nodes: function( path ) { + let _this = this; codiad.modal.load( 400, this.dialog, { action: 'delete', path: path }); $( '#modal-content form' ) - .live( 'submit', function( e ) { + .on( 'submit', function( e ) { + e.preventDefault(); - $.get( _this.controller + '?action=deleteInner&path=' + encodeURIComponent( path ), function( data ) { - let deleteResponse = codiad.jsend.parse( data ); - if( deleteResponse != 'error' ) { - let node = $( '#file-manager a[data-path="' + path + '"]' ).parent( 'ul' ).remove(); + $.get( _this.controller + '?action=delete_children&path=' + encodeURIComponent( path ), function( data ) { + + + let response = codiad.jsend.parse( data ); + if( response != 'error' ) { + + let node = $( '#file-manager a[data-path="' + path + '"]' ); + let parent_path = node.parent().parent().prev().attr( 'data-path' ); + node.parent( 'li' ).remove(); // Close any active files - $( '#active-files a' ) - .each( function() { - let curPath = $( this ) - .attr( 'data-path' ); - if( curPath.indexOf( path ) == 0 ) { + $( '#active-files a' ).each( function() { + + let curPath = $( this ).attr( 'data-path' ); + + console.log( curPath, curPath.indexOf( path ) ); + + if( path.indexOf( curPath ) == 0 ) { + codiad.active.remove( curPath ); } }); - //Rescan Folder - node.parent() - .find( 'a.open' ) - .each( function() { - _this.rescanChildren.push( $( this ) - .attr( 'data-path' ) ); - }); - /* Notify listeners. */ amplify.publish( 'filemanager.onDelete', { - deletePath: path + "/*", path: path }); } @@ -455,39 +292,150 @@ }); }, - ////////////////////////////////////////////////////////////////// - // Download - ////////////////////////////////////////////////////////////////// + display_context_menu: function( e, path, type, name ) { + + let _this = this; + let top = e.pageY; + + $( '#context-menu a, #context-menu hr' ).hide(); + + // Selective options + switch ( type ) { + + case 'directory': + + $( '#context-menu .directory-only, #context-menu .non-root, #context-menu .both' ).show(); + break; + + case 'file': + + $( '#context-menu .file-only, #context-menu .non-root, #context-menu .both' ).show(); + break; + + case 'root': + + $( '#context-menu .directory-only, #context-menu .root-only' ).show(); + $( '#context-menu .non-root' ).hide(); + break; + + case 'editor': + + $( '#context-menu .editor-only' ).show(); + break; + } + + if( codiad.project.isAbsPath( $( '#file-manager a[data-type="root"]' ).attr( 'data-path' ) ) ) { + + $( '#context-menu .no-external' ).hide(); + } else if( type == "editor" ) { + + $( '#context-menu .no-external' ).hide(); + } else { + + $( '#context-menu .no-external' ).show(); + } + + // Show menu + + if( top > $( window ).height() - $( '#context-menu' ).height() ) { + + top -= $( '#context-menu' ).height(); + } + + if( top < 10 ) { + + top = 10; + } + let max = $( window ).height() - top - 10; + + $( '#context-menu' ) + .css( { + 'top': top + 'px', + 'left': e.pageX + 'px', + 'max-height': max + 'px' + }) + .fadeIn( 200 ) + .attr( 'data-path', path ) + .attr( 'data-type', type ) + .attr( 'data-name', name ); + + // Show faded 'paste' if nothing in clipboard + if( this.clipboard === '' ) { + + $( '#context-menu a[content="Paste"]' ) + .addClass( 'disabled' ); + } else { + + $( '#context-menu a[data-action="paste"]' ) + .removeClass( 'disabled' ); + } + + // Hide menu + /** + * make sure that the user has moved their mouse far enough + * away from the context menu to warrant a close. + */ + $( '#file-manager, #editor-region' ).on( 'mousemove', codiad.filemanager.context_menu_track_mouse ); + $( '#context-menu, #editor-region' ).on( 'paste', codiad.editor.paste ); + $( '#context-menu, #editor-region' ).on( 'click', _this.hide_context_menu ); + + /* Notify listeners. */ + amplify.publish( 'context-menu.onShow', { + e: e, + path: path, + type: type + }); + + // Hide on click + $( '#context-menu a' ).on( 'click', _this.hide_context_menu ); + }, download: function( path ) { + let type = this.getType( path ); $( '#download' ) .attr( 'src', 'components/filemanager/download.php?path=' + encodeURIComponent( path ) + '&type=' + type ); + }, - ////////////////////////////////////////////////////////////////// - // Return extension - ////////////////////////////////////////////////////////////////// - - getExtension: function( path ) { - return path.split( '.' ) - .pop(); + get_extension: function( path ) { + + return path.split( '.' ).pop(); }, - ////////////////////////////////////////////////////////////////// - // Return the node name (sans path) - ////////////////////////////////////////////////////////////////// - - getShortName: function( path ) { - return path.split( '/' ) - .pop(); + get_indexes: async function( path ) { + + let r = await $.get( this.controller + '?action=index&path=' + encodeURIComponent( path ) ); + return r; }, - ////////////////////////////////////////////////////////////////// - // Return type - ////////////////////////////////////////////////////////////////// + get_opened_indexes: async function( files ) { + + let _this = codiad.filemanager; + + for( let i = files.length;i--; ) { + + files[i].name = files[i].path; + + if( files[i].type == "directory" && _this.opened_folders.includes( files[i].path ) ) { + + files[i].opened = true; + + let data = await _this.get_indexes( files[i].path ); + let response = codiad.jsend.parse( data ); + let children = _this.get_opened_indexes( response ); + _this.set_children( path, children, response ); + } + } + return files; + }, - getType: function( path ) { + get_short_name: function( path ) { + + return path.split( '/' ).pop(); + }, + + get_type: function( path ) { if( path.match( /\\/g ) ) { @@ -497,139 +445,32 @@ return $( '#file-manager a[data-path="' + path + '"]' ).attr( 'data-type' ); }, - ////////////////////////////////////////////////////////////////// - // Loop out all files and folders in directory path - ////////////////////////////////////////////////////////////////// - - opened_folders: [], - files: [], - - get_indexes: async function( path ) { + hide_context_menu: function() { - let r = await $.get( this.controller + '?action=index&path=' + encodeURIComponent( path ) ); - return r; + $( '#context-menu' ).fadeOut( 200 ); + $( '#file-manager a' ).removeClass( 'context-menu-active' ); + + /* Notify listeners. */ + amplify.publish( 'context-menu.onHide' ); }, - get_index_list: async function( filters = {}, callbacks = [] ) { + index: async function( path, rescan = false ) { let _this = codiad.filemanager; - let data = []; - let response = await _this.get_indexes( codiad.project.getCurrent() ); - response = codiad.jsend.parse( response ); - - if( response.index ) { - - data = response.index; - } - - let children = _this.create_indexes( data, null, filters, callbacks ); - let div = $( '
    ' ); - - div.html( children ); - return div - }, - - find_index: function( path, files ) { - - let _this = this; - let index = {}; - let total = ( !!files ) ? files.length : 0; - - for( let i = 0;i < total;i++ ) { - - if( path == files[i].dirname ) { - - index = files[i]; - break; - } else { - - if( files[i].children !== undefined ) { - - index = _this.find_index( path, files[i].children ); - - if( Object.keys( index ).length > 0 ) { - - break; - } - } - } - } - return index; - }, - - set_index_children: function( path, files, children ) { - - let _this = this; - let index = {}; - let total = ( !!files ) ? files.length : 0; - - for( let i = 0;i < total;i++ ) { - - if( path == files[i].dirname ) { - - files[i].children = children; - index = files[i]; - break; - } else { - - if( files[i].children !== undefined ) { - - index = _this.set_index_children( path, files[i].children, children ); - - if( Object.keys( index ).length > 0 ) { - - break; - } - } - } - } - return index; - }, - - index: async function( path, rescan ) { - - let _this = codiad.filemanager; - let node = $( '#file-manager a[data-path="' + path + '"]' ); - let parentNode = node.parent(); - let span = node.prev(); - let total_saved = _this.files.length; + let children = 0; let container = $( '' ); let files = []; - let open_children = $( '#file-manager a[data-type="root"]' ).parent().find( 'a.open' ); + let node = $( '#file-manager a[data-path="' + path + '"]' ); + let parentNode = node.parent(); let root = false; - let file = _this.find_index( path, _this.files ); - let children = 0; + let span = node.prev(); + let total_saved = _this.files.length; - _this.opened_folders = []; - - if( rescan === undefined ) { - - rescan = false; - } - - if( node.hasClass( "directory" ) ) { - - node.droppable({ - accept: _this.object_accept, - drop: _this.object_drop, - over: _this.object_over, - out: _this.object_out - }); - } - - if( file.children ) { - - children = file.children.length; - } + rescan = !!rescan; node.addClass( 'loading' ); - for( let i = open_children.length;i--; ) { - - _this.opened_folders.push( $( open_children[i] ).attr( "data-path" ) ); - } - if( rescan || total_saved == 0 || ! children ) { let data = await _this.get_indexes( path ); @@ -647,71 +488,48 @@ files = result; } else { - _this.set_index_children( path, _this.files, result ); + _this.set_children( path, _this.files, result ); files = result; } - - let total_opened = _this.opened_folders.length; - for( let i = 0;i < total_opened;i++ ) { - - if( _this.is_child( path, _this.opened_folders[i] ) ) { - - _this.index( _this.opened_folders[i], rescan ); - } - } } else { files = file.children; } - //Add Legacy support for new file layout - for( let i = files.length;i--; ) { - - files[i].name = files[i].path; - } - - console.log( file, files ); + files = _this.get_opened_indexes( files ); + _this.index_nodes( + path, + node, + files, + {}, + { + directory: [_this.index_directory_callback], + file: [_this.index_file_callback], + } + ); /* Notify listener */ amplify.publish( "filemanager.onIndex", { path: path, files: files }); - - root = ( node.attr( 'data-type' ) == 'root' ); - let plus = span.hasClass( 'plus' ); - let minus = span.hasClass( 'minus' ); - let trigger = ( plus || span.hasClass( 'none' ) || root || ( rescan && minus ) ); - - console.log( plus, span.hasClass( 'none' ), root, ( rescan && minus ) ); - console.log( trigger ); - - _this.toggle_open_close( node.parent(), trigger ); - - if( trigger ) { - - _this.create_indexes( files, container ); - let ul = node.parent( 'li' ).children( 'ul' ); - - if( ul.length ) { - - ul.replaceWith( container ); - } else { - - $( container ).insertAfter( node ); - } - - console.log( ul, container ); - } - node.removeClass( 'loading' ); }, - filemanager_index: function( node, container, file, files ) { + index_directory_callback: function( entry, container, i, files ) { - let _this = this; - let link = node.children( 'a' ); + let _this = codiad.filemanager; + entry.children( 'a' ).droppable({ + accept: _this.object_accept, + drop: _this.object_drop, + over: _this.object_over, + out: _this.object_out + }); + }, + + index_file_callback: function( entry, container, i, files ) { - node.draggable({ + let _this = codiad.filemanager; + entry.draggable({ opacity: 0.85, revert: true, @@ -719,98 +537,58 @@ stop: _this.object_stop, zIndex: 100 }); - - if( file.type == "directory" ) { - - link.droppable({ - accept: _this.object_accept, - drop: _this.object_drop, - over: _this.object_over, - out: _this.object_out - }); - } }, - create_indexes: function( files, container = null, filters = {}, callbacks = [] ) { + index_nodes: function( path, node, files, filters, callbacks ) { - let _this = this; + let container = $( '' ); let total_files = files.length; - let root = null; - if( ! container ) { - - let project_path = $( '#project-root' ).attr( 'data-path' ); - let project_name = $( '#project-root' ).text(); - - let a = $( `${project_name}` ); - let li = $( '
  • ' ); - root = $( '' ); - container = $( '' ); - - li.html( a ); - li.append( container ); - root.html( li ); - - console.log( a, li, root, container ); - } else { - - root = container - } + let link = node.children( 'a' ); + let ul = node.parent( 'li' ).children( 'ul' ); for( let i = 0;i < total_files;i++ ) { - let value = files[i]; - let expanded = _this.opened_folders.includes( value.path ); + let v = files[i]; let ext = ''; let name = ''; - let nodeClass = 'none'; + let node_class = 'none'; let entry = $( "
  • " ); let span = $( "" ); let link = $( "" ); let type = null; - if( value.type == "file" ) { + if( v.type == "file" ) { if( filters.type == "directories" ) { continue; } - ext = "ext-" + value.extension; - name = value.basename; + ext = "ext-" + v.extension; + name = v.basename; type = 'file'; link.addClass( ext ); - } else { - if( filters.type == "files" ) { - - continue; - } + } else if( v.type == "directory" ) { - if( value.children !== null ) { + if( v.children ) { - if( expanded ) { + if( v.open ) { - let sub_container = $( '' ); - - nodeClass = 'minus'; - link.addClass( 'open' ); - _this.create_indexes( value.children, sub_container, filters, callbacks ); - $( sub_container ).insertAfter( container ); + node_class = "minus"; + _this.index_nodes( v.path, link, v.children, filters, callbacks ); } else { - nodeClass = 'plus'; + node_class = "plus"; } } - - name = value.basename; - type = 'directory'; } span.addClass( nodeClass ); link.addClass( type ); link.attr( "data-type", type ); - link.attr( "data-path", value.path ); + link.attr( "data-path", v.path ); link.text( name ); entry.append( span, link ); @@ -818,17 +596,39 @@ if( typeof callbacks == "function" ) { - callbacks( entry, container, value, files ); + callbacks( entry, container, v, files ); } else if( Array.isArray( callbacks ) ) { let total_callbacks = callbacks.length; for( let j = 0;j < total_callbacks;j++ ) { - callbacks[j](); + callbacks[j]( entry, container, v, files ); + } + } else if( callbacks === Object( callbacks ) ) { + + if( typeof callbacks[v.type] == "function" ) { + + callbacks[v.type]( entry, container, v, files ); + } else if( Array.isArray( callbacks[v.type] ) ) { + + let total_callbacks = callbacks[v.type].length; + for( let j = 0;j < total_callbacks;j++ ) { + + callbacks[v.type][j]( entry, container, v, files ); + } } } } - return root; + + if( ul.length ) { + + ul.replaceWith( container ); + } else { + + $( container ).insertAfter( node ); + } + + node.removeClass( 'loading' ); }, is_child: function( parent, child ) { @@ -842,11 +642,7 @@ return parentTokens.every( ( t, i ) => child.split( '/' )[i] === t ) }, - ////////////////////////////////////////////////////////////////// - // Listen for dbclick events on nodes - ////////////////////////////////////////////////////////////////// - - nodeListener: function() { + node_listeners: function() { let _this = this; @@ -880,7 +676,7 @@ // Context Menu e.preventDefault(); - _this.contextMenuShow( + _this.display_context_menu( e, $( this ).attr( 'data-path' ), $( this ).attr( 'data-type' ), @@ -905,122 +701,21 @@ .on( 'selectstart', false ); }, - object_accept: function( e, i ) { - - return true; - }, - - object_drag: function( e, i ) { - - }, - - object_drop: function( e, i ) { - - let _this = codiad.filemanager; - let drag = i.helper[0]; - let drop = e.target; - - $( drop ).removeClass( "drag_over" ); - - console.log( drop ); - console.log( drag ); - - let drop_path = $( drop ).attr( "data-path" ); - let drag_path = $( drag ).children( "a" ).attr( "data-path" ); - let path = drag_path; - let newPath = `${drop_path}/` + path.split( "/" ).pop(); - - _this.rename( path, newPath ); - }, - - object_out: function( e, i ) { - - let drag = i.helper[0]; - let drop = e.target; - - $( drop ).removeClass( "drag_over" ); - }, - - object_over: function( e, i ) { - - let drag = i.helper[0]; - let drop = e.target; - - $( drop ).addClass( "drag_over" ); - }, - - object_start: function( e, i ) { - - let drag = i.helper[0]; - $( drag ).addClass( "drag_start" ); - $( drag ).children( 'a' ).removeClass( "a:hover" ); - }, - - object_stop: function( e, i ) { - - let drag = i.helper[0]; - $( drag ).removeClass( "drag_start" ); - //$( drag ).removeClass( "hover" ); - }, - - open_selector: async function( type, callback, limit ) { - - let _this = this; - return new Promise( function( resolve, reject ) { - - codiad.modal.load( - 300, - _this.dialog, - { - action: 'selector', - type: type, - }, - async function( container ) { - - let _this = codiad.filemanager; - let div = await _this.get_index_list( {type: type}, [] ); - _this.selector_listeners( div, limit ); - container.html( div ); - - let select = $( '' ); - let cancel = $( '' ); - - container.append( select ); - container.append( cancel ); - - select.on( 'click', function( e ) { - - codiad.modal.unload(); - resolve( _this.selected ); - }); - cancel.on( 'click', function( e ) { - - codiad.modal.unload(); - reject( null ); - }); - }, - ); - }); - }, - - ////////////////////////////////////////////////////////////////// - // Open File - ////////////////////////////////////////////////////////////////// - - openFile: function( path, focus=true ) { + open_file: function( path, focus = true ) { /* Notify listeners. */ amplify.publish( 'filemanager.onFileWillOpen', { path: path }); + let _this = codiad.filemanager; let node = $( '#file-manager a[data-path="' + path + '"]' ); - let ext = this.getExtension( path ); + let ext = _this.getExtension( path ); - if( $.inArray( ext.toLowerCase(), this.noOpen ) < 0 ) { + if( $.inArray( ext.toLowerCase(), _this.noOpen ) < 0 ) { node.addClass( 'loading' ); - $.get( this.controller + '?action=open&path=' + encodeURIComponent( path ), function( data ) { + $.get( _this.controller + '?action=open&path=' + encodeURIComponent( path ), function( data ) { let openResponse = codiad.jsend.parse( data ); if( openResponse != 'error' ) { @@ -1033,12 +728,12 @@ if( ! codiad.project.isAbsPath( path ) ) { - if( $.inArray( ext.toLowerCase(), this.noBrowser ) < 0 ) { + if( $.inArray( ext.toLowerCase(), _this.noBrowser ) < 0 ) { - this.download( path ); + _this.download( path ); } else { - this.openInModal( path ); + _this.openInModal( path ); } } else { @@ -1047,662 +742,18 @@ } }, - ////////////////////////////////////////////////////////////////// - // Open in browser - ////////////////////////////////////////////////////////////////// - - openInBrowser: function( path ) { - - let _this = this; - - $.ajax( { - url: this.controller + '?action=open_in_browser&path=' + encodeURIComponent( path ), - success: function( data ) { - - console.log( data ) - - let openIBResponse = codiad.jsend.parse( data ); - - console.log( openIBResponse ); - - if( openIBResponse != 'error' ) { - - _this.preview = window.open( openIBResponse, '_newtab' ); - - let editor = codiad.editor.getActive(); - - if( _this.auto_reload && editor !== null ) { - - codiad.editor.getActive().addEventListener( "change", _this.refreshPreview ); - } - - - } - }, - async: false - }); - }, - - openInModal: function( path ) { - - let type = ""; - let ext = this.getExtension( path ).toLowerCase(); - - if( this.noAudio.includes( ext ) ) { - - type = 'music_preview'; - } else if( this.noImages.includes( ext ) ) { - - type = 'preview'; - } - - codiad.modal.load( 250, this.dialog, { - action: type, - path: path - }); - }, - - ////////////////////////////////////////////////////////////////// - // Paste - ////////////////////////////////////////////////////////////////// - - pasteNode: function( path ) { - let _this = this; - if( this.clipboard == '' ) { - codiad.message.error( i18n( 'Nothing in Your Clipboard' ) ); - } else if( path == this.clipboard ) { - codiad.message.error( i18n( 'Cannot Paste Directory Into Itself' ) ); - } else { - let shortName = _this.getShortName( _this.clipboard ); - if( $( '#file-manager a[data-path="' + path + '/' + shortName + '"]' ) - .length ) { // Confirm overwrite? - codiad.modal.load( 400, this.dialog, { - action: 'overwrite', - path: path + '/' + shortName - }); - $( '#modal-content form' ) - .live( 'submit', function( e ) { - e.preventDefault(); - let duplicate = false; - if( $( '#modal-content form select[name="or_action"]' ).val() == 1 ) { - duplicate = true; - //console.log( 'Dup!' ); - } - _this.processPasteNode( path, duplicate ); - }); - } else { // No conflicts; proceed... - _this.processPasteNode( path, false ); - } - } - }, - - processPasteNode: function( path, duplicate ) { - let _this = this; - let shortName = this.getShortName( this.clipboard ); - let type = this.getType( this.clipboard ); - - $.get( this.controller + '?action=duplicate&path=' + - encodeURIComponent( this.clipboard ) + '&destination=' + - encodeURIComponent( path + '/' + shortName ) + '&duplicate=' + encodeURIComponent( duplicate ), - function( data ) { - let pasteResponse = codiad.jsend.parse( data ); - if( pasteResponse != 'error' ) { - _this.createObject( path, path + '/' + shortName, type ); - codiad.modal.unload(); - /* Notify listeners. */ - amplify.publish( 'filemanager.onPaste', { - path: path, - shortName: shortName, - duplicate: duplicate - }); - codiad.filemanager.rescan( path ); - } - }); - }, - - refreshPreview: function( event ) { - - let _this = codiad.filemanager; - - /** - * When reloading after every change, we encounter performance issues - * in the editor. Therefore, we implement the same logic as the - * auto_save module where we only reload after the user has finished - * changing their document. - */ - - if( _this.refresh_interval !== null ) { - - clearTimeout( _this.refresh_interval ); - _this.refresh_interval = null; - } - _this.refresh_interval = setTimeout( function() { - - if( _this.preview == null ) { - - return; - } - - try { - - if( ( typeof _this.preview.location.reload ) == "undefined" ) { - - _this.preview = null; - codiad.editor.getActive().removeEventListener( "change", _this.refreshPreview ); - return; - } - _this.preview.location.reload( true ); - } catch ( e ) { - - console.log( e ); - codiad.message.error( 'Please close your previously opened preview window.' ); - _this.preview = null; - codiad.editor.getActive().removeEventListener( "change", _this.refreshPreview ); - } - }, 500 ); - }, - - rename: function( path, newPath ) { - - let _this = this; - $.get( _this.controller, { - action: 'rename', - path: path, - destination: newPath - }, function( data ) { - - let type = _this.getType( path ); - let renameResponse = codiad.jsend.parse( data ); - let renamedMessage = ""; - let project = codiad.project.getCurrent(); - - if( renameResponse != 'error' ) { - - if( type == undefined ) { - - renamedMessage = 'Successfully Renamed' - } else { - - renamedMessage = type.charAt( 0 ).toUpperCase() + type.slice( 1 ) + ' Renamed' - } - - codiad.message.success( renamedMessage ); - let node = $( '#file-manager a[data-path="' + path + '"]' ); - // Change pathing and name for node - node.attr( 'data-path', newPath ).html( newPath.split( "/" ).pop() ); - if( type == 'file' ) { - - // Change icons for file - curExtClass = 'ext-' + _this.getExtension( path ); - newExtClass = 'ext-' + _this.getExtension( newPath ); - $( '#file-manager a[data-path="' + newPath + '"]' ) - .removeClass( curExtClass ) - .addClass( newExtClass ); - } else { - - // Change pathing on any sub-files/directories - _this.repathSubs( path, newPath ); - } - // Change any active files - codiad.active.rename( path, newPath ); - codiad.modal.unload(); - - let parent = path.split( '/' ); - let newParent = newPath.split( '/' ); - parent.pop(); - newParent.pop(); - - codiad.filemanager.rescan( parent.join( '/' ) ); - codiad.filemanager.rescan( newParent.join( '/' ) ); - - /* Notify listeners. */ - amplify.publish( 'filemanager.onRename', { - path: path, - newPath: newPath, - project: project - }); - } - }); - }, - - ////////////////////////////////////////////////////////////////// - // Rename - ////////////////////////////////////////////////////////////////// - - renameNode: function( path ) { - let shortName = this.getShortName( path ); - let type = this.getType( path ); - let _this = this; - codiad.modal.load( 250, this.dialog, { - action: 'rename', - path: path, - short_name: shortName, - type: type - }); - $( '#modal-content form' ) - .live( 'submit', function( e ) { - let project = codiad.project.getCurrent(); - e.preventDefault(); - let newName = $( '#modal-content form input[name="object_name"]' ).val(); - // Build new path - let arr = path.split( '/' ); - let temp = new Array(); - for( i = 0; i < arr.length - 1; i++ ) { - temp.push( arr[i] ) - } - let newPath = temp.join( '/' ) + '/' + newName; - _this.rename( path, newPath ); - }); - }, - - repathSubs: function( oldPath, newPath ) { - $( '#file-manager a[data-path="' + newPath + '"]' ) - .siblings( 'ul' ) - .find( 'a' ) - .each( function() { - // Hit the children, hit 'em hard - let curPath = $( this ) - .attr( 'data-path' ); - let revisedPath = curPath.replace( oldPath, newPath ); - $( this ) - .attr( 'data-path', revisedPath ); - }); - }, - - rescanChildren: [], - rescanCounter: 0, - - rescan: function( path ) { - - let _this = this; - _this.index( path, true ); - }, - - ////////////////////////////////////////////////////////////////// - // Save file - ////////////////////////////////////////////////////////////////// - - saveFile: function( path, content, callbacks, messages = true ) { - this.saveModifications( path, { - content: content - }, callbacks, messages ); - }, - - saveModifications: function( path, data, callbacks, messages = true ) { - - callbacks = callbacks || {}; - let _this = this, action; - let notifySaveErr = function() { - - codiad.message.error( i18n( 'File could not be saved' ) ); - if( typeof callbacks.error === 'function' ) { - - let context = callbacks.context || _this; - callbacks.error.apply( context, [data] ); - } - } - let post = { - "data": JSON.stringify( data ) - }; - $.post( this.controller + '?action=modify&path=' + encodeURIComponent( path ), post, function( resp ) { - - resp = $.parseJSON( resp ); - if( resp.status == 'success' ) { - if( messages === true ) { - codiad.message.success( i18n( 'File saved' ) ); - } - if( typeof callbacks.success === 'function' ) { - let context = callbacks.context || _this; - callbacks.success.call( context, resp.data.mtime ); - } - } else { - if( resp.message == 'Client is out of sync' ) { - let reload = confirm( - "Server has a more updated copy of the file. Would " + - "you like to refresh the contents ? Pressing no will " + - "cause your changes to override the server's copy upon " + - "next save." - ); - if( reload ) { - codiad.active.close( path ); - codiad.active.removeDraft( path ); - _this.openFile( path ); - } else { - let session = codiad.editor.getActive().getSession(); - session.serverMTime = null; - session.untainted = null; - } - //} else codiad.message.error( i18n( 'File could not be saved' ) ); - } else codiad.message.error( i18n( resp.message ) ); - - if( typeof callbacks.error === 'function' ) { - - let context = callbacks.context || _this; - callbacks.error.apply( context, [resp.data] ); - } - } - }).error( notifySaveErr ); - }, - - savePatch: function( path, patch, mtime, callbacks, alerts ) { - if( patch.length > 0 ) - this.saveModifications( path, { - patch: patch, - mtime: mtime - }, callbacks, alerts ); - else if( typeof callbacks.success === 'function' ) { - let context = callbacks.context || this; - callbacks.success.call( context, mtime ); - } - }, - - ///////////////////////////////////////////////////////////////// - // saveSearchResults - ///////////////////////////////////////////////////////////////// - saveSearchResults: function( searchText, searchType, fileExtensions, searchResults ) { - let lastSearched = { - searchText: searchText, - searchType: searchType, - fileExtension: fileExtensions, - searchResults: searchResults - }; - localStorage.setItem( "lastSearched", JSON.stringify( lastSearched ) ); - }, - - ////////////////////////////////////////////////////////////////// - // Search - ////////////////////////////////////////////////////////////////// - - search: function( path ) { - codiad.modal.load( 500, this.dialog, { - action: 'search', - path: path - }); - - codiad.modal.load_process.done( async function() { - let lastSearched = JSON.parse( await codiad.settings.get_option( "lastSearched" ) ); - if( lastSearched ) { - - $( '#modal-content form input[name="search_string"]' ).val( lastSearched.searchText ); - $( '#modal-content form input[name="search_file_type"]' ).val( lastSearched.fileExtension ); - $( '#modal-content form select[name="search_type"]' ).val( lastSearched.searchType ); - if( lastSearched.searchResults != '' ) { - $( '#filemanager-search-results' ).slideDown().html( lastSearched.searchResults ); - } - } - }); - codiad.modal.hideOverlay(); - let _this = this; - $( '#modal-content form' ).live( 'submit', function( e ) { - $( '#filemanager-search-processing' ).show(); - e.preventDefault(); - searchString = $( '#modal-content form input[name="search_string"]' ).val(); - fileExtensions = $( '#modal-content form input[name="search_file_type"]' ).val(); - searchFileType = $.trim( fileExtensions ); - if( searchFileType != '' ) { - //season the string to use in find command - searchFileType = "\\(" + searchFileType.replace( /\s+/g, "\\|" ) + "\\)"; - } - searchType = $( '#modal-content form select[name="search_type"]' ) - .val(); - let options = { - filetype: fileExtensions, - }; - $.post( _this.controller + '?action=search', { - path: path, - query: searchString, - options: JSON.stringify( options ) - }, function( data ) { - - let searchResponse = codiad.jsend.parse( data ); - let results = ''; - - console.log( data ); - console.log( searchResponse ); - - if( searchResponse != 'error' ) { - $.each( searchResponse.index, function( key, val ) { - // Cleanup file format - if( val['file'].substr( -1 ) == '/' ) { - val['file'] = val['file'].substr( 0, str.length - 1 ); - } - val['file'] = val['file'].replace( '//', '/' ); - // Add result - results += '
    Line ' + val['line'] + ': ' + val['file'] + '
    '; - }); - $( '#filemanager-search-results' ) - .slideDown() - .html( results ); - } else { - $( '#filemanager-search-results' ) - .slideUp(); - } - _this.saveSearchResults( searchString, searchType, fileExtensions, results ); - $( '#filemanager-search-processing' ) - .hide(); - }); - }); - }, - - selected: [], - - selector_listeners: function( node, limit ) { - - let _this = codiad.filemanager; - - $( node ) - .on( 'click', 'a', async function( e ) { - - let i = $( e.target ); - - // Select or Expand - if( codiad.editor.settings.fileManagerTrigger ) { - - _this.toggle_open_close( i.parent(), null, async function() { - - let result = await _this.get_indexes( i.attr( 'data-path' ) ); - result = codiad.jsend.parse( result ); - let indexes = result.index; - let ul = $( '' ); - let children = _this.create_indexes( indexes, ul, {type: 'directories'}, [] ); - i.parent().children( 'ul' ).remove(); - i.parent().append( ul ); - }, - async function() { - - i.parent().children( 'ul' ).remove(); - }); - } else { - - _this.toggle_select_node( $( e.target ), limit ); - } - }) - .on( 'click', 'span', async function( e ) { - - let i = $( e.target ).parent().children( 'a' ); - - _this.toggle_open_close( i.parent(), null, async function() { - - let result = await _this.get_indexes( i.attr( 'data-path' ) ); - result = codiad.jsend.parse( result ); - let indexes = result.index; - let ul = $( '' ); - let children = _this.create_indexes( indexes, ul, {type: 'directories'}, [] ); - i.parent().children( 'ul' ).remove(); - i.parent().append( ul ); - }, - async function() { - - i.parent().children( 'ul' ).remove(); - }); - }) - .on( 'dblclick', 'a', async function( e ) { - - let i = $( e.target ); - - if( ! codiad.editor.settings.fileManagerTrigger ) { - - _this.toggle_open_close( i.parent(), null, async function() { - - let result = await _this.get_indexes( i.attr( 'data-path' ) ); - result = codiad.jsend.parse( result ); - let indexes = result.index; - let ul = $( '' ); - let children = _this.create_indexes( indexes, ul, {type: 'directories'}, [] ); - i.parent().children( 'ul' ).remove(); - i.parent().append( ul ); - }, - async function() { - - i.parent().children( 'ul' ).remove(); - }); - } else { - - _this.toggle_select_node( $( e.target ), limit ); - } - }) - .on( 'selectstart', false ); - }, - - toggle_select_node: function( node, limit = 0 ) { - - let _this = codiad.filemanager; - let selected = false; - let path = node.attr( 'data-path' ); - let i = 1; - - for( i = _this.selected.length;i--; ) { - - if( _this.selected[i] == path ) { - - selected = true; - break; - } - } - - if( limit > 0 && _this.selected.length >= limit ) { - - for( i = _this.selected.length;i--; ) { - - $( `[data-path='${_this.selected[i]}']` ).css( "background", "" ); - } - _this.selected = []; - _this.selected.push( path ); - } else { - - if( selected ) { - - _this.selected.splice( i, 1 ); - } else { - - _this.selected.push( path ); - } - } - - console.log( path, _this.selected ); - }, - - toggle_open_close: function( node, open, open_callback, close_callback ) { - - let span = node.children( 'span' ); - let a = node.children( 'a' ); - let plus = span.hasClass( 'plus' ); - let minus = span.hasClass( 'minus' ); - let ul = node.children( 'ul' ); - - if( open === null ) { - - open = ( plus || span.hasClass( 'none' ) || a.attr( 'data-type' ) == 'root' ); - } - - if( open ) { - - if( plus ) { - - span.removeClass( 'plus' ) - span.addClass( 'minus' ); - } - a.addClass( 'open' ); - - if( typeof open_callback == "function" ) { - - open_callback(); - } - } else { - - span.removeClass( 'minus' ); - span.addClass( 'plus' ); - node.children( 'ul' ) - .slideUp( 300, function() { - - $( this ).remove(); - node.removeClass( 'open' ); - node.children( 'span' ).removeClass( 'minus' ).addClass( 'plus' ); - node.children().find( 'span' ).removeClass( 'minus' ).addClass( 'plus' ); - node.children().find( 'a' ).removeClass( 'open' ); - - if( typeof close_callback == "function" ) { - - close_callback(); - } - }); - } - }, - - ////////////////////////////////////////////////////////////////// - // Upload - ////////////////////////////////////////////////////////////////// - - upload: async function( files, destination = null ) { - - let _this = this; - let uploads = []; - - if( ! destination ) { - - destination = await codiad.filemanager.open_selector( 'directories', null, 1 ); - } - - if( `${destination}`.charAt( destination.length - 1 ) !== "/" ) { - - destination = destination + "/"; - } - - console.log( files ); - console.log( destination ); - - for( let i = files.length;i--; ) { - - let entry = files[i]; - - if( entry.isFile ) { - - entry.file( function( file ) { - - _this.upload_blobs( file, destination + file.name ); - }); - } else if( entry.isDirectory ) { - - _this.upload_read_directory( entry, destination, _this.upload_blobs ); - } - } - }, - - upload_blob: function( blob, path ) { + rename: function( path, new_path ) { return new Promise( function( resolve, reject ) { - let _this = codiad.filemanager; - let form = new FormData(); - - form.append( 'path', path ); - form.append( 'data', blob ); $.ajax({ type: 'POST', - url: _this.controller + '?action=upload', - data: form, - processData: false, - contentType: false, + url: _this.controller + '?action=rename', + data: { + + path: path, + destination: newPath + }, success: function( data ) { resolve( data ); @@ -1715,192 +766,153 @@ }); }, - upload_blobs: function( file, path, start = 0, status = null ) { + rename_node: function( path, new_path ) { + + let shortName = this.getShortName( path ); + let type = this.getType( path ); + let _this = this; + let project = codiad.project.getCurrent(); + + codiad.modal.load( 250, this.dialog, { + action: 'rename', + path: path, + short_name: shortName, + type: type + }); + + $( '#modal-content form' ) + .on( 'submit', async function( e ) { + + e.preventDefault(); + let arr = path.split( '/' ); + let message = "Successfully Renamed." + let newName = $( '#modal-content form input[name="object_name"]' ).val(); + let newParent = newPath.split( '/' ); + let parent = path.split( '/' ); + let temp = []; + + for( i = 0; i < arr.length - 1; i++ ) { + temp.push( arr[i] ) + } + + + let newPath = temp.join( '/' ) + '/' + newName; + let result = codiad.jsend.parse( await _this.rename( path, newPath ) ); + codiad.modal.unload(); + + if( result != 'error' ) { + + if( type !== undefined ) { + + let node = $( '#file-manager a[data-path="' + path + '"]' ); + + + node.attr( 'data-path', newPath ).html( newPath.split( "/" ).pop() ); + message = type.charAt( 0 ).toUpperCase() + type.slice( 1 ) + ' Renamed' + codiad.message.success( message ); + + // Change icons for file + let current_class = 'ext-' + _this.get_extension( path ); + let new_class = 'ext-' + _this.get_extension( newPath ); + + $( '#file-manager a[data-path="' + newPath + '"]' ) + .removeClass( current_class ) + .addClass( new_class ); + codiad.active.rename( path, newPath ); + + parent = parent.pop(); + newParent = newParent.pop(); + + codiad.filemanager.rescan( parent.join( '/' ) ); + codiad.filemanager.rescan( newParent.join( '/' ) ); + + /* Notify listeners. */ + amplify.publish( 'filemanager.onRename', { + path: path, + newPath: newPath, + project: project + }); + } + } + }); + }, + + preview_path: function( path ) { + + + }, + + rescan: function( path ) { - console.log( file, path ); let _this = codiad.filemanager; - let blob_size = 0; - let total_size = file.size; - let current = 0; - let reader = new FileReader(); - let blob = null - let upload_status = null; - let total_blobs = 0; + _this.index( path, true ); + }, + + save_file: function() {}, + + save_modifications: function() {}, + + save_patch: function() {}, + + set_children: function( path, files, children ) { - if( isNaN( _this.post_max_size ) || _this.post_max_size > ( 1024*1024*2 ) ) { - - blob_size = ( 1024*1024*2 ); - } else { - - blob_size = ( _this.post_max_size / 4 ) * 3; - } + let _this = this; + let index = {}; + let total = ( !!files ) ? files.length : 0; - console.log( total_size, blob_size, ( total_size / blob_size ) ); - - if( total_size < blob_size ) { + for( let i = 0;i < total;i++ ) { - blob_size = total_size; - current = start + blob_size; - blob = file; - } else { - - total_blobs = ( Math.round( ( total_size / blob_size ) ) ); - current = start + blob_size; - blob = file.slice( current, current + blob_size ); - } - - console.log( current, blob_size, total_blobs ); - - if( status === null ) { - - status = $().toastmessage( 'showToast', { - text: 'Uploading blobs 0%', - sticky: true, - position: 'top-right', - type: 'warning', - }); - } - - reader.onload = async function( e ) { - - console.log( path, current, total_size ) - upload_status = await _this.upload_blob( e.target.result, path ); - try { + if( path == files[i].dirname ) { - console.log( upload_status ) + files[i].children = children; + index = files[i]; + break; + } else { - let result = JSON.parse( upload_status ); - if( result.bytes > 0 && current <= total_size ) { + if( files[i].children !== undefined ) { - status.text( ( ( current / total_size )*100 ).toFixed( 2 ) + '%' ); + index = _this.set_children( path, files[i].children, children ); - if( current < total_size ) { + if( Object.keys( index ).length > 0 ) { - _this.upload_blobs( file, path, current, status ); + break; } - } else { - - $().toastmessage( 'removeToast', status ); - _this.rescan( path.substring( 0, path.lastIndexOf( "/" ) ) ); } - } catch( exception ) { - - console.log( 'done?', upload_status ); - console.log( exception ) } - }; - reader.readAsDataURL( blob ); - }, - - upload_data: { - - timers: { - off: null, - }, - entries: [], - }, - - upload_drop: function( e ) { - - let _this = codiad.filemanager; - let data = []; - let drop = $( '.drop-overlay' ); - let items = e.originalEvent.dataTransfer.items; - - for( let i = items.length;i--; ) { - - data.push( items[i].webkitGetAsEntry() ); } - - _this.upload_overlay_off(); - _this.upload( data ) + return index; }, - upload_overlay_off: function() { + unarchive: function( path ) { - $( '.drop-overlay' ).css( 'display', 'none' ); - $( document ).off( 'keypress', codiad.filemanager.upload_overlay_off ); - }, - - upload_overlay_on: function( e ) { + let _this = this; - e.preventDefault(); - e.stopPropagation(); - - let _this = codiad.filemanager; - let drop = $( e.target ); - let path = drop.attr( 'data-path' ); - - if( _this.file_.timers.off ) { + $.get( _this.controller + '?action=unarchive&path=' + encodeURIComponent( path ), function( data ) { - clearTimeout( _this.upload_data.timers.off ); - } - - _this.upload_data.timers.off = setTimeout( _this.upload_overlay_off, 1500 ); + console.log( data ); + let response = codiad.jsend.parse( data ); + console.log( response ); + parent = path.split( '/' ); + parent.pop(); + _this.rescan( parent.join( '/' ) ); + }); }, - upload_read_directory: function( item, path, file_callback ) { - - let _this = codiad.filemanager; - let files = []; - - if( item.isFile ) { - - // Get file - item.file( function( file ) { - - file_callback( file, path + file.name ); - }); - } else if( item.isDirectory ) { - - // Get folder contents - let dirReader = item.createReader(); - dirReader.readEntries( function( entries ) { - - let total = entries.length; - for( let i = 0;i < total; i++ ) { - - _this.upload_read_directory( entries[i], path + item.name + "/", file_callback ); - } - }); - } - }, + upload: function() {}, - uploadToNode: function( path ) { - - let _this = codiad.filemanager; - codiad.modal.load( - 500, - this.dialog, { - action: 'upload', - }, - async function( container ) { - - let text = $( '

    ' ) - let input = $( '' ); - - text.html( `Codiad has a new file uploader!
    Drag and drop a file or folder anywhere over the screen at any time
    ( you dont even have to have this window open ) and the upload will prompt for a destination.
    Or
    You can click here to upload files.` ); - - - input.on( 'change', function( e ) { - - console.log( e ); - - let items = e.target.files; - - - _this.upload( items, path ); - }); - text.on( 'click', function( e ) { - - input.click(); - }); - - container.html( '' ); - container.append( text ); - container.append( input ); - } - ); - }, + //Compatibility functions + + copyNode: this.copy_node, + createNode: this.create_node, + createObject: this.create_object, + deleteNode: this.delete_node, + getExtension: this.get_extension, + getShortName: this.get_short_name, + getType: this.get_type, + openFile: this.open_file, + openInBrowser: this.preview, + pasteNode: this.paste_node, + renameNode: this.rename_node, + saveFile: this.save_file, }; })( this, jQuery ); \ No newline at end of file