🎨 Improve syntax and remove current site structure

This commit is contained in:
brunosimon
2017-07-18 02:02:51 +02:00
parent c909611c8c
commit b545869813
223 changed files with 3342 additions and 26501 deletions
+72 -69
View File
@@ -1,55 +1,55 @@
'use strict'
class Event_Emitter
class EventEmitter
{
/**
* Constructor
*/
constructor()
{
this.callbacks = {}
this.callbacks = {}
this.callbacks.base = {}
}
/**
* On
*/
on( names, callback )
on(_names, callback)
{
let that = this
const that = this
// Errors
if( typeof names === 'undefined' || names === '' )
if(typeof _names === 'undefined' || _names === '')
{
console.warn( 'wrong names' )
console.warn('wrong names')
return false
}
if( typeof callback === 'undefined' )
if(typeof callback === 'undefined')
{
console.warn( 'wrong callback' )
console.warn('wrong callback')
return false
}
// Resolve names
names = this.resolve_names( names )
const names = this.resolveNames(names)
// Each name
names.forEach( function( name )
names.forEach(function(_name)
{
// Resolve name
name = that.resolve_name( name )
const name = that.resolveName(_name)
// Create namespace if not exist
if( !( that.callbacks[ name.namespace ] instanceof Object ) )
if(!(that.callbacks[ name.namespace ] instanceof Object))
that.callbacks[ name.namespace ] = {}
// Create callback if not exist
if( !( that.callbacks[ name.namespace ][ name.value ] instanceof Array ) )
if(!(that.callbacks[ name.namespace ][ name.value ] instanceof Array))
that.callbacks[ name.namespace ][ name.value ] = []
// Add callback
that.callbacks[ name.namespace ][ name.value ].push( callback )
that.callbacks[ name.namespace ][ name.value ].push(callback)
})
return this
@@ -58,28 +58,28 @@ class Event_Emitter
/**
* Off
*/
off( names )
off(_names)
{
let that = this
const that = this
// Errors
if( typeof names === 'undefined' || names === '' )
if(typeof _names === 'undefined' || _names === '')
{
console.warn( 'wrong name' )
console.warn('wrong name')
return false
}
// Resolve names
names = this.resolve_names( names )
const names = this.resolveNames(_names)
// Each name
names.forEach( function( name )
names.forEach(function(_name)
{
// Resolve name
name = that.resolve_name( name )
const name = that.resolveName(_name)
// Remove namespace
if( name.namespace !== 'base' && name.value === '' )
if(name.namespace !== 'base' && name.value === '')
{
delete that.callbacks[ name.namespace ]
}
@@ -88,29 +88,29 @@ class Event_Emitter
else
{
// Default
if( name.namespace === 'base' )
if(name.namespace === 'base')
{
// Try to remove from each namespace
for( let namespace in that.callbacks )
for(const namespace in that.callbacks)
{
if( that.callbacks[ namespace ] instanceof Object && that.callbacks[ namespace ][ name.value ] instanceof Array )
if(that.callbacks[ namespace ] instanceof Object && that.callbacks[ namespace ][ name.value ] instanceof Array)
{
delete that.callbacks[ namespace ][ name.value ]
// Remove namespace if empty
if( Object.keys(that.callbacks[ namespace ] ).length === 0 )
if(Object.keys(that.callbacks[ namespace ]).length === 0)
delete that.callbacks[ namespace ]
}
}
}
// Specified namespace
else if( that.callbacks[ name.namespace ] instanceof Object && that.callbacks[ name.namespace ][ name.value ] instanceof Array )
else if(that.callbacks[ name.namespace ] instanceof Object && that.callbacks[ name.namespace ][ name.value ] instanceof Array)
{
delete that.callbacks[ name.namespace ][ name.value ]
// Remove namespace if empty
if( Object.keys( that.callbacks[ name.namespace ] ).length === 0 )
if(Object.keys(that.callbacks[ name.namespace ]).length === 0)
delete that.callbacks[ name.namespace ]
}
}
@@ -122,77 +122,79 @@ class Event_Emitter
/**
* Trigger
*/
trigger( name, args )
trigger(_name, _args)
{
// Errors
if( typeof name === 'undefined' || name === '' )
if(typeof _name === 'undefined' || _name === '')
{
console.warn( 'wrong name' )
console.warn('wrong name')
return false
}
let that = this,
final_result,
result
const that = this
let finalResult = null
let result = null
// Default args
if( !( args instanceof Array ) )
args = []
const args = !(args instanceof Array) ? [] : _args
// Resolve names (should on have one event)
name = this.resolve_names( name )
let name = this.resolveNames(_name)
// Resolve name
name = that.resolve_name( name[ 0 ] )
name = this.resolveName(name[ 0 ])
// Default namespace
if( name.namespace === 'base' )
if(name.namespace === 'base')
{
// Try to find callback in each namespace
for( let namespace in that.callbacks )
for(const namespace in that.callbacks)
{
if( that.callbacks[ namespace ] instanceof Object && that.callbacks[ namespace ][ name.value ] instanceof Array )
if(that.callbacks[ namespace ] instanceof Object && that.callbacks[ namespace ][ name.value ] instanceof Array)
{
that.callbacks[ namespace ][ name.value ].forEach( function( callback )
that.callbacks[ namespace ][ name.value ].forEach(function(callback)
{
result = callback.apply( that,args )
result = callback.apply(that,args)
if( typeof final_result === 'undefined' )
final_result = result
} )
if(typeof finalResult === 'undefined')
{
finalResult = result
}
})
}
}
}
// Specified namespace
else if( this.callbacks[ name.namespace ] instanceof Object )
else if(this.callbacks[ name.namespace ] instanceof Object)
{
if( name.value === '' )
if(name.value === '')
{
console.warn( 'wrong name' )
console.warn('wrong name')
return this
}
that.callbacks[ name.namespace ][ name.value ].forEach( function( callback )
that.callbacks[ name.namespace ][ name.value ].forEach(function(callback)
{
result = callback.apply( that, args )
result = callback.apply(that, args)
if( typeof final_result === 'undefined' )
final_result = result
if(typeof finalResult === 'undefined')
finalResult = result
})
}
return final_result
return finalResult
}
/**
* Resolve names
*/
resolve_names( names )
resolveNames(_names)
{
names = names.replace( /[^a-zA-Z0-9 ,\/.]/g, '' )
names = names.replace( /[,\/]+/g, ' ' )
names = names.split( ' ' )
let names = _names
names = names.replace(/[^a-zA-Z0-9 ,\/.]/g, '')
names = names.replace(/[,\/]+/g, ' ')
names = names.split(' ')
return names
}
@@ -200,22 +202,23 @@ class Event_Emitter
/**
* Resolve name
*/
resolve_name( name )
resolveName(name)
{
let new_name = {},
const newName = {}
const parts = name.split('.')
parts = name.split( '.' )
new_name.original = name
new_name.value = parts[ 0 ]
new_name.namespace = 'base' // Base namespace
newName.original = name
newName.value = parts[ 0 ]
newName.namespace = 'base' // Base namespace
// Specified namespace
if( parts.length > 1 && parts[ 1 ] !== '' )
new_name.namespace = parts[ 1 ]
if(parts.length > 1 && parts[ 1 ] !== '')
{
newName.namespace = parts[ 1 ]
}
return new_name
return newName
}
}
module.exports = Event_Emitter
module.exports = EventEmitter
+88 -82
View File
@@ -1,106 +1,112 @@
'use strict'
// Depedencies
let diff = require( 'diff' ),
util = require( 'util' ),
paths = require( '../utils/paths.class.js' ),
ids = require( '../utils/ids.class.js' )
const diff = require('diff')
const ids = require('../utils/ids.class.js')
class File
{
constructor( _options )
{
// Set up
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.versions = []
this.socket = _options.socket
constructor(_options)
{
// Set up
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.versions = []
this.socket = _options.socket
let name_parts = this.name.split( '.' )
if( name_parts.length > 1 )
this.extension = name_parts[ name_parts.length - 1 ]
else
this.extension = ''
const nameParts = this.name.split('.')
// Create first version
if( typeof _options.content !== 'undefined' )
this.create_version( _options.content )
}
if(nameParts.length > 1)
{
this.extension = nameParts[ nameParts.length - 1 ]
}
else
{
this.extension = ''
}
create_version( content )
{
// Create version
let version = {},
last_version = this.get_last_version()
// Create first version
if(typeof _options.content !== 'undefined')
{
this.createVersion(_options.content)
}
}
version.date = new Date()
version.content = content
createVersion(content)
{
// Create version
const version = {}
const lastVersion = this.getLastVersion()
if( !last_version )
{
version.diff = false
}
else if( version.content === '' )
{
version.diff = [ { count: 1, added: true, removed: undefined, value: 'a'} ]
version.date = new Date()
version.content = content
if( last_version.content === '' )
{
return false
}
}
else
{
version.diff = diff.diffLines(
last_version.content,
version.content,
{
// ignoreWhitespace: true
}
)
if(!lastVersion)
{
version.diff = false
}
else if(version.content === '')
{
version.diff = [{ count: 1, added: true, removed: undefined, value: 'a' }]
// No changed
if( version.diff.length === 1 && last_version.content !== '' )
{
return false
}
}
if(lastVersion.content === '')
{
return false
}
}
else
{
version.diff = diff.diffLines(
lastVersion.content,
version.content,
{
// ignoreWhitespace: true
}
)
// Emit
this.socket.emit( 'create_version', { file: this.path.full, version: version } )
// No changed
if(version.diff.length === 1 && lastVersion.content !== '')
{
return false
}
}
// Save
this.versions.push( version )
}
// Emit
this.socket.emit('createVersion', { file: this.path.full, version })
get_last_version()
{
if( this.versions.length === 0 )
return false
// Save
this.versions.push(version)
}
return this.versions[ this.versions.length - 1 ]
}
getLastVersion()
{
if(this.versions.length === 0)
return false
describe()
{
// Set up
let result = {}
result.id = this.id
result.name = this.name
result.path = this.path
result.versions = this.versions
result.extension = this.extension
return this.versions[ this.versions.length - 1 ]
}
return result
}
describe()
{
// Set up
const result = {}
destructor()
{
result.id = this.id
result.name = this.name
result.path = this.path
result.versions = this.versions
result.extension = this.extension
}
return result
}
destructor()
{
}
}
module.exports = File
+108 -107
View File
@@ -1,145 +1,146 @@
'use strict'
// Dependencies
let paths = require( '../utils/paths.class.js' ),
File = require( './file.class.js' )
const paths = require('../utils/paths.class.js')
const File = require('./file.class.js')
class Files
{
constructor( _options )
{
this.items = {}
this.count = 0
this.socket = _options.socket
this.last_version_date = new Date()
}
constructor(_options)
{
this.items = {}
this.count = 0
this.socket = _options.socket
this.lastVersionDate = new Date()
}
create( _path, _content )
{
// Set up
let normalized_path = paths.normalize( _path ),
parsed_path = paths.parse( normalized_path )
create(_path, _content)
{
// Set up
const normalizedPath = paths.normalize(_path)
const parsedPath = paths.parse(normalizedPath)
// Retrieve file
let file = this.get( normalized_path )
// Retrieve file
let file = this.get(normalizedPath)
// File already exist
if( file )
return false
// File already exist
if(file)
{
return false
}
// Create
file = new File( {
name : parsed_path.base,
path : parsed_path.dir,
content: _content,
socket : this.socket
} )
// Create
file = new File({
name : parsedPath.base,
path : parsedPath.dir,
content: _content,
socket : this.socket
})
// Save
this.items[ normalized_path ] = file
this.count++
this.last_version_date = new Date()
// Save
this.items[ normalizedPath ] = file
this.count++
this.lastVersionDate = new Date()
// Emit
this.socket.emit( 'create_file', file.describe() )
// Emit
this.socket.emit('create_file', file.describe())
return file
}
return file
}
create_version( _path, _content )
{
// Set up
let normalized_path = paths.normalize( _path )
createVersion(_path, _content)
{
// Set up
const normalizedPath = paths.normalize(_path)
// Retrieve file
let file = this.get( normalized_path, true )
// Retrieve file
const file = this.get(normalizedPath, true)
if( typeof _content !== 'undefined' )
{
// Create version
file.create_version( _content )
}
if(typeof _content !== 'undefined')
{
// Create version
file.createVersion(_content)
}
// Save
this.last_version_date = new Date()
// Save
this.lastVersionDate = new Date()
return file
}
return file
}
get( _path, _force_creation )
{
// Params
if( typeof _force_creation === 'undefined' )
_force_creation = false
get(_path, _forceCreation)
{
const forceCreation = typeof _forceCreation === 'undefined' ? false : _forceCreation
// Set up
let normalized_path = paths.normalize( _path )
// Set up
const normalizedPath = paths.normalize(_path)
// Retrieve file
let file = this.items[ normalized_path ]
// Retrieve file
let file = this.items[ normalizedPath ]
// File found
if( typeof file !== 'undefined' )
{
return file
}
// File found
if(typeof file !== 'undefined')
{
return file
}
// Force creation
if( _force_creation )
{
file = this.create( normalized_path )
return file
}
// Force creation
if(forceCreation)
{
file = this.create(normalizedPath)
return file
}
// Not found
return false
}
// Not found
return false
}
delete( _path )
{
// Set up
let normalized_path = paths.normalize( _path )
delete(_path)
{
// Set up
const normalizedPath = paths.normalize(_path)
// Retrieve file
let file = this.get( normalized_path )
// Retrieve file
const file = this.get(normalizedPath)
// File found
if( file )
{
// Delete file
file.destructor()
delete this.items[ normalized_path ]
this.count--
// File found
if(file)
{
// Delete file
file.destructor()
delete this.items[ normalizedPath ]
this.count--
// Save
this.last_version_date = new Date()
// Save
this.lastVersionDate = new Date()
// Emit
this.socket.emit( 'delete_file', file.describe() )
// Emit
this.socket.emit('delete_file', file.describe())
return true
}
return true
}
return false
}
return false
}
describe()
{
// Set up
let result = {}
result.count = this.count
result.items = {}
describe()
{
// Set up
const result = {}
// Each file
for( let _file_path in this.items )
{
let _file = this.items[ _file_path ]
result.count = this.count
result.items = {}
result.items[ _file_path ] = _file.describe()
}
// Each file
for(const _filePath in this.items)
{
const file = this.items[_filePath]
return result
}
result.items[_filePath] = file.describe()
}
return result
}
}
module.exports = Files
+57 -56
View File
@@ -1,75 +1,76 @@
'use strict'
// Dependencies
let slug = require( 'slug' ),
Files = require( './files.class.js' )
const slug = require('slug')
const Files = require('./files.class.js')
class Project
{
constructor( _options )
{
this.set_options( _options )
this.set_name( _options.name )
this.set_socket( _options.socket )
constructor(_options)
{
this.setOptions(_options)
this.setName(_options.name)
this.setSocket(_options.socket)
this.files = new Files( { socket: this.socket } )
this.date = new Date()
}
this.files = new Files({ socket: this.socket })
this.date = new Date()
}
/**
* Set options
*/
set_options( _options )
{
if( typeof _options.debug === 'undefined' )
{
_options.debug = false
}
/**
* Set options
*/
setOptions(_options)
{
if(typeof _options.debug === 'undefined')
{
_options.debug = false
}
// Save
this.options = _options
}
// Save
this.options = _options
}
set_name( _name )
{
this.name = _name
this.slug = slug( this.name, { lower: true } )
}
setName(_name)
{
this.name = _name
this.slug = slug(this.name, { lower: true })
}
set_socket( socket )
{
// Set up
this.original_socket = socket
this.socket = this.original_socket.of( '/project/' + this.slug )
setSocket(socket)
{
// Set up
this.originalSocket = socket
this.socket = this.originalSocket.of('/project/' + this.slug)
// Connection event
this.socket.on( 'connection', ( socket ) =>
{
this.socket.emit( 'update_project', this.describe() )
// Connection event
this.socket.on('connection', (socket) =>
{
this.socket.emit('update_project', this.describe())
// Debug
if( this.options.debug )
{
console.log( 'socket projects'.green.bold + ' - ' + 'connect'.cyan + ' - ' + socket.id.cyan )
}
} )
}
// Debug
if(this.options.debug)
{
console.log('socket projects'.green.bold + ' - ' + 'connect'.cyan + ' - ' + socket.id.cyan)
}
})
}
describe()
{
// Set up
let result = {}
result.name = this.name
result.files = this.files.describe()
result.date = this.date
describe()
{
// Set up
const result = {}
return result
}
result.name = this.name
result.files = this.files.describe()
result.date = this.date
destructor()
{
this.socket.emit( 'destruct' )
}
return result
}
destructor()
{
this.socket.emit('destruct')
}
}
module.exports = Project
+105 -103
View File
@@ -1,136 +1,138 @@
'use strict'
let Project = require( './project.class.js' )
const Project = require('./project.class.js')
class Projects
{
constructor( _options )
{
this.all = {}
constructor(_options)
{
this.all = {}
this.set_options( _options )
this.set_socket( _options.socket )
}
this.setOptions(_options)
this.setSocket(_options.socket)
}
/**
* Set options
*/
set_options( _options )
{
if( typeof _options.debug === 'undefined' )
{
_options.debug = false
}
/**
* Set options
*/
setOptions(_options)
{
if(typeof _options.debug === 'undefined')
{
_options.debug = false
}
// Save
this.options = _options
}
// Save
this.options = _options
}
set_socket( socket )
{
// Set up
this.original_socket = socket
this.socket = this.original_socket.of( '/projects' )
setSocket(socket)
{
// Set up
this.originalSocket = socket
this.socket = this.originalSocket.of('/projects')
// Connection event
this.socket.on( 'connection', ( socket ) =>
{
this.socket.emit( 'update_projects', this.describe() )
// Connection event
this.socket.on('connection', (socket) =>
{
this.socket.emit('update_projects', this.describe())
if( this.options.debug )
{
console.log( 'socket projects'.green.bold + ' - ' + 'connect'.cyan + ' - ' + socket.id.cyan )
}
} )
}
if(this.options.debug)
{
console.log('socket projects'.green.bold + ' - ' + 'connect'.cyan + ' - ' + socket.id.cyan)
}
})
}
create_project( _name )
{
// Create project
let project = new Project( { name: _name, socket: this.original_socket, debug: this.options.debug } ),
same_name_project = this.all[ project.slug ]
createProject(_name)
{
// Create project
const project = new Project({ name: _name, socket: this.originalSocket, debug: this.options.debug })
let sameNameProject = this.all[ project.slug ]
// Try to found same name project
while( typeof same_name_project !== 'undefined' )
{
// Found new number
let last_number = same_name_project.name.match(/\d+$/),
new_number = 2
// Try to found same name project
while(typeof sameNameProject !== 'undefined')
{
// Found new number
let lastNumber = sameNameProject.name.match(/\d+$/)
let newNumber = 2
if( last_number && last_number.length )
{
last_number = ~~last_number[ 0 ]
if(lastNumber && lastNumber.length)
{
lastNumber = ~~lastNumber[ 0 ]
new_number = last_number + 1
}
newNumber = lastNumber + 1
}
// Update project name
project.set_name( _name + ' ' + new_number )
// Update project name
project.set_name(_name + ' ' + newNumber)
// Try to found
same_name_project = this.all[ project.slug ]
}
// Try to found
sameNameProject = this.all[ project.slug ]
}
// Save
this.all[ project.slug ] = project
// Save
this.all[ project.slug ] = project
// Emit
this.socket.emit( 'update_projects', this.describe() )
// Emit
this.socket.emit('update_projects', this.describe())
// Return
return project
}
// Return
return project
}
delete_project( _slug )
{
// Set up
let project = this.all[ _slug ]
deleteProject(_slug)
{
// Set up
const project = this.all[ _slug ]
// Project found
if( typeof project !== 'undefined' )
{
// Delete
delete this.all[ _slug ]
project.destructor()
// Project found
if(typeof project !== 'undefined')
{
// Delete
delete this.all[ _slug ]
project.destructor()
// Emit
this.socket.emit( 'update_projects', this.describe() )
}
}
// Emit
this.socket.emit('update_projects', this.describe())
}
}
get_project_by_slug( _slug )
{
// Find project
let project = this.all[ _slug ]
getProjectBySlug(_slug)
{
// Find project
const project = this.all[ _slug ]
// Found
if( project )
return project
// Found
if(project)
{
return project
}
// Not found
return false
}
// Not found
return false
}
describe()
{
let result = {}
result.all = {}
describe()
{
const result = {}
result.all = {}
for( let _slug in this.all )
{
let _project = this.all[ _slug ]
for(const _slug in this.all)
{
const _project = this.all[ _slug ]
result.all[ _slug ] = {
slug : _project.slug,
name : _project.name,
files_count : _project.files.count,
date : _project.date,
last_update_date: _project.files.last_version_date,
}
}
result.all[ _slug ] = {
slug: _project.slug,
name: _project.name,
filesCount: _project.files.count,
date: _project.date,
lastUpdateDate: _project.files.last_version_date
}
}
return result
}
return result
}
}
module.exports = Projects