summaryrefslogtreecommitdiff
path: root/public/assets/js/lib/views/search/results.js
blob: 8c793f1a907d03181f741eff121370e86fd5351f (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 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 ? '<a href="' + make_link(result.file) + '">' + bold_terms(result.file.filename, terms) + '</a>' : ''
      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')
  },

})