import { Component , Input , OnInit } from '@angular/core' ; import { ModalController } from '@ionic/angular' ; import { UIMillStorage } from '../../../services/uiMillStorage' ; import { Mill } from '../../../classes/mill/mill' ; @ Component ( { selector : 'mill-modal-select' , templateUrl : './mill-modal-select.component.html' , styleUrls : [ './mill-modal-select.component.scss' ] , } ) export class MillModalSelectComponent implements OnInit { public static COMPONENT_ID = 'mill-modal-select' ; public objs : Array < Mill > = [ ] ; public multipleSelection = { } ; public radioSelection : string ; public mill_segment : string = 'open' ; @ Input ( ) public multiple : boolean ; @ Input ( ) private selectedValues : Array < string > ; @ Input ( ) public showFinished : boolean ; constructor ( private readonly modalController , private readonly uiMillStorage ) { } public ionViewDidEnter ( ) { this . objs = this . uiMillStorage . getAllEntries ( ) ; if ( this . multiple ) { for ( const obj of this . objs ) { this . multipleSelection [ obj . config . uuid ] = this . selectedValues . filter ( ( e ) => e === obj . config . uuid ) . length > 0 ; } } else { if ( this . selectedValues . length > 0 ) { this . radioSelection = this . selectedValues [ 0 ] ; } } } public ngOnInit ( ) { } public getOpenMills ( ) { return this . objs . filter ( ( e ) => ! e . finished ) . sort ( ( a , b ) => { const nameA = a . name . toUpperCase ( ) ; const nameB = b . name . toUpperCase ( ) ; if ( nameA < nameB ) { return - 1 ; } if ( nameA > nameB ) { return 1 ; } return 0 ; } ) ; } public getArchivedMills ( ) { return this . objs . filter ( ( e ) => e . finished ) . sort ( ( a , b ) => { const nameA = a . name . toUpperCase ( ) ; const nameB = b . name . toUpperCase ( ) ; if ( nameA < nameB ) { return - 1 ; } if ( nameA > nameB ) { return 1 ; } return 0 ; } ) ; } public async choose ( ) { const chosenKeys = [ ] ; if ( this . multiple ) { for ( const key in this . multipleSelection ) { if ( this . multipleSelection [ key ] === true ) { chosenKeys . push ( key ) ; } } } else { chosenKeys . push ( this . radioSelection ) ; } let selected_text = '' ; for ( const val of chosenKeys ) { const mill = this . uiMillStorage . getByUUID ( val ) ; selected_text += mill . name + ', ' ; } selected_text = selected_text . substr ( 0 , selected_text . lastIndexOf ( ', ' ) ) ; this . modalController . dismiss ( { selected_values : chosenKeys , selected_text : selected_text , } , undefined , MillModalSelectComponent . COMPONENT_ID ) ; } public async dismiss ( ) { this . modalController . dismiss ( undefined , undefined , MillModalSelectComponent . COMPONENT_ID ) ; } }