summaryrefslogtreecommitdiff
path: root/public/assets/js/lib/views/mail/mailbox.js
blob: 199eeee77993ef2cf7e9b0f7b37d32bdbac4efbf (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
var MailboxView = View.extend({
  el: "#messages",
  
  events: {
  },
  
  action: "/api/mailbox/",
  
  initialize: function(){
    this.__super__.initialize.call(this)
    this.template = this.$(".template").html()
    this.boxlist = new BoxList ()
  },
  
  load: function(name){
    name = sanitize(name) || "inbox"
    $("h1").html(name)
    $.get(this.action + name, this.populate.bind(this))
  },
  
  populate: function(data){
    this.boxlist.load(data.boxes)
    data.messages.forEach(function(message){
      this.appendMessage(message, data.user)
    }.bind(this))
  },
  
  parse: function(message, user){
    var datetime = verbose_date(message.date)
    var size = hush_size(message.size)
    var id = message.id

    var is_sender = message.sender == user.username
    var t = this.template
                .replace(/{{id}}/g, message.id)
                .replace(/{{to}}/g, is_sender ? "to " : "")
                .replace(/{{username}}/g, is_sender ? message.recipient : message.sender)
                .replace(/{{subject}}/g, message.subject)
                .replace(/{{date}}/g, datetime[0])
                .replace(/{{time}}/g, datetime[1])
                .replace(/{{date_class}}/g, carbon_date(message.lastmodified) )
                .replace(/{{size}}/g, size[1] )
                .replace(/{{size_class}}/g, size[0] )
    return t
  },

  appendMessage: function(message, user){
    var $row = $( this.parse(message, user) )
    this.$el.append($row)
  },

})