summaryrefslogtreecommitdiff
path: root/public/assets/js/lib/views/index/threadbox.js
blob: 0c6a4bed0b1927b65d369ccf68ed3c6a9b21de4f (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
var ThreadBox = View.extend({
  el: "#threads",
  
  events: {
  },
  
  initialize: function(){
    this.__super__.initialize.call(this)
    this.template = this.$(".template").html()
    this.keywordTemplate = this.$(".keywordTemplate").html()
  },
  
  load: function(data){
    if (data.keyword) {
      this.appendKeyword(data.keyword)
      data.threads.forEach(this.appendThread.bind(this))
    }
    else if (this.options.latest) {
      data.threads.sort( (a,b) => {
        return b.lastmodified - a.lastmodified
      }).slice(0, 50).forEach(this.appendThread.bind(this))
    }
    else {
      var keywords = {}
      data.threads.forEach((thread) => {
        var keyword = thread.keyword || '__unsorted'
        keywords[keyword] = keywords[keyword] || []
        keywords[keyword].push(thread)
      })
      Object.keys(keywords).sort().forEach((keyword) => {
        this.appendKeyword({ keyword })
        keywords[keyword].forEach(this.appendThread.bind(this))
      })
    }
  },
  
  parse: function(thread){
    var views = hush_views(thread.viewed)
    var size = hush_size(thread.size)
    var comments = hush_null(thread.comment_count, "c")
    var files = hush_null(thread.file_count, "f")
    var dot = privacy_dot(thread.private)
    var datetime = verbose_date(thread.lastmodified)
    var age = get_age(thread.lastmodified)
    var id = thread.id + get_revision(thread)

    var t = this.template
                .replace(/{{id}}/g, id)
                .replace(/{{username}}/g, thread.username)
                .replace(/{{privacy_dot}}/g, dot)
                .replace(/{{title}}/g, thread.title)
                .replace(/{{date}}/g, datetime[0])
                .replace(/{{time}}/g, datetime[1])
                .replace(/{{date_class}}/g, carbon_date(thread.lastmodified) )
                .replace(/{{views}}/g, views[1])
                .replace(/{{comments}}/g, comments[1])
                .replace(/{{files}}/g, files[1])
                .replace(/{{size}}/g, size[1] )
                .replace(/{{views_class}}/g, views[0])
                .replace(/{{comments_class}}/g, comments[0])
                .replace(/{{files_class}}/g, files[0])
                .replace(/{{show_files}}/g, thread.file_count == 0 ? "hidden" : "")
                .replace(/{{size_class}}/g, size[0] )
                .replace(/{{color}}/g, thread.color || "ivory" )
    return t
  },
  
  parseKeyword: function(keyword){
    var t = this.keywordTemplate
                .replace(/{{keyword}}/g, keyword.keyword)
    return t
  },
  
  prependThread: function(thread){
    var $row = $( this.parse(thread) )
    this.$el.prepend($row)
  },

  appendThread: function(thread){
    var $row = $( this.parse(thread) )
    this.$el.append($row)
  },

  appendKeyword: function(keyword){
    var $row = $( this.parseKeyword(keyword) )
    this.$el.append($row)
  },
})