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

  el: "body",
  events: {
    "click #prev": "prev",
    "click #next": "next",
    "click #refresh": "refresh",
    "click #random": "random",
  },
  
  initialize: function(){
    this.id = -1
    this.$pip = this.$("#pip img")
    this.$image = this.$("#luckyimage")
    
    $(window).on("keydown", app.keydown)
    this.latest()
  },
  
  keydown: function(e){
    switch (e.keyCode) {
      case 37: // left
        this.prev()
        break
      case 39: // right
        this.next()
        break
      case 38: // up
        this.latest()
        break
      case 40: // down
        this.random()
        break
    }
  },
  
  latest: function(){
    this.onLatest = true
    $.get("/get/latest", this.display.bind(this))
  },
  
  prev: function(){
    if (this.id - 1 < 1) { return }
    this.onLatest = false
    $.get("/get/" + (this.id-1), this.display.bind(this))
  },
  
  next: function(){
    if (this.onLatest || this.id < 1) { return }
    this.onLatest = false
    $.get("/get/" + (this.id+1), this.display.bind(this))
  },
  
  random: function(){
    this.onLatest = false
    $.get("/get/random", this.display.bind(this))
  },
  
  display: function(data){
    this.id = data.id
    this.$pip.attr("src", data.url)
    this.$image.css("background-image", "url(" + data.url + ")")
  },

})