Files
ohif-viewer/Packages/ohif-viewerbase/client/components/viewer/downloadDialog/downloadDialog.js
T
André Botelho Almeida 057735aab3 OHIF-184: Add menu tool option to download viewport as PNG or JPEG (#130)
* OHIF-184: added download icon to the OHIF's tool bar

* OHIF-184: refactor dialog toggle method to support cine dialog and download dialog

* OHIF-184: Added hotKey support for downloadDialog.

* OHIF-184: added disabled verify function

* OHIF-184: Finished form structure. Created preview canvas. Added width and height config of the preview. Added the download feature. Added Cancel button.

* OHIF-184: setting default extension as png

* OHIF-184: moved download icon to more in toolbar

* OHIF-184: enabling/disabling tools when toggling show annotations checkbox

* OHIF-184: using a dynamic canvas to create the download content

* OHIF-184: Download with configurable width and height. download distorced

* OHIF-184: using dynamic elemento to manage download image instead of using the preview element

* OHIF-184: make dialog dragable through preview

* OHIF-184: removing useless line

* OHIF-184: adding download icon to leasion tracker

* OHIF-184: adding download dialog template to lesion tracker viewer
2017-11-17 11:33:41 +01:00

110 lines
4.0 KiB
JavaScript

import { Template } from 'meteor/templating';
import { Session } from 'meteor/session';
import { OHIF } from 'meteor/ohif:core';
import { viewportUtils } from '../../../lib/viewportUtils';
function setElementSize(element, canvas, size, value) {
$(element)[size](value);
canvas[size] = value;
canvas.style[size] = `${value}px`;
}
Template.downloadDialog.onCreated(() => {
const instance = Template.instance();
instance.autorun(() => {
Session.get('UpdateDownloadViewport');
const activeViewport = viewportUtils.getActiveViewportElement();
if (activeViewport) {
const enabledElement = cornerstone.getEnabledElement(activeViewport);
cornerstone.loadImage(enabledElement.image.imageId).then(function (image) {
cornerstone.displayImage(instance.$previewElement, image);
cornerstone.displayImage(instance.$downloadElement, image);
cornerstone.resize(instance.$previewElement, true);
setElementSize(instance.$downloadElement, instance.$downloadCanvas, 'width', 300);
setElementSize(instance.$downloadElement, instance.$downloadCanvas, 'height', 200);
cornerstone.fitToWindow(instance.$downloadElement);
});
}
})
});
Template.downloadDialog.onRendered(() => {
const instance = Template.instance();
const $dialog = instance.$('#downloadDialog');
instance.$previewElement = $('#previewElement')[0];
instance.$downloadElement = document.createElement('div');
instance.availableTools = ['length', 'probe', 'simpleAngle', 'arrowAnnotate', 'ellipticalRoi', 'rectangleRoi'];
instance.showAnnotations = false;
cornerstone.enable(instance.$previewElement);
cornerstone.enable(instance.$downloadElement);
instance.$downloadCanvas = $(instance.$downloadElement).find('canvas')[0];
// Make the dialog bounded and draggable
$dialog.draggable({ defaultElementCursor: 'move' });
// Polyfill for older browsers
dialogPolyfill.registerDialog($dialog.get(0));
// // Prevent dialog from being dragged when user clicks any button
const $controls = $dialog.find('.form-group, .instructions');
$controls.on('mousedown touchstart', event => event.stopPropagation());
});
Template.downloadDialog.events({
'change .form-group input[name=width]'(event, instance){
const width = $(event.currentTarget).val();
setElementSize(instance.$downloadElement, instance.$downloadCanvas, 'width', width);
cornerstone.fitToWindow(instance.$downloadElement);
},
'change .form-group input[name=height]'(event, instance){
const height = $(event.currentTarget).val();
setElementSize(instance.$downloadElement, instance.$downloadCanvas, 'height', height);
cornerstone.fitToWindow(instance.$downloadElement);
},
'change #downloadDialog .form-group .form-check input[type=checkbox]'(event, instance) {
const $previewElement = instance.$previewElement;
const $downloadElement = instance.$downloadElement;
instance.showAnnotations = !instance.showAnnotations;
const action = (instance.showAnnotations) ? 'enable' : 'disable';
instance.availableTools.forEach(tool => {
cornerstoneTools[tool][action]($previewElement);
cornerstoneTools[tool][action]($downloadElement);
});
},
'click .dropdown-menu .dropdown-item'(event, instance) {
const extension = $(event.currentTarget).text();
const $extensionButton = $('.btn.extension');
$extensionButton.text(extension);
},
'click button.download'(event, instance) {
const fileName = $('.fileName').val();
const extension = $('.btn.extension').text().trim();
if (!fileName || !extension) {
return;
}
cornerstoneTools.saveAs(instance.$downloadElement, `${fileName}.${extension}`, `image/${extension}`);
},
'click button.cancel'(event, instance) {
viewportUtils.toggleDownloadDialog();
}
});