-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement auto-recover functionality [B: 1403] (#483)
- Loading branch information
1 parent
29bbae0
commit e7ed08e
Showing
49 changed files
with
2,494 additions
and
284 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { clearSavedFormValues } from "./common"; | ||
|
||
export default function createCancelButton(el: HTMLElement | JQuery<HTMLElement>) { | ||
const $el = $(el); | ||
if ($el[0].tagName !== 'BUTTON') return; | ||
$el.data('cancel-button', "true"); | ||
$el.on('click', async () => { | ||
const href = $el.data('href'); | ||
await clearSavedFormValues($el.closest('form')); | ||
if (href) | ||
window.location.href = href; | ||
else | ||
window.history.back(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import "../../../testing/globals.definitions"; | ||
import {layoutId, recordId, table_key} from "./common"; | ||
|
||
describe("Common button tests",()=>{ | ||
it("should populate table_key",()=>{ | ||
expect(table_key()).toBe("linkspace-record-change-undefined-0"); // Undefined because $('body').data('layout-identifier') is not defined | ||
}); | ||
|
||
it("should have a layoutId", ()=>{ | ||
$('body').data('layout-identifier', 'layoutId'); | ||
expect(layoutId()).toBe('layoutId'); | ||
}); | ||
|
||
it("should have a recordId", ()=>{ | ||
expect(isNaN(parseInt(location.pathname.split('/').pop() ?? ""))).toBe(true); | ||
expect(recordId()).toBe(0); | ||
}); | ||
|
||
it("should populate table_key fully",()=>{ | ||
$('body').data('layout-identifier', 'layoutId'); | ||
expect(table_key()).toBe("linkspace-record-change-layoutId-0"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import gadsStorage from "util/gadsStorage"; | ||
|
||
/** | ||
* Clear all saved form values for the current record | ||
* @param $form The form to clear the data for | ||
*/ | ||
export async function clearSavedFormValues($form: JQuery<HTMLElement>) { | ||
if (!$form || $form.length === 0) return; | ||
const layout = layoutId(); | ||
const record = recordId(); | ||
const ls = storage(); | ||
const item = await ls.getItem(table_key()); | ||
|
||
if (item) ls.removeItem(`linkspace-record-change-${layout}-${record}`); | ||
await Promise.all($form.find(".linkspace-field").map(async (_, el) => { | ||
const field_id = $(el).data("column-id"); | ||
const item = await gadsStorage.getItem(`linkspace-column-${field_id}-${layout}-${record}`); | ||
if (item) gadsStorage.removeItem(`linkspace-column-${field_id}-${layout}-${record}`); | ||
})); | ||
} | ||
|
||
/** | ||
* Get the layout identifier from the body data | ||
* @returns The layout identifier | ||
*/ | ||
export function layoutId() { | ||
return $('body').data('layout-identifier'); | ||
} | ||
|
||
/** | ||
* Get the record identifier from the body data | ||
* @returns The record identifier | ||
*/ | ||
export function recordId() { | ||
return $('body').find('.form-edit').data('current-id') || 0; | ||
} | ||
|
||
/** | ||
* Get the key for the table used for saving form values | ||
* @returns The key for the table | ||
*/ | ||
export function table_key() { | ||
return `linkspace-record-change-${layoutId()}-${recordId()}`; | ||
} | ||
|
||
/** | ||
* Get the storage object - this originally was used in debugging to allow for the storage object to be mocked | ||
* @returns The storage object | ||
*/ | ||
export function storage() { | ||
return gadsStorage; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 5 additions & 1 deletion
6
src/frontend/components/button/lib/submit-draft-record-button.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
import { clearSavedFormValues } from "./common"; | ||
|
||
/** | ||
* Create a submit draft record button | ||
* @param element {JQuery<HTMLElement>} The button element | ||
*/ | ||
export default function createSubmitDraftRecordButton(element: JQuery<HTMLElement>) { | ||
element.on("click", (ev: JQuery.ClickEvent) => { | ||
element.on("click", async (ev: JQuery.ClickEvent) => { | ||
const $button = $(ev.target).closest('button'); | ||
const $form = $button.closest("form"); | ||
|
||
// Remove the required attribute from hidden required dependent fields | ||
$form.find(".form-group *[aria-required]").removeAttr('required'); | ||
// As the draft should save all changed values, we clear them from the local storage | ||
await clearSavedFormValues(ev.target.closest("form")); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/frontend/components/form-group/autosave/_autosave.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
.field--changed { | ||
input, .jstree-container-ul, .form-control { | ||
background-color: $field-highlight; | ||
border-radius: $input-border-radius; | ||
} | ||
} | ||
|
||
.modal-autosave { | ||
max-height: 20rem; | ||
overflow-y: auto; | ||
overflow-x: hidden; | ||
} | ||
|
||
li.li-success { | ||
list-style: none; | ||
&::before { | ||
content: '\2713'; | ||
color: green; | ||
font-size: 1.5em; | ||
margin-right: 0.5em; | ||
} | ||
} | ||
|
||
li.li-error { | ||
list-style: none; | ||
&::before { | ||
content: '\2717'; | ||
color: red; | ||
font-size: 1.5em; | ||
margin-right: 0.5em; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { initializeComponent } from 'component'; | ||
import AutosaveComponent from './lib/component'; | ||
import AutosaveModal from './lib/modal'; | ||
import gadsStorage from 'util/gadsStorage'; | ||
|
||
export default (scope) => { | ||
if (gadsStorage.enabled) { | ||
try { | ||
initializeComponent(scope, '.linkspace-field', AutosaveComponent); | ||
initializeComponent(scope, '#restoreValuesModal', AutosaveModal); | ||
} catch(e) { | ||
console.error(e); | ||
$('.content-block__main-content').prepend('<div class="alert alert-danger">Auto-recover failed to initialize. ' + e.message ? e.message : e + '</div>'); | ||
} | ||
} else { | ||
$('.content-block__main-content').prepend('<div class="alert alert-warning">Auto-recover is disabled as your browser does not support encryption</div>'); | ||
} | ||
}; |
38 changes: 38 additions & 0 deletions
38
src/frontend/components/form-group/autosave/lib/autosave.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import "../../../../testing/globals.definitions"; | ||
import AutosaveBase from './autosaveBase'; | ||
|
||
class TestAutosave extends AutosaveBase { | ||
initAutosave(): void { | ||
console.log('initAutosave'); | ||
} | ||
} | ||
|
||
describe('AutosaveBase', () => { | ||
beforeAll(() => { | ||
document.body.innerHTML = ` | ||
<body> | ||
<div id="test"></div> | ||
</body> | ||
`; | ||
$('body').data('layout-identifier', 1); | ||
}); | ||
|
||
afterAll(()=>{ | ||
document.body.innerHTML = ''; | ||
}); | ||
|
||
it('should return layoutId', () => { | ||
const autosave = new TestAutosave(document.getElementById('test')!); | ||
expect(autosave.layoutId).toBe(1); | ||
}); | ||
|
||
it('should return recordId', () => { | ||
const autosave = new TestAutosave(document.getElementById('test')!); | ||
expect(autosave.recordId).toBe(0); | ||
}); | ||
|
||
it('should return table_key', () => { | ||
const autosave = new TestAutosave(document.getElementById('test')!); | ||
expect(autosave.table_key).toBe('linkspace-record-change-1-0'); | ||
}); | ||
}); |
Oops, something went wrong.