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 var bcmagic_process_callback = function(response, opt) {
12 //FIXME: temp bypass hack
13 var textStatus = 'success';
14 if (textStatus != 'success')
16 bcmagic_message('AJAX Status: '+textStatus);
20 var data = Ext.decode(response.responseText);
26 if (data['mode'] == 'clear')
28 bcmagic_status('','');
30 else if (data['mode'] == 'redirect')
34 bcmagic_redirect(data['url']);
38 bcmagic_status('Error', 'No redirect URL provided by server');
41 else if (data['mode'] == 'autofill')
43 bcmagic_autofill(data['field'], data['value'])
46 bcmagic_message('Message recieved!');
47 bcmagic_status(data['mode'], data['status']);
53 bcmagic_message(data['msg']);
58 var bcmagic_callback = function(data, textStatus)
60 if (textStatus != 'success')
61 bcmagic_message('Failed!');
63 bcmagic_message('Success!');
66 var bcmagic_process = function(){
67 var magic = Ext.get("bcmagic_input_field");
68 var text = magic.getValue();
72 //var bcm_mode = $("#id_bcm_mode");
73 //var mode = bcm_mode.attr('value');
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');
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}
87 var bcmagic_keyhandler = function(e) {
88 //Process upon enter key as input.
93 //---------------------------------------
95 //---------------------------------------
96 var bcmagic_message = function(text)
99 //$("#bcm_msg").html(text);
102 // clear message after 3000ms
104 setTimeout(function() {
105 $("#bcm_msg").html('');
110 var bcmagic_status = function(state, text)
112 //var msg = $('#bcm_status');
113 if (state.length > 0 || text.length > 0)
114 quick_msg(state+': '+text);
120 var bcmagic_redirect = function(url)
122 bcmagic_message('Redirecting to:' + url);
123 window.location = url;
126 var bcmagic_autofill = function(field, val)
128 var txtbox = $('#'+field);
129 txtbox.attr('value', val);
131 var input_fields = $('form input').not(':hidden').not('[type=submit]');
133 // Count the number of text fields which have been filled in.
135 input_fields.each( function(){
136 if(this.value.length > 0){
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)
144 bcmagic_status('Form Full', 'Form is now full and ready to process');
152 bcmagic_status('Form Fill Count', 'Count(' + count +') - Total(' + input_fields.length + ')');
156 //---------------------------------------
157 // Main Ready Function
158 //---------------------------------------
159 /*$(document).ready(function() {
161 // Grab initial focus on magic text input
162 $("#id_magic").focus();
164 // Set some initial text
165 //$("#id_magic").attr('value','Moo cow!');
167 // Trigger when enterkey is pressed
168 $("#id_magic").keypress(bcmagic_keyhandler)