Pulling in barcode magic from sample tracker and starting to update it to use ExtJS
[htsworkflow.git] / htsworkflow / frontend / static / 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 if (data['mode'] == 'autofill')
37             {
38                 bcmagic_autofill(data['field'], data['value'])
39             }
40             else {
41                 bcmagic_message('Message recieved!');
42                 bcmagic_status(data['mode'], data['status']);    
43             }
44             
45         }
46         if (key == 'msg')
47         {
48             bcmagic_message(data['msg']);
49         }
50     }
51 }
52
53 var bcmagic_callback = function(data, textStatus)
54 {
55     if (textStatus != 'success')
56         bcmagic_message('Failed!');
57     else
58         bcmagic_message('Success!');
59 }
60
61 var bcmagic_process = function(){
62     var magic = $("#id_magic");
63     var text = magic.attr('value');
64     magic.attr('value', '');
65     
66     var bcm_mode = $("#id_bcm_mode");
67     var mode = bcm_mode.attr('value');
68     
69     // Show what we have captured
70     bcmagic_message('Sent command to server');
71     $.post('/bcmagic/magic/', {'text': text, 'bcm_mode': mode}, bcmagic_process_callback, 'json');
72 }
73
74 var bcmagic_keyhandler = function(e) {
75     //Process upon enter key as input.
76     if (e.which == 13)
77       bcmagic_process();
78 }
79
80 //---------------------------------------
81 // Utility Functions
82 //---------------------------------------
83 var bcmagic_message = function(text)
84 {
85     // Show message
86     $("#bcm_msg").html(text);
87     
88     // clear message after 3000ms
89     setTimeout(function() {
90         $("#bcm_msg").html('');
91         }, 3000);
92 }
93
94 var bcmagic_status = function(state, text)
95 {
96     var msg = $('#bcm_status');
97     if (state.length > 0 || text.length > 0)
98         msg.html('<b>'+state+':</b> '+text);
99     else
100         msg.html('');
101 }
102
103
104 var bcmagic_redirect = function(url)
105 {
106     bcmagic_message('Redirecting to:' + url);
107     window.location = url;
108 }
109
110 var bcmagic_autofill = function(field, val)
111 {
112     var txtbox = $('#'+field);
113     txtbox.attr('value', val);
114     
115     var input_fields = $('form input').not(':hidden').not('[type=submit]');
116     
117     // Count the number of text fields which have been filled in.
118     var count = 0;
119     input_fields.each( function(){
120                    if(this.value.length > 0){
121                         count = count + 1;
122                    }
123                 });
124     
125     // If the number of text fields filled in is equal to the number of input_fields in the form, submit the form!
126     if (count == input_fields.length)
127     {
128         bcmagic_status('Form Full', 'Form is now full and ready to process');
129         form = $('form');
130         form.submit();
131         form.reset();
132     
133     }
134     else
135     {
136         bcmagic_status('Form Fill Count', 'Count(' + count +') - Total(' + input_fields.length + ')');
137     }
138 }
139
140 //---------------------------------------
141 // Main Ready Function
142 //---------------------------------------
143 /*$(document).ready(function() {
144         
145         // Grab initial focus on magic text input
146         $("#id_magic").focus();
147         
148         // Set some initial text
149         //$("#id_magic").attr('value','Moo cow!');
150         
151         // Trigger when enterkey is pressed
152         $("#id_magic").keypress(bcmagic_keyhandler)
153 });*/