Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
588 lines (440 sloc) 17.2 KB
var Application = Application || {};
Application.namespace = Application.namespace || {};
/* Database */
/* Neurons */
Application.namespace.shorthand_map = {
'id': 'i',
'display': 'd',
'display_alternatives': 'a',
'type': 't',
'slug': 's',
'parents': 'p'
};
Application.namespace.install = function ( ) {
var namespace = {
'namespace': { 'key': Application.namespace.shorthand( 'id' ), 'incrementing_key': true, 'unique': 'true' }
};
var namespace_idxs = {};
namespace_idxs[ 'namespace' ] = {
'id': {},
'display': {},
'type': {},
'slug': {}
};
//namespace_idxs[ '' ][ Application.namespace.shorthand( '' ) ] = '';
namespace_idxs.namespace[ 'id' ][ Application.namespace.shorthand( 'id' ) ] = false;
namespace_idxs.namespace[ 'type' ][ Application.namespace.shorthand( 'type' ) ] = false;
namespace_idxs.namespace[ 'display' ][ Application.namespace.shorthand( 'display' ) ] = false;
namespace_idxs.namespace[ 'slug' ][ Application.namespace.shorthand( 'slug' ) ] = false;
console.log( 'Application_namespace_install', namespace, namespace_idxs );
InDB.trigger( 'InDB_do_stores_create', { 'stores': namespace, 'on_success': function( context ) {
InDB.trigger( 'InDB_do_indexes_create', { 'indexes': namespace_idxs, 'on_complete': function( context2 ) {
console.log( 'namespace store loaded', context2 );
} } );
} } );
}
Application.namespace.shorthand = function ( key ) {
if( 'undefined' !== typeof Application.namespace.shorthand_map[ key ] ) {
return Application.namespace.shorthand_map[ key ];
} else {
return key;
}
};
Application.namespace.shorthand_reverse = function ( key ) {
var k = key;
var reversed = {};
for( var item in Application.namespace.shorthand_map ) {
if( Application.namespace.shorthand_map.hasOwnProperty( item ) ) {
reversed[ Application.namespace.shorthand_map[ item ] ] = item;
}
}
if( 'undefined' !== typeof reversed[ k ] ) {
return reversed[ k ];
} else {
return k;
}
};
//recursive
Application.namespace.shorthand_decode = function( object ) {
var encoded = {};
var total = 0;
for( var itemobj in object ) {
if( 'undefined' !== typeof itemobj && object.hasOwnProperty( itemobj ) ) {
//recursive case: object value
//base case: string value
var value = object[ itemobj ];
if( 'object' === typeof value ) {
encoded[ Application.namespace.shorthand_reverse( itemobj ) ] = Application.namespace.shorthand_decode( value );
delete value;
} else {
encoded[ Application.namespace.shorthand_reverse( itemobj ) ] = value;
delete value;
}
}
total++;
}
if( total > 0 ) {
return encoded;
} else {
return {};
}
}
//recursive
Application.namespace.shorthand_encode = function( object ) {
var encoded = {};
for( var item in object ) {
if( object.hasOwnProperty( item ) ) {
//recursive case: object value
//base case: string value
if( 'object' === typeof object[ item ] ) {
encoded[ Application.namespace.shorthand( item ) ] = Application.namespace.shorthand_encode( object[ item ] );
} else {
encoded[ Application.namespace.shorthand( item ) ] = object[ item ];
}
}
}
return encoded;
}
/* Methods */
/* Relationship helpers */
/*
Each takes an object w/attributes { options: obj, on_success: fn, on_error: fn }
*/
Application.namespace.cursor = Application.namespace.cursor || {};
/* Synapses Set */
Application.namespace.setAttr = function( request ) {
if( !!Application.debug ) {
console.log( 'Application.namespace.setAttr', request );
}
var on_success = function( context ) {
if( !!Application.debug ) {
console.log( 'Application.namespace.setAttr success', context );
}
if( 'function' == typeof request.on_success ) {
request.on_success( context );
}
};
var on_error = function( context ) {
if( !!Application.debug ) {
console.log( 'Application.namespace.setAttr error', context );
}
if( 'function' == typeof request.on_error ) {
request.on_error( context );
}
};
request.on_success = on_success;
request.on_error = on_error;
var data = {};
data[ Application.namespace.shorthand( 'strength' ) ] = request.strength;
Application.namespace.update( request );
};
/* Synapses Get */
//TODO: need to implement update/no replace
// ( 'key': string, 'index': string (requred), 'strength': int, 'on_success': fn, 'on_error': fn }
Application.namespace.getAttr = function( request ) {
var on_success = function( value ) {
if( !!Application.debug ) {
console.log( 'Application.namespace.setAttr success', context );
}
if( 'function' == typeof request.on_success ) {
var result = ( 'undefined' !== typeof result ) ? value[ request.attribute ] : null;
request.on_success( result );
}
};
var on_error = function( context ) {
if( !!Application.debug ) {
console.log( 'Application.namespace.getAttr error', context );
}
if( 'function' == typeof request.on_error ) {
request.on_error( context );
}
};
request.on_success = on_success;
request.on_error = on_error;
Application.namespace.get( request );
};
/* Synapses Cursor set */
// ( 'key': string, 'index': string (requred), 'strength': int, 'on_success': fn, 'on_error': fn }
Application.namespace.cursor.setAttr = function( request ) {
var on_success = function( context ) {
console.log( 'Application.namespace.cursor.setAttr success', context );
};
var on_error = function( context ) {
console.log( 'Application.namespace.cursor.setAttr', context );
};
var data = {};
data[ Application.namespace.shorthand( 'strength' ) ] = request.strength;
Application.namespace.cursor.update( request.key, request.index, data, request.replace, replace.expecting, request.on_success, request.on_error, request.left_inclusive, request.right_inclusive );
};
/* Synapses Cursor get */
Application.namespace.cursor.getAttr = function( request ) {
var on_success = function( context ) {
console.log( 'Application.namespace.setAttr success', context );
var value = InDB.cursor.value( context.event );
request.on_success( value[ request.attribute ] );
};
var on_error = function( context ) {
console.log( 'Application.namespace.setAttr error', context );
request.on_error( context );
};
Application.namespace.get( request.key, request.index, request.on_success, request.on_error );
};
/* Single */
/* Get */
Application.namespace.get = function ( request ) {
if( !!Application.debug ) {
console.log( 'Application.namespace.get', request );
}
var on_success = function( context ) {
if( 'function' == typeof request.on_success ) {
var value = Application.namespace.shorthand_decode( InDB.row.value( context ) );
request.on_success( value );
}
}
var on_error = function( context ) {
if( 'function' == typeof request.on_error ) {
request.on_error( context );
}
}
InDB.trigger( 'InDB_do_row_get', { 'store': 'namespace', 'key': request.key, 'index': request.index, 'on_success': on_success, 'on_error': on_error, 'on_abort': request.on_abort, 'on_complete': request.on_complete } );
}
/* Remove */
Application.namespace.delete = function ( request ) {
if( !!Application.debug ) {
console.log( 'Application.namespace.delete', request );
}
var on_success = function( context ) {
if( 'function' == typeof request.on_success ) {
var value = InDB.row.value( context );
request.on_success( value );
}
}
var on_error = function( context ) {
if( 'function' == typeof request.on_error ) {
request.on_error( context );
}
}
InDB.trigger( 'InDB_do_row_delete', { 'store': 'namespace', 'key': request.key, 'on_success': on_success, 'on_error': on_error, 'on_abort': request.on_abort, 'on_complete': request.on_complete } );
}
/* Put */
Application.namespace.put = function ( request ) {
if( !!Application.debug ) {
console.log( 'Application.namespace.put', request );
}
var on_success = function( context ) {
if( 'function' == typeof request.on_success ) {
var value = InDB.row.value( context );
request.on_success( value );
}
}
var on_error = function( context ) {
if( 'function' == typeof request.on_error ) {
request.on_error( context );
}
}
var data = request.data;
if( 'function' !== typeof data ) {
data = Application.namespace.shorthand_encode( data );
}
InDB.trigger( 'InDB_do_row_put', { 'store': 'namespace', 'data': data, 'on_success': on_success, 'on_error': on_error, 'on_abort': request.on_abort, 'on_complete': request.on_complete } );
}
/* Add */
Application.namespace.add = function ( request ) {
if( !!Application.debug ) {
console.log( 'Application.namespace.add', request );
}
var on_success = function( context ) {
if( 'function' == typeof request.on_success ) {
var value = InDB.row.value( context );
request.on_success( value );
}
};
var on_error = function( context ) {
if( 'function' == typeof request.on_error ) {
request.on_error( context );
}
};
var data = request.data;
if( 'function' !== typeof data ) {
data = Application.namespace.shorthand_encode( data );
}
InDB.trigger( 'InDB_do_row_add', { 'store': 'namespace', 'data': data, 'on_success': on_success, 'on_error': on_error, 'on_abort': request.on_abort, 'on_complete': request.on_complete } );
}
/* Update */
Application.namespace.update = function ( request ) {
if( !!Application.debug ) {
console.log( 'Application.namespace.update', request );
}
var on_success = function( context ) {
if( 'function' == typeof request.on_success ) {
var value = InDB.row.value( context );
request.on_success( value );
}
}
var on_error = function( context ) {
if( 'function' == typeof request.on_error ) {
request.on_error( context );
}
}
var data = request.data;
var new_data;
if( 'function' !== typeof data ) {
new_data = Application.namespace.shorthand_encode( data );
} else {
new_data = function( arg ) {
return Application.namespace.shorthand_encode( data( Application.namespace.shorthand_decode( arg ) ) );
};
}
var expected = request.expected;
if( 'function' !== typeof expected ) {
expected = Application.namespace.shorthand_encode( expected );
}
InDB.trigger( 'InDB_do_row_update', { 'store': 'namespace', 'key': request.key, 'index': request.index, 'data': new_data, 'replace': request.replace, 'expected': expected, 'on_success': on_success, 'on_error': on_error, 'on_abort': request.on_abort, 'on_complete': request.on_complete } );
}
/* Multi */
Application.namespace.cursor = Application.namespace.cursor || {};
/* Cursor Get */
Application.namespace.cursor.get = function( request ) {
/* Action */
var index = request.index;
var direction = request.direction;
var limit = request.limit;
var key = request.key;
var begin = request.begin;
var end = request.end;
var left_inclusive = request.left_inclusive;
var right_inclusive = request.right_inclusive;
if( !!Application.debug ) {
console.log( 'Application.namespace.cursor.get', request );
}
jQuery(document).trigger('cursor_get_namespace',request);
/* Defaults */
direction = ( InDB.cursor.isDirection( direction ) ) ? direction : InDB.cursor.direction.next();
limit = ( 'undefined' !== typeof limit ) ? limit : null;
begin = ( 'undefined' !== typeof begin ) ? begin : null;
end = ( 'undefined' !== typeof end ) ? end : null;
left_inclusive = ( 'undefined' !== typeof left_inclusive ) ? left_inclusive : null;
right_inclusive = ( 'undefined' !== typeof right_inclusive ) ? right_inclusive : null;
key = ( 'undefined' !== typeof begin && 'undefined' !== typeof end ) ? key : null;
/* Setup */
var keyRange = InDB.range.get( key, begin, end, left_inclusive, right_inclusive );
/* Callbacks */
var on_success = function ( context ) {
var item = Application.namespace.shorthand_reverse( InDB.cursor.value( context.event ) );
console.log('blah',item);
if( 'function' == typeof request.on_success ) {
if( !!Application.debug ) console.log( 'success', item );
request.on_success( item );
}
};
var on_error = function ( context ) {
if( 'function' == typeof request.on_error ) {
request.on_error( context );
}
};
/* Request */
InDB.trigger( 'InDB_do_cursor_get', { 'store': 'namespace', 'keyRange': keyRange, 'index': index, 'direction': direction, 'limit': limit, 'on_success': on_success, 'on_error': on_error, 'on_abort': request.on_abort, 'on_complete': request.on_complete } );
}
/* Cursor Delete */
Application.namespace.cursor.delete = function( request ) {
/* Setup */
var index = request.index;
var direction = request.direction;
var limit = request.limit;
var key = request.key;
var begin = request.begin;
var end = request.end;
var left_inclusive = request.left_inclusive;
var right_inclusive = request.right_inclusive;
/* Callbacks */
var on_success = function ( context ) {
var result = InDB.cursor.value( context.event );
if( 'function' == typeof request.on_success ) {
request.on_success( result );
}
};
var on_error = function ( context ) {
if( 'function' == typeof request.on_error ) {
request.on_error( context );
}
};
/* Action */
jQuery(document).trigger('cursor_delete_namespace', { "index": index, "key": key, 'direction': direction, 'limit': limit, "begin": begin, "end": end, "left_inclusive": left_inclusive, "right_inclusive": right_inclusive, "on_success": on_success, 'on_error': on_error, 'on_abort': request.on_abort, 'on_complete': request.on_complete } );
/* Defaults */
begin = ( 'undefined' !== typeof begin ) ? begin : null;
end = ( 'undefined' !== typeof end ) ? end : null;
left_inclusive = ( 'undefined' !== typeof left_inclusive ) ? left_inclusive : null;
right_inclusive = ( 'undefined' !== typeof right_inclusive ) ? right_inclusive : null;
direction = ( InDB.cursor.isDirection( direction ) ) ? direction : InDB.cursor.direction.next();
limit = ( 'undefined' !== typeof limit ) ? limit : null;
key = ( 'undefined' !== typeof begin && 'undefined' !== typeof end ) ? key : null;
/* Setup */
var keyRange = InDB.range.get( key, begin, end, left_inclusive, right_inclusive );
/* Request */
InDB.trigger( 'InDB_do_cursor_delete', { 'store': 'namespace', 'keyRange': keyRange, 'index': index, 'direction': direction, 'limit': limit, 'on_success': on_success, 'on_error': on_error, 'on_abort': request.on_abort, 'on_complete': request.on_complete } );
};
/* Cursor Update */
Application.namespace.cursor.update = function( request ) {
/* Setup */
var index = request.index;
var direction = request.direction;
var limit = request.limit;
var key = request.key;
var data = request.data;
var replace = request.replace;
var begin = request.begin;
var end = request.end;
var data = request.data;
var left_inclusive = request.left_inclusive;
var right_inclusive = request.right_inclusive;
/* Callbacks */
var on_success = function ( context ) {
var item = Application.namespace.shorthand_reverse( InDB.cursor.value( context.event ) );
if( 'function' == typeof request.on_success ) {
request.on_success( item );
}
};
var on_error = function ( context ) {
if( 'function' == typeof request.on_error ) {
request.on_error( context );
}
};
/* Shorthand Encoding */
var new_data;
if( 'function' !== typeof data ) {
new_data = Application.namespace.shorthand_encode( data );
} else {
new_data = function( arg ) {
return Application.namespace.shorthand_encode( data( Application.namespace.shorthand_decode( arg ) ) );
};
}
/* Action */
jQuery(document).trigger('cursor_update_namespace', { 'data': new_data, "index": index, "key": key, "begin": begin, "end": end, "left_inclusive": left_inclusive, "right_inclusive": right_inclusive, "replace": replace, 'direction': direction, 'limit': limit, "on_success": on_success, 'on_error': on_error, 'on_abort': request.on_abort, 'on_complete': request.on_complete } );
/* Defaults */
replace = ( true == replace ) ? true : false;
direction = ( InDB.cursor.isDirection( direction ) ) ? direction : InDB.cursor.direction.next();
limit = ( 'undefined' !== typeof limit ) ? limit : null;
begin = ( 'undefined' !== typeof begin ) ? begin : null;
end = ( 'undefined' !== typeof end ) ? end : null;
left_inclusive = ( 'undefined' !== typeof left_inclusive ) ? left_inclusive : null;
right_inclusive = ( 'undefined' !== typeof right_inclusive ) ? right_inclusive : null;
key = ( 'undefined' !== typeof begin && 'undefined' !== typeof end ) ? key : null;
/* Setup */
var keyRange = InDB.range.get( key, begin, end, left_inclusive, right_inclusive );
/* Request */
InDB.trigger( 'InDB_do_cursor_update', { 'store': 'namespace', 'data': new_data, 'keyRange': keyRange, 'index': index, 'replace': replace, 'direction': direction, 'limit': limit, 'on_success': on_success, 'on_error': on_error, 'on_abort': request.on_abort, 'on_complete': request.on_complete } );
}
Application.namespace.clear = function( request ) {
/* Callbacks */
var on_success = function ( context ) {
if( 'function' == typeof request.on_success ) {
request.on_success( context );
}
};
var on_error = function ( context ) {
if( 'function' == typeof request.on_error ) {
request.on_error( context );
}
};
InDB.trigger( 'InDB_do_store_clear', { 'store': 'namespace', 'on_success': on_success, 'on_error': on_error, 'on_abort': request.on_abort } );
};