1
1
const graphElem = document . getElementById ( "idea-graph" ) ;
2
2
const Graph = ForceGraph3D ( ) ( graphElem )
3
+ var dataState = { }
3
4
displayGraph ( ) ;
4
5
5
6
window . onclick = function ( event ) {
@@ -9,11 +10,9 @@ window.onclick = function (event) {
9
10
}
10
11
11
12
async function displayGraph ( ) {
12
- let newData
13
13
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 ) ) )
17
16
. nodeLabel ( "name" )
18
17
. onNodeHover ( node => graphElem . style . cursor = node ? "pointer" : null )
19
18
. onNodeClick ( node => {
@@ -30,7 +29,7 @@ async function displayGraph() {
30
29
} ) ;
31
30
}
32
31
catch ( error ) {
33
- console . log ( error )
32
+ console . log ( error ) ;
34
33
}
35
34
}
36
35
@@ -42,7 +41,7 @@ function displayDetail(node) {
42
41
const detailDescription = document . getElementById ( "detail-description" ) ;
43
42
44
43
detailName . textContent = node . name ;
45
- detailTags . textContent = [ ... node . tags ] . join ( ', ' ) ;
44
+ detailTags . textContent = node . tags . filter ( Boolean ) . toString ( ) ;
46
45
detailCreator . textContent = node . creator ;
47
46
detailDescription . textContent = node . description ;
48
47
@@ -66,24 +65,30 @@ function onModalSubmitClick(event, form) {
66
65
const ideaDescription = form [ "description" ] ;
67
66
const creatorName = form [ "creator-name" ] ;
68
67
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 ( ) ) ) ;
71
70
72
71
const newIdea = {
73
- id : id ,
72
+ id : id . toString ( ) ,
74
73
name : ideaName . value ,
75
- tags : new Set ( ideaTags . value . split ( "," ) . map ( tag => tag . trim ( ) . toLowerCase ( ) ) ) ,
74
+ tags : Array . from ( newTags ) ,
76
75
creator : creatorName . value ,
77
76
description : ideaDescription . value ,
78
77
} ;
79
- const newLinks = matchTags ( newIdea , nodes )
78
+ const newLinks = matchTags ( newIdea , dataState . nodes )
80
79
81
- Graph . graphData ( {
82
- nodes : [ ...nodes , { ...newIdea } ] ,
83
- links : [ ...links , ...newLinks ]
84
- } ) ;
80
+ dataState . nodes = [ ...dataState . nodes , { ...newIdea } ]
81
+ dataState . links = [ ...dataState . links , ...newLinks ]
85
82
83
+ Graph . graphData ( JSON . parse ( JSON . stringify ( dataState ) ) )
86
84
form . reset ( ) ;
85
+
86
+ try {
87
+ storeData ( dataState ) ;
88
+ }
89
+ catch ( error ) {
90
+ console . log ( error ) ;
91
+ }
87
92
}
88
93
89
94
function onModalCloseClick ( modal ) {
@@ -93,8 +98,8 @@ function onModalCloseClick(modal) {
93
98
function matchTags ( newIdea , nodes ) {
94
99
var newLinks = [ ] ;
95
100
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 ) {
98
103
newLinks = [ ...newLinks , { source : newIdea . id , target : idea . id } ]
99
104
}
100
105
}
0 commit comments