11const graphElem = document . getElementById ( "idea-graph" ) ;
22const Graph = ForceGraph3D ( ) ( graphElem )
3+ var dataState = { }
34displayGraph ( ) ;
45
56window . onclick = function ( event ) {
@@ -9,11 +10,9 @@ window.onclick = function (event) {
910}
1011
1112async function displayGraph ( ) {
12- let newData
1313 try {
14- newData = await getData ( ) ;
15- newData . nodes = newData . nodes . map ( node => ( { ...node , tags : new Set ( node . tags ) } ) ) ;
16- Graph . graphData ( newData )
14+ dataState = await getData ( ) ;
15+ Graph . graphData ( JSON . parse ( JSON . stringify ( dataState ) ) )
1716 . nodeLabel ( "name" )
1817 . onNodeHover ( node => graphElem . style . cursor = node ? "pointer" : null )
1918 . onNodeClick ( node => {
@@ -30,7 +29,7 @@ async function displayGraph() {
3029 } ) ;
3130 }
3231 catch ( error ) {
33- console . log ( error )
32+ console . log ( error ) ;
3433 }
3534}
3635
@@ -42,7 +41,7 @@ function displayDetail(node) {
4241 const detailDescription = document . getElementById ( "detail-description" ) ;
4342
4443 detailName . textContent = node . name ;
45- detailTags . textContent = [ ... node . tags ] . join ( ', ' ) ;
44+ detailTags . textContent = node . tags . filter ( Boolean ) . toString ( ) ;
4645 detailCreator . textContent = node . creator ;
4746 detailDescription . textContent = node . description ;
4847
@@ -66,24 +65,30 @@ function onModalSubmitClick(event, form) {
6665 const ideaDescription = form [ "description" ] ;
6766 const creatorName = form [ "creator-name" ] ;
6867
69- const { nodes , links } = Graph . graphData ( ) ;
70- const id = nodes . length ;
68+ const id = dataState . nodes . length ;
69+ const newTags = new Set ( ideaTags . value . split ( "," ) . map ( tag => tag . trim ( ) . toLowerCase ( ) ) ) ;
7170
7271 const newIdea = {
73- id : id ,
72+ id : id . toString ( ) ,
7473 name : ideaName . value ,
75- tags : new Set ( ideaTags . value . split ( "," ) . map ( tag => tag . trim ( ) . toLowerCase ( ) ) ) ,
74+ tags : Array . from ( newTags ) ,
7675 creator : creatorName . value ,
7776 description : ideaDescription . value ,
7877 } ;
79- const newLinks = matchTags ( newIdea , nodes )
78+ const newLinks = matchTags ( newIdea , dataState . nodes )
8079
81- Graph . graphData ( {
82- nodes : [ ...nodes , { ...newIdea } ] ,
83- links : [ ...links , ...newLinks ]
84- } ) ;
80+ dataState . nodes = [ ...dataState . nodes , { ...newIdea } ]
81+ dataState . links = [ ...dataState . links , ...newLinks ]
8582
83+ Graph . graphData ( JSON . parse ( JSON . stringify ( dataState ) ) )
8684 form . reset ( ) ;
85+
86+ try {
87+ storeData ( dataState ) ;
88+ }
89+ catch ( error ) {
90+ console . log ( error ) ;
91+ }
8792}
8893
8994function onModalCloseClick ( modal ) {
@@ -93,8 +98,8 @@ function onModalCloseClick(modal) {
9398function matchTags ( newIdea , nodes ) {
9499 var newLinks = [ ] ;
95100 for ( idea of nodes ) {
96- var intersection = new Set ( [ ... idea . tags ] . filter ( x => newIdea . tags . has ( x ) ) ) ;
97- if ( intersection . size != 0 ) {
101+ var intersection = idea . tags . filter ( x => newIdea . tags . includes ( x ) ) ;
102+ if ( intersection . length != 0 ) {
98103 newLinks = [ ...newLinks , { source : newIdea . id , target : idea . id } ]
99104 }
100105 }
0 commit comments