import { Component, OnInit, SimpleChanges, Input} from '@angular/core'; import {DatasetProfileService} from '../../services/dataset-profile.service'; import {ActivatedRoute} from '@angular/router'; import '../../../assets/custom.js'; declare function simple_notifier(type: string, title: string, message:string): any; @Component({ selector: 'profile-editor', templateUrl: './profile-editor.component.html', styleUrls: ['./profile-editor.component.css'] }) export class ProfileEditorComponent implements OnInit { public profileID: string = null; profile : any = new Array(); //for update sensing... private haveChanges : boolean = false; constructor(private datasetProfileService: DatasetProfileService, private route: ActivatedRoute) { this.profileID = route.snapshot.params['profileID']; //console.log(route.snapshot.params['profileID']) } ngOnChanges(changes: SimpleChanges) { /* if(this.profileID==null) return; if(changes.profileID != null){ this.datasetProfileService.getDatasetProfileById(this.profileID).subscribe((data) => { this.profile = data; }); } */ } ngOnInit() { this.datasetProfileService.getDatasetProfileById(this.profileID).subscribe((data) => { this.profile = data; //console.log(data) }); } onFocusIn($event){ console.log("on-focus-in"); this.haveChanges = false; } onValueChanged($event){ console.log("on-text-changed"); this.haveChanges = true; } onFocusOut($event, value:string){ console.log("on-focus-out"); if(this.haveChanges){ var fieldName = $event.target.name; this.datasetProfileService.getDatasetProfileById(this.profileID).subscribe((data) => { var database_profile : any = data; //update appropriate field if(fieldName == 'profile-label') database_profile.label = this.profile.label; if(fieldName == 'profile-definition') database_profile.definition = this.profile.definition; if(fieldName == 'viewstyle-label') database_profile.viewstyle.label = this.profile.viewstyle.label; if(fieldName == 'viewstyle-definition') database_profile.viewstyle.definition = this.profile.viewstyle.definition; if(fieldName == 'ruleset-label') database_profile.ruleset.label = this.profile.ruleset.label; if(fieldName == 'ruleset-definition') database_profile.ruleset.definition = this.profile.ruleset.definition; //save back this.datasetProfileService.updateDatasetProfile(database_profile); simple_notifier("success",null,"Updated value of field"); }); } } preventDefaults($event){ if($event.code == 'Tab') { $event.preventDefault(); //AND NOW HACK THE TAB KEY WITHIN THE TEXT EDITOR var start = $event.srcElement.selectionStart; var end = $event.srcElement.selectionEnd; // set textarea value to: text before caret + tab + text after caret $event.target.value = $event.target.value.substring(0, start)+"\t"+$event.target.value.substring(end); // put caret at right position again $event.srcElement.selectionStart = $event.srcElement.selectionEnd = start + 1; } } }