import { Component } from '@angular/core' ; import { AlertController } from '@ionic/angular' ; import { ActivatedRoute } from '@angular/router' ; import { DataService } from 'src/app/core/services/data.service' ; import { NoticeService } from 'src/app/core/services/notice.service' ; @ Component ( { selector : 'page-room-edit' , templateUrl : 'room-edit.html' , styleUrls : [ 'room-edit.scss' ] } ) export class RoomEditPage { roomName ; tempRoomName = "unknown" ; alert ; get roomDataDict ( ) { return this . dataService . room . dict ; } get roomDataList ( ) { return this . dataService . room . list ; } get deviceDataDict ( ) { return this . dataService . device . dict ; } get deviceDataList ( ) { return this . dataService . device . list ; } get currentRoomData ( ) { return this . dataService . room . dict [ this . roomName ] ; } set currentRoomData ( data ) { this . dataService . room . dict [ this . roomName ] = data ; } constructor ( private dataService , private alertCtrl , private noticeService , private activatedRoute ) { } ngOnInit ( ) { this . roomName = this . activatedRoute . snapshot . params [ 'room' ] ; this . tempRoomName = this . roomName ; this . removeInvalidDevice ( ) ; } removeInvalidDevice ( ) { let newRoomData = [ ] ; for ( let deviceId of this . currentRoomData ) { if ( typeof this . deviceDataDict [ deviceId ] != 'undefined' ) { newRoomData . push ( deviceId ) ; } } this . currentRoomData = newRoomData ; } async changeRoomName ( ) { this . alert = await this . alertCtrl . create ( { header : '修改房间名' , inputs : [ { name : 'newRoomName' , value : this . tempRoomName , placeholder : this . tempRoomName } ] , buttons : [ { text : '取消' , handler : data => { console . log ( 'Cancel clicked' ) ; } } , { text : '确认修改' , handler : data => { if ( data . newRoomName . length == 0 ) return ; if ( data . newRoomName . length > 10 ) { this . noticeService . showToast ( 'tooLongroomName' ) ; return ; } if ( this . roomIsExist ( data . newRoomName ) ) { this . noticeService . showToast ( 'sameroomName' ) ; return ; } this . tempRoomName = data . newRoomName ; this . renameRoom ( this . tempRoomName ) ; } } ] } ) ; this . alert . present ( ) ; } renameRoom ( newRoomName ) { let oldRoomnam = this . roomName ; let index = this . roomDataList . indexOf ( oldRoomnam ) ; this . roomDataList . splice ( index + 1 , 0 , newRoomName ) ; this . roomDataDict [ newRoomName ] = this . roomDataDict [ oldRoomnam ] ; this . roomName = newRoomName ; this . roomDataList . splice ( index , 1 ) ; delete this . roomDataDict [ oldRoomnam ] ; } isExist ( deviceName ) { if ( this . currentRoomData . indexOf ( deviceName ) > - 1 ) return true ; return false ; } roomIsExist ( roomName ) { if ( this . roomDataList . indexOf ( roomName ) > - 1 ) return true ; return false ; } delDevice ( deviceName ) { let index = this . currentRoomData . indexOf ( deviceName ) ; if ( index > - 1 ) { this . currentRoomData . splice ( index , 1 ) ; } } addDevice ( deviceName ) { this . currentRoomData . push ( deviceName ) ; } }