@@ -25,11 +25,8 @@ const Main = (function() {
2525 /**
2626 * init: set up singleton classes and create initial drawing
2727 */
28- let _initialized = false ;
2928 function init ( ) {
3029 // setup
31- if ( _initialized ) { return ; }
32- _initialized = true ;
3330 body = document . body . getBoundingClientRect ( ) ;
3431 svg = SVG ( 'main' )
3532 . size ( body . width , window . innerHeight - body . top - 10 ) ;
@@ -44,6 +41,12 @@ const Main = (function() {
4441 // load and render initial dataset by default
4542 changeDataset ( ) ;
4643
44+ setupSVGListeners ( ) ;
45+ setupUIListeners ( ) ;
46+ } // end init
47+
48+
49+ function setupSVGListeners ( ) {
4750 // svg event listeners
4851 svg . on ( 'row-resize' , function ( e ) {
4952 lm . stopEditing ( ) ;
@@ -104,7 +107,9 @@ const Main = (function() {
104107 tree . resize ( ) ;
105108 }
106109 } ) ;
110+ }
107111
112+ function setupUIListeners ( ) {
108113 // window event listeners
109114 // resize function
110115 function resizeWindow ( ) {
@@ -215,7 +220,114 @@ const Main = (function() {
215220 e . target . classList . remove ( 'open' ) ;
216221 }
217222 } ) ;
223+
224+ document . getElementById ( 'file-input' ) . onchange = loadFile ;
225+ }
226+
227+ function printErrorMessage ( text ) {
228+ document . getElementById ( 'error-message' ) . textContent = text ;
218229 }
230+ function clearErrorMessage ( ) {
231+ document . getElementById ( 'error-message' ) . textContent = '' ;
232+ }
233+ function clearFile ( ) {
234+ document . getElementById ( 'form' ) . reset ( ) ;
235+ document . getElementById ( 'text-input' ) = '' ;
236+ }
237+
238+ /**
239+ * loadFile: read file
240+ */
241+ function loadFile ( e ) {
242+ let files = e . target . files ;
243+ console . log ( files ) ;
244+
245+ // get extension
246+ if ( files . length === 1 ) {
247+ const file = files [ 0 ] ;
248+ let fr = new FileReader ( ) ;
249+ fr . readAsText ( file ) ;
250+ fr . onload = parseFile ;
251+
252+ function parseFile ( ) {
253+ clearErrorMessage ( ) ;
254+ const text = fr . result ;
255+
256+ // try to coerce it into an accepted format
257+ try {
258+ let w , l , c ;
259+ if ( file . type === 'application/json' ) {
260+ // reach json
261+ parser . parseJson ( JSON . parse ( text ) ) ;
262+ [ w , l , c ] = buildWordsAndLinks ( ) ;
263+ } else if ( ! file . type ) {
264+ // brat standoff
265+ [ w , l , c ] = buildWordsLinksAnn ( text ) ;
266+ } else {
267+ printErrorMessage ( "Could not read file type: " + file . type ) ;
268+ }
269+
270+ if ( w && l && c ) {
271+ clear ( ) ;
272+ [ words , links , clusters ] = [ w , l , c ] ;
273+ setSyntaxVisibility ( ) ;
274+ draw ( ) ;
275+ document . getElementById ( 'text-input' ) . textContent = text ;
276+ }
277+ } catch ( e ) {
278+ console . log ( fr . result , e ) ;
279+ printErrorMessage ( "See error in console" ) ;
280+ }
281+ }
282+ }
283+ else if ( files . length > 1 ) {
284+ // search for matching .ann and .txt file
285+
286+ // sort by name
287+ let sortedFiles = [ ] . slice . call ( files ) . sort ( ( a , b ) => a . name . localeCompare ( b . name ) ) ;
288+
289+ let prev = 0 ;
290+
291+ for ( let i = 1 ; i < sortedFiles . length ; ++ i ) {
292+ let f1 = sortedFiles [ prev ] . name . toLowerCase ( ) ,
293+ f2 = sortedFiles [ i ] . name . toLowerCase ( ) ;
294+ let prefixesMatch = f1 . slice ( 0 , f1 . lastIndexOf ( '.' ) ) === f2 . slice ( 0 , f2 . lastIndexOf ( '.' ) ) ;
295+ let typesMatch = f1 . endsWith ( '.ann' ) || f2 . endsWith ( '.ann' ) ;
296+ if ( prefixesMatch && typesMatch && f1 !== f2 ) {
297+ // matching files found
298+ let fr = new FileReader ( ) ;
299+ fr . readAsText ( sortedFiles [ prev ] ) ;
300+ fr . onload = function ( ) {
301+ let f1Text = fr . result ;
302+ fr . readAsText ( sortedFiles [ i ] ) ;
303+ fr . onload = function ( ) {
304+ let w , l , c , text ;
305+ if ( f1 . endsWith ( '.ann' ) ) {
306+ text = fr . result ;
307+ [ w , l , c ] = buildWordsLinksAnn ( fr . result , f1Text ) ;
308+ }
309+ else {
310+ text = f1Text ;
311+ [ w , l , c ] = buildWordsLinksAnn ( f1Text , fr . result ) ;
312+ }
313+
314+ if ( w && l && c ) {
315+ clear ( ) ;
316+ [ words , links , clusters ] = [ w , l , c ] ;
317+ setSyntaxVisibility ( ) ;
318+ draw ( ) ;
319+ document . getElementById ( 'text-input' ) . textContent = text ;
320+ }
321+ }
322+ } ;
323+ break ;
324+ } else {
325+ prev = i ;
326+ }
327+ }
328+ }
329+ }
330+
219331
220332 /**
221333 * changeDataset: read and parse data from a json file in the /data folder
@@ -327,7 +439,6 @@ const Main = (function() {
327439 w . setSyntaxId ( token . id ) ;
328440 return w ;
329441 } ) ;
330- console . log ( 'words' , words ) ;
331442 const clusters = [ ] ;
332443
333444 [ ] . concat ( parser . data . entities , parser . data . triggers ) . forEach ( el => {
@@ -407,8 +518,8 @@ const Main = (function() {
407518 return [ words , links , clusters ] ;
408519 }
409520
410- function buildWordsLinksAnn ( text ) {
411- let data = parseAnn ( text ) ;
521+ function buildWordsLinksAnn ( text , ann ) {
522+ let data = parseAnn ( text , ann ) ;
412523 let mentions = { } ;
413524
414525 // build words
0 commit comments