From 65f9265baed1909a51ae6d05368424d133491347 Mon Sep 17 00:00:00 2001 From: Jan Konstant Date: Mon, 29 Nov 2021 17:03:44 +0100 Subject: [PATCH 01/14] removed io.js --- vis/js/actions/index.js | 4 +- vis/js/datamanagers/DataManager.js | 207 ++++ vis/js/default-config.js | 11 - vis/js/intermediate.js | 150 ++- vis/js/io.js | 350 ------- vis/js/mediator.js | 147 +-- vis/js/reducers/areas.js | 4 +- vis/js/reducers/contextLine.js | 15 +- vis/js/reducers/data.js | 8 +- vis/js/templates/listentry/BasicListEntry.jsx | 2 +- .../listentry/ClassificationListEntry.jsx | 2 +- vis/js/templates/listentry/Details.jsx | 4 +- .../templates/listentry/StandardListEntry.jsx | 2 +- vis/js/utils/data.js | 133 +++ vis/test/component/knowledgemap-base.test.js | 13 +- vis/test/component/list-base.test.js | 15 +- vis/test/datamanagers/datamanager.test.js | 19 + .../__snapshots__/list-base.test.js.snap | 4 +- vis/test/snapshot/list-base.test.js | 12 +- vis/test/store/areas.test.js | 22 +- vis/test/store/data.test.js | 12 +- vis/test/store/initialize.test.js | 945 +++++++++++++++--- 22 files changed, 1390 insertions(+), 691 deletions(-) create mode 100644 vis/js/datamanagers/DataManager.js delete mode 100644 vis/js/io.js create mode 100644 vis/test/datamanagers/datamanager.test.js diff --git a/vis/js/actions/index.js b/vis/js/actions/index.js index 0f2e16af2..642f039dc 100644 --- a/vis/js/actions/index.js +++ b/vis/js/actions/index.js @@ -70,7 +70,8 @@ export const initializeStore = ( chartSize, streamWidth, streamHeight, - listHeight + listHeight, + scalingFactors, ) => ({ type: "INITIALIZE", configObject, @@ -81,6 +82,7 @@ export const initializeStore = ( streamWidth, streamHeight, listHeight, + scalingFactors, }); /** diff --git a/vis/js/datamanagers/DataManager.js b/vis/js/datamanagers/DataManager.js new file mode 100644 index 000000000..8493728ed --- /dev/null +++ b/vis/js/datamanagers/DataManager.js @@ -0,0 +1,207 @@ +import $ from "jquery"; + +import { + getAuthorsList, + getInternalMetric, + getOpenAccessLink, + getOutlink, + getVisibleMetric, + isOpenAccess, + parseCoordinate, +} from "../utils/data"; + +// attributes that aren't escaped +const PROTECTED_ATTRS = new Set(["paper_abstract", "authors"]); + +class DataManager { + constructor(config) { + this.config = config; + // outputs + this.context = {}; + this.papers = []; + this.streams = []; + this.areas = []; + } + + parseData(backendData) { + this.__parseContext(backendData); + + this.__parsePapers(backendData); + + this.__parseAreas(backendData); + this.__parseStreams(backendData); + } + + __parseContext(backendData) { + this.context = {}; + if (typeof backendData.context === "object") { + this.context = backendData.context; + } + if (typeof this.context.params === "string") { + this.context.params = JSON.parse(this.context.params); + } + } + + __parsePapers(backendData) { + this.papers = this.__getPapersArray(backendData); + + this.__sanitizePapers(); + + this.__processPapers(); + } + + // migrated from legacy code + __getPapersArray(backendData) { + if (this.config.show_context) { + if (typeof backendData.data === "string") { + return JSON.parse(backendData.data); + } + return backendData.data; + } + if (typeof backendData.data === "object") { + return backendData.data; + } + + return backendData; + } + + __sanitizePapers() { + const loc = this.config.localization[this.config.language]; + this.papers.forEach((paper) => { + this.__setFallbackValue(paper, "area", loc.default_area); + this.__setFallbackValue(paper, "authors", loc.default_author); + this.__setFallbackValue(paper, "file_hash", loc.default_hash); + this.__setFallbackValue(paper, "id", loc.default_id); + this.__setFallbackValue(paper, "paper_abstract", loc.default_abstract); + this.__setFallbackValue(paper, "published_in", loc.default_published_in); + this.__setFallbackValue(paper, "readers", loc.default_readers); + this.__setFallbackValue(paper, "title", loc.no_title); + this.__setFallbackValue(paper, "url", loc.default_url); + this.__setFallbackValue(paper, "x", loc.default_x); + this.__setFallbackValue(paper, "y", loc.default_y); + this.__setFallbackValue(paper, "year", loc.default_year); + this.__setFallbackValue(paper, "comments", []); + this.__setFallbackValue(paper, "subject_orig", ""); + this.config.scale_types.forEach((type) => { + this.__setFallbackValue(paper, type, loc.default_readers); + }); + }); + } + + __setFallbackValue(paper, property, fallback) { + if (typeof paper[property] === "undefined" || paper[property] === null) { + paper[property] = fallback; + } + } + + __processPapers() { + const blockedCoords = {}; + this.papers.forEach((paper) => { + paper.safe_id = this.__getSafeId(paper); + this.__escapeStrings(paper); + this.__parseAuthors(paper); + this.__parseCoordinates(paper); + while (blockedCoords[`${paper.x}:${paper.y}`]) { + this.__adjustCoordinates(paper); + } + blockedCoords[`${paper.x}:${paper.y}`] = true; + this.__parseAccess(paper); + this.__parseLink(paper); + this.__parseComments(paper); + this.__countMetrics(paper); + }); + } + + // migrated from legacy code + __getSafeId(paper) { + const id = paper.id.toString(); + + return id.replace(/[^a-zA-Z0-9]/g, (s) => { + const c = s.charCodeAt(0); + if (c === 32) { + return "-"; + } + return "__" + ("000" + c.toString(16)).slice(-4); + }); + } + + __parseAuthors(paper) { + paper.authors_list = getAuthorsList( + paper.authors, + this.config.convert_author_names + ); + + paper.authors_string = paper.authors_list.join(", "); + } + + // migrated from legacy code + __escapeStrings(paper) { + for (const field in paper) { + if (typeof paper[field] === "string") { + paper[field] = $("