1 |
// Set this to something unique to this client |
2 |
|
3 |
function update_selected( by ) { |
4 |
var e = $('#clear_selection'); |
5 |
var v = parseInt( e.attr('value') ); |
6 |
e.attr( 'value', v + by ); |
7 |
console.debug('selected', v); |
8 |
return v; |
9 |
} |
10 |
|
11 |
function read_tag( id ) { |
12 |
console.debug('read_tag',id); |
13 |
|
14 |
var item = $('#item'+id); |
15 |
|
16 |
if ( item.length > 0 ) { |
17 |
console.error('element', id, 'allready exists', item); |
18 |
item.addClass('in_range'); |
19 |
} else { |
20 |
var tabindex = update_selected(+1) + 1; |
21 |
|
22 |
$('#koha').append( '<div class="item in_range loading" id="item'+id+'" tabindex='+tabindex+'>Loading item '+id+' from Koha</div>' ) |
23 |
|
24 |
var item = $('#item'+id); |
25 |
|
26 |
$.ajax({ |
27 |
url: '/koha/' + id, |
28 |
success: function(html) { |
29 |
console.info('success',id); |
30 |
item.html( html ); |
31 |
|
32 |
item.removeClass('loading'); |
33 |
|
34 |
item.click( function() { |
35 |
// remove selected item |
36 |
item.fadeOut('slow', function() { |
37 |
console.debug('remove',id); |
38 |
item.remove(); |
39 |
var selected = update_selected( -1 ); |
40 |
console.info('selected', selected); |
41 |
}); |
42 |
}); |
43 |
}, |
44 |
error: function (XMLHttpRequest, textStatus, errorThrown) { |
45 |
item.replace('Error loading: ' + testStatus); |
46 |
item.removeClass('loading'); |
47 |
} |
48 |
}); |
49 |
} |
50 |
} |
51 |
|
52 |
function remove_tag( id ) { |
53 |
var item = $('#item'+id); |
54 |
if ( item.length > 0 ) { |
55 |
console.info('item', id, 'out of range'); |
56 |
item.removeClass('in_range'); |
57 |
} else { |
58 |
console.error('item', id, 'not found on page'); |
59 |
} |
60 |
} |
61 |
|
62 |
function process(data) { |
63 |
var a = data.split('|'); |
64 |
console.info('process', a); |
65 |
|
66 |
if ( a[0] == 'info' ) { |
67 |
$('#info').html( a[1]+'<br><tt>'+a[2]+'</tt>' ); |
68 |
} else if ( a[0] == 'info-none-in-range' ) { |
69 |
$('.in_range').removeClass('in_range'); |
70 |
$('#info').html( 'No items in reader range<br><tt>'+a[1]+'</tt>'); |
71 |
} else if ( a[0] == 'info-in-range' ) { |
72 |
$('#info').html( 'Items in reader range: '+a[1]+'<br><tt>'+a[2]+'</tt>'); |
73 |
$('.in_range').removeClass('in_range'); |
74 |
// FIXME: refresh in_range classes? |
75 |
var items = a[1].split(' '); |
76 |
for ( i in items ) { |
77 |
$('#item'+items[i]).addClass('in_range'); |
78 |
} |
79 |
console.debug(items, 'in range'); |
80 |
} else if ( a[0] == 'read' ) { |
81 |
read_tag( a[1] ); |
82 |
} else if ( a[0] == 'removed' ) { |
83 |
remove_tag( a[1] ); |
84 |
} else { |
85 |
console.error( 'unknown', a ); |
86 |
} |
87 |
}; |
88 |
|
89 |
$(document).ready( function() { |
90 |
|
91 |
var channel = 'test'; |
92 |
|
93 |
Meteor.hostid = '409897502705'; |
94 |
|
95 |
// Our Meteor server is on the data. subdomain |
96 |
Meteor.host = 'data.' + location.hostname; |
97 |
Meteor.port = 4670; |
98 |
|
99 |
// Call the test() function when data arrives |
100 |
Meteor.registerEventCallback("process", process ); |
101 |
|
102 |
console.info('Meteor connect', channel, Meteor.status ); |
103 |
|
104 |
// Join the demo channel and get last five events, then stream |
105 |
Meteor.joinChannel( channel, 5 ); |
106 |
Meteor.mode = 'stream'; |
107 |
|
108 |
// Start streaming! |
109 |
Meteor.connect(); |
110 |
|
111 |
// clear selection |
112 |
$('#clear_selection').click( function() { |
113 |
$('#koha').html('No items selected'); |
114 |
$('#clear_selection').attr('value', '0'); |
115 |
console.debug('clear selection'); |
116 |
}); |
117 |
|
118 |
}); |