Import of sampletracker.bzr revno 42! What a great revision number to migrate to...
[htsworkflow.git] / www / js / magicbc.js
1 //-----------------------------------------------
2 // Barcode Magic JavaScript
3 // Authors: Brandon W. King
4 // Feb. 2009
5 //-----------------------------------------------
6
7 //---------------------------------------
8 // BCMagic Core Processing AJAX Callback
9 //---------------------------------------
10 var bcmagic_process_callback = function(data, textStatus) {
11     if (textStatus != 'success')
12     {
13         bcmagic_message('AJAX Status: '+textStatus);
14         return;
15     }
16     
17     for (key in data)
18     {
19         if (key == 'mode')
20         {
21             if (data['mode'] == 'clear')
22             {
23                 bcmagic_status('','');
24             }
25             else if (data['mode'] == 'redirect')
26             {
27                 if ('url' in data)
28                 {
29                     bcmagic_redirect(data['url']);
30                 }
31                 else
32                 {
33                     bcmagic_status('Error', 'No redirect URL provided by server');
34                 }
35             }
36             else {
37                 bcmagic_message('Message recieved!');
38                 bcmagic_status(data['mode'], data['status']);    
39             }
40             
41         }
42         if (key == 'msg')
43         {
44             bcmagic_message(data['msg']);
45         }
46     }
47 }
48
49 var bcmagic_callback = function(data, textStatus)
50 {
51     if (textStatus != 'success')
52         bcmagic_message('Failed!');
53     else
54         bcmagic_message('Success!');
55 }
56
57 var bcmagic_process = function(){
58     var magic = $("#id_magic");
59     var text = magic.attr('value');
60     magic.attr('value', '');
61     
62     // Show what we have captured
63     bcmagic_message('Sent command to server');
64     $.post('/bcmagic/magic/', {'text': text}, bcmagic_process_callback, 'json');
65 }
66
67 var bcmagic_keyhandler = function(e) {
68     //Process upon enter key as input.
69     if (e.which == 13)
70       bcmagic_process();
71 }
72
73 //---------------------------------------
74 // Utility Functions
75 //---------------------------------------
76 var bcmagic_message = function(text)
77 {
78     // Show message
79     $("#bcm_msg").html(text);
80     
81     // clear message after 3000ms
82     setTimeout(function() {
83         $("#bcm_msg").html('');
84         }, 3000);
85 }
86
87 var bcmagic_status = function(state, text)
88 {
89     var msg = $('#bcm_status');
90     if (state.length > 0 || text.length > 0)
91         msg.html('<b>'+state+':</b> '+text);
92     else
93         msg.html('');
94 }
95
96
97 var bcmagic_redirect = function(url)
98 {
99     bcmagic_message('Redirecting to:' + url);
100     window.location = url;
101 }
102
103 //---------------------------------------
104 // Main Ready Function
105 //---------------------------------------
106 $(document).ready(function() {
107         
108         // Grab initial focus on magic text input
109         $("#id_magic").focus();
110         
111         // Set some initial text
112         //$("#id_magic").attr('value','Moo cow!');
113         
114         // Trigger when enterkey is pressed
115         $("#id_magic").keypress(bcmagic_keyhandler)
116 });