summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/autosave.js
blob: 4d2d4db7c0e33ed466550fbef4d0420f4335b613 (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
/* 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 */
import AccessorUtilities from './lib/utils/accessor';

window.Autosave = (function() {
  function Autosave(field, key, resource) {
    this.field = field;
    this.isLocalStorageAvailable = AccessorUtilities.isLocalStorageAccessSafe();
    this.resource = resource;
    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() {
    var text;

    if (!this.isLocalStorageAvailable) return;

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

    if ((text != null ? text.length : void 0) > 0) {
      this.field.val(text);
    }
    if (!this.resource && this.resource !== 'issue') {
      this.field.trigger('input');
    } else {
      // v-model does not update with jQuery trigger
      // https://github.com/vuejs/vue/issues/2804#issuecomment-216968137
      const event = new Event('change', { bubbles: true, cancelable: false });
      const field = this.field.get(0);
      if (field) {
        field.dispatchEvent(event);
      }
    }
  };

  Autosave.prototype.save = function() {
    var text;
    text = this.field.val();

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

    return this.reset();
  };

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

    return window.localStorage.removeItem(this.key);
  };

  return Autosave;
})();

export default window.Autosave;