index.js
115 lines
| 1 | import editBase from '../edit-base'; |
| 2 | import { getPrimaryId } from '../../../functions'; |
| 3 | |
| 4 | |
| 5 | const getEditableColumn = ( state, column, record ) => { |
| 6 | const position = getPrimaryId( record ); |
| 7 | const { base } = state; |
| 8 | |
| 9 | if ( ! position ) { |
| 10 | throw new Error( 'Empty primary column' ); |
| 11 | } |
| 12 | base.editedList[ position ] = base.editedList[ position ] || {}; |
| 13 | |
| 14 | return [ base.editedList[ position ][ column ] || {}, position ]; |
| 15 | }; |
| 16 | |
| 17 | const getters = { |
| 18 | editedColumn: state => ( column, record ) => { |
| 19 | const position = getPrimaryId( record ); |
| 20 | const { base } = state; |
| 21 | |
| 22 | if ( ! position || ! base.editedList[ position ] |
| 23 | || ! base.editedList[ position ][ column ] |
| 24 | ) { |
| 25 | throw new Error( 'Column is not edited' ); |
| 26 | } |
| 27 | |
| 28 | return base.editedList[ position ][ column ]; |
| 29 | }, |
| 30 | editedColumnProp: state => ( column, record, prop ) => { |
| 31 | const result = getters.editedColumn( state )( column, record )[ prop ] ?? false; |
| 32 | |
| 33 | if ( false === result ) { |
| 34 | throw new Error( 'Prop is not defined' ); |
| 35 | } |
| 36 | |
| 37 | return result; |
| 38 | }, |
| 39 | }; |
| 40 | |
| 41 | export default { |
| 42 | modules: { |
| 43 | base: editBase, |
| 44 | }, |
| 45 | getters: { |
| 46 | ...getters, |
| 47 | editedCellValue: state => ( column, record ) => { |
| 48 | try { |
| 49 | return getters.editedColumnProp( state )( column, record, 'value' ); |
| 50 | } catch ( error ) { |
| 51 | return record[ column ]?.value ?? 'NULL'; |
| 52 | } |
| 53 | }, |
| 54 | editedListValues: state => { |
| 55 | const values = {}; |
| 56 | |
| 57 | const { base } = state; |
| 58 | |
| 59 | for ( const [ position, currentRow ] of Object.entries( base.editedList ) ) { |
| 60 | const row = {}; |
| 61 | |
| 62 | for ( const [ column, { value } ] of Object.entries( currentRow ) ) { |
| 63 | row[ column ] = value; |
| 64 | } |
| 65 | |
| 66 | values[ position ] = row; |
| 67 | } |
| 68 | |
| 69 | return values; |
| 70 | }, |
| 71 | }, |
| 72 | mutations: { |
| 73 | updateEditableCell( state, { column, props, record } ) { |
| 74 | let position, columnPair; |
| 75 | const { base } = state; |
| 76 | |
| 77 | try { |
| 78 | [ columnPair, position ] = getEditableColumn( state, column, record ); |
| 79 | } catch ( error ) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | base.editedList[ position ] = { |
| 84 | ...base.editedList[ position ], |
| 85 | [ column ]: { |
| 86 | ...columnPair, |
| 87 | ...props, |
| 88 | }, |
| 89 | }; |
| 90 | base.hasChanges = true; |
| 91 | }, |
| 92 | revertChangesColumn( state, { column, record } ) { |
| 93 | let position, columnPair; |
| 94 | const { base } = state; |
| 95 | |
| 96 | try { |
| 97 | [ columnPair, position ] = getEditableColumn( state, column, record ); |
| 98 | } catch ( error ) { |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | Vue.delete( base.editedList[ position ], column ); |
| 103 | |
| 104 | const currentColumns = Object.keys( base.editedList[ position ] ); |
| 105 | |
| 106 | if ( ! currentColumns.length ) { |
| 107 | Vue.delete( base.editedList, position ); |
| 108 | |
| 109 | if ( ! Object.keys( base.editedList ).length ) { |
| 110 | base.hasChanges = false; |
| 111 | } |
| 112 | } |
| 113 | }, |
| 114 | }, |
| 115 | }; |