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 = sanitize(res.meta.query) var terms = res.meta.terms console.log(res) $("[name=query]").val(query) this.$(".query").html(query) this.$(".total").html(parseInt(res.meta.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.start + res.meta.limit > res.meta.count) 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(sanitize(result.file.filename), terms) + '' : '' var t = this.template .replace(/{{thread_id}}/g, sanitize("" + result.thread.id)) .replace(/{{meta}}/, metadata(result.thread)) .replace(/{{image}}/, image_path) .replace(/{{title}}/, bold_terms(sanitize(result.thread.title), terms)) .replace(/{{comment}}/, result.comment ? bold_terms(sanitize(result.comment.comment), terms) : '') .replace(/{{file}}/, file_tag) this.$("#results").append(t) }) $("body").removeClass('loading') }, }) function bold_terms (s, terms) { s = sanitize(s) terms.forEach( (term) => { s = s.replace(new RegExp(term, "ig"), "" + term + "") }) return s } function querystring(opt){ var s = Object.keys(opt).map((key) => { return encodeURIComponent(key) + "=" + encodeURIComponent(opt[key]) }).join("&") }