Skip to content
Snippets Groups Projects
Select Git revision
  • 72a867d05290ac642356ae587e78e35b865bf0f6
  • master default protected
  • leo
  • dex
  • pendulum
  • apfelstruder
  • littlerascal
7 results

open_json.js

Blame
  • open_json.js 1.02 KiB
    /*
    
    open csv, setup outputs for fields (?)
    
    */
    
    import {
      Hunkify,
      Input,
      Output,
      State
    } from '../hunks.js'
    
    
    export default function OpenJSON() {
      Hunkify(this)
    
      let outRef = new Output('reference', 'data', this)
      this.outputs.push(outRef)
    
      let refUpdated = false
      let theRef = {}
      let bumpState = new State('boolean', 'release', false)
      this.states.push(bumpState)
      bumpState.onChange = (value) => {
        refUpdated = true
      }
    
      let readJSON = (file) => {
        let reader = new FileReader()
        reader.onload = (evt) => {
          theRef = JSON.parse(evt.target.result)
          // that's it,
          refUpdated = true
        }
        reader.readAsText(file)
      }
    
      this.init = () => {
        this.dom = $('<div>').get(0)
      }
    
      this.onload = () => {
        let btn = $('<input type="file" accept=".json">').get(0)
        $(btn).on('change', (evt) => {
          readJSON(evt.target.files[0])
        })
        $(this.dom).append(btn)
      }
    
      this.loop = () => {
        if(refUpdated && !outRef.io()){
          outRef.put(theRef)
          refUpdated = false
        }
      }
    
    }