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", this.keydown.bind(this)) $(document).ajaxError(function(){ this.fetching = false }.bind(this)) this.latest() }, keydown: function(e){ 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.fetching = true this.onLatest = true $.get("/get/latest", this.display.bind(this)) }, prev: function(){ if (this.fetching) return if (this.id - 1 < 1) { return } this.fetching = true this.onLatest = false $.get("/get/" + (this.id-1), this.display.bind(this)) }, next: function(){ if (this.fetching) return if (this.onLatest || this.id < 1) { return } this.fetching = true this.onLatest = false $.get("/get/" + (this.id+1), this.display.bind(this)) }, random: function(){ if (this.fetching) return this.fetching = true this.onLatest = false $.get("/get/random", this.display.bind(this)) }, display: function(data){ this.fetching = false if (! data.id) { this.onLatest = true return } this.id = data.id this.$pip.attr("src", data.url) this.$image.css("background-image", "url(" + data.url + ")") history.pushState(data, document.title, "/p/" + data.id) }, })