@@ -57,109 +57,121 @@ const graphOpts = {
57
57
const ipfs = new Ipfs ( )
58
58
const { Buffer } = Ipfs
59
59
60
- const cy = cytoscape ( {
61
- elements : [ ] ,
62
- container : document . getElementById ( 'root' ) ,
63
- ...graphOpts
64
- } )
60
+ const addToVis = async ( cy , cid , seen = [ ] ) => {
61
+ const { value : source } = await ipfs . dag . get ( cid )
62
+
63
+ let nodeData = { }
65
64
66
- const slowMap = async ( items , fn , delay = 10 ) => {
67
- const res = [ ]
68
- for ( let i = 0 ; i < items . length ; i ++ ) {
69
- res . push ( await fn ( items [ i ] , i ) )
70
- await new Promise ( resolve => setTimeout ( resolve , delay ) )
65
+ try {
66
+ // it's a unix system?
67
+ nodeData = unixfs . unmarshal ( source . data )
68
+ } catch ( err ) {
69
+ // dag-pb but not a unixfs.
70
+ console . log ( err )
71
71
}
72
- return res
73
- }
74
72
75
- const addToVis = async ( cy , cid ) => {
76
- const { value : source } = await ipfs . dag . get ( cid )
77
- await slowMap ( source . links , async ( link ) => {
78
- const { value : target } = await ipfs . dag . get ( link . cid )
79
- let nodeData = { }
80
- try {
81
- // it's a unix system?
82
- nodeData = unixfs . unmarshal ( target . data )
83
- } catch ( err ) {
84
- // dag-pb but not a unixfs.
85
- console . log ( err )
86
- }
87
- console . log ( `adding leaf ${ link . cid } to parent ${ cid } ` , nodeData )
88
- cy . add ( [ {
73
+ for ( let i = 0 ; i < source . links . length ; i ++ ) {
74
+ await addToVis ( cy , source . links [ i ] . cid )
75
+ }
76
+
77
+ if ( ! cy . getElementById ( cid . toString ( ) ) . length ) {
78
+ cy . add ( {
89
79
group : 'nodes' ,
90
80
data : {
91
- id : link . cid . toString ( ) ,
81
+ id : cid . toString ( ) ,
92
82
...nodeData
93
83
} ,
94
- classes : [
95
- nodeData . blockSizes . length ? 'meta' : 'leaf'
96
- ]
97
- } , {
98
- group : 'edges' ,
99
- data : {
100
- source : cid . toString ( ) ,
101
- target : link . cid . toString ( )
102
- }
103
- } ] )
104
-
105
- cy . layout ( graphOpts . layout ) . run ( )
84
+ classes : source . links . length ? [ ] : [ 'leaf' ]
85
+ } )
86
+ }
106
87
107
- return addToVis ( cy , link . cid )
108
- } )
88
+ cy . add ( source . links . map ( link => ( {
89
+ group : 'edges' ,
90
+ data : {
91
+ source : cid . toString ( ) ,
92
+ target : link . cid . toString ( )
93
+ }
94
+ } ) ) )
109
95
}
110
96
111
97
const show = el => { el . style . visibility = 'visible' }
112
98
const hide = el => { el . style . visibility = 'hidden' }
113
99
100
+ const rootEl = document . getElementById ( 'root' )
114
101
const fileEl = document . getElementById ( 'file' )
115
102
const chunkerEl = document . getElementById ( 'chunker' )
116
103
const strategyEl = document . getElementById ( 'strategy' )
117
104
const maxChildrenEl = document . getElementById ( 'max-children' )
118
105
const layerRepeatEl = document . getElementById ( 'layer-repeat' )
119
106
120
- strategyEl . addEventListener ( 'change' , e => {
121
- if ( [ 'balanced' , 'trickle' ] . includes ( e . target . value ) ) {
107
+ function updateAvailableOptions ( ) {
108
+ if ( [ 'balanced' , 'trickle' ] . includes ( strategyEl . value ) ) {
122
109
show ( maxChildrenEl )
123
110
} else {
124
111
hide ( maxChildrenEl )
125
112
}
126
113
127
- if ( e . target . value === 'trickle' ) {
114
+ if ( strategyEl . value === 'trickle' ) {
128
115
show ( layerRepeatEl )
129
116
} else {
130
117
hide ( layerRepeatEl )
131
118
}
132
- } )
119
+ }
120
+
121
+ updateAvailableOptions ( )
122
+
123
+ strategyEl . addEventListener ( 'change' , updateAvailableOptions )
133
124
134
125
ipfs . on ( 'ready' , ( ) => {
135
126
console . log ( 'IPFS is ready!' )
136
127
137
128
show ( fileEl )
138
129
130
+ let vis
131
+ const rawFiles = [ ]
132
+
133
+ ; [ chunkerEl , strategyEl , maxChildrenEl , layerRepeatEl ] . forEach ( el => {
134
+ el . addEventListener ( 'change' , addAndRender )
135
+ } )
136
+
137
+ async function addAndRender ( ) {
138
+ if ( ! rawFiles . length ) return
139
+
140
+ if ( vis ) {
141
+ vis . container ( ) . parentNode . removeChild ( vis . container ( ) )
142
+ vis = null
143
+ }
144
+
145
+ const files = rawFiles . map ( ( { path, content } ) => ( { path, content } ) )
146
+ const res = await ipfs . add ( files , {
147
+ chunker : chunkerEl . value ,
148
+ strategy : strategyEl . value ,
149
+ maxChildrenPerNode : parseInt ( maxChildrenEl . value ) ,
150
+ layerRepeat : parseInt ( layerRepeatEl . value ) ,
151
+ wrapWithDirectory : files . length > 1
152
+ } )
153
+
154
+ console . log ( 'added' , res [ res . length - 1 ] . hash )
155
+
156
+ const container = document . createElement ( 'div' )
157
+ container . style . height = '100%'
158
+ rootEl . appendChild ( container )
159
+
160
+ const cy = cytoscape ( { elements : [ ] , container, ...graphOpts } )
161
+
162
+ await addToVis ( cy , res [ res . length - 1 ] . hash )
163
+
164
+ cy . layout ( graphOpts . layout ) . run ( )
165
+ vis = cy
166
+ }
167
+
139
168
fileEl . addEventListener ( 'change' , e => {
140
169
const file = e . target . files [ 0 ]
141
170
const fileReader = new FileReader ( )
142
171
143
172
fileReader . onload = async e => {
144
- const path = file . name
145
- const content = Buffer . from ( e . target . result )
146
- const res = await ipfs . add ( { path, content } , {
147
- chunker : chunkerEl . value ,
148
- strategy : strategyEl . value ,
149
- maxChildrenPerNode : parseInt ( maxChildrenEl . value ) ,
150
- layerRepeat : parseInt ( layerRepeatEl . value )
151
- } )
152
-
153
- console . log ( 'added' , res [ 0 ] . hash )
154
-
155
- cy . add ( {
156
- group : 'nodes' ,
157
- data : {
158
- id : res [ 0 ] . hash
159
- }
160
- } )
161
-
162
- addToVis ( cy , res [ 0 ] . hash )
173
+ rawFiles . push ( { path : file . name , content : Buffer . from ( e . target . result ) } )
174
+ addAndRender ( )
163
175
}
164
176
165
177
fileReader . readAsArrayBuffer ( file )
0 commit comments