1 //-----------------------------------------------
2 // Barcode Magic JavaScript
3 // Authors: Brandon W. King
5 //-----------------------------------------------
7 //---------------------------------------
8 // BCMagic Core Processing AJAX Callback
9 //---------------------------------------
10 var bcmagic_process_callback = function(data, textStatus) {
11 if (textStatus != 'success')
13 bcmagic_message('AJAX Status: '+textStatus);
21 if (data['mode'] == 'clear')
23 bcmagic_status('','');
25 else if (data['mode'] == 'redirect')
29 bcmagic_redirect(data['url']);
33 bcmagic_status('Error', 'No redirect URL provided by server');
37 bcmagic_message('Message recieved!');
38 bcmagic_status(data['mode'], data['status']);
44 bcmagic_message(data['msg']);
49 var bcmagic_callback = function(data, textStatus)
51 if (textStatus != 'success')
52 bcmagic_message('Failed!');
54 bcmagic_message('Success!');
57 var bcmagic_process = function(){
58 var magic = $("#id_magic");
59 var text = magic.attr('value');
60 magic.attr('value', '');
62 // Show what we have captured
63 bcmagic_message('Sent command to server');
64 $.post('/bcmagic/magic/', {'text': text}, bcmagic_process_callback, 'json');
67 var bcmagic_keyhandler = function(e) {
68 //Process upon enter key as input.
73 //---------------------------------------
75 //---------------------------------------
76 var bcmagic_message = function(text)
79 $("#bcm_msg").html(text);
81 // clear message after 3000ms
82 setTimeout(function() {
83 $("#bcm_msg").html('');
87 var bcmagic_status = function(state, text)
89 var msg = $('#bcm_status');
90 if (state.length > 0 || text.length > 0)
91 msg.html('<b>'+state+':</b> '+text);
97 var bcmagic_redirect = function(url)
99 bcmagic_message('Redirecting to:' + url);
100 window.location = url;
103 //---------------------------------------
104 // Main Ready Function
105 //---------------------------------------
106 $(document).ready(function() {
108 // Grab initial focus on magic text input
109 $("#id_magic").focus();
111 // Set some initial text
112 //$("#id_magic").attr('value','Moo cow!');
114 // Trigger when enterkey is pressed
115 $("#id_magic").keypress(bcmagic_keyhandler)