109 lines
4.2 KiB
JavaScript
109 lines
4.2 KiB
JavaScript
import { OHIF } from 'meteor/ohif:core';
|
|
import { Template } from 'meteor/templating';
|
|
import { Session } from 'meteor/session';
|
|
import { _ } from 'meteor/underscore';
|
|
|
|
Template.toolbarSectionButton.onCreated(() => {
|
|
const instance = Template.instance();
|
|
|
|
instance.isActive = activeToolId => {
|
|
OHIF.commands.last.dep.depend();
|
|
const instance = Template.instance();
|
|
const subTools = instance.data.subTools;
|
|
const currentId = instance.data.id;
|
|
const isCurrentTool = currentId === activeToolId;
|
|
const isSubTool = subTools && _.findWhere(subTools, { id: activeToolId });
|
|
const activeCommandButtons = Session.get('ToolManagerActiveCommandButtons') || [];
|
|
const isActiveCommandButton = activeCommandButtons.indexOf(instance.data.id) !== -1;
|
|
const isActive = typeof instance.data.active === 'function' && instance.data.active();
|
|
|
|
// Check if the current tool, a sub tool or a command button is active
|
|
return isActive || isCurrentTool || isSubTool || isActiveCommandButton;
|
|
};
|
|
|
|
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;
|
|
}
|
|
};
|
|
|
|
instance.autorun(computation => {
|
|
// Get the last executed command
|
|
const lastCommand = OHIF.commands.last.get();
|
|
|
|
// 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;
|
|
const activeToolId = OHIF.viewerbase.toolManager.getActiveTool();
|
|
if (lastCommand !== id || instance.isActive(activeToolId)) return;
|
|
|
|
// Add an active class to a button for 100ms to give the impression the button was pressed
|
|
const flashButton = $element => {
|
|
$element.addClass('active');
|
|
setTimeout(() => {
|
|
if ($element.hasClass('expandable') && $element.find('.toolbarSectionButton.active').length) return;
|
|
$element.removeClass('active');
|
|
}, 100);
|
|
};
|
|
|
|
// Flash the active button
|
|
const $button = instance.$('.toolbarSectionButton').first();
|
|
flashButton($button);
|
|
|
|
// Flash the parent button as well in case of this button is inside a drawer
|
|
const $parentButton = $button.closest('.toolbarSectionButton.expandable');
|
|
if ($parentButton.length) {
|
|
flashButton($parentButton);
|
|
}
|
|
});
|
|
});
|
|
|
|
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');
|
|
return instance.getActiveToolSubProperty('svgLink', activeToolId);
|
|
},
|
|
|
|
iconClasses() {
|
|
const instance = Template.instance();
|
|
const activeToolId = Session.get('ToolManagerActiveTool');
|
|
return instance.getActiveToolSubProperty('iconClasses', activeToolId);
|
|
},
|
|
|
|
disableButton() {
|
|
const instance = Template.instance();
|
|
return instance.data.disableFunction && instance.data.disableFunction();
|
|
}
|
|
});
|
|
|
|
Template.toolbarSectionButton.events({
|
|
'click .toolbarSectionButton:not(.expandable)'(event, instance) {
|
|
// Prevent the event from bubbling to parent tools
|
|
event.stopPropagation();
|
|
|
|
// Stop here if the button is disabled
|
|
if ($(event.currentTarget).hasClass('disabled')) return;
|
|
|
|
// Run the command attached to the button
|
|
OHIF.commands.run(instance.data.id);
|
|
}
|
|
});
|