2017-01-10 18:02:20 -05:00
|
|
|
/* 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 */
|
2017-05-05 13:59:41 -04:00
|
|
|
import AccessorUtilities from './lib/utils/accessor';
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
window.Autosave = (function() {
|
2017-08-13 07:02:31 -04:00
|
|
|
function Autosave(field, key, resource) {
|
2017-03-11 02:30:44 -05:00
|
|
|
this.field = field;
|
2017-05-05 13:59:41 -04:00
|
|
|
this.isLocalStorageAvailable = AccessorUtilities.isLocalStorageAccessSafe();
|
2017-08-13 07:02:31 -04:00
|
|
|
this.resource = resource;
|
2017-03-11 02:30:44 -05:00
|
|
|
if (key.join != null) {
|
2017-08-13 07:02:31 -04:00
|
|
|
key = key.join('/');
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
2017-08-13 07:02:31 -04:00
|
|
|
this.key = 'autosave/' + key;
|
|
|
|
this.field.data('autosave', this);
|
2017-03-11 02:30:44 -05:00
|
|
|
this.restore();
|
2017-08-13 07:02:31 -04:00
|
|
|
this.field.on('input', (function(_this) {
|
2017-03-11 02:30:44 -05:00
|
|
|
return function() {
|
|
|
|
return _this.save();
|
|
|
|
};
|
|
|
|
})(this));
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
Autosave.prototype.restore = function() {
|
2017-05-05 13:59:41 -04:00
|
|
|
var text;
|
|
|
|
|
|
|
|
if (!this.isLocalStorageAvailable) return;
|
|
|
|
|
|
|
|
text = window.localStorage.getItem(this.key);
|
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
if ((text != null ? text.length : void 0) > 0) {
|
|
|
|
this.field.val(text);
|
|
|
|
}
|
2017-08-13 07:02:31 -04:00
|
|
|
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 });
|
2017-08-13 12:18:45 -04:00
|
|
|
const field = this.field.get(0);
|
|
|
|
if (field) {
|
|
|
|
field.dispatchEvent(event);
|
|
|
|
}
|
2017-08-13 07:02:31 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
};
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
Autosave.prototype.save = function() {
|
|
|
|
var text;
|
|
|
|
text = this.field.val();
|
2017-05-05 13:59:41 -04:00
|
|
|
|
|
|
|
if (this.isLocalStorageAvailable && (text != null ? text.length : void 0) > 0) {
|
|
|
|
return window.localStorage.setItem(this.key, text);
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
2017-05-05 13:59:41 -04:00
|
|
|
|
|
|
|
return this.reset();
|
2017-03-11 02:30:44 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
Autosave.prototype.reset = function() {
|
2017-05-05 13:59:41 -04:00
|
|
|
if (!this.isLocalStorageAvailable) return;
|
|
|
|
|
|
|
|
return window.localStorage.removeItem(this.key);
|
2017-03-11 02:30:44 -05:00
|
|
|
};
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
return Autosave;
|
|
|
|
})();
|
2017-05-05 13:59:41 -04:00
|
|
|
|
|
|
|
export default window.Autosave;
|