2023-03-17 16:20:25 +05:30
|
|
|
import { parseBoolean } from '~/lib/utils/common_utils';
|
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;
|
2023-03-17 16:20:25 +05:30
|
|
|
this.type = this.field.getAttribute('type');
|
2021-11-11 11:23:49 +05:30
|
|
|
this.isLocalStorageAvailable = AccessorUtilities.canUseLocalStorage();
|
2023-03-17 16:20:25 +05:30
|
|
|
this.key = Array.isArray(key) ? `autosave/${key.join('/')}` : `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;
|
2017-08-17 22:00:37 +05:30
|
|
|
this.restore();
|
2023-03-17 16:20:25 +05:30
|
|
|
this.saveAction = this.save.bind(this);
|
|
|
|
// used by app/assets/javascripts/deprecated_notes.js
|
|
|
|
this.field.$autosave = this;
|
|
|
|
this.field.addEventListener('input', this.saveAction);
|
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
|
|
|
const text = window.localStorage.getItem(this.key);
|
2020-01-01 13:55:28 +05:30
|
|
|
const fallbackText = window.localStorage.getItem(this.fallbackKey);
|
2023-03-17 16:20:25 +05:30
|
|
|
const newValue = text || fallbackText;
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
if (newValue == null) return;
|
|
|
|
|
|
|
|
let originalValue = this.field.value;
|
2022-10-11 01:57:18 +05:30
|
|
|
if (this.type === 'checkbox') {
|
2023-03-17 16:20:25 +05:30
|
|
|
originalValue = this.field.checked;
|
|
|
|
this.field.checked = parseBoolean(newValue);
|
|
|
|
} else {
|
|
|
|
this.field.value = newValue;
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
if (originalValue === newValue) return;
|
|
|
|
this.triggerInputEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
triggerInputEvents() {
|
|
|
|
// trigger events so @input, @change and v-model trigger in Vue components
|
|
|
|
const inputEvent = new Event('input', { bubbles: true, cancelable: false });
|
|
|
|
const changeEvent = new Event('change', { bubbles: true, cancelable: false });
|
|
|
|
this.field.dispatchEvent(inputEvent);
|
|
|
|
this.field.dispatchEvent(changeEvent);
|
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() {
|
2023-03-17 16:20:25 +05:30
|
|
|
if (!this.isLocalStorageAvailable) return undefined;
|
2020-05-24 23:13:21 +05:30
|
|
|
return window.localStorage.getItem(this.lockVersionKey);
|
|
|
|
}
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
save() {
|
2023-03-17 16:20:25 +05:30
|
|
|
const value = this.type === 'checkbox' ? this.field.checked : this.field.value;
|
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() {
|
2023-03-17 16:20:25 +05:30
|
|
|
if (!this.isLocalStorageAvailable) return undefined;
|
2017-08-17 22:00:37 +05:30
|
|
|
|
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() {
|
2023-03-17 16:20:25 +05:30
|
|
|
delete this.field.$autosave;
|
|
|
|
this.field.removeEventListener('input', this.saveAction);
|
2018-11-18 11:00:15 +05:30
|
|
|
}
|
2018-03-17 18:26:18 +05:30
|
|
|
}
|