Update server side structure

This commit is contained in:
Bruno SIMON
2016-06-09 19:07:15 +02:00
parent 4ecc6103f0
commit bd936322ef
6 changed files with 152 additions and 38 deletions
+5 -4
View File
@@ -41,8 +41,8 @@ class App
// Tests
var project = this.projects.create_project( 'dummy' )
project.create_folder( './toto' )
project.get_folder( './toto//tutu/tete', true )
// project.create_folder( './toto' )
// project.get_folder( './toto//tutu/tete', true )
project.create_file( './coucou/coco.txt', '1234' )
project.create_file( './test-1.txt', 'content 1' )
project.update_file( './test-1.txt', 'content 2' )
@@ -50,9 +50,10 @@ class App
project.update_file( './toto/tata/lorem.txt', '123456789' )
project.update_file( './toto/tata/lorem.txt', '1aze' )
project.update_file( './toto/tata/ipsum.txt', 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Mollitia asperiores iure, animi voluptatibus ut officiis. Molestias, quod perferendis hic totam doloremque, porro aperiam enim tenetur, maxime inventore consequuntur nisi in?' )
project.delete_folder( './toto/tutu', true )
project.delete_folder( './toto/tata', true )
// project.delete_file( './toto/tata/ipsum.txt' )
// console.log( util.inspect( project.folders, { depth: null, colors: true } ) )
console.log( util.inspect( project.folders, { depth: null, colors: true } ) )
console.log( util.inspect( project.files, { depth: null, colors: true } ) )
}
/**
+25 -6
View File
@@ -1,19 +1,26 @@
'use strict'
// Depedencies
let diff = require( 'diff' )
let diff = require( 'diff' ),
paths = require( '../utils/paths.class.js' ),
ids = require( '../utils/ids.class.js' )
class File
{
constructor( _name, _content )
constructor( _options )
{
// Set up
this.name = _name
this.id = ids.get_id()
this.name = _options.name
this.path = {}
this.path.directory = _options.path
this.path.full = this.path.directory + '/' + this.name
this.path.parts = this.path.full.split( paths.separator )
this.versions = []
// Create first version
if( typeof _content !== 'undefined' )
this.create_version( _content )
if( typeof _options.content !== 'undefined' )
this.create_version( _options.content )
}
create_version( content )
@@ -38,9 +45,21 @@ class File
return this.versions[ this.versions.length - 1 ]
}
describe()
{
// Set up
let result = {}
result.id = this.id
result.name = this.name
result.path = this.path
result.versions = this.versions
return result
}
destructor()
{
console.log( 'destruct' )
}
}
+79 -24
View File
@@ -2,6 +2,7 @@
// Dependencies
let paths = require( '../utils/paths.class.js' ),
ids = require( '../utils/ids.class.js' ),
util = require( 'util' ),
slug = require( 'slug' ),
File = require( './file.class.js' )
@@ -12,6 +13,7 @@ class Project
{
this.name = options.name
this.slug = slug( this.name )
this.files = {}
this.folders = {
'.':
{
@@ -36,7 +38,7 @@ class Project
console.log( 'socket projects'.green.bold + ' - ' + 'connect'.cyan + ' - ' + socket.id.cyan )
this.socket.emit( 'update_project', this.describe() )
} );
} )
}
create_file( _path, _content )
@@ -56,15 +58,18 @@ class Project
let folder = this.get_folder( parsed_path.dir, true )
// Create
file = new File( parsed_path.base )
folder.files[ file.name ] = file
file = new File( {
name : parsed_path.base,
path : parsed_path.dir,
content: _content
} )
// Has content
if( _content )
file.create_version( _content )
// Save
folder.files[ file.name ] = file.id
this.files[ file.id ] = file
// Emit
this.socket.emit( 'update_project', this.describe() )
this.socket.emit( 'update_file', file.describe() )
return file
}
@@ -87,17 +92,20 @@ class Project
return false
// Retrieve file
let file = folder.files[ parsed_path.base ]
let file_id = folder.files[ parsed_path.base ]
// File found
if( file )
if( file_id )
{
let file = this.files[ file_id ]
return file
}
// Force creation
if( _force_creation )
{
// Create folders
file = this.create_file( normalized_path )
let file = this.create_file( normalized_path )
return file
}
@@ -118,7 +126,9 @@ class Project
file.create_version( _content )
// Emit
this.socket.emit( 'update_project', this.describe() )
this.socket.emit( 'update_file', file.describe() )
return file
}
delete_file( _path )
@@ -132,17 +142,21 @@ class Project
// Folder found
if( folder )
{
let file = folder.files[ parsed_path.base ]
let file_id = folder.files[ parsed_path.base ]
// File found
if( file )
if( file_id )
{
let file = this.files[ file_id ]
// Delete file
file.destructor()
delete folder.files[ parsed_path.base ]
delete folder.files[ file.name ]
delete this.files[ file.id ]
// Emit
this.socket.emit( 'update_project', this.describe() )
this.socket.emit( 'delete_file', file_id )
this.socket.emit( 'update_folders', this.describe_folders )
}
}
}
@@ -174,7 +188,7 @@ class Project
}
// Emit
this.socket.emit( 'update_project', this.describe() )
this.socket.emit( 'update_folders', this.describe_folders() )
return folder
}
@@ -230,38 +244,79 @@ class Project
return false
// Recursive callback
let delete_folder = function( folder )
let empty_folder = ( folder ) =>
{
// Each folder
for( let _folder_name in folder.folders )
{
delete_folder( folder.folders[ _folder_name ] )
empty_folder( folder.folders[ _folder_name ] )
// Delete folder
delete folder.folders[ _folder_name ]
}
// Each file
for( let _file_name in folder.files )
{
let file = folder.files[ _file_name ]
// Set up
let file_id = folder.files[ _file_name ],
file = this.files[ file_id ]
file.destructor()
delete folder.files[ _file_name ]
// File found
if( file )
{
// Delete file
this.delete_file( file.path.full )
}
}
}
delete_folder( folder )
empty_folder( folder )
// Delete folder
let parsed_path = paths.parse( normalized_path ),
parent_folder = this.get_folder( parsed_path.dir )
if( parent_folder )
delete parent_folder.folders[ folder.name ]
// Emit
this.socket.emit( 'update_project', this.describe() )
this.socket.emit( 'update_folders', this.describe_folders() )
return true
}
describe()
{
// Set up
let result = {}
result.name = this.name
result.folders = this.folders
result.folders = this.describe_folders()
result.files = this.describe_files()
return result
}
describe_files()
{
// Set up
let result = {}
// Each file
for( let _file_id in this.files )
{
let _file = this.files[ _file_id ]
result[ _file_id ] = _file.describe()
}
return result
}
describe_folders()
{
return this.folders
}
}
module.exports = Project
+22 -1
View File
@@ -94,9 +94,30 @@ application.factory(
console.log('connected');
} );
socket.on( 'update_file', function( data )
{
console.log('update_file');
console.log(data);
} );
socket.on( 'delete_file', function( data )
{
console.log('delete_file');
console.log(data);
} );
socket.on( 'update_folders', function( data )
{
console.log('update_folders');
console.log(data);
} );
// Update project event
socket.on( 'update_project', function( data )
{
console.log('update_folders');
console.log(data);
// Reformat versions
result.each_version( data, function( version )
{
@@ -140,7 +161,7 @@ application.factory(
} );
};
result.data = {"name":"","folders":{}};
result.data = {'name':'',folders:{},files:{}};
return result;
}
+1 -1
View File
@@ -30,7 +30,7 @@ section.page-project {position:fixed;top:60px;right:0;bottom:0;left:0;}
section.page-project .aside {position:absolute;top:0;bottom:0;left:0;width:240px;}
section.page-project section.file {position:absolute;top:0;right:0;bottom:0;left:240px;}
section.page-project section.file .file-header {position:absolute;top:0;right:0;left:0;height:30px;line-height:30px;padding:0 0 0 10px;background:#00f;}
section.page-project section.file .versions-nav {position:absolute;top:30px;bottom:0;left:0;width:140px;background:#f0f;}
section.page-project section.file .versions-nav {position:absolute;top:30px;bottom:0;left:0;width:140px;background:#555;}
section.page-project section.file .code {position:absolute;top:30px;right:0;bottom:0;left:140px;background:#ff0;}
section.page-project section.file .code pre code {position:absolute;top:0;right:0;bottom:0;left:0;}
+18
View File
@@ -0,0 +1,18 @@
'use strict'
class IDs
{
constructor()
{
this.last_id = 0
}
get_id()
{
return ++this.last_id
}
}
let ids = new IDs()
module.exports = ids