Skip to content

Commit

Permalink
Merge pull request #202 from Tehsurfer/context-cards-for-external-opens
Browse files Browse the repository at this point in the history
Get context cards working for external scaffold opens
  • Loading branch information
alan-wu authored May 8, 2024
2 parents 814abbe + 4b2d6ed commit bdd88c0
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/components/MapContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { useSettingsStore } from '../stores/settings';
import { findSpeciesKey } from './scripts/utilities.js';
import { MapSvgSpriteColor} from '@abi-software/svg-sprite';
import { initialState } from "./scripts/utilities.js";
import RetrieveContextCardMixin from "../mixins/RetrieveContextCardMixin.js"
import {
ElLoading as Loading
} from "element-plus";
Expand All @@ -40,6 +41,7 @@ export default {
Loading,
SplitFlow,
},
mixins: [RetrieveContextCardMixin],
props: {
/**
* A link (URL) to share.
Expand Down Expand Up @@ -158,7 +160,7 @@ export default {
* instead change the current entry by setting the state.
* @arg state
*/
setCurrentEntry: function(state) {
setCurrentEntry: async function(state) {
if (state && state.type) {
if (state.type === "Scaffold" && state.url) {
//State for scaffold containing the following items:
Expand All @@ -167,14 +169,17 @@ export default {
// resource - the url to metadata
// state - state to restore (viewport)
// viewUrl - relative path of the view file to metadata
const newView = {
let newView = {
type: state.type,
label: state.label,
region: state.region,
resource: state.url,
state: state.state,
viewUrl: state.viewUrl
};
// Add content from scicrunch for the context card
const contextCardInfo = await this.retrieveContextCardFromUrl(state.url);
newView = {...newView, ...contextCardInfo};
this.$refs.flow.createNewEntry(newView);
} else if (state.type === "MultiFlatmap") {
//State for scaffold containing the following items:
Expand Down
83 changes: 83 additions & 0 deletions src/mixins/RetrieveContextCardMixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@

/* eslint-disable no-alert, no-console */
export default {
// Note that the setting store is included in MapContent.vue
methods: {
retrieveContextCardFromUrl: async function (url) {
// split the url to get the datasetId
const [datasetId, basePath, scaffoldPath, s3uri] = this.splitInfoFromUrl(url);

// get the context file from scicrunch
const sciResults = await this.getContextFileFromScicrunch(datasetId, scaffoldPath);
if (!sciResults.success){
return {} // return empty object if no context file is found (the empty object will be added to the entry)
}

// return the context file
const fullPath = basePath + sciResults.contextFile + s3uri;
return {
s3uri: sciResults.s3uri,
contextCardUrl: fullPath,
}
},
splitInfoFromUrl: function (url) {
// example url: "https://mapcore-demo.org/current/sparc-api-v2/s3-resource/221/3/files/derivative/Scaffolds/mouse_colon_metadata.json",
// find the part after 's3-resource'
let s3path = url.split('s3-resource')[1];
let basePath = url.split('files/')[0] + 'files/' // This gives us the base path for our relative path we will get from scicrunch
let scaffoldPath = url.split('files/')[1].split('?')[0] // This gives us the relative path to the file we want to get from scicrunch
let s3uri = '?' + url.split('?')[1] // This gives us the uri needed to get the file from s3

// split the url by '/'
const parts = s3path.split('/');
// remove the first part
parts.shift();
// return the datasetId which is the first part
const datasetId = parts[0];

return [datasetId, basePath, scaffoldPath, s3uri];
},
getContextFileFromScicrunch: async function (datasetId, scaffoldPath) {
// get the context file from scicrunch
let results = await fetch(`${this.settingsStore.sparcApi}/dataset_info/using_multiple_discoverIds/?discoverIds=${datasetId}`)
.then(response => response.json())
.then(data => {
// get the context info from the response
if (data.numberOfHits === 1) { // check if there is only one hit (We don't want to use the data if there are multiple hits)
const contextFile = data.results[0]['abi-contextual-information']

// check if there is only one context file and if so return it
if ( contextFile && contextFile.length === 1) {
return {
success: true,
contextFile: contextFile[0],
s3uri: data.results[0]['s3uri']
}
}

// If there are multiple context files, find the one that matches the scaffold path
else if (contextFile && contextFile.length > 1) {
let search = this.findContextInforForFilePath(data.results[0]['abi-context-file'], scaffoldPath);
if (search) {
return {
success: true,
contextFile: search,
s3uri: data.results[0]['s3uri']
}
}
}
}
return {success: false};
}).catch(error => {
console.error('Error:', error);
return {success: false};
});
return results;
},
findContextInforForFilePath: function (dataciteInfo, filePath) {
// find the context file that matches the scaffold path
let result = dataciteInfo.find((info) => info.datacite.isDerivedFrom.path.includes(filePath))
return result?.dataset?.path
}
}
}

0 comments on commit bdd88c0

Please sign in to comment.