BigW Consortium Gitlab

autosave.js 1.44 KB
Newer Older
1
/* eslint-disable */
Fatih Acet committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
(function() {
  this.Autosave = (function() {
    function Autosave(field, key) {
      this.field = field;
      if (key.join != null) {
        key = key.join("/");
      }
      this.key = "autosave/" + key;
      this.field.data("autosave", this);
      this.restore();
      this.field.on("input", (function(_this) {
        return function() {
          return _this.save();
        };
      })(this));
    }

    Autosave.prototype.restore = function() {
20
      var e, text;
Fatih Acet committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
      if (window.localStorage == null) {
        return;
      }
      try {
        text = window.localStorage.getItem(this.key);
      } catch (error) {
        e = error;
        return;
      }
      if ((text != null ? text.length : void 0) > 0) {
        this.field.val(text);
      }
      return this.field.trigger("input");
    };

    Autosave.prototype.save = function() {
      var text;
      if (window.localStorage == null) {
        return;
      }
      text = this.field.val();
      if ((text != null ? text.length : void 0) > 0) {
        try {
          return window.localStorage.setItem(this.key, text);
45
        } catch (error) {}
Fatih Acet committed
46 47 48 49 50 51 52 53 54 55 56
      } else {
        return this.reset();
      }
    };

    Autosave.prototype.reset = function() {
      if (window.localStorage == null) {
        return;
      }
      try {
        return window.localStorage.removeItem(this.key);
57
      } catch (error) {}
Fatih Acet committed
58 59 60 61 62 63 64
    };

    return Autosave;

  })();

}).call(this);