summaryrefslogtreecommitdiff
path: root/client/index.js
blob: 835d859c62f61af41e22066b81ad32d1c1089382 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import React from 'react'
import ReactDOM from 'react-dom'
import { AppContainer } from 'react-hot-loader'
import { Provider } from 'react-redux'
import 'waypoints/lib/noframework.waypoints.min.js'

import { toArray } from './util'
import Applet from './applet'
import { store } from './store'
import appendMap from './map'
import { ModalImage } from './modalImage'

function appendReactApplet(el, payload) {
  ReactDOM.render(
    <AppContainer>
      <Provider store={store}>
        <Applet payload={payload} />
      </Provider>
    </AppContainer>, el
  )
}

function appendModalImage() {
  const el = document.createElement('div')
  document.body.appendChild(el)
  ReactDOM.render(<ModalImage />, el)
}

function fetchDataset(payload) {
  if (payload.command === 'face_analysis') return new Promise(resolve => resolve())
  if (payload.dataset === 'info') return new Promise(resolve => resolve())
  const url = "https://megapixels.nyc3.digitaloceanspaces.com/v1/citations/verified/" + payload.dataset + ".json"
  return fetch(url, { mode: 'cors' }).then(r => r.json())
}

function appendApplets(applets) {
  applets.forEach(([el, payload]) => {
    el.parentNode.classList.add(payload.cmd)
    switch (payload.cmd) {
      case 'citations':
      case 'load_file':
        el.parentNode.classList.add('wide')
        appendReactApplet(el, payload)
        el.classList.add('loaded')
        break
      case 'map':
        el.parentNode.classList.add('wide')
        appendMap(el, payload)
        el.classList.add('loaded')
        break
      default:
        appendReactApplet(el, payload)
        el.classList.add('loaded')
        break
    }
  })
}

function runApplets() {
  const applets = toArray(document.querySelectorAll('.applet')).map(el => {
    // console.log(el.dataset.payload)
    let payload
    try {
      payload = JSON.parse(el.dataset.payload)
      // console.log(payload)
    } catch (e) {
      return null
    }
    let cmdPartz = payload.command.split(" ")
    let cmd = cmdPartz.shift()
    let dataset = payload.dataset || null
    let url = null
    let opt = null
    payload.cmd = cmd
    payload.partz = cmdPartz
    if (payload.cmd === 'load_file' || payload.cmd === 'single_pie_chart') {
      payload.url = 'https://nyc3.digitaloceanspaces.com/megapixels/v1' + cmdPartz.shift()
      return [el, payload]
    }

    if (payload.partz.length) {
      opt = payload.partz.shift().trim()
      if (opt.indexOf('http') === 0) {
        dataset = null
        url = opt
      } else if (opt.indexOf('assets') === 0) {
        dataset = null
        // console.log(url)
      } else {
        dataset = opt
        url = null
      }
    }
    if ((('datasets' in payload) && !dataset && !url) || window.location.pathname.match('datasets')) {
      const path = window.location.pathname.split('/').filter(s => !!s)
      if (path.length > 1) {
        dataset = path.pop()
        if (dataset === 'index.html') {
          dataset = path.pop()
        }
        // console.log('dataset from path:', dataset)
      } else {
        // console.log('not on a dataset page')
        return [el, payload]
      }
    }
    payload.dataset = dataset
    payload.url = url
    // console.log(payload)
    return [el, payload]
  }).filter(a => !!a)
  const withDataset = applets.map(a => a[1].dataset ? a[1] : null).filter(a => !!a)
  if (withDataset.length) {
    fetchDataset(withDataset[0]).then(data => {
      withDataset.forEach(dataset => dataset.data = data)
      appendApplets(applets)
    })
  } else {
    appendApplets(applets)
  }
}

function buildWaypoints() {
  const element = document.querySelector('.content section:nth-child(2)')
  if (!element) return null
  let waypoint = new Waypoint({
    element,
    handler: direction => {
      if (direction === 'down') {
        document.body.classList.add('scrolled')
      } else {
        document.body.classList.remove('scrolled')
      }
      // console.log(direction)
      // console.log('Scrolled to waypoint!')
    }
  })
  return waypoint
}

function main() {
  // const paras = document.querySelectorAll('section p')
  // if (paras.length) {
  //   paras[0].classList.add('first_paragraph')
  // }
  let active = ''
  const { href } = window.location
  if (href.match('news')) {
    active = 'news'
  } else if (href.match('about')) {
    active = 'about'
  } else if (href.match('datasets')) {
    active = 'datasets'
  } else if (href.match('research')) {
    active = 'research'
  } else {
    active = href
  }
  toArray(document.querySelectorAll('header .links a')).some(tag => {
    if (tag.href.match(active)) {
      tag.classList.add('active')
      return true
    }
    return false
  })
  runApplets()
  buildWaypoints()
  appendModalImage()
}

main()