summaryrefslogtreecommitdiff
path: root/public/js/lib/views/room/settings.js
blob: e192503e681cace84f5c0c497757e511ec34a6dd (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
var SettingsView = View.extend({

  el: "#settings",

  events: {
    "click": "stopPropagation",
    "click .open": "open",
    "blur [name=bg]": "updateBackground",
    "submit form": "save",
  },
  
  initialize: function(socket){
    this.$bg = this.$("[name=bg]")
    socket.on("settings", this.update.bind(this))
  },
  
  open: function(){
    if (this.isOpen) {
      return this.close()
    }
    this.isOpen = true
    this.$el.addClass("active")
    this._close = this.close.bind(this)
    $(document.body).on("click", this._close)
  },
  
  close: function(){
    this.isOpen = false
    this.$el.addClass("active")
    $(document.body).off("click", this._close)
  },

  serialize: function(){
    var fd = {}
    
    this.$("input[name], select[name], textarea[name]").each( function(){
      if (this.type == "password") {
        if (this.value.length > 0) {
          fd[this.name] = SHA1.hex('lol$' + this.value + '$asdf')
        }
      }
      else {
        fd[this.name] = sanitize(this.value)
      }
    })
    
    return fd
  },

  save: function(e){
    if (this.saving) { return }
    this.saving = true
    e.preventDefault()
    var data = this.serialize()
    app.socket.emit("settings", data)
  },
  
  update: function(data){
    var base = this
    if (this.saving) {
      this.saving = false
      return
    }
    Object.keys(data).forEach(function(key){
      switch (key) {
        case 'bg':
          console.log(data.bg)
          base.$bg.val(data.bg.url)
          bg.change(data.bg)
          break
      }
    })
  },
  
  updateBackground: function(){
    if (this.saving) { return }
    var url = this.$bg.stripHTML()
    bg.change(url)
    app.socket.emit("settings", { bg: { url: url, tile: false } })
  }
})