BigW Consortium Gitlab

autosave.js 1.47 KB
Newer Older
1
/* eslint-disable func-names, space-before-function-paren, wrap-iife, no-param-reassign, quotes, prefer-template, no-var, one-var, no-unused-vars, one-var-declaration-per-line, no-void, consistent-return, no-empty, max-len */
2
import AccessorUtilities from './lib/utils/accessor';
Fatih Acet committed
3

4 5 6
window.Autosave = (function() {
  function Autosave(field, key) {
    this.field = field;
7 8
    this.isLocalStorageAvailable = AccessorUtilities.isLocalStorageAccessSafe();

9 10 11 12 13 14 15 16 17 18 19 20
    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));
  }
Fatih Acet committed
21

22
  Autosave.prototype.restore = function() {
23 24 25 26 27 28
    var text;

    if (!this.isLocalStorageAvailable) return;

    text = window.localStorage.getItem(this.key);

29 30 31 32 33
    if ((text != null ? text.length : void 0) > 0) {
      this.field.val(text);
    }
    return this.field.trigger("input");
  };
Fatih Acet committed
34

35 36 37
  Autosave.prototype.save = function() {
    var text;
    text = this.field.val();
38 39 40

    if (this.isLocalStorageAvailable && (text != null ? text.length : void 0) > 0) {
      return window.localStorage.setItem(this.key, text);
41
    }
42 43

    return this.reset();
44 45 46
  };

  Autosave.prototype.reset = function() {
47 48 49
    if (!this.isLocalStorageAvailable) return;

    return window.localStorage.removeItem(this.key);
50
  };
Fatih Acet committed
51

52 53
  return Autosave;
})();
54 55

export default window.Autosave;