//data structure for autocompleter //list of available patient IDs var patients = [ '11578995', '12123473', '13578789' ]; //JSON representation of patient information //keyed by patient ID i.e. c_local_ref from patients array var patientDetails = { '11578995' : { 'c_clinician' : 'Dr Finlay', 'c_laboratory' : 'Oxford', 'c_datetested' : '03/04/10', 'c_age' : 67, 'fs_gender' : 'Male', 'c_smoking' : 'Current', 'c_ethnicity' : 'Caucasian', 'fs_performanc' : '2', 'fs_stage' : '0', 'fs_group_methodolog' : 'Pyrosequencing', 'c_egfr_mutat' : 'Mutation', 'fs_sample_res' : 'Yes', 'c_turnaround' : '3', 'fs_sample_mee' : 'Yes', 'c_histology' : 'NSCLC NOS', 'fs_undifferen' : 'Undifferentiated', 'fs_grade' : 'GX', 'c_stain' : 'ABC', 'fs_ihc_protoc' : 'CK 5/6', 'c_technique' : 'Fibre-Optic Bronchoscopy', 'fs_sample_tak' : 'Secondary', 'fs_enriched' : 'Macrodissected', 'c_percentage' : '50', 'c_number_cel' : '3000', 'fs_kras_test' : 'Yes', 'c_comments' : 'Some other comments' }, '12123473' : { 'c_clinician' : 'Dr Hook', 'c_laboratory' : 'Oxford', 'c_datetested' : '03/05/10', 'c_age' : 68, 'fs_gender' : 'Female', 'c_smoking' : 'Stopped', 'c_ethnicity' : 'Other Asian', 'fs_performanc' : '1', 'fs_stage' : 'Occult', 'fs_group_methodolog' : 'DxS EGFR29', 'c_egfr_mutat' : 'Failed sample', 'fs_sample_res' : 'Yes', 'c_turnaround' : '5', 'fs_sample_mee' : 'No', 'c_histology' : 'Carcinoid', 'fs_undifferen' : '', 'fs_grade' : 'G1', 'c_stain' : '', 'fs_ihc_protoc' : 'p63', 'c_technique' : 'Ultrasound Guided', 'fs_sample_tak' : 'Secondary Spread', 'fs_enriched' : 'Microdissected', 'c_percentage' : '100', 'c_number_cel' : '2000', 'fs_kras_test' : 'Yes', 'c_comments' : 'Some other comments' }, '13578789' : { 'c_clinician' : 'Dr Kildare', 'c_laboratory' : 'Bicester', 'c_datetested' : '13/02/10', 'c_age' : 76, 'fs_gender' : 'Female', 'c_smoking' : 'Never Smoker', 'c_ethnicity' : 'Oriental Asian', 'fs_performanc' : '5', 'fs_stage' : 'IA', 'fs_group_methodolog' : 'Other sequencing', 'c_egfr_mutat' : 'Wild', 'fs_sample_res' : 'No', 'c_turnaround' : '2', 'fs_sample_mee' : 'Yes', 'c_histology' : 'Adenocarcinoma', 'fs_undifferen' : 'Undifferentiated', 'fs_grade' : 'GX', 'c_stain' : 'DEF', 'fs_ihc_protoc' : 'CK 5/6|34B12', 'c_technique' : 'Fine Needle Aspirate', 'fs_sample_tak' : 'Primary Tumour', 'fs_enriched' : 'Macrodissected', 'c_percentage' : '10', 'c_number_cel' : '1000', 'fs_kras_test' : 'Yes', 'c_comments' : 'Some comments' } }; //TODO don't forget the 'other' fields //valueMatch should be in questionnaire prompt really. Syntax is identical to server var fieldNames = [ 'c_clinician', 'c_laboratory', 'c_datetested', 'c_age', 'fs_gender', 'c_smoking', 'c_pack_years', 'c_ethnicity', 'fs_performanc', 'fs_stage', 'fs_group_methodolog', 'c_egfr_mutat', 'fs_sample_res', 'c_turnaround', 'fs_sample_mee', 'c_histology', 'fs_undifferen', 'fs_grade', 'c_stain', 'fs_ihc_protoc', 'c_technique', 'fs_sample_tak', 'fs_enriched', 'c_percentage', 'c_number_cel', 'fs_kras_test', 'c_comments' ]; var rules = { 'c_pack_years' : { 'ruleset' : 'cv'} }; var localJs = function(){ //anything here is private var fieldToSearchOn = 'c_local_ref'; return{ //anything here is public /* * Creates a reset button at the top of the form and a cancel button at the bottom * naturally they do the same thing */ addReset : function(){ if($('fs_group_local_ref')){ var p = new Element('p',{'class' : 'buttonholder'}); var reset = new Element('input',{'type' : 'reset', 'class' : 'submit reset'}); p.appendChild(reset); //add a "generate random ID" button var generateRandomId = new Element('button',{'id' : 'generateId', 'class' : 'submit'}); //add text as a node to button for IE generateRandomId.appendChild(document.createTextNode('Generate Reference ID')); generateRandomId.observe('click',localJs.generateRandomId); p.appendChild(generateRandomId); //add a "print labels" button var printPatientLabels = new Element('button',{'id' : 'printPatientLabels', 'class' : 'submit'}); printPatientLabels.appendChild(document.createTextNode('Print patient labels')); p.appendChild(printPatientLabels); $('fs_group_local_ref').appendChild(p); printPatientLabels.observe('click',localJs.printPatientLabels); //also add a cancel button by the submit submitHolder = $$('.submitholder'); if(submitHolder.length != 0){ var submits = $$('.submitholder .submit'); submits[0].writeAttribute('value','Submit'); var cancel = new Element('input',{'type' : 'reset', 'class' : 'submit reset', 'value' : 'Cancel'}); submitHolder[0].appendChild(cancel); } } }, /* * Provides an autocompletion function on the patient ID textbox */ autoComplete : function(){ if($(fieldToSearchOn)){ //first off, add the reset button to the form localJs.addReset(); //construct the element to show the search matches var resultsDiv = new Element('div',{'id':'results'}); $(fieldToSearchOn).up(1).appendChild(resultsDiv); var options = {fullSearch: true, afterUpdateElement: localJs.populate}; new Autocompleter.Local(fieldToSearchOn, resultsDiv, patients, options); } }, generateRandomId : function(event){ event.stop(); var random = Math.round(Math.random() * 100000000); $('c_local_ref').setValue(random); }, /* * inputField is the input on which the autocompleter operates * selectedLi is the li element in the list of options shown by the autocompleter * */ populate : function(inputField, selectedLi){ //first match the local reference to the patient data //on production this may be done using an AJAX call var patientId = inputField.value; fieldNames.each(function(item){ var retrievedValue = patientDetails[patientId][item]; if(retrievedValue === undefined){ //TODO try to do this more neatly, throw $break or something //for some reason this is not present on the data model for the patient, go to the next field //console.log(item + 'is undefined for this person'); }else{ //see if there is a ruleset for whether this field appears or not. var input = $(item); //different methods of population depending on control type //textboxes and texareas if(input.type == 'text' || input.tagName == 'TEXTAREA'){ $(item).value = retrievedValue; } //select boxes (dropdowns) if(input.tagName == 'SELECT'){ var options = input.options; for(i=0;i