comparison PalantirExplorer/explorer.js @ 0:3959d33612cc

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 19 Jul 2012 14:32:22 +0200
parents
children de56f3951fad f6d12037f886
comparison
equal deleted inserted replaced
-1:000000000000 0:3959d33612cc
1 // http://stackoverflow.com/questions/1663741/is-there-a-good-jquery-drag-and-drop-file-upload-plugin
2
3
4 // Forbid the access to IE
5 if ($.browser.msie)
6 {
7 alert("Please use Mozilla Firefox or Google Chrome. Microsoft Internet Explorer is not supported.");
8 }
9
10 // http://jquerymobile.com/demos/1.1.0/docs/api/globalconfig.html
11 //$.mobile.ajaxEnabled = false;
12 //$.mobile.page.prototype.options.addBackBtn = true;
13 //$.mobile.defaultPageTransition = 'slide';
14
15 // http://stackoverflow.com/a/4673436
16 String.prototype.format = function() {
17 var args = arguments;
18 return this.replace(/{(\d+)}/g, function(match, number) {
19 /*return typeof args[number] != 'undefined'
20 ? args[number]
21 : match;*/
22
23 return args[number];
24 });
25 };
26
27
28 $(document).ready(function() {
29 var $tree = $('#dicom-tree');
30 $tree.tree({
31 autoEscape: false
32 });
33
34 $('#dicom-tree').bind(
35 'tree.click',
36 function(event) {
37 if (event.node.is_open)
38 $tree.tree('closeNode', event.node, true);
39 else
40 $tree.tree('openNode', event.node, true);
41 }
42 );
43 });
44
45
46 function SplitLongUid(s)
47 {
48 return '<span>' + s.substr(0, s.length / 2) + '</span> <span>' + s.substr(s.length / 2, s.length - s.length / 2) + '</span>';
49 }
50
51
52 function ParseDicomDate(s)
53 {
54 y = parseInt(s.substr(0, 4), 10);
55 m = parseInt(s.substr(4, 2), 10) - 1;
56 d = parseInt(s.substr(6, 2), 10);
57
58 if (y == null || m == null || d == null ||
59 !isFinite(y) || !isFinite(m) || !isFinite(d))
60 {
61 return null;
62 }
63
64 if (y < 1900 || y > 2100 ||
65 m < 0 || m >= 12 ||
66 d <= 0 || d >= 32)
67 {
68 return null;
69 }
70
71 return new Date(y, m, d);
72 }
73
74
75 function FormatDicomDate(s)
76 {
77 if (s == undefined)
78 return "No date";
79
80 var d = ParseDicomDate(s);
81 if (d == null)
82 return '?';
83 else
84 return d.toString('dddd, MMMM d, yyyy');
85 }
86
87
88
89 function SortOnDicomTag(arr, tag, isInteger, reverse)
90 {
91 arr.sort(function(a, b) {
92 var ta = a.MainDicomTags[tag];
93 var tb = b.MainDicomTags[tag];
94 var order;
95
96 if (isInteger)
97 {
98 ta = parseInt(ta, 10);
99 tb = parseInt(tb, 10);
100 order = ta - tb;
101 }
102 else
103 {
104 if (ta < tb)
105 order = -1;
106 else if (ta > tb)
107 order = 1;
108 else
109 order = 0;
110 }
111
112 if (reverse)
113 return -order;
114 else
115 return order;
116 });
117 }
118
119
120
121 function GetSingleResource(type, uuid, callback)
122 {
123 var resource = null;
124 $.ajax({
125 url: '/' + type + '/' + uuid,
126 dataType: 'json',
127 async: false,
128 success: function(s) {
129 callback(s);
130 }
131 });
132 }
133
134
135 function GetMultipleResources(type, uuids, callback)
136 {
137 if (uuids == null)
138 {
139 $.ajax({
140 url: '/' + type,
141 dataType: 'json',
142 async: false,
143 success: function(s) {
144 uuids = s;
145 }
146 });
147 }
148
149 var resources = [];
150 var ajaxRequests = uuids.map(function(uuid) {
151 return $.ajax({
152 url: '/' + type + '/' + uuid,
153 dataType: 'json',
154 async: true,
155 success: function(s) {
156 resources.push(s);
157 }
158 });
159 });
160
161 // Wait for all the AJAX requests to end
162 $.when.apply($, ajaxRequests).then(function() {
163 callback(resources);
164 });
165 }
166
167
168
169 function CompleteFormatting(s, link, isReverse)
170 {
171 if (link != null)
172 {
173 s = 'href="' + link + '">' + s + '</a>';
174
175 if (isReverse)
176 s = 'data-direction="reverse" '+ s;
177
178 s = '<a ' + s;
179 }
180
181 if (isReverse)
182 return '<li data-icon="back">' + s + '</li>';
183 else
184 return '<li>' + s + '</li>';
185 }
186
187
188
189 function FormatPatient(patient, link, isReverse)
190 {
191 var s = ('<h3>{1}</h3>' +
192 '<p>Patient ID: <strong>{2}</strong></p>' +
193 '<p>Accession Number: <strong>{3}</strong></p>' +
194 '<p>Date of Birth: <strong>{4}</strong></p>' +
195 '<p>Sex: <strong>{5}</strong></p>' +
196 '<span class="ui-li-count">{6}</span>'
197 ).format
198 (link,
199 patient.MainDicomTags.PatientName,
200 patient.DicomPatientID,
201 patient.MainDicomTags.AccessionNumber,
202 FormatDicomDate(patient.MainDicomTags.PatientBirthDate),
203 patient.MainDicomTags.PatientSex,
204 patient.Studies.length
205 );
206
207 return CompleteFormatting(s, link, isReverse);
208 }
209
210
211
212 function FormatStudy(study, link, isReverse)
213 {
214 var s = ('<h3>{0}</h3>' +
215 '<p>Study Instance UID: <strong>{1}</strong></p>' +
216 '<span class="ui-li-count">{2}</span>'
217 ).format
218 (study.MainDicomTags.StudyDescription,
219 SplitLongUid(study.DicomStudyInstanceUID),
220 study.Series.length
221 );
222
223 return CompleteFormatting(s, link, isReverse);
224 }
225
226
227
228 function FormatSeries(series, link, isReverse)
229 {
230 var s = ('<h3>{0}</h3>' +
231 '<p>Modality: <strong>{1}</strong></p>' +
232 '<p>Protocol: <strong>{2}</strong></p>' +
233 '<p>Station name: <strong>{3}</strong></p>' +
234 '<p>Series Instance UID: <strong>{4}</strong></p>' +
235 '<span class="ui-li-count">{5}</span>').format
236 (series.MainDicomTags.SeriesDescription,
237 series.MainDicomTags.Modality,
238 series.MainDicomTags.ProtocolName,
239 series.MainDicomTags.StationName,
240 SplitLongUid(series.DicomSeriesInstanceUID),
241 series.Instances.length
242 );
243
244 return CompleteFormatting(s, link, isReverse);
245 }
246
247
248 function FormatInstance(instance, link, isReverse)
249 {
250 var s = ('<h3>Instance {0}</h3>' +
251 '<p>SOP Instance UID: <strong>{1}</strong></p>'
252 ).format
253 (instance.MainDicomTags.InstanceNumber,
254 instance.DicomSOPInstanceUID
255 );
256
257 return CompleteFormatting(s, link, isReverse);
258 }
259
260
261
262
263 $('#find-patients').live('pagebeforeshow', function() {
264 GetMultipleResources('patients', null, function(patients) {
265 var target = $('#all-patients');
266 $('li', target).remove();
267
268 SortOnDicomTag(patients, 'PatientName', false, false);
269
270 for (var i = 0; i < patients.length; i++) {
271 var p = FormatPatient(patients[i], '#patient?uuid=' + patients[i].ID);
272 target.append(p);
273 }
274
275 target.listview('refresh');
276 });
277 });
278
279
280
281 $('#patient').live('pagebeforeshow', function() {
282 if ($.mobile.pageData) {
283 GetSingleResource('patients', $.mobile.pageData.uuid, function(patient) {
284 GetMultipleResources('studies', patient.Studies, function(studies) {
285 SortOnDicomTag(studies, 'StudyDate', false, true);
286
287 $('#patient-info li').remove();
288 $('#patient-info')
289 .append('<li data-role="list-divider">Patient</li>')
290 .append(FormatPatient(patient))
291 .listview('refresh');
292
293 var target = $('#list-studies');
294 $('li', target).remove();
295
296 for (var i = 0; i < studies.length; i++) {
297 if (i == 0 || studies[i].MainDicomTags.StudyDate != studies[i - 1].MainDicomTags.StudyDate)
298 {
299 target.append('<li data-role="list-divider">{0}</li>'.format
300 (FormatDicomDate(studies[i].MainDicomTags.StudyDate)));
301 }
302
303 target.append(FormatStudy(studies[i], '#study?uuid=' + studies[i].ID));
304 }
305
306 target.listview('refresh');
307 });
308 });
309 }
310 });
311
312
313 $('#study').live('pagebeforeshow', function() {
314 if ($.mobile.pageData) {
315 GetSingleResource('studies', $.mobile.pageData.uuid, function(study) {
316 GetSingleResource('patients', study.ParentPatient, function(patient) {
317 GetMultipleResources('series', study.Series, function(series) {
318 SortOnDicomTag(series, 'SeriesDate', false, true);
319
320 $('#study-info li').remove();
321 $('#study-info')
322 .append('<li data-role="list-divider">Patient</li>')
323 .append(FormatPatient(patient, '#patient?uuid=' + patient.ID, true))
324 .append('<li data-role="list-divider">Study</li>')
325 .append(FormatStudy(study))
326 .listview('refresh');
327
328 var target = $('#list-series');
329 $('li', target).remove();
330 for (var i = 0; i < series.length; i++) {
331 if (i == 0 || series[i].MainDicomTags.SeriesDate != series[i - 1].MainDicomTags.SeriesDate)
332 {
333 target.append('<li data-role="list-divider">{0}</li>'.format
334 (FormatDicomDate(series[i].MainDicomTags.SeriesDate)));
335 }
336 target.append(FormatSeries(series[i], '#series?uuid=' + series[i].ID));
337 }
338 target.listview('refresh');
339 });
340 });
341 });
342 }
343 });
344
345
346 $('#series').live('pagebeforeshow', function() {
347 if ($.mobile.pageData) {
348 GetSingleResource('series', $.mobile.pageData.uuid, function(series) {
349 GetSingleResource('studies', series.ParentStudy, function(study) {
350 GetSingleResource('patients', study.ParentPatient, function(patient) {
351 GetMultipleResources('instances', series.Instances, function(instances) {
352 SortOnDicomTag(instances, 'InstanceNumber', true, false);
353
354 $('#series-info li').remove();
355 $('#series-info')
356 .append('<li data-role="list-divider">Patient</li>')
357 .append(FormatPatient(patient, '#patient?uuid=' + patient.ID, true))
358 .append('<li data-role="list-divider">Study</li>')
359 .append(FormatStudy(study, '#study?uuid=' + study.ID, true))
360 .append('<li data-role="list-divider">Series</li>')
361 .append(FormatSeries(series))
362 .listview('refresh');
363
364 var target = $('#list-instances');
365 $('li', target).remove();
366 for (var i = 0; i < instances.length; i++) {
367 target.append(FormatInstance(instances[i], '#instance?uuid=' + instances[i].ID));
368 }
369 target.listview('refresh');
370 });
371 });
372 });
373 });
374 }
375 });
376
377
378
379 function ConvertForTree(dicom)
380 {
381 var result = [];
382
383 for (var i in dicom) {
384 if (dicom [i] != null) {
385 if (typeof dicom[i] == 'string')
386 {
387 result.push({
388 label: i + ': <strong>' + dicom[i] + '</strong>',
389 children: []
390 });
391 }
392 else if (typeof dicom[i] == 'number')
393 {
394 result.push({
395 label: i + ': <i>Too long</i>',
396 children: []
397 });
398 }
399 else
400 {
401 var c = [];
402 for (var j = 0; j < dicom[i].length; j++) {
403 c.push({
404 label: 'Item ' + j,
405 children: ConvertForTree(dicom[i][j])
406 });
407 }
408
409 result.push({
410 label: i + '[]',
411 children: c
412 });
413 }
414 }
415 }
416
417 return result;
418 }
419
420
421 $('#instance').live('pagebeforeshow', function() {
422 if ($.mobile.pageData) {
423 GetSingleResource('instances', $.mobile.pageData.uuid, function(instance) {
424 GetSingleResource('series', instance.ParentSeries, function(series) {
425 GetSingleResource('studies', series.ParentStudy, function(study) {
426 GetSingleResource('patients', study.ParentPatient, function(patient) {
427
428 $('#instance-info li').remove();
429 $('#instance-info')
430 .append('<li data-role="list-divider">Patient</li>')
431 .append(FormatPatient(patient, '#patient?uuid=' + patient.ID, true))
432 .append('<li data-role="list-divider">Study</li>')
433 .append(FormatStudy(study, '#study?uuid=' + study.ID, true))
434 .append('<li data-role="list-divider">Series</li>')
435 .append(FormatSeries(series, '#series?uuid=' + series.ID, true))
436 .append('<li data-role="list-divider">Instance</li>')
437 .append(FormatInstance(instance))
438 .listview('refresh');
439
440 $.ajax({
441 url: '/instances/' + instance.ID + '/all-tags',
442 dataType: 'json',
443 success: function(s) {
444 $('#dicom-tree').tree('loadData', ConvertForTree(s));
445 }
446 });
447
448 });
449 });
450 });
451 });
452 }
453 });
454
455
456
457 function DeleteResource(path)
458 {
459 $.ajax({
460 url: path,
461 type: 'DELETE',
462 dataType: 'json',
463 async: false,
464 success: function(s) {
465 var ancestor = s.RemainingAncestor;
466 if (ancestor == null)
467 $.mobile.changePage('#find-patients');
468 else
469 $.mobile.changePage('#' + ancestor.Type + '?uuid=' + ancestor.ID);
470 }
471 });
472 }
473
474
475
476 function OpenDeleteResourceDialog(path, title)
477 {
478 $(document).simpledialog2({
479 // http://dev.jtsage.com/jQM-SimpleDialog/demos2/
480 // http://dev.jtsage.com/jQM-SimpleDialog/demos2/options.html
481 mode: 'button',
482 animate: false,
483 headerText: title,
484 headerClose: true,
485 width: '500px',
486 buttons : {
487 'OK': {
488 click: function () {
489 DeleteResource(path);
490 },
491 icon: "delete",
492 theme: "c"
493 },
494 'Cancel': {
495 click: function () {
496 }
497 }
498 }
499 });
500 }
501
502
503
504 $('#instance-delete').live('click', function() {
505 OpenDeleteResourceDialog('/instances/' + $.mobile.pageData.uuid,
506 'Delete this instance?');
507 });
508
509 $('#study-delete').live('click', function() {
510 OpenDeleteResourceDialog('/studies/' + $.mobile.pageData.uuid,
511 'Delete this study?');
512 });
513
514 $('#series-delete').live('click', function() {
515 OpenDeleteResourceDialog('/series/' + $.mobile.pageData.uuid,
516 'Delete this series?');
517 });
518
519 $('#patient-delete').live('click', function() {
520 OpenDeleteResourceDialog('/patients/' + $.mobile.pageData.uuid,
521 'Delete this patient?');
522 });
523
524
525 $('#instance-download-dicom').live('click', function(e) {
526 // http://stackoverflow.com/a/1296101
527 e.preventDefault(); //stop the browser from following
528 window.location.href = '/instances/' + $.mobile.pageData.uuid + '/file';
529 });
530
531 $('#instance-download-json').live('click', function(e) {
532 // http://stackoverflow.com/a/1296101
533 e.preventDefault(); //stop the browser from following
534 window.location.href = '/instances/' + $.mobile.pageData.uuid + '/all-tags';
535 });
536
537
538 $('#instance-preview').live('click', function(e) {
539 jQuery.slimbox('/instances/' + $.mobile.pageData.uuid + '/normalized-image', '', {
540 overlayFadeDuration : 1,
541 resizeDuration : 1,
542 imageFadeDuration : 1
543 });
544 });
545
546 $('#series-preview').live('click', function(e) {
547 if ($.mobile.pageData) {
548 GetSingleResource('series', $.mobile.pageData.uuid, function(series) {
549 GetMultipleResources('instances', series.Instances, function(instances) {
550 SortOnDicomTag(instances, 'InstanceNumber', true, false);
551
552 var images = [];
553 for (var i = 0; i < instances.length; i++) {
554 images.push([ '/instances/' + instances[i].ID + '/normalized-image',
555 '{0}/{1}'.format(i + 1, instances.length) ])
556 }
557
558 jQuery.slimbox(images, 0, {
559 overlayFadeDuration : 1,
560 resizeDuration : 1,
561 imageFadeDuration : 1,
562 loop : true
563 });
564 })
565 });
566 }
567 });
568
569
570
571
572
573
574 function ChooseDicomModality(callback)
575 {
576 $.ajax({
577 url: '/modalities',
578 type: 'GET',
579 dataType: 'json',
580 async: false,
581 success: function(modalities) {
582 var clickedModality = '';
583 var items = $('<ul>')
584 .attr('data-role', 'listview');
585
586 for (var i = 0; i < modalities.length; i++) {
587 var modality = modalities[i];
588 var item = $('<li>')
589 .html('<a href="#" rel="close">' + modality + '</a>')
590 .attr('modality', modality)
591 .click(function() {
592 clickedModality = $(this).attr('modality');
593 });
594 items.append(item);
595 }
596
597 $('#dialog').simpledialog2({
598 mode: 'blank',
599 animate: false,
600 headerText: 'DICOM modality',
601 headerClose: true,
602 width: '100%',
603 blankContent: items,
604 callbackClose: function() {
605 var timer;
606 function WaitForDialogToClose() {
607 if (!$('#dialog').is(':visible')) {
608 clearInterval(timer);
609 callback(clickedModality);
610 }
611 }
612 timer = setInterval(WaitForDialogToClose, 100);
613 }
614 });
615 }
616 });
617 }
618
619
620 $('#instance-store,#series-store').live('click', function(e) {
621 ChooseDicomModality(function(modality) {
622 if (modality != '') {
623 $.ajax({
624 url: '/modalities/' + modality + '/store',
625 type: 'POST',
626 dataType: 'text',
627 data: $.mobile.pageData.uuid,
628 async: true, // Necessary to block UI
629 beforeSend: function() {
630 $.blockUI({ message: $('#loading') });
631 },
632 complete: function(s) {
633 $.unblockUI();
634 },
635 success: function(s) {
636 console.log('done !');
637 },
638 error: function() {
639 alert('Error during C-Store');
640 }
641 });
642
643 }
644 });
645 });