Skip to content

Commit

Permalink
Loading crates across pages is almost working
Browse files Browse the repository at this point in the history
  • Loading branch information
spikelynch committed Mar 30, 2024
1 parent 287dcf6 commit e096f92
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 81 deletions.
2 changes: 1 addition & 1 deletion docs/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ to explore the contents and structure of an [RO-Crate](https://www.researchobjec

The source code is available on [GitHub](https://github.com/Sydney-Informatics-Hub/observable-crate/)

Developed by Mike Lynch at the [Sydney Informatics Hub](https://www.sydney.edu.au/research/facilities/sydney-informatics-hub.html)
Developed by [Mike Lynch](mailto:m.lynch@sydney.edu.au) at the [Sydney Informatics Hub](https://www.sydney.edu.au/research/facilities/sydney-informatics-hub.html)
45 changes: 17 additions & 28 deletions docs/components/crate.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ export function nil_crate() {
}

export function root_entity(entities) {
const root_id = entities['ro-crate-metadata.json']['links_from']['about'][0];
return entities[root_id];
if( 'ro-crate-metadata.json' in entities ) {
const root_id = entities['ro-crate-metadata.json']['links_from']['about'][0];
return entities[root_id];
} else {
return null;
}
}

export function crate_link(entities, i) {
Expand Down Expand Up @@ -70,31 +74,16 @@ ${entity_links(nodes, "links_from", node)}
}


export function naive_tree(crate) {
const nodes = crate.nodes;
const links = crate.links;
const seen = new Set();
const tree = {};
const path_delim = "|";

const root = root_entity(crate.nodes);

tree[root.id] = root.name;

naive_tree_r(nodes, seen, root.id, tree, root.links_from);
return Object.keys(tree);
}

function naive_tree_r(nodes, seen, stem, tree, links) {
for( const prop in links ) {
for( const i of links[prop] ) {
if( !seen.has(i) ) {
seen.add(i);
const path = stem + '|' + nodes[i].name;
tree[path] = nodes[i].name;
naive_tree_r(nodes, seen, path, tree, nodes[i].links_from)
}
export function current_crate() {
const current_json = sessionStorage.getItem("ro-crate");
if( current_json ) {
try {
const crate = JSON.parse(current_json);
return crate;
} catch(e) {
console.log("Bad result from sessionStorage")
return nil_crate();
}
}
}

return nil_crate();
}
4 changes: 2 additions & 2 deletions docs/force-graph.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ toc: false
---
```js

import { root_entity, make_colour_map } from "./components/crate.js";
import { root_entity, current_crate, make_colour_map } from "./components/crate.js";
import { forcegraph } from "./components/forcegraph.js";

const crate = await FileAttachment("./data/crate.json").json();
const crate = current_crate();
const root = root_entity(crate.nodes);
const nodes = Object.keys(crate.nodes).map((eid) => crate.nodes[eid]);

Expand Down
12 changes: 8 additions & 4 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ ul.relations {
</style>
```js

import { root_entity, entity } from "./components/crate.js";
import { root_entity, entity, current_crate } from "./components/crate.js";

const crate = await FileAttachment("./data/crate.json").json();
const crate = current_crate();
const nodes = crate.nodes;

const nodes_array = Object.keys(crate.nodes).map((eid) => crate.nodes[eid]);
Expand All @@ -28,7 +28,7 @@ let hash = Generators.observe(notify => {

```

## ${root.name }
## ${root ? root.name : 'No RO-Crate loaded'}

```js
function hash_to_item(hash) {
Expand Down Expand Up @@ -66,6 +66,10 @@ const filtered = nodes_array.filter((n) => match_node(n, search));
if( search ) {
filtered.map((n) => display(entity(nodes, n)));
} else {
display(entity(nodes, node));
if( node ) {
display(entity(nodes, node));
} else {
display(html`<p><a href="load.html">Load an RO-Crate</a></p>`)
}
}
```
4 changes: 2 additions & 2 deletions docs/statistics.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ toc: false
## Entities by type

```js
import { make_colour_map } from "./components/crate.js";
import { current_crate, make_colour_map } from "./components/crate.js";

const crate = FileAttachment("./data/crate.json").json();
const crate = current_crate();

```

Expand Down
53 changes: 10 additions & 43 deletions docs/upload.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ toc: false
```js

import { load_crate } from "./components/loader.js";
import { nil_crate, root_entity, make_colour_map } from "./components/crate.js";
import { forcegraph } from "./components/forcegraph.js";
import { current_crate, root_entity } from "./components/crate.js";


```
## RO-Crate

Upload an rocrate-metadata.json file (note that this is very experimental
and the uploaded ro-crate won't be passed through to the other pages)
Load an rocrate-metadata.json file: as this is a static site, the crate is
stored in your browser session and won't persist.

Note that this is very experimental and will likely fail for files > 5MB.

```js

Expand All @@ -26,58 +27,24 @@ const cratefile = view(Inputs.file({label: "ro-crate JSON", accept: ".json", req
const cratejson = cratefile.json();
```



```js


const crate = load_crate(cratejson);

const root = root_entity(crate.nodes);
const nodes = Object.keys(crate.nodes).map((eid) => crate.nodes[eid]);


const colours = make_colour_map(crate.types);

```

```js
const use_types = view(Inputs.checkbox(crate.types, {
multiple: true,
label: "Entity types",
value: crate.types,
}));
```

```js
const use_rels = view(Inputs.checkbox(crate.relations, {
multiple: true,
label: "Relationships",
value: crate.relations,
}));
```

```js
const use_externals = view(Inputs.checkbox(crate.externals, {
multiple: true,
label: "External ID domains",
}));
```
sessionStorage.setItem('ro-crate', JSON.stringify(crate))
console.log("Wrote crate to storage");

display(root.name);
display(root.description)

```js
const show_nodes = nodes.filter((n) => use_types.includes(n.type[0]));
const show_ids = show_nodes.map((n) => n.id);
const show_links = crate.links.filter(
(l) => {
if( !use_rels.includes(l.property) ) {
return false;
}
return show_ids.includes(l.source) && show_ids.includes(l.target);
});
```

const svg = forcegraph({nodes: show_nodes, links: show_links}, colours);

display(svg.node());

```
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e096f92

Please sign in to comment.