summaryrefslogtreecommitdiff
path: root/public/assets/js/nav.js
blob: 247ed0552203fc78b925dd5cc32c5705eae8ff21 (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
var NavView = View.extend({

  el: "body",
  events: {
    "click .prev": "prev",
    "click .next": "next",
    "click .link": "link",
    "click .refresh": "refresh",
    "click .random": "random",
  },

  initialize: function(){
    this.id = -1
    this.$pip = this.$("#pip")
    this.$pip_img = this.$("#pip img")
    this.$image = this.$("#image")

    $(window).on("keydown", this.keydown.bind(this))
    $(document).ajaxError(function(){
      this.fetching = false
    }.bind(this))

    window.onpopstate = function(event) {
      this.display(event.state)
    }.bind(this)

    if (window.location.pathname.indexOf("/p/") !== -1) {
      var id = window.location.pathname.split("/")[2]
      if (parseInt(id) || id === 'random' || id === 'latest') {
        return this.fetch(id)
      }
    }
    if (window.location.pathname.indexOf("/panke/") !== -1) {
      document.body.classList.add("panke")
      return this.fetchAscii()
    }
    return this.latest()
  },

  keydown: function(e){
    if (e.shiftKey || e.metaKey || e.ctrlKey || e.altKey) { return }
    switch (e.keyCode) {
      case 37: // left
        this.prev()
        break
      case 39: // right
        this.next()
        break
      case 38: // up
        this.random()
        break
      case 40: // down
        this.latest()
        break
    }
  },

  latest: function(){
    if (this.fetching) return
    this.fetch("latest")
    this.onLatest = true
  },

  prev: function(){
    if (this.fetching) return
    if (this.id - 1 < 1) { return }
    this.fetch(this.id-1)
  },

  next: function(){
    if (this.fetching) return
    if (this.onLatest || this.id < 1) { return }
    this.fetch(this.id+1)
  },

  random: function(){
    if (this.fetching) return
    this.fetch("random")
  },

  fetch: function(id){
    this.fetching = true
    this.onLatest = false
    console.log('fetch', id)
    $.get("/p/get/" + id, function(data){
      console.log(data)
      this.fetching = false
      this.display(data)
      history.pushState(data, document.title, "/p/" + data.id)
    }.bind(this))
  },

  fetchAscii: function(){
    this.fetching = true
    this.onLatest = false
    console.log('fetch ascii')
    $.get("/p/get/randomascii", function(data){
      // console.log(data)
      var asciiData = {
        id: "ascii",
        url: "https://s3.amazonaws.com/i.asdf.us/im/" + data.dir + "/" + data.newfile
      }
      this.fetching = false
      var img = new Image()
      var loaded = false
      img.onload = function(){
        if (loaded) return
        loaded = true
        this.display(asciiData)
        setTimeout(this.fetchAscii.bind(this), 15000)
      }.bind(this)
      img.onerror = function(){
        this.fetchAscii()
      }.bind(this)
      img.src = asciiData.url
      if (img.complete && !loaded) img.onload()
      // history.pushState(data, document.title, "/p/" + data.id)
    }.bind(this))
  },

  display: function(data){
    if (! data || ! data.id) {
      this.onLatest = true
      return
    }
    this.data = data
    this.id = data.id
    // if (is_desktop) {
    //   this.$pip.attr("href", data.url)
    //   this.$pip_img.attr("src", "")
    //   setTimeout(function(){ this.$pip_img.attr("src", data.url) }.bind(this))
    // }
    this.$image.css("background-image", "url(" + data.url + ")")
  },

  link: function(){
    window.open(this.data.url, "_blank")
  },

})