summaryrefslogtreecommitdiff
path: root/public/assets/js/lib/views/profile/profile.js
blob: 077db4eb104f5d464e9b64918a292034030c836d (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
var ProfileView = View.extend({

  el: "#profile",
  
  events: {
  },
  
  action: "/api/user/",
  
  initialize: function(opt){
    this.template = this.$(".template").html()
  },
  
  load: function(username){
    $.get(this.action + username, this.populate.bind(this))
  },
  
  populate: function(user){
    $("body").removeClass('loading')
    var $table = this.$("table")
    var username = sanitize(user.username)
    var is_own_profile = (username === auth.user.username)
    if (is_own_profile) {
      $(".edit_profile a").attr("href", "/profile/" + username + "/edit")
    } else {
      $(".edit_profile").hide()
    }
    this.$("img").attr("src", profile_image(username))
    var fields = [
      ["username", "Username"],
      ["realname", "Real Name"],
      ["location", "Location"],
      ["phone", "Phone"],
    ].map(pair => {
      var key = pair[0], label = pair[1]
      if (! user[key]) return;
      return [key, sanitize(user[key])]
    })
    
    if (user.email) {
      fields.push([
        'Email',
        '<a href="mailto:' + sanitize(user.email) + '">' + sanitize(user.email) + '</a>'
      ])
    }
    if (user.twitter) {
      if (user.twitter.match(/^http/)) {
        var partz = user.twitter.split('/')
        if (partz.length > 2) {
          var handle = sanitize(partz[3])
          fields.push([
            'Twitter',
            '<a href="https://twitter.com/' + handle + '">@' + handle + '</a>'
          ])
        }
      } else {
        var handle = sanitize(user.twitter)
        fields.push([
          'Twitter',
          '<a href="https://twitter.com/' + handle + '">@' + handle + '</a>'
        ])
      }
    }
    if (user.website) {
      var websit = sanitize(user.website)
      fields.push([
        'Website',
        '<a href="https://twitter.com/' + website + '">' + website + '</a>'
      ])
    }
    
    fields = [
      ["firstseen", "First Seen"],
      ["lastseen", "Last Seen"],
    ].split(" ").map((key) => {
      var key = pair[0], label = pair[1]
      if (! user[key]) return;
      var date = verbose_date(user[key])
      return [key, date[0] + ' <small>' + date[1] + '</small>']
    }).concat(fields)

    var t = this.template.replace(/{{key}}/, "&nbsp;")
                         .replace(/{{value}}/, '<a href="/mail/compose/' + username + '">send ' + username + ' a message</a>')
    $table.append(t)
  },

})