Revision 86 (by dpavlin, 2009/04/10 17:57:40) turn debug mode on and test other modes under Firefox 3.5
and none of them work really well...
// Set this to something unique to this client

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( '<div class="item in_range loading" id="item'+id+'" tabindex='+tabindex+'>Loading item '+id+' from Koha</div>' )

		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);

	if ( a[0] == 'info' ) {
		$('#info').html( a[1]+'<br><tt>'+a[2]+'</tt>' );
	} else if ( a[0] == 'info-none-in-range' ) {
		$('.in_range').removeClass('in_range');
		$('#info').html( 'No items in reader range<br><tt>'+a[1]+'</tt>');
	} else if ( a[0] == 'info-in-range' ) {
		$('#info').html( 'Items in reader range: '+a[1]+'<br><tt>'+a[2]+'</tt>');
		$('.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 );
	}
};

$(document).ready( function() {

	var channel = 'test';

	Meteor.hostid = '409897502705';

	// Our Meteor server is on the data. subdomain
	Meteor.host = 'data.' + location.hostname;
	Meteor.port = 4670;

	// Call the test() function when data arrives
	Meteor.registerEventCallback("process", process );

	console.info('Meteor connect', channel, Meteor.status );

	// Join the demo channel and get last five events, then stream
	Meteor.joinChannel( channel, 5 );
	Meteor.mode = 'stream';
//	Meteor.mode = 'iframe';
//	Meteor.mode = 'longpoll';
//	Meteor.mode = 'xhrinteractive';
//	Meteor.mode = 'xhrinteractive';
	Meteor.debugmode = 1;

	// Start streaming!
	Meteor.connect();

	// clear selection
	$('#clear_selection').click( function() {
		$('#koha').html('No items selected');
		$('#clear_selection').attr('value', '0');
		console.debug('clear selection');
	});

});