Skip to content

Commit

Permalink
refa: with graphology
Browse files Browse the repository at this point in the history
  • Loading branch information
Myllaume committed Nov 9, 2024
1 parent 8ced7c8 commit a978abe
Show file tree
Hide file tree
Showing 32 changed files with 969 additions and 1,603 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ jobs:
uses: actions/upload-artifact@v3
if: failure()
with:
name: cypress-screenshots
name: temp_dir
path: temp
2 changes: 0 additions & 2 deletions controllers/create-record.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ function createRecord(
undefined,
undefined,
undefined,
undefined,
undefined,
config.opts,
);
if (saveIdOnYmlFrontMatter === false) {
Expand Down
51 changes: 25 additions & 26 deletions controllers/modelize.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import fs from 'node:fs';
import path from 'node:path';
import getHistorySavePath from './history.js';
import Graph from '../core/models/graph.js';
import Cosmoscope from '../core/models/cosmoscope.js';
import Link from '../core/models/link.js';
import Record from '../core/models/record.js';
import Config from '../core/models/config.js';
import Template from '../core/models/template.js';
import Report from '../models/report-cli.js';
import { DowloadOnlineCsvFilesError } from '../core/models/errors.js';
import { downloadFile } from '../core/utils/misc.js';
import { tmpdir } from 'node:os';
import getGraph from '../core/utils/getGraph.js';

async function modelize(options) {
let config = Config.get(Config.configFilePath);
Expand All @@ -24,17 +23,10 @@ async function modelize(options) {
})
.filter(({ value }) => value === true);

const optionsGraph = options
.filter(({ name }) => Graph.validParams.has(name))
.map(({ name }) => name);
const optionsTemplate = options
.filter(({ name }) => Template.validParams.has(name))
.map(({ name }) => name);

if (optionsGraph.includes('sample')) {
config = new Config(Config.getSampleConfig());
}

const {
select_origin: originType,
files_origin: filesPath,
Expand Down Expand Up @@ -77,19 +69,26 @@ async function modelize(options) {
break;
}

console.log(getModelizeMessage(optionsGraph, optionsTemplate, originType));
console.log(getModelizeMessage(optionsTemplate, originType));

let records;
switch (originType) {
case 'directory':
case 'directory': {
const files = Cosmoscope.getFromPathFiles(filesPath, config.opts);
records = Cosmoscope.getRecordsFromFiles(
files,
optionsTemplate.includes('citeproc'),
config.opts,
);

records = Cosmoscope.getRecordsFromFiles(files, config.opts);

if (
optionsTemplate.includes('citeproc') &&
config.opts['references_as_nodes'] &&
config.canCiteproc()
) {
records = records.concat(Cosmoscope.getBibliographicRecords(records, config.opts));
}

break;
case 'online':
}
case 'online': {
const tempDir = tmpdir();
nodesPath = path.join(tempDir, 'cosma-nodes.csv');
linksPath = path.join(tempDir, 'cosma-links.csv');
Expand All @@ -101,16 +100,17 @@ async function modelize(options) {
} catch (error) {
throw new DowloadOnlineCsvFilesError(error);
}
case 'csv':
}
case 'csv': {
let [formatedRecords, formatedLinks] = await Cosmoscope.getFromPathCsv(nodesPath, linksPath);
const links = Link.formatedDatasetToLinks(formatedLinks);
records = Record.formatedDatasetToRecords(formatedRecords, links, config);
records = Record.formatedDatasetToRecords(formatedRecords, formatedLinks, config);
break;
}
}

const graph = new Cosmoscope(records, config.opts, []);
const graph = getGraph(records, config);

const { html } = new Template(graph, optionsTemplate);
const { html } = new Template(records, graph, optionsTemplate);

fs.writeFile(path.join(exportPath, 'cosmoscope.html'), html, (err) => {
// Cosmoscope file for export folder
Expand All @@ -122,7 +122,7 @@ async function modelize(options) {
}
console.log(
['\x1b[34m', 'Cosmoscope generated', '\x1b[0m'].join(''),
`(${graph.records.length} records)`,
`(${records.length} records)`,
);
});

Expand Down Expand Up @@ -158,13 +158,12 @@ async function modelize(options) {
}

/**
* @param {string[]} optionsGraph
* @param {string[]} optionsTemplate
* @param {string} originType
*/

function getModelizeMessage(optionsGraph, optionsTemplate, originType) {
const settings = [...optionsGraph, ...optionsTemplate];
function getModelizeMessage(optionsTemplate, originType) {
const settings = optionsTemplate.filter((setting) => setting !== 'publish');

const msgSetting =
settings.length === 0 ? '' : `; settings: \x1b[1m${settings.join(', ')}\x1b[0m`;
Expand Down
16 changes: 9 additions & 7 deletions core/frontend/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@
*/

import { setNodesDisplaying } from './graph.js';
import filterPriority from './filterPriority.js';

window.addEventListener('DOMContentLoaded', () => {
/** @type {HTMLFormElement} */
const form = document.getElementById('types-form');
/** @type {HTMLInputElement[]} */
const inputs = form.querySelectorAll('input');

/** @type {[string, string[]][]} */
const types = Object.entries(typeList);

/**
* Default state
*/

for (const [name, { active }] of Object.entries(typeList)) {
form.querySelector(`[name="${name}"]`).checked = active;
for (const [name] of types) {
form.querySelector(`[name="${name}"]`).checked = true;
}
changeTypesState();

Expand All @@ -30,7 +32,7 @@ window.addEventListener('DOMContentLoaded', () => {
const filtersFromSearch = searchParams.get('filters')?.split('-');

if (filtersFromSearch?.length) {
for (const [name] of Object.entries(typeList)) {
for (const [name] of types) {
form.querySelector(`[name="${name}"]`).checked = filtersFromSearch.includes(name);
}
changeTypesState();
Expand All @@ -48,13 +50,13 @@ window.addEventListener('DOMContentLoaded', () => {

const nodeIdsToDisplay = new Set();

formState = Object.entries(typeList)
types
.filter(([name]) => !!formState[name])
.forEach(([, { nodes }]) => {
.forEach(([, nodes]) => {
nodes.forEach((id) => nodeIdsToDisplay.add(id));
});

setNodesDisplaying(Array.from(nodeIdsToDisplay), filterPriority.filteredByType);
setNodesDisplaying(Array.from(nodeIdsToDisplay));
}

let filterNameAltMode;
Expand Down
9 changes: 0 additions & 9 deletions core/frontend/filterPriority.js

This file was deleted.

40 changes: 5 additions & 35 deletions core/frontend/focus.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import GraphEngine from 'graphology';
import { bfsFromNode as neighborsExtend } from 'graphology-traversal/bfs.js';
import hotkeys from 'hotkeys-js';
import { displayNodesAll, setNodesDisplaying } from './graph.js';
import filterPriority from './filterPriority.js';
import { getRecordIdFromHash } from './records.js';

let graph = getGraphEngine();
import { graph, displayNodesAll, setNodesDisplaying } from './graph.js';
import { getRecordIdFromHash, displayAllIndex } from './records.js';

window.addEventListener('DOMContentLoaded', () => {
/** @type {HTMLInputElement} */
Expand Down Expand Up @@ -49,7 +45,8 @@ window.addEventListener('DOMContentLoaded', () => {
input.classList.remove('active');
input.removeEventListener('input', display);
modeSelect.removeEventListener('change', changeMode);
displayNodesAll(filterPriority.filteredByFocus);
displayNodesAll();
displayAllIndex();
}
});

Expand Down Expand Up @@ -83,38 +80,11 @@ window.addEventListener('DOMContentLoaded', () => {
{ mode: focusMode },
);

setNodesDisplaying(neighborsNodeIds, filterPriority.filteredByFocus);
setNodesDisplaying(neighborsNodeIds);
}

function changeMode() {
focusMode = modeSelect.value;
display();
}
});

/**
* @returns {GraphEngine}
*/

function getGraphEngine() {
const graph = new GraphEngine();

for (const { id, label } of data.nodes) {
if (graph.hasNode(id)) {
continue;
}

graph.addNode(id, {
label,
});
}
for (const { source, target } of data.links) {
if (graph.hasEdge(source.id, target.id)) {
continue;
}

graph.addEdge(source.id, target.id);
}

return graph;
}
Loading

0 comments on commit a978abe

Please sign in to comment.