summaryrefslogtreecommitdiff
path: root/old/server/app/static/js/app.js
blob: 454d5c37003aef58dfa33aa1b1f27f9ae5ba450f (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
var app = (function(){

  var app = {}

  app.init = function(){
    upload.init()
    app.bind()
    app.build()
    app.resize()
    app.route()
  }
  app.bind = function(){
    $(window).on('resize', app.resize)
    $(".about_button").on('click', app.about_show)
    $(".privacy_button").on('click', app.privacy_show)
    $(".modal").on('click', app.modal_hide)
    $(".modal .btn").on('click', app.modal_hide)
    $(".modal .inner").on('click', preventDefault)
    $("#destroy_data").on('click', app.destroyData)
    $("#show_all_results").on('click', app.showAllResults)
  }
  app.build = function(){
    var items = JSON.parse(decodeEntities($("#dropdown_options").html()))
    var $dropdown = $("#dropdown")
    var options = Object.keys(items).sort().map(key => {
      var item = items[key]
      var option = document.createElement('option')
      option.value = item.name
      option.innerHTML = item.title
      if (item.selected) option.selected = true
      $dropdown.append(option)
    })
    var loader = new Image ()
    loader.src = '/static/img/loader.gif'
  }
  app.resize = function(){
    var $el = $('#photo_area')
    var w = $el.width()
    $el.height($el.width())
  }
  app.route = function(){
    const path = window.location.pathname.split('/')
    path.shift()
    switch (path[0]) {
      case 'd':
        app.processingComplete(path[1], true) // public
        break
      case 'p':
        app.processingComplete(path[1], false) // private
        break
      case 'about':
        app.about_show()
        break
      case 'privacy':
        app.privacy_show()
        break
      default:
        // load index, default state
        break
    }
  }

  /* upload UI changes */

  app.didPickPhoto = function(){
    $('#upload_controls').fadeIn()
    $('#user_photo_canvas').show()
    $('#take_photo_btn').hide()
  }
  app.didClickUpload = function(){
    $('#upload_controls').slideUp('fast')
    $('#user_photo_canvas').hide()
    $('#preloader_anim').fadeIn('fast')
    $('#progress').fadeIn()
  }
  app.uploadDidComplete = function(){
    $('#preloader_anim').hide()
    $('#progress').hide()
  }
  app.uploadDidComplete = function(){
    // $('#preloader_anim').hide()
    // $('#progress').hide()
  }
  app.updateProgress = function(message, percentage){
    message = message || "Processing..."
    percentage = percentage || 0
    $("#progress").html(message)
  }
  app.processingComplete = function(uuid, is_public){
    $('#preloader_anim').hide()
    $('#progress').hide()
    // 
    $("header h2").html("Your dull result")
    $(".upload_view").hide()
    $(".results_view").show()
    var endpoint = is_public ? 'json' : 'json_private'
    $.getJSON('/static/media/' + endpoint + '/' + uuid + '.json', function(data){
      console.log(data)
      var template = $("#result_template").html()
      var final_result = new Image
      final_result.src = data.files[data.files.length-1].fn
      $(".final_result").empty()
      $(".all_results").empty()
      $(".final_result").append(final_result)
      data.files.forEach(function(file){
        var t = template.replace(/{img}/, file.fn).replace(/{title}/, file.title)
        $(".all_results").append(t)
      })
      $(".result_view").show()
      $(".permalink").attr('href', window.location.href)
    }).fail(function(){
      console.log('error fetching json')
      window.location.href = '/'
    })
  }
  var detailed = false
  app.showAllResults = function(){
    if (!detailed) {
      detailed = true
      $(this).html('Hide')
      $(".all_results").fadeIn('fast')
    } else {
      detailed = false
      $(this).html('Detailed Analysis')
      $(".all_results").slideUp('fast')
    }
  }
  app.destroyData = function(){
    var uuid = window.location.pathname.split('/')[2]
    var confirmed = confirm("Do you really want to delete your dull dream?")
    if (confirmed) {
      $.get( [window.location.pathname, 'destroy'].join('/').replace('//', '/') ).always(function(){
        alert('Dull dream deleted!')
        window.location.href = '/'
      })
    }
  }

  /* modals */

  app.about_show = function(e){
    e.preventDefault()
    $(".about_view").addClass('visible')
  }
  app.privacy_show = function(e){
    e.preventDefault()
    $(".privacy_view").addClass('visible')
  }
  app.modal_hide = function(e){
    e.preventDefault()
    e.stopPropagation()
    $(".modal").removeClass('visible')
  }

  document.addEventListener('DOMContentLoaded', app.init)

  return app
})()