aboutsummaryrefslogtreecommitdiff
path: root/identify.js
blob: eb8827cecc4158fc7cf8f221c52267548dc52462 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
function identify (src, profile, keys) {

  var identifyDiv = h('div')

  if ((src[0] == '@') && (src != keys.publicKey)) {
    identifyDiv.appendChild(h('p', ['Please note: ' + src + ' is not you.']))
  }

  var photoURL = {}

  // this could be a hell of a lot dry-er

  // also we need to get rid of UI glitches when you hit the cancel button (it should return to the same state it started in)

  var newBackground = h('span', [
    h('input', {id: 'input', type: 'file', 
      onclick: function () {
        var canvas = document.getElementById("canvas")
        var ctx = canvas.getContext("2d")
        
        var maxW
        var maxH
        
        var input = document.getElementById('input')
        input.addEventListener('change', handleFiles)
        
        function handleFiles(e) {
          var img = new Image
          img.onload = function() {
            var iw = img.width
            var ih = img.height

            maxW = 800
            maxH = 800
            
            var scale = Math.min((maxW/iw), (maxH/ih))
            var iwScaled = iw*scale
            var ihScaled = ih*scale
            canvas.width = iwScaled
            canvas.height = ihScaled
            ctx.drawImage(img, 0, 0, iwScaled, ihScaled)
            photoURL.value = canvas.toDataURL('image/jpeg', 0.7)
          }
          img.src = URL.createObjectURL(e.target.files[0])
        } 
      }
    }),
    h('canvas', {id: 'canvas', width: '0', height: '0'}),
    h('button', {
      onclick: function () {
        identifyDiv.appendChild(identifyButtons)
        newBackground.parentNode.removeChild(newBackground)
      }
    }, ['Cancel']),
    h('button', {
      onclick: function () {
        if (photoURL.value) {
          content = {
            type: 'background',
            backgrounded: src,
            background: photoURL.value
          }
          localforage.removeItem('image:' + src)
          publish(content, keys).then(post => {
            open(post).then(msg => {
              nameInput.value = ''
              scroller.insertBefore(render(msg, keys), scroller.childNodes[1])
            })
          })
          newBackground.parentNode.removeChild(newBackground)
          identifyDiv.appendChild(identifyButton)
        }
      }
    }, ['Publish'])
  ])

  var newPhoto = h('span', [
    h('input', {id: 'input', type: 'file', 
      onclick: function () {
        var canvas = document.getElementById("canvas")
        var ctx = canvas.getContext("2d")
        
        var maxW
        var maxH
        
        var input = document.getElementById('input')
        input.addEventListener('change', handleFiles)
        
        function handleFiles(e) {
          var img = new Image
          img.onload = function() {
            var iw = img.width
            console.log(iw)
            var ih = img.height
            console.log(ih)

            if (iw > ih) {
              maxW = 680
              maxH = 500
            } 
            if (iw < ih) {
              maxW = 500
              maxH = 680
            } 
            if (iw == ih) {
              maxW = 500
              maxH = 500
            }  
            
            var scale = Math.min((maxW/iw), (maxH/ih))
            var iwScaled = iw*scale
            var ihScaled = ih*scale
            canvas.width = iwScaled
            canvas.height = ihScaled
            ctx.drawImage(img, 0, 0, iwScaled, ihScaled)
            photoURL.value = canvas.toDataURL('image/jpeg', 0.9)
          }
          img.src = URL.createObjectURL(e.target.files[0])
        } 
      }
    }),
    h('canvas', {id: 'canvas', width: '0', height: '0'}),
    h('button', {
      onclick: function () {
        identifyDiv.appendChild(identifyButtons)
        newPhoto.parentNode.removeChild(newPhoto)
      }
    }, ['Cancel']),
    h('button', {
      onclick: function () {
        if (photoURL.value) {
          content = {
            type: 'image',
            imaged: src,
            image: photoURL.value
          }
          localforage.removeItem('image:' + src)
          publish(content, keys).then(post => {
            open(post).then(msg => {
              nameInput.value = ''
              scroller.insertBefore(render(msg, keys), scroller.childNodes[1])
            })
          })
          newPhoto.parentNode.removeChild(newPhoto)
          identifyDiv.appendChild(identifyButton)
        }
      }
    }, ['Publish'])
  ])




  var nameInput = h('input', {placeholder: 'New name'})

  var newName = h('div', [
    nameInput,
    h('button', {
      onclick: function () {
        if (nameInput.value) {
          content = {
            type: 'name',
            named: src,
            name: nameInput.value
          }
          localforage.removeItem('name:' + src)
          publish(content, keys).then(post => {
            open(post).then(msg => {
              nameInput.value = ''
              scroller.insertBefore(render(msg, keys), scroller.childNodes[1])
            })
          })
          newName.parentNode.removeChild(newName)
          identifyDiv.appendChild(identifyButton)
        }
      }
    }, ['Publish']),
    h('button', {
      onclick: function () {
        identifyDiv.appendChild(identifyButtons)
        newName.parentNode.removeChild(newName)
      }
    }, ['Cancel'])
  ])
 
  var identifyButtons = h('span')

  if (src[0] == '@') {
    identifyButtons.appendChild(h('button', {
      onclick: function () {
        identifyDiv.appendChild(newName)
        identifyButtons.parentNode.removeChild(identifyButtons)
      }
    }, ['New name']))
    identifyButtons.appendChild(h('button', {
      onclick: function () {
        identifyDiv.appendChild(newBackground)
        identifyButtons.parentNode.removeChild(identifyButtons)
      }
    }, ['New background']))
  }
  //}, ['Identify ' + src.substring(0, 10) + '... with a new name']),
  identifyButtons.appendChild(h('button', {
    onclick: function () {
      identifyDiv.appendChild(newPhoto)
      identifyButtons.parentNode.removeChild(identifyButtons)
    }
  }, ['New image']))
  //}, ['Identify ' + src.substring(0, 10) + '... with a new photo']),
  identifyButtons.appendChild(h('button', {
    onclick: function () {
      identifyDiv.appendChild(identifyButton)
      identifyButtons.parentNode.removeChild(identifyButtons)
    }
  }, ['Cancel']))

  if (src[0] == '@') {
    var identifyButton = h('button', {
      onclick: function () {
        profile.appendChild(identifyDiv) 
        profile.appendChild(identifyButtons)
        identifyButton.parentNode.removeChild(identifyButton)
      }
    }, ['Identify ' + src.substring(0, 10) + '...'])
  } else {
    var identifyButton = h('button', {
      onclick: function () {
        profile.appendChild(identifyDiv) 
        profile.appendChild(identifyButtons)
        identifyButton.parentNode.removeChild(identifyButton)
      }
    }, ['Add to ' + src.substring(0, 10) + '...'])
  }
  return identifyButton
}