2020-01-01 13:55:28 +05:30
|
|
|
/* eslint-disable no-param-reassign, consistent-return */
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
import AccessorUtilities from './lib/utils/accessor';
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
export default class Autosave {
|
2020-05-24 23:13:21 +05:30
|
|
|
constructor(field, key, fallbackKey, lockVersion) {
|
2017-08-17 22:00:37 +05:30
|
|
|
this.field = field;
|
2022-10-11 01:57:18 +05:30
|
|
|
this.type = this.field.prop('type');
|
2021-11-11 11:23:49 +05:30
|
|
|
this.isLocalStorageAvailable = AccessorUtilities.canUseLocalStorage();
|
2017-08-17 22:00:37 +05:30
|
|
|
if (key.join != null) {
|
2018-03-17 18:26:18 +05:30
|
|
|
key = key.join('/');
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
2019-12-21 20:55:43 +05:30
|
|
|
this.key = `autosave/${key}`;
|
2020-01-01 13:55:28 +05:30
|
|
|
this.fallbackKey = fallbackKey;
|
2020-05-24 23:13:21 +05:30
|
|
|
this.lockVersionKey = `${this.key}/lockVersion`;
|
|
|
|
this.lockVersion = lockVersion;
|
2018-03-17 18:26:18 +05:30
|
|
|
this.field.data('autosave', this);
|
2017-08-17 22:00:37 +05:30
|
|
|
this.restore();
|
2018-03-17 18:26:18 +05:30
|
|
|
this.field.on('input', () => this.save());
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
restore() {
|
2017-08-17 22:00:37 +05:30
|
|
|
if (!this.isLocalStorageAvailable) return;
|
2018-03-27 19:54:05 +05:30
|
|
|
if (!this.field.length) return;
|
|
|
|
const text = window.localStorage.getItem(this.key);
|
2020-01-01 13:55:28 +05:30
|
|
|
const fallbackText = window.localStorage.getItem(this.fallbackKey);
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
if (this.type === 'checkbox') {
|
|
|
|
this.field.prop('checked', text || fallbackText);
|
|
|
|
} else if (text) {
|
2017-08-17 22:00:37 +05:30
|
|
|
this.field.val(text);
|
2020-01-01 13:55:28 +05:30
|
|
|
} else if (fallbackText) {
|
|
|
|
this.field.val(fallbackText);
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
2018-03-27 19:54:05 +05:30
|
|
|
|
|
|
|
this.field.trigger('input');
|
|
|
|
// 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);
|
2018-11-08 19:23:39 +05:30
|
|
|
if (field) {
|
|
|
|
field.dispatchEvent(event);
|
|
|
|
}
|
2018-03-17 18:26:18 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
getSavedLockVersion() {
|
|
|
|
if (!this.isLocalStorageAvailable) return;
|
|
|
|
return window.localStorage.getItem(this.lockVersionKey);
|
|
|
|
}
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
save() {
|
2018-03-27 19:54:05 +05:30
|
|
|
if (!this.field.length) return;
|
2022-10-11 01:57:18 +05:30
|
|
|
const value = this.type === 'checkbox' ? this.field.is(':checked') : this.field.val();
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
if (this.isLocalStorageAvailable && value) {
|
2020-01-01 13:55:28 +05:30
|
|
|
if (this.fallbackKey) {
|
2022-10-11 01:57:18 +05:30
|
|
|
window.localStorage.setItem(this.fallbackKey, value);
|
2020-01-01 13:55:28 +05:30
|
|
|
}
|
2020-05-24 23:13:21 +05:30
|
|
|
if (this.lockVersion !== undefined) {
|
|
|
|
window.localStorage.setItem(this.lockVersionKey, this.lockVersion);
|
|
|
|
}
|
2022-10-11 01:57:18 +05:30
|
|
|
return window.localStorage.setItem(this.key, value);
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return this.reset();
|
2018-03-17 18:26:18 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
reset() {
|
2017-08-17 22:00:37 +05:30
|
|
|
if (!this.isLocalStorageAvailable) return;
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
window.localStorage.removeItem(this.lockVersionKey);
|
2020-01-01 13:55:28 +05:30
|
|
|
window.localStorage.removeItem(this.fallbackKey);
|
2017-08-17 22:00:37 +05:30
|
|
|
return window.localStorage.removeItem(this.key);
|
2018-03-17 18:26:18 +05:30
|
|
|
}
|
2018-11-18 11:00:15 +05:30
|
|
|
|
|
|
|
dispose() {
|
2021-02-22 17:27:13 +05:30
|
|
|
// eslint-disable-next-line @gitlab/no-global-event-off
|
2018-11-18 11:00:15 +05:30
|
|
|
this.field.off('input');
|
|
|
|
}
|
2018-03-17 18:26:18 +05:30
|
|
|
}
|