summaryrefslogtreecommitdiff
path: root/public/assets/js/lib/views/mail/compose.js
blob: 3d7501671315a73e84aa94ea4c5405139d38949b (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
var ComposeView = FormView.extend({
  
  el: "#compose",
  
  events: {
  },
  
  action: "/api/mail/send",
  
  initialize: function(){
    this.__super__.initialize.call(this)
    this.template = this.$(".template").html()
  },
  
  load: function(username){
    var id = parseInt(username)
    if (! isNaN(id)) {
      $.get('/api/message/' + id, function(data){
        var message = data.message
        var date = verbose_date(message.date)
        var tmpl = $("#reply-template").html()
          .replace(/{{username}}/, message.sender)
          .replace(/{{date}}/, date[0])
          .replace(/{{time}}/, date[1])
          .replace(/ /g, " ")
        // console.log(tmpl)
        this.$("[name=username]").val(message.sender)
        this.$("[name=subject]").val(message.subject)
        this.$("[name=body]").val(tmpl + message.body).focus()
        setCaretToPos(this.$("[name=body]").get(0), 0, 0)
        $("body").removeClass('loading')
      }.bind(this))
      return
    }
    this.$("[name=username]").val(username)
    if (!username) {
      this.$("[name=username]").focus()
    }
    else {
      this.$("[name=body]").focus()
    }
    $("body").removeClass('loading')
  },
  
  validate: function(){
    var errors = []
    var username = $("[name=username]").val()
    var body = $("[name=body]").val()
    if (! username || ! username.length) {
      errors.push("Please enter who this message is going to.")
    }
    if (! body || ! body.length) {
      errors.push("Please enter your body.")
    }
    return errors.length ? errors : null
  },

  success: function(){
    window.location.href = "/mail/outbox"
  }
})