1 (function(){ 2 3 if ( typeof(jQuery) == 'undefined' ){ 4 document.writeln('<'+'script src="'+DATAFACE_URL+'/js/jquery.packed.js"><'+'/script>'); 5 } 6 7 if ( typeof(window.Xataface) != 'object' ) window.Xataface = {}; 8 window.Xataface.insert = insertRecord; 9 window.Xataface.update = updateRecord; 10 window.Xataface.deleteRecord = deleteRecord; 11 window.Xataface.load = loadRecord; 12 window.Xataface.submit = submitForm; 13 window.Xataface.form = decorateForm; 14 15 16 function decorateForm(form, successCallback, errorCallback, suppressAlerts){ 17 18 var $ = jQuery; 19 $(form).submit(function(){ 20 submitForm(form, successCallback, errorCallback, suppressAlerts); 21 return false; 22 }); 23 24 // Now get extra data about this form. 25 26 var fields = []; 27 $('.xf-field', form).each(function(){ 28 fields.push($(this).attr('name')); 29 }); 30 31 var q = { 32 '-action': 'rest_form', 33 '-table': getFormTable(form), 34 '--fields': fields.join(',') 35 }; 36 $.get(DATAFACE_SITE_HREF, q, function(result){ 37 38 try { 39 if ( typeof(result) == 'string' ){ 40 eval('result='+result+';'); 41 } 42 43 if ( result.code == 200 ){ 44 $.each(result.form, function(key,val){ 45 if ( typeof(val.validators) == 'object' && val.validators.required ){ 46 47 $('.xf-field[name="'+key+'"]', form).each(function(){ 48 var dot = document.createElement('img'); 49 $(dot).attr('src', DATAFACE_URL+'/images/required.gif'); 50 $(dot).attr('title', 'This field is required'); 51 $(this).after(dot); 52 }); 53 54 } 55 }); 56 } else { 57 throw result.message; 58 } 59 } catch (e){ 60 61 alert(e); 62 } 63 64 }); 65 } 66 67 68 function insertRecord(table, values, callback, errorCallback){ 69 70 if ( typeof(callback) != 'function' ) callback = function(){}; 71 if ( typeof(errorCallback) != 'function' ) errorCallback = function(result){ 72 throw result.message; 73 }; 74 75 var q = jQuery.extend({ 76 '-action': 'rest_insert', 77 '-table': table 78 }, values); 79 80 //alert(q['-action']); 81 82 jQuery.post(DATAFACE_SITE_HREF, q, function(result){ 83 try { 84 if ( typeof(result) == 'string' ){ 85 eval('result='+result+';'); 86 } 87 88 if ( result.code == 200 ){ 89 callback(result); 90 } else { 91 errorCallback(result); 92 } 93 94 } catch (e){ 95 alert(e); 96 } 97 98 }); 99 100 } 101 102 103 function getFormTable(form){ 104 var table; 105 if ( $(form).attr('data-xf-table') ){ 106 table = $(form).attr('data-table'); 107 } else { 108 var tableInput = $('input[name="-table"]', form); 109 if ( tableInput.size() > 0 ){ 110 table = tableInput.val(); 111 } else { 112 throw "No table found for form"; 113 } 114 } 115 return table; 116 117 } 118 119 120 function submitForm(form, successCallback, errorCallback, suppressAlerts){ 121 if ( typeof(suppressAlerts) == 'undefined' ) suppressAlerts = false; 122 clearFormErrors(form); 123 var $ = jQuery; 124 var values = {}; 125 var table = getFormTable(form); 126 127 // Now for the values 128 $('.xf-field', form).each(function(){ 129 values[$(this).attr('name')] = $(this).val(); 130 }); 131 132 133 var submitButton = $('input[type="submit"]', form); 134 var progressImage = document.createElement('img'); 135 $(progressImage).attr('src', DATAFACE_URL+'/images/progress.gif'); 136 $(submitButton).after(progressImage); 137 $(submitButton).attr('disabled',true); 138 139 140 function mySuccessCallback(result){ 141 $(progressImage).remove(); 142 $(submitButton).attr('disabled', false); 143 clearFormErrors(form); 144 145 if ( typeof(result.record) == 'object' ){ 146 for ( var i in result.record ){ 147 $('.xf-field[name="'+i+'"]', form).val(result.record[i]); 148 } 149 } 150 if ( typeof(successCallback) == 'function' ) successCallback(result); 151 152 153 } 154 155 function myErrorCallback(result){ 156 $(progressImage).remove(); 157 $(submitButton).attr('disabled', false); 158 if ( typeof(result.errors) != 'undefined' ){ 159 $.each(result.errors, function(i, val){ 160 if ( typeof(i) == 'string' ){ 161 162 // try to find the field 163 $('.xf-field[name="'+i+'"]', form).each(function(){ 164 165 $(this).addClass('submit-error'); 166 var infoIcon = document.createElement('a'); 167 $(infoIcon) 168 .attr('href','#') 169 .attr('title', result.errors[i]) 170 .click(function(){ alert(result.errors[i]); return false;}) 171 .addClass('xf-field-error-icon') 172 .html('<img src="'+DATAFACE_URL+'/images/error.png"/>'); 173 174 $(this).after(infoIcon); 175 176 }); 177 178 } 179 }); 180 } 181 if ( !suppressAlerts ){ 182 183 if ( result.code == 501 ){ 184 alert('Validation Error. Please check the marked fields and try again.'); 185 } else { 186 alert(result.message); 187 } 188 } 189 if ( typeof(errorCallback) == 'function' ) errorCallback(result); 190 } 191 192 if ( $(form).attr('data-recordID') ){ 193 Xataface.update($(form).attr('data-recordID'), values, mySuccessCallback, myErrorCallback); 194 } else { 195 Xataface.insert(table, values, mySuccessCallback, myErrorCallback); 196 } 197 198 } 199 200 201 function clearFormErrors(form){ 202 var $ = jQuery; 203 $('.xf-field-error-icon', form).remove(); 204 $('.xf-field', form).removeClass('submit-error'); 205 206 } 207 208 209 function updateRecord(recordID, values, callback){ 210 throw "Not implemented yet"; 211 } 212 213 function deleteRecord(recordID, callback){ 214 var $ = jQuery; 215 216 var q = { 217 '--record_id': recordID, 218 '-action': 'rest_delete' 219 }; 220 $.post(DATAFACE_SITE_HREF, q, function(res){ 221 try { 222 if ( res.code == 200 ){ 223 if ( typeof(callback) == 'function' ){ 224 callback(res); 225 } 226 } else { 227 if ( res.message ){ 228 throw res.message; 229 } else { 230 throw 'An Unspecified Server Error Has Occurred. Failed to delete record.'; 231 } 232 } 233 } catch (e){ 234 alert(e); 235 } 236 }); 237 238 } 239 240 function loadRecord(recordID, callback){ 241 throw "Not implemented yet"; 242 } 243 244 245 })();