--- trunk/public_html/koha.js 2009/03/28 14:21:21 72 +++ trunk/public_html/koha.js 2009/03/29 17:53:31 85 @@ -1,18 +1,89 @@ // Set this to something unique to this client -var rfid_visible = {}; +function update_selected( by ) { + var e = $('#clear_selection'); + var v = parseInt( e.attr('value') ); + e.attr( 'value', v + by ); + console.debug('selected', v); + return v; +} + +function read_tag( id ) { + console.debug('read_tag',id); + + var item = $('#item'+id); + + if ( item.length > 0 ) { + console.warn('element', id, 'allready exists', item); + item.addClass('in_range'); + } else { + var tabindex = update_selected(+1) + 1; + + $('#koha').append( '
Loading item '+id+' from Koha
' ) + + var item = $('#item'+id); + + $.ajax({ + url: '/koha/' + id, + success: function(html) { + console.info('success',id); + item.html( html ); + + item.removeClass('loading'); + + item.click( function() { + // remove selected item + item.fadeOut('slow', function() { + console.debug('remove',id); + item.remove(); + var selected = update_selected( -1 ); + console.info('selected', selected); + }); + }); + }, + error: function (XMLHttpRequest, textStatus, errorThrown) { + console.error( textStatus ); + item.replace('Error loading: ' + testStatus); + item.removeClass('loading'); + } + }); + } +} + +function remove_tag( id ) { + var item = $('#item'+id); + if ( item.length > 0 ) { + console.info('item', id, 'out of range'); + item.removeClass('in_range'); + } else { + console.error('item', id, 'not found on page'); + } +} function process(data) { var a = data.split('|'); console.info('process', a); - $('#test').html( a[1] ); - if ( a[0] > 0 && ! rfid_visible[ a[0] ] ) { - $('#koha').append( '
Loading item '+a[0]+' from Koha
' ) - $('#' + a[0]).load( '/koha/' + a[0] ); - rfid_visible[ a[0] ]++; - } else if ( a[0] < 0 ) { - $('#koha').html( 'No RFID chips in range' ); - rfid_visible = {}; + + if ( a[0] == 'info' ) { + $('#info').html( a[1]+'
'+a[2]+'' ); + } else if ( a[0] == 'info-none-in-range' ) { + $('.in_range').removeClass('in_range'); + $('#info').html( 'No items in reader range
'+a[1]+''); + } else if ( a[0] == 'info-in-range' ) { + $('#info').html( 'Items in reader range: '+a[1]+'
'+a[2]+''); + $('.in_range').removeClass('in_range'); + // FIXME: refresh in_range classes? + var items = a[1].split(' '); + for ( i in items ) { + $('#item'+items[i]).addClass('in_range'); + } + console.debug(items, 'in range'); + } else if ( a[0] == 'read' ) { + read_tag( a[1] ); + } else if ( a[0] == 'removed' ) { + remove_tag( a[1] ); + } else { + console.error( 'unknown', a ); } }; @@ -38,5 +109,11 @@ // Start streaming! Meteor.connect(); + // clear selection + $('#clear_selection').click( function() { + $('#koha').html('No items selected'); + $('#clear_selection').attr('value', '0'); + console.debug('clear selection'); + }); });