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 |
this.fadeOut('slow', function() { |
37 |
console.debug('remove',id); |
38 |
update_selection( -1 ); |
39 |
this.remove(); |
40 |
}); |
41 |
}); |
42 |
}, |
43 |
error: function (XMLHttpRequest, textStatus, errorThrown) { |
44 |
item.replace('Error loading: ' + testStatus); |
45 |
item.removeClass('loading'); |
46 |
} |
47 |
}); |
48 |
} |
49 |
} |
50 |
|
51 |
function remove_tag( id ) { |
52 |
var item = $('#item'+id); |
53 |
if ( item.length > 0 ) { |
54 |
console.info('item', id, 'out of range'); |
55 |
item.removeClass('in_range'); |
56 |
} else { |
57 |
console.error('item', id, 'not found on page'); |
58 |
} |
59 |
} |
60 |
|
61 |
function process(data) { |
62 |
var a = data.split('|'); |
63 |
console.info('process', a); |
64 |
|
65 |
if ( a[0] == 'info' ) { |
66 |
$('#info').html( a[1]+'<br><tt>'+a[2]+'</tt>' ); |
67 |
} else if ( a[0] == 'info-none-in-range' ) { |
68 |
$('.in_range').removeClass('in_range'); |
69 |
$('#info').html( 'No items in reader range<br><tt>'+a[1]+'</tt>'); |
70 |
} else if ( a[0] == 'info-in-range' ) { |
71 |
$('#info').html( 'Items in reader range: '+a[1]+'<br><tt>'+a[2]+'</tt>'); |
72 |
$('.in_range').removeClass('in_range'); |
73 |
// FIXME: refresh in_range classes? |
74 |
var items = a[1].split(' '); |
75 |
for ( i in items ) { |
76 |
$('#item'+items[i]).addClass('in_range'); |
77 |
} |
78 |
console.debug(items, 'in range'); |
79 |
} else if ( a[0] == 'read' ) { |
80 |
read_tag( a[1] ); |
81 |
} else if ( a[0] == 'removed' ) { |
82 |
remove_tag( a[1] ); |
83 |
} else { |
84 |
console.error( 'unknown', cmd, id ); |
85 |
} |
86 |
}; |
87 |
|
88 |
$(document).ready( function() { |
89 |
|
90 |
var channel = 'test'; |
91 |
|
92 |
Meteor.hostid = '409897502705'; |
93 |
|
94 |
// Our Meteor server is on the data. subdomain |
95 |
Meteor.host = 'data.' + location.hostname; |
96 |
Meteor.port = 4670; |
97 |
|
98 |
// Call the test() function when data arrives |
99 |
Meteor.registerEventCallback("process", process ); |
100 |
|
101 |
console.info('Meteor connect', channel, Meteor.status ); |
102 |
|
103 |
// Join the demo channel and get last five events, then stream |
104 |
Meteor.joinChannel( channel, 5 ); |
105 |
Meteor.mode = 'stream'; |
106 |
|
107 |
// Start streaming! |
108 |
Meteor.connect(); |
109 |
|
110 |
// clear selection |
111 |
$('#clear_selection').click( function() { |
112 |
$('#koha').html('No items selected'); |
113 |
$('#clear_selection').attr('value', '0'); |
114 |
console.debug('clear selection'); |
115 |
}); |
116 |
|
117 |
}); |