import { isItemSpecificType } from '../type_helpers' ; import _ from '../util' ; import store from '../store' ; import LDParse from '../ld_parse' ; import Layout from '../layout' ; function findPLIItem ( filename , colorCode ) { return store . state . pliItems . find ( pliItem => { if ( pliItem . filename === filename && pliItem . colorCode === colorCode ) { const parent = store . get . parent ( pliItem ) ; if ( isItemSpecificType ( parent , 'page' ) && parent . subtype === 'inventoryPage' ) { return true ; } } return false ; } ) ; } export interface InventoryPageMutationInterface { add ( ) : void ; delete ( { page } ) : void ; deleteAll ( ) : void ; addPart ( { filename , colorCode , part , doLayout } ) : void ; removePart ( { filename , colorCode , part , doLayout } ) : void ; } interface PartListEntry { [ key ] : number ; } export const InventoryPageMutations = { add ( ) { if ( store . model == null ) { return ; } const lastPage = store . get . lastPage ( ) ; const page = store . mutations . page . add ( { subtype : 'inventoryPage' , pageNumber : lastPage ? lastPage . number + 1 : 0 , } ) ; const itemList = [ ] ; function buildPartList ( model ) { ( model . parts || [ ] ) . forEach ( ( { filename , colorCode } ) => { if ( LDParse . model . isSubmodel ( filename ) ) { buildPartList ( LDParse . model . get . abstractPart ( filename ) ) ; } else { if ( itemList [ colorCode ] && itemList [ colorCode ] [ filename ] ) { itemList [ colorCode ] [ filename ] ++ ; } else { itemList [ colorCode ] = itemList [ colorCode ] || { } ; itemList [ colorCode ] [ filename ] = 1 ; } } } ) ; } buildPartList ( store . model ) ; itemList . forEach ( ( partList , colorCode ) => { _ . forOwn ( partList , ( quantity , filename ) => { store . mutations . pliItem . add ( { parent : page , filename , colorCode , quantity } ) ; } ) ; } ) ; Layout . allInventoryPages ( ) ; } , delete ( { page } ) { const item = store . get . page ( page ) ; if ( item == null ) { return ; } if ( item . numberLabelID != null ) { store . mutations . item . delete ( { item : store . get . numberLabel ( item . numberLabelID ) } ) ; } store . mutations . item . deleteChildList ( { item , listType : 'pliItem' } ) ; store . mutations . item . delete ( { item } ) ; } , deleteAll ( ) { const pages = store . get . inventoryPages ( ) || [ ] ; pages . forEach ( page => { store . mutations . inventoryPage . delete ( { page } ) ; } ) ; } , addPart ( { filename , colorCode , part , doLayout = false } ) { const page = store . get . inventoryPages ( ) [ 0 ] ; if ( page == null ) { return ; } let localFilename = filename ; let localColorCode = colorCode ; if ( part != null ) { if ( filename == null ) { localFilename = part . filename ; } if ( colorCode == null ) { localColorCode = part . colorCode ; } } if ( localFilename == null || localColorCode == null ) { return ; } const pliItem = findPLIItem ( localFilename , localColorCode ) ; if ( pliItem ) { pliItem . quantity += 1 ; } else { store . mutations . pliItem . add ( { parent : page , filename : localFilename , colorCode : localColorCode , quantity : 1 , } ) ; } if ( doLayout ) { store . mutations . page . layout ( { page } ) ; } } , removePart ( { filename , colorCode , part , doLayout = false } ) { const page = store . get . inventoryPages ( ) [ 0 ] ; if ( page == null ) { return ; } let localFilename = filename ; let localColorCode = colorCode ; if ( part != null ) { if ( filename == null ) { localFilename = part . filename ; } if ( colorCode == null ) { localColorCode = part . colorCode ; } } if ( localFilename == null || localColorCode == null ) { return ; } const pliItem = findPLIItem ( localFilename , localColorCode ) ; if ( pliItem ) { if ( pliItem . quantity === 1 ) { store . mutations . pliItem . delete ( { pliItem } ) ; } else { pliItem . quantity -- ; } } if ( doLayout ) { store . mutations . page . layout ( { page } ) ; } } , } ;