application.factory( 'project', [ 'config', '$timeout', function( config, $timeout ) { // Result var result = {}; result.name = ''; result.files = {}; result.tree = new Tree( { auto_wash: false } ); // Callback var update_callback = null; result.on_update = function( callback ) { update_callback = callback; }; // Connect result.connect = function( project_name ) { // Connect to socket var socket = io( config.domain + '/project/' + project_name ); // Connect event socket.on( 'connect', function() { console.log('connected'); } ); socket.on( 'create_file', function( data ) { // console.log('create_file'); // console.log(data); var file = result.files[ data.path.full ]; // Doesn't exist yet if( typeof file === 'undefined' ) { file = Object.assign( {}, data ); file.notif = 0; result.files[ data.path.full ] = file; result.tree.add_file( data.path.full, file ) } file.notif++; // Apply callback if( typeof update_callback === 'function' ) update_callback.apply( this, [ data ] ); } ); socket.on( 'create_version', function( data ) { // console.log('create_version'); // console.log(data); var file = result.files[ data.file ]; // Doesn't exist yet if( typeof file !== 'undefined' ) { file.versions.push( data.version ); file.notif++; // Apply callback if( typeof update_callback === 'function' ) update_callback.apply( this, [ data ] ); } } ); socket.on( 'delete_file', function( data ) { // console.log('delete_file'); // console.log(data); var file = result.files[ data.path.full ]; // Doesn't exist yet if( typeof file !== 'undefined' ) { result.tree.remove_file( file.path.full ) delete result.files[ data.path.full ] // Apply callback if( typeof update_callback === 'function' ) update_callback.apply( this, [ data ] ); } } ); // Update project event socket.on( 'update_project', function( data ) { // console.log('update_project'); // console.log(data); // Duplicate files for( var _file_key in data.files ) { var data_file = data.files[ _file_key ], existing_file = result.files[ _file_key ]; // Doesn't exist yet if( typeof existing_file === 'undefined' ) { existing_file = Object.assign( {}, data_file ); existing_file.notif = 0; result.files[ _file_key ] = existing_file; result.tree.add_file( _file_key, existing_file ) } existing_file.notif++; } // Apply callback if( typeof update_callback === 'function' ) update_callback.apply( this, [ data ] ); } ); // Destruct event socket.on( 'destruct', function( data ) { alert( 'destruct' ); } ); }; return result; } ] );