summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/util/minotaur.js
blob: d165ccc01b1f3b00e3a1097ee4231948daa4b398 (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
(function(){

  var Monitor = function () {
    var base = this
    base.$el = $("#minotaur")
    base.timeout = null
    base.delay = 2500
    base.objects = {}
    
    base.init = function () {
      base.$el.removeClass()
      base.$el.click(base.save)
    }

    base.watch = function (object) {
      base.objects[object.type] = base.objects[object.type] || {}
      base.objects[object.type][object._id] = object
      base.clear()
      base.timeout = setTimeout(base.save, base.delay)
    }
    
    base.unwatch = function (object) {
      if (base.objects[object.type] && base.objects[object.type][object._id]) {
        delete base.objects[object.type][object._id]
      }
    }
    
    base.clear = function () {
      if (base.timeout) clearTimeout(base.timeout)
      base.timeout = false
    }
    
    base.save = function () {
      var saving = false
      base.clear()
      
      for (var type in base.objects) {
        for (var id in base.objects[type]) {
          var obj = base.objects[type][id]
          if (obj) {
            obj.save(null, function(){ base.hide() }, function(){})
          }
          delete base.objects[type][id]
          saving = true
        }
      }

      saving ? base.show() : base.hide()
    }
    
    base.show = function () {
      base.$el.removeClass().addClass('saving')
    }
    
    base.hide = function () {
      setTimeout(function(){
        base.$el.removeClass()
      }, 500)
    }
    
    base.init();
  }
  
  window.Minotaur = new Monitor ();

})()