Pulling in barcode magic from sample tracker and starting to update it to use ExtJS
[htsworkflow.git] / htsworkflow / frontend / static / js / bcmagic-ext.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 var bcmagic_process_callback = function(response, opt) {
12     //FIXME: temp bypass hack
13     var textStatus = 'success';
14     if (textStatus != 'success')
15     {
16         bcmagic_message('AJAX Status: '+textStatus);
17         return;
18     }
19     
20     var data = Ext.decode(response.responseText);
21     
22     for (key in data)
23     {
24         if (key == 'mode')
25         {
26             if (data['mode'] == 'clear')
27             {
28                 bcmagic_status('','');
29             }
30             else if (data['mode'] == 'redirect')
31             {
32                 if ('url' in data)
33                 {
34                     bcmagic_redirect(data['url']);
35                 }
36                 else
37                 {
38                     bcmagic_status('Error', 'No redirect URL provided by server');
39                 }
40             }
41             else if (data['mode'] == 'autofill')
42             {
43                 bcmagic_autofill(data['field'], data['value'])
44             }
45             else {
46                 bcmagic_message('Message recieved!');
47                 bcmagic_status(data['mode'], data['status']);    
48             }
49             
50         }
51         if (key == 'msg')
52         {
53             bcmagic_message(data['msg']);
54         }
55     }
56 }
57
58 var bcmagic_callback = function(data, textStatus)
59 {
60     if (textStatus != 'success')
61         bcmagic_message('Failed!');
62     else
63         bcmagic_message('Success!');
64 }
65
66 var bcmagic_process = function(){
67     var magic = Ext.get("bcmagic_input_field");
68     var text = magic.getValue();
69     magic.dom.value = '';
70     magic.focus();
71     
72     //var bcm_mode = $("#id_bcm_mode");
73     //var mode = bcm_mode.attr('value');
74     var mode = 'default';
75     
76     // Show what we have captured
77     bcmagic_message('Sent command to server');
78     //$.post('/bcmagic/magic/', {'text': text, 'bcm_mode': mode}, bcmagic_process_callback, 'json');
79     Ext.Ajax.request({
80         url: '/bcmagic/magic/',
81         success: bcmagic_process_callback,
82         failure: function (r, o) { quick_msg('Some AJAX Fail!'); },
83         params: {'text': text, 'bcm_mode': mode}
84     });
85 }
86
87 var bcmagic_keyhandler = function(e) {
88     //Process upon enter key as input.
89     if (e.which == 13)
90       bcmagic_process();
91 }
92
93 //---------------------------------------
94 // Utility Functions
95 //---------------------------------------
96 var bcmagic_message = function(text)
97 {
98     // Show message
99     //$("#bcm_msg").html(text);
100     quick_msg(text);
101     
102     // clear message after 3000ms
103     /*
104     setTimeout(function() {
105         $("#bcm_msg").html('');
106         }, 3000);
107     */
108 }
109
110 var bcmagic_status = function(state, text)
111 {
112     //var msg = $('#bcm_status');
113     if (state.length > 0 || text.length > 0)
114         quick_msg(state+': '+text);
115     //else
116     //    msg.html('');
117 }
118
119
120 var bcmagic_redirect = function(url)
121 {
122     bcmagic_message('Redirecting to:' + url);
123     window.location = url;
124 }
125
126 var bcmagic_autofill = function(field, val)
127 {
128     var txtbox = $('#'+field);
129     txtbox.attr('value', val);
130     
131     var input_fields = $('form input').not(':hidden').not('[type=submit]');
132     
133     // Count the number of text fields which have been filled in.
134     var count = 0;
135     input_fields.each( function(){
136                    if(this.value.length > 0){
137                         count = count + 1;
138                    }
139                 });
140     
141     // If the number of text fields filled in is equal to the number of input_fields in the form, submit the form!
142     if (count == input_fields.length)
143     {
144         bcmagic_status('Form Full', 'Form is now full and ready to process');
145         form = $('form');
146         form.submit();
147         form.reset();
148     
149     }
150     else
151     {
152         bcmagic_status('Form Fill Count', 'Count(' + count +') - Total(' + input_fields.length + ')');
153     }
154 }
155
156 //---------------------------------------
157 // Main Ready Function
158 //---------------------------------------
159 /*$(document).ready(function() {
160         
161         // Grab initial focus on magic text input
162         $("#id_magic").focus();
163         
164         // Set some initial text
165         //$("#id_magic").attr('value','Moo cow!');
166         
167         // Trigger when enterkey is pressed
168         $("#id_magic").keypress(bcmagic_keyhandler)
169 });*/