var SearchResults = View.extend({ el: "#search", template: $("#search .template").html(), events: { }, action: "/api/search", initialize: function(opt){ }, load: function(){ var query = window.location.search.substr(1) if (! query || ! query.length) { $("#search").hide() return } $.get(this.action, query, this.populate.bind(this)) }, populate: function(res){ var query = sanitizeHTML(res.meta.query) var terms = res.meta.terms console.log(res) $("title").html('bucky search "' + query + '"') $("[name=query]").val(res.meta.query) this.$(".query").html(query) var total = parseInt(res.meta.total) this.$(".total").html(total + " result" + courtesy_s(total)) var next_page = { query: res.meta.terms.join("+"), start: res.meta.start + res.meta.limit, limit: res.meta.limit, } this.$(".next_page").toggle(res.meta.next < res.meta.total) this.$(".next_page").attr("href", querystring(next_page)) res.results.forEach((result) => { if (! result.thread) { return } var image if (result.file && is_image(result.file.filename)) { image = result.file } else if (result.thread.flagged && is_image(result.thread.flagged.filename)) { image = result.thread.flagged } var image_path = image ? '/data/' + result.thread.id + '/' + sanitize(image.filename) : '' var file_tag = result.file ? '' + bold_terms(result.file.filename, terms) + '' : '' var t = this.template .replace(/{{thread_id}}/g, sanitizeHTML("" + result.thread.id)) .replace(/{{meta}}/, metadata(result.thread)) .replace(/{{image}}/, image_path) .replace(/{{title}}/, bold_terms(result.thread.title, terms)) .replace(/{{comment}}/, result.comment ? bold_terms(result.comment.comment, terms) : '') .replace(/{{file}}/, file_tag) .replace(/{{strength}}/, result.strength) this.$("#results").append(t) }) $("body").removeClass('loading').addClass('search') }, })