Skip to content

Instantly share code, notes, and snippets.

@will-moore
Created September 30, 2020 16:07
Show Gist options
  • Save will-moore/89f4cc277f730d6304c9e58e1efd1203 to your computer and use it in GitHub Desktop.
Save will-moore/89f4cc277f730d6304c9e58e1efd1203 to your computer and use it in GitHub Desktop.
// Configure OMERO.web open_with, pointing to the script_url to this gist
// $ omero config append omero.web.open_with '["openwith_url", "https://hms-dbmi.github.io/vizarr?source=https%3A%2F%2Fs3.embassy.ebi.ac.uk%2Fidr%2Fzarr%2Fv0.1%2F$ID.zarr", {"script_url": "url/to/omero_open_with_url.js", "label":"vizarr"}]'
function getIdrIdForNode(node) {
let idrId = node.id;
// for testing IDR images imported to other OMERO.servers,
// we can rename the images to be the IDR ID
let idFromName = parseInt(node.name);
if (!isNaN(idFromName)) {
idrId = idFromName;
}
return idrId;
}
// See https://docs.openmicroscopy.org/omero/5.6.2/developers/Web/LinkingFromWebclient.html#open-with
// Here we set an 'enabled' handler that is passed a list of selected objects
OME.setOpenWithEnabledHandler("openwith_url", function (selected) {
// selected is a list of objects containing id, name, type
console.log('selected', selected);
// Only support single image
if (selected.length !== 1 || selected[0].type !== 'image') {
return false;
}
let idrId = getIdrIdForNode(selected[0]);
// We need to test whether image ID is available here...
let url = `https://s3.embassy.ebi.ac.uk/idr/zarr/v0.1/${idrId}.zarr/.zgroup`;
fetch(url, { mode: 'cors'})
.then(response => {
// If 404 etc, we disable the 'vizarr' menu item
// Since this is async, we have already returned 'true' below
// So we have to find and manually "disable" the menu-item
if (!response.ok) {
$(".jstree-contextmenu").find('li').each(function () {
let $li = $(this);
let itemText = $li.text();
// Find the child <li> with 'vizarr' label
if (itemText.trim() === 'vizarr') {
$li.addClass('vakata-contextmenu-disabled');
// this pseudo 'disabled' doesn't prevent the item being clicked
// so we add a flag that can be tested for below
$li.data('disabled', true);
}
});
}
})
//
return true;
});
// The URL provider returns a function, so that we can test
// for the disabled flag added above and do nothing if 'disabled'
OME.setOpenWithUrlProvider("openwith_url", function (selected, url) {
return function () {
// Have to find the 'vizarr' <li>
$(".jstree-contextmenu").find('li').each(function () {
let itemText = $(this).text();
if (itemText.trim() === 'vizarr') {
// If it's not disabled, create URL and open window
if (!$(this).data('disabled')) {
idrId = getIdrIdForNode(selected[0]);
// We use the url from config, replacing '$ID'
url = url.replace('$ID', idrId);
window.open(url, '_blank');
}
}
});
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment