2016-07-12 20:37:53 +02:00
|
|
|
import { OHIF } from 'meteor/ohif:core';
|
2017-01-04 11:18:01 +01:00
|
|
|
import { Template } from 'meteor/templating';
|
|
|
|
|
import { Session } from 'meteor/session';
|
2017-01-04 14:20:13 +01:00
|
|
|
import { _ } from 'meteor/underscore';
|
2016-07-12 20:37:53 +02:00
|
|
|
|
2017-01-04 20:41:47 +01:00
|
|
|
Template.toolbarSectionButton.onCreated(() => {
|
|
|
|
|
const instance = Template.instance();
|
|
|
|
|
|
|
|
|
|
instance.isActive = activeToolId => {
|
2016-07-22 18:23:36 +02:00
|
|
|
// TODO: Find a way to prevent the 'flash' after a click, but before this helper runs
|
2016-06-29 22:39:40 +02:00
|
|
|
const instance = Template.instance();
|
2017-01-04 14:20:13 +01:00
|
|
|
const subTools = instance.data.subTools;
|
|
|
|
|
const currentId = instance.data.id;
|
2017-01-04 20:41:47 +01:00
|
|
|
const isCurrentTool = currentId === activeToolId;
|
|
|
|
|
const isSubTool = subTools && _.findWhere(subTools, { id: activeToolId });
|
2017-01-05 20:39:58 +01:00
|
|
|
const activeCommandButtons = Session.get('ToolManagerActiveCommandButtons') || [];
|
|
|
|
|
const isActiveCommandButton = activeCommandButtons.indexOf(instance.data.id) !== -1;
|
2016-06-29 22:39:40 +02:00
|
|
|
|
2017-01-05 20:39:58 +01:00
|
|
|
// Check if the current tool, a sub tool or a command button is active
|
|
|
|
|
return isCurrentTool || isSubTool || isActiveCommandButton;
|
2017-01-04 20:41:47 +01:00
|
|
|
};
|
2017-01-05 18:33:13 +01:00
|
|
|
|
|
|
|
|
instance.getActiveToolSubProperty = (propertyName, activeToolId) => {
|
|
|
|
|
const instance = Template.instance();
|
|
|
|
|
const subTools = instance.data.subTools;
|
|
|
|
|
const defaultProperty = instance.data[propertyName];
|
|
|
|
|
const currentId = instance.data.id;
|
|
|
|
|
|
|
|
|
|
if (subTools && activeToolId !== currentId && instance.isActive(activeToolId)) {
|
|
|
|
|
const subTool = _.findWhere(subTools, { id: activeToolId });
|
|
|
|
|
return subTool ? subTool[propertyName] : defaultProperty;
|
|
|
|
|
} else {
|
|
|
|
|
return defaultProperty;
|
|
|
|
|
}
|
|
|
|
|
};
|
2017-04-25 22:43:14 +02:00
|
|
|
|
|
|
|
|
instance.autorun(computation => {
|
|
|
|
|
// Get the last executed command
|
|
|
|
|
const lastCommand = Session.get('lastCommand');
|
|
|
|
|
|
|
|
|
|
// Prevent running this computation on its first run
|
|
|
|
|
if (computation.firstRun) return;
|
|
|
|
|
|
|
|
|
|
// Stop here if it's not the last command or if it's already an active tool
|
|
|
|
|
const { id } = instance.data;
|
|
|
|
|
if (lastCommand !== id || instance.isActive(id)) return;
|
|
|
|
|
|
|
|
|
|
// Add an active class to a button for 100ms to give the impression the button was pressed
|
|
|
|
|
const $button = instance.$('.toolbarSectionButton:first');
|
|
|
|
|
$button.addClass('active');
|
|
|
|
|
setTimeout(() => $button.removeClass('active'), 100);
|
|
|
|
|
});
|
2017-01-04 20:41:47 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Template.toolbarSectionButton.helpers({
|
|
|
|
|
activeClass() {
|
|
|
|
|
const instance = Template.instance();
|
|
|
|
|
const activeToolId = Session.get('ToolManagerActiveTool');
|
|
|
|
|
const isActive = instance.isActive(activeToolId);
|
|
|
|
|
return isActive ? 'active' : '';
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
svgLink() {
|
|
|
|
|
const instance = Template.instance();
|
|
|
|
|
const activeToolId = Session.get('ToolManagerActiveTool');
|
2017-01-05 18:33:13 +01:00
|
|
|
return instance.getActiveToolSubProperty('svgLink', activeToolId);
|
|
|
|
|
},
|
2017-01-04 20:41:47 +01:00
|
|
|
|
2017-01-05 18:33:13 +01:00
|
|
|
iconClasses() {
|
|
|
|
|
const instance = Template.instance();
|
|
|
|
|
const activeToolId = Session.get('ToolManagerActiveTool');
|
|
|
|
|
return instance.getActiveToolSubProperty('iconClasses', activeToolId);
|
2016-11-07 15:01:03 +01:00
|
|
|
},
|
2017-01-04 11:18:01 +01:00
|
|
|
|
2016-11-07 15:01:03 +01:00
|
|
|
disableButton() {
|
2016-12-29 14:37:25 +01:00
|
|
|
const instance = Template.instance();
|
2017-01-06 17:29:23 +01:00
|
|
|
return instance.data.disableFunction && instance.data.disableFunction();
|
2016-06-29 22:39:40 +02:00
|
|
|
}
|
|
|
|
|
});
|
2016-07-01 21:40:28 +02:00
|
|
|
|
|
|
|
|
Template.toolbarSectionButton.events({
|
2017-04-25 22:43:14 +02:00
|
|
|
'click .toolbarSectionButton:not(.expandable)'(event, instance) {
|
2017-01-04 11:18:01 +01:00
|
|
|
// Prevent the event from bubbling to parent tools
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
2017-04-25 22:43:14 +02:00
|
|
|
// Stop here if the button is disabled
|
|
|
|
|
if ($(event.currentTarget).hasClass('disabled')) return;
|
2016-07-01 21:40:28 +02:00
|
|
|
|
2017-04-25 22:43:14 +02:00
|
|
|
// Run the command attached to the button
|
|
|
|
|
OHIF.commands.run(instance.data.id);
|
2016-07-01 21:40:28 +02:00
|
|
|
}
|
|
|
|
|
});
|