summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/util/minotaur.js
diff options
context:
space:
mode:
Diffstat (limited to 'public/assets/javascripts/rectangles/util/minotaur.js')
-rw-r--r--public/assets/javascripts/rectangles/util/minotaur.js58
1 files changed, 58 insertions, 0 deletions
diff --git a/public/assets/javascripts/rectangles/util/minotaur.js b/public/assets/javascripts/rectangles/util/minotaur.js
new file mode 100644
index 0000000..b4c81f0
--- /dev/null
+++ b/public/assets/javascripts/rectangles/util/minotaur.js
@@ -0,0 +1,58 @@
+$(function(){
+
+ var Monitor = function () {
+ var base = this;
+ base.$el = $("#save");
+ base.timeout = null;
+ base.delay = 500;
+ base.objects = {};
+
+ base.init = function () {
+ base.$el.addClass('saved').html('Save');
+ 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.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]) {
+ if (base.timeout)
+ return;
+ var obj = base.objects[type][id];
+ if (obj) obj.save(function(){
+ base.$el.removeClass('unsaved saving').addClass('saved').html('Saved');
+ saving = true;
+ });
+ base.objects[type][id] = false;
+ }
+ }
+
+ if (saving) {
+ base.$el.removeClass('unsaved saved').addClass('saving').html('Saving');
+ }
+ else {
+ base.$el.removeClass('unsaved saving').addClass('saved').html('Saved');
+ }
+
+ base.objects = {};
+ };
+
+ base.init();
+ };
+
+ window.Minotaur = new Monitor ();
+});