4627 |
21 Nov 17 |
nicklas |
var MapData = function() |
4627 |
21 Nov 17 |
nicklas |
2 |
{ |
4627 |
21 Nov 17 |
nicklas |
var map = {}; |
4627 |
21 Nov 17 |
nicklas |
var debug = 0; |
4627 |
21 Nov 17 |
nicklas |
5 |
|
4627 |
21 Nov 17 |
nicklas |
map.initPage = function() |
4627 |
21 Nov 17 |
nicklas |
7 |
{ |
4627 |
21 Nov 17 |
nicklas |
// Step 1 |
4627 |
21 Nov 17 |
nicklas |
Events.addEventHandler('step-1', 'wizard-validate', map.validateStep1); |
4627 |
21 Nov 17 |
nicklas |
Wizard.initFileSelectionField('file'); |
5663 |
11 Oct 19 |
nicklas |
Events.addEventHandler('file', 'change', map.fileChanged); |
5663 |
11 Oct 19 |
nicklas |
12 |
|
5663 |
11 Oct 19 |
nicklas |
Events.addEventHandler('mapFrom', 'change', map.mapFromChanged); |
4627 |
21 Nov 17 |
nicklas |
14 |
|
4627 |
21 Nov 17 |
nicklas |
// Navigation |
4627 |
21 Nov 17 |
nicklas |
Buttons.addClickHandler('gocancel', Wizard.cancelWizard); |
4627 |
21 Nov 17 |
nicklas |
Buttons.addClickHandler('gorestart', Wizard.restartWizard); |
4627 |
21 Nov 17 |
nicklas |
Buttons.addClickHandler('goexport', Wizard.goRegister); |
4627 |
21 Nov 17 |
nicklas |
Buttons.addClickHandler('godownload', map.download); |
4627 |
21 Nov 17 |
nicklas |
20 |
|
4627 |
21 Nov 17 |
nicklas |
// Final registration |
4627 |
21 Nov 17 |
nicklas |
Events.addEventHandler('wizard', 'wizard-submit', map.submit); |
4627 |
21 Nov 17 |
nicklas |
23 |
|
4627 |
21 Nov 17 |
nicklas |
Doc.show('step-1'); |
4627 |
21 Nov 17 |
nicklas |
Doc.show('goexport'); |
5663 |
11 Oct 19 |
nicklas |
Events.sendChangeEvent('mapFrom'); |
4627 |
21 Nov 17 |
nicklas |
27 |
} |
4627 |
21 Nov 17 |
nicklas |
28 |
|
6824 |
30 Aug 22 |
nicklas |
map.mapFromChanged = function() |
5663 |
11 Oct 19 |
nicklas |
30 |
{ |
5663 |
11 Oct 19 |
nicklas |
var frm = document.forms['reggie']; |
5740 |
20 Nov 19 |
nicklas |
var fromPnr = frm.mapFrom.value == 'PersonalNo' || frm.mapFrom.value == 'PAD'; |
5663 |
11 Oct 19 |
nicklas |
33 |
|
5663 |
11 Oct 19 |
nicklas |
Doc.showHide('exportPersonalNo', !fromPnr); |
5740 |
20 Nov 19 |
nicklas |
Doc.showHide('alsoExportPad', !fromPnr); |
5663 |
11 Oct 19 |
nicklas |
Doc.showHide('exportPatientId', fromPnr); |
5663 |
11 Oct 19 |
nicklas |
Doc.showHide('alsoExportCaseId', fromPnr); |
5663 |
11 Oct 19 |
nicklas |
Doc.showHide('alsoExportSpecimenId', fromPnr); |
5663 |
11 Oct 19 |
nicklas |
39 |
} |
5663 |
11 Oct 19 |
nicklas |
40 |
|
5663 |
11 Oct 19 |
nicklas |
map.fileChanged = function() |
5663 |
11 Oct 19 |
nicklas |
42 |
{ |
5663 |
11 Oct 19 |
nicklas |
var frm = document.forms['reggie']; |
5663 |
11 Oct 19 |
nicklas |
var file = frm.file.files[0]; |
5663 |
11 Oct 19 |
nicklas |
var reader = new FileReader(); |
5663 |
11 Oct 19 |
nicklas |
reader.onload = map.fileLoaded; |
5663 |
11 Oct 19 |
nicklas |
reader.readAsText(file.slice(0, 100000)); // Read at most 100kB -- I think we should get at least 2 lines.... |
5663 |
11 Oct 19 |
nicklas |
48 |
} |
5663 |
11 Oct 19 |
nicklas |
49 |
|
5663 |
11 Oct 19 |
nicklas |
map.fileLoaded = function(event) |
5663 |
11 Oct 19 |
nicklas |
51 |
{ |
5663 |
11 Oct 19 |
nicklas |
var frm = document.forms['reggie']; |
5663 |
11 Oct 19 |
nicklas |
var data = event.target.result; |
5663 |
11 Oct 19 |
nicklas |
var lines = data.split(/[\n\r]+/); |
5663 |
11 Oct 19 |
nicklas |
55 |
|
5663 |
11 Oct 19 |
nicklas |
var lineNo = 0; |
5663 |
11 Oct 19 |
nicklas |
var mapFrom = null; |
6474 |
04 Nov 21 |
nicklas |
var separator = null; |
5663 |
11 Oct 19 |
nicklas |
var preview = ''; |
5663 |
11 Oct 19 |
nicklas |
while (lineNo < lines.length && lineNo < 5) |
5663 |
11 Oct 19 |
nicklas |
61 |
{ |
5663 |
11 Oct 19 |
nicklas |
var line = lines[lineNo]; |
6474 |
04 Nov 21 |
nicklas |
if (lineNo < 4) |
5663 |
11 Oct 19 |
nicklas |
64 |
{ |
5663 |
11 Oct 19 |
nicklas |
preview += Strings.encodeTags(line) + '\n'; |
5663 |
11 Oct 19 |
nicklas |
66 |
} |
6474 |
04 Nov 21 |
nicklas |
if (separator == null) separator = map.detectSeparator(line); |
5740 |
20 Nov 19 |
nicklas |
if (mapFrom == null) |
5663 |
11 Oct 19 |
nicklas |
69 |
{ |
6474 |
04 Nov 21 |
nicklas |
if (line.match(/^[QCS]\d+([\t,;].*)?$/))// We look for lines starting with 'Q', 'C' or 'S' followed by {digits} and a {tab/comma/semicolon} |
5663 |
11 Oct 19 |
nicklas |
71 |
{ |
5740 |
20 Nov 19 |
nicklas |
var firstChar = line.charAt(0); |
5740 |
20 Nov 19 |
nicklas |
if (firstChar == 'Q') |
5740 |
20 Nov 19 |
nicklas |
74 |
{ |
5740 |
20 Nov 19 |
nicklas |
mapFrom = 'PatientId'; |
5740 |
20 Nov 19 |
nicklas |
76 |
} |
5740 |
20 Nov 19 |
nicklas |
else if (firstChar == 'C') |
5740 |
20 Nov 19 |
nicklas |
78 |
{ |
5740 |
20 Nov 19 |
nicklas |
mapFrom = 'CaseId'; |
5740 |
20 Nov 19 |
nicklas |
80 |
} |
5740 |
20 Nov 19 |
nicklas |
else if (firstChar == 'S') |
5740 |
20 Nov 19 |
nicklas |
82 |
{ |
5740 |
20 Nov 19 |
nicklas |
mapFrom = 'SpecimenId'; |
5740 |
20 Nov 19 |
nicklas |
84 |
} |
5663 |
11 Oct 19 |
nicklas |
85 |
} |
6474 |
04 Nov 21 |
nicklas |
else if (lineNo == 0 && line.match(/^[^\t,;]*pad/i)) // If the first header column contains 'pad' (ignoring case) we assume it contains PAD values |
5663 |
11 Oct 19 |
nicklas |
87 |
{ |
5740 |
20 Nov 19 |
nicklas |
mapFrom = 'PAD'; |
5663 |
11 Oct 19 |
nicklas |
89 |
} |
5663 |
11 Oct 19 |
nicklas |
90 |
} |
5663 |
11 Oct 19 |
nicklas |
lineNo++; |
5663 |
11 Oct 19 |
nicklas |
92 |
} |
6474 |
04 Nov 21 |
nicklas |
if (lines.length > 4) preview += '<div id="more-lines">+' + (lines.length-4) + ' more lines...</div>'; |
6474 |
04 Nov 21 |
nicklas |
frm.separator.value = separator ? separator.id : 'tab'; |
5663 |
11 Oct 19 |
nicklas |
frm.mapFrom.value = mapFrom || 'PersonalNo'; |
5663 |
11 Oct 19 |
nicklas |
Events.sendChangeEvent(frm.mapFrom); |
5663 |
11 Oct 19 |
nicklas |
Doc.element('preview').innerHTML = preview; |
5663 |
11 Oct 19 |
nicklas |
98 |
} |
5663 |
11 Oct 19 |
nicklas |
99 |
|
6474 |
04 Nov 21 |
nicklas |
100 |
/** |
6474 |
04 Nov 21 |
nicklas |
Detect the "best" separator to use by selecting the one |
6474 |
04 Nov 21 |
nicklas |
that appear the most times in the given text line. If no |
6474 |
04 Nov 21 |
nicklas |
separator is found null is returned. |
6474 |
04 Nov 21 |
nicklas |
104 |
*/ |
6474 |
04 Nov 21 |
nicklas |
map.detectSeparator = function(text) |
6474 |
04 Nov 21 |
nicklas |
106 |
{ |
6474 |
04 Nov 21 |
nicklas |
var separators = |
6474 |
04 Nov 21 |
nicklas |
108 |
[ |
6474 |
04 Nov 21 |
nicklas |
{ 'id': 'tab', 'test': /\t/g, 'sep': '\t'}, |
6474 |
04 Nov 21 |
nicklas |
{ 'id': 'comma', 'test': /,/g, 'sep': ','}, |
6474 |
04 Nov 21 |
nicklas |
{ 'id': 'semicolon', 'test': /;/g, 'sep': ';'} |
6474 |
04 Nov 21 |
nicklas |
112 |
]; |
6474 |
04 Nov 21 |
nicklas |
var maxCount = 0; |
6474 |
04 Nov 21 |
nicklas |
var bestMatch = 0; |
6474 |
04 Nov 21 |
nicklas |
for (var i=0; i < separators.length; i++) |
6474 |
04 Nov 21 |
nicklas |
116 |
{ |
6474 |
04 Nov 21 |
nicklas |
var sep = separators[i]; |
6474 |
04 Nov 21 |
nicklas |
var m = text.match(sep.test); |
6474 |
04 Nov 21 |
nicklas |
if (m && m.length > maxCount) |
6474 |
04 Nov 21 |
nicklas |
120 |
{ |
6474 |
04 Nov 21 |
nicklas |
maxCount = m.length; |
6474 |
04 Nov 21 |
nicklas |
bestMatch = i; |
6474 |
04 Nov 21 |
nicklas |
123 |
} |
6474 |
04 Nov 21 |
nicklas |
124 |
} |
6474 |
04 Nov 21 |
nicklas |
return maxCount == 0 ? null : separators[bestMatch]; |
6474 |
04 Nov 21 |
nicklas |
126 |
} |
5663 |
11 Oct 19 |
nicklas |
127 |
|
4627 |
21 Nov 17 |
nicklas |
map.validateStep1 = function(event) |
4627 |
21 Nov 17 |
nicklas |
129 |
{ |
4627 |
21 Nov 17 |
nicklas |
var valid = true; |
4627 |
21 Nov 17 |
nicklas |
valid &= Wizard.isValid('file'); |
4627 |
21 Nov 17 |
nicklas |
if (!valid) event.preventDefault(); |
4627 |
21 Nov 17 |
nicklas |
133 |
} |
4627 |
21 Nov 17 |
nicklas |
134 |
|
4627 |
21 Nov 17 |
nicklas |
map.submit = function() |
4627 |
21 Nov 17 |
nicklas |
136 |
{ |
4627 |
21 Nov 17 |
nicklas |
var frm = document.forms['reggie']; |
4627 |
21 Nov 17 |
nicklas |
138 |
|
4627 |
21 Nov 17 |
nicklas |
var formData = new FormData(); |
4627 |
21 Nov 17 |
nicklas |
var file = frm.file.files[0]; |
4627 |
21 Nov 17 |
nicklas |
formData.append('file', file, file.name); |
4627 |
21 Nov 17 |
nicklas |
142 |
|
4627 |
21 Nov 17 |
nicklas |
var url = '../Export.servlet?ID='+App.getSessionId(); |
4627 |
21 Nov 17 |
nicklas |
url += '&cmd=MapDataToReleases'; |
6474 |
04 Nov 21 |
nicklas |
url += '&separator='+frm.separator.value; |
5663 |
11 Oct 19 |
nicklas |
url += '&mapFrom='+Strings.encodeTags(frm.mapFrom.value); |
5663 |
11 Oct 19 |
nicklas |
url += '&exportCaseId='+(frm.exportCaseId.checked ? 1 : 0); |
5663 |
11 Oct 19 |
nicklas |
url += '&exportSpecimenId='+(frm.exportSpecimenId.checked ? 1 : 0); |
5740 |
20 Nov 19 |
nicklas |
url += '&exportPad='+(frm.exportPad.checked ? 1 : 0); |
5663 |
11 Oct 19 |
nicklas |
url += '&sortOutput='+(frm.sortOutputOrder.checked ? 1 : 0); |
4627 |
21 Nov 17 |
nicklas |
Wizard.showLoadingAnimation('Working...', 'map-data-progress'); |
4627 |
21 Nov 17 |
nicklas |
Wizard.asyncJsonRequest(url, map.submissionResults, 'POST', formData); |
4627 |
21 Nov 17 |
nicklas |
153 |
} |
4627 |
21 Nov 17 |
nicklas |
154 |
|
4627 |
21 Nov 17 |
nicklas |
map.submissionResults = function(response) |
4627 |
21 Nov 17 |
nicklas |
156 |
{ |
4627 |
21 Nov 17 |
nicklas |
Wizard.showFinalMessage(response.messages); |
4627 |
21 Nov 17 |
nicklas |
Doc.show('godownload'); |
5332 |
20 Mar 19 |
nicklas |
Doc.show('gorestart'); |
4627 |
21 Nov 17 |
nicklas |
160 |
} |
4627 |
21 Nov 17 |
nicklas |
161 |
|
4627 |
21 Nov 17 |
nicklas |
map.download = function() |
4627 |
21 Nov 17 |
nicklas |
163 |
{ |
4627 |
21 Nov 17 |
nicklas |
var url = '../Export.servlet?ID='+App.getSessionId(); |
4627 |
21 Nov 17 |
nicklas |
url += '&cmd=DownloadMappedData'; |
4627 |
21 Nov 17 |
nicklas |
location.href = url; |
4627 |
21 Nov 17 |
nicklas |
167 |
} |
4627 |
21 Nov 17 |
nicklas |
168 |
|
4627 |
21 Nov 17 |
nicklas |
return map; |
4627 |
21 Nov 17 |
nicklas |
170 |
}(); |
4627 |
21 Nov 17 |
nicklas |
171 |
|
4627 |
21 Nov 17 |
nicklas |
Doc.onLoad(MapData.initPage); |
4627 |
21 Nov 17 |
nicklas |
173 |
|