1+ import gadsStorage from "util/gadsStorage" ;
2+
3+ /**
4+ * Clear all saved form values for the current record
5+ * @param $form The form to clear the data for
6+ */
7+ export async function clearSavedFormValues ( $form : JQuery < HTMLElement > ) {
8+ if ( ! $form || $form . length === 0 ) return ;
9+ const layout = layoutId ( ) ;
10+ const record = recordId ( ) ;
11+ const ls = storage ( ) ;
12+ const item = await ls . getItem ( table_key ( ) ) ;
13+
14+ if ( item ) ls . removeItem ( `linkspace-record-change-${ layout } -${ record } ` ) ;
15+ await Promise . all ( $form . find ( ".linkspace-field" ) . map ( async ( _ , el ) => {
16+ const field_id = $ ( el ) . data ( "column-id" ) ;
17+ const item = await gadsStorage . getItem ( `linkspace-column-${ field_id } -${ layout } -${ record } ` ) ;
18+ if ( item ) gadsStorage . removeItem ( `linkspace-column-${ field_id } -${ layout } -${ record } ` ) ;
19+ } ) ) ;
20+ }
21+
22+ /**
23+ * Get the layout identifier from the body data
24+ * @returns The layout identifier
25+ */
26+ export function layoutId ( ) {
27+ return $ ( 'body' ) . data ( 'layout-identifier' ) ;
28+ }
29+
30+ /**
31+ * Get the record identifier from the body data
32+ * @returns The record identifier
33+ */
34+ export function recordId ( ) {
35+ return $ ( 'body' ) . find ( '.form-edit' ) . data ( 'current-id' ) || 0 ;
36+ }
37+
38+ /**
39+ * Get the key for the table used for saving form values
40+ * @returns The key for the table
41+ */
42+ export function table_key ( ) {
43+ return `linkspace-record-change-${ layoutId ( ) } -${ recordId ( ) } ` ;
44+ }
45+
46+ /**
47+ * Get the storage object - this originally was used in debugging to allow for the storage object to be mocked
48+ * @returns The storage object
49+ */
50+ export function storage ( ) {
51+ return gadsStorage ;
52+ }
0 commit comments