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") setInterval(this.fetchAscii.bind(this), 5000) 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 this.display(asciiData) // 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") }, })