summaryrefslogtreecommitdiff
path: root/public/assets/js/lib/views/details/files.js
blob: b19cf3cd6117c98c3d438254a91aceddd7438778 (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
var FilesView = FormView.extend({
  
  el: "#files",
  
  events: {
  },
  
  initialize: function(){
    this.__super__.initialize.call(this)
    this.template = this.$(".template").html()
    this.templateTotal = this.$(".templateTotal").html()
  },
  
  load: function(files){
    if (! files.length) {
      this.$el.hide()
    }
    var total = 0
    files.forEach(function(file){
      this.appendFile(file)
      total += file.size
    }.bind(this))
    
    var size = hush_size(total)
    var t = this.templateTotal.replace(/{{size_class}}/g, size[0])
                              .replace(/{{size}}/g, size[1])
    this.$el.append(t)
  },
  
  parse: function(file){
    var size = hush_size(file.size)
    var datetime = verbose_date(file.date, true)
    var date_class = carbon_date(file.date)

    var t = this.template.replace(/{{username}}/g, file.username)
                         .replace(/{{link}}/g, file.filename)
                         .replace(/{{filename}}/g, file.filename)
                         .replace(/{{date_class}}/g, date_class)
                         .replace(/{{date}}/g, datetime[0])
                         .replace(/{{time}}/g, datetime[1])
                         .replace(/{{size_class}}/g, size[0])
                         .replace(/{{size}}/g, size[1])
    return t
  },
  
  prependFile: function(file){
    var $el = $( this.parse(file) )
    this.$el.prepend($el)
  },

  appendFile: function(file){
    var $el = $( this.parse(file) )
    this.$el.append($el)
  },
  
  success: function(){
    this.prependFile(file)
  }
})