2     Copyright (C) 2019 CMCC, Inc. and others. All rights reserved.
 
   4     Licensed under the Apache License, Version 2.0 (the "License");
 
   5     you may not use this file except in compliance with the License.
 
   6     You may obtain a copy of the License at
 
   8             http://www.apache.org/licenses/LICENSE-2.0
 
  10     Unless required by applicable law or agreed to in writing, software
 
  11     distributed under the License is distributed on an "AS IS" BASIS,
 
  12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  13     See the License for the specific language governing permissions and
 
  14     limitations under the License.
 
  24 } from "@angular/core";
 
  25 import { NgbActiveModal } from "@ng-bootstrap/ng-bootstrap";
 
  26 import { RestApiService } from "src/app/core/services/rest-api.service";
 
  27 import { Template } from "src/app/core/models/template.model";
 
  30 import { NgxSpinnerService } from "ngx-spinner";
 
  33   selector: 'app-edit-template-modal',
 
  34   templateUrl: './edit-template-modal.component.html',
 
  35   styleUrls: ['./edit-template-modal.component.css']
 
  37 export class EditTemplateModalComponent implements OnInit {
 
  38   @Input() edittemplate: Template;
 
  39   templateInput: Template;
 
  40   defaultDesigntype: String;
 
  41   defaultTopicname: String;
 
  42   templatetypedata: Array<any> = [];
 
  43   topicname: Array<any> = [];
 
  44   dbList: Array<any> = [];
 
  46   tempSeletedDbs: any = [];
 
  47   @Output() passEntry: EventEmitter<any> = new EventEmitter();
 
  49   @ViewChild("templatetype") templatetype: ElementRef;
 
  50   @ViewChild("topic") topic: ElementRef;
 
  53     public activeModal: NgbActiveModal,
 
  54     public dashboardApiService: RestApiService,
 
  55     private spinner: NgxSpinnerService,
 
  58   inputtemplateName = null;
 
  59   templateInputTitle = "";
 
  64     this.templateInput = new Template();
 
  66       id: this.edittemplate.id,
 
  67       name: this.edittemplate.name,
 
  68       submitted: this.edittemplate.submitted,
 
  69       body: this.edittemplate.body,
 
  70       note: this.edittemplate.note,
 
  71       topicName: this.edittemplate.topicName,
 
  72       designType: this.edittemplate.designType,
 
  73       designTypeName: this.edittemplate.designTypeName,
 
  74       dbs: this.edittemplate.dbs,
 
  76     this.templateInput = feed;
 
  77     this.templateInputTitle = "" + this.edittemplate.name;
 
  78     this.tempSeletedDbs = this.templateInput.dbs.map(item => { return Number(item) });
 
  81     this.getTemplateTypeName();
 
  84     this.dashboardApiService.getTopicsFromFeeder().subscribe(data => {
 
  85       this.topicname = data;
 
  90     this.templatetypedata.map(item => {
 
  91       if (item.name === this.defaultDesigntype) {
 
  95     this.dashboardApiService.getTempDbList(this.dbId).subscribe(data => {
 
  96       Object.keys(data).map(item => {
 
  97         this.dbList.push({ key: item, name: data[item] })
 
 102   updateSelectedDB(event: any, name: any) {
 
 103     if (event.target.checked) {
 
 104       if (!this.tempSeletedDbs.find(db => db === name)) {
 
 105         this.tempSeletedDbs.push(Number(name.key));
 
 108       const index = this.tempSeletedDbs.indexOf(+name.key, 0);
 
 110         this.tempSeletedDbs.splice(index, 1);
 
 115   getTemplateTypeName() {
 
 116     this.dashboardApiService.getTemplateTypeName().subscribe(data => {
 
 117       this.templatetypedata = data;
 
 118       this.getDefaultOptions();
 
 122   getDefaultOptions() {
 
 123     this.templatetypedata.map(item => {
 
 124       if (item.id === this.templateInput.designType) {
 
 125         return this.defaultDesigntype = item.name;
 
 128     this.defaultTopicname = this.templateInput.topicName;
 
 134     var file = (<HTMLInputElement>document.querySelector("#f-file")).files[0];
 
 135     this.fileName = file.name;
 
 136     var reader = new FileReader();
 
 137     reader.onload = function () {
 
 138       thiss.templateInput.body = String(this.result);
 
 140     reader.readAsText(file);
 
 145     if (this.templateInput.name == '' || this.templateInput.name == undefined) {
 
 148     this.edittemplate = this.templateInput;
 
 149     // this.templatetypedata.map(item => {
 
 150     //   if (item.name === this.templatetype.nativeElement.value) {
 
 151     //     return this.edittemplate.designType = item.id;
 
 154     this.edittemplate.designType = this.templatetypedata.filter(item => {
 
 155       return item.name === this.templatetype.nativeElement.value;
 
 158     this.edittemplate.designTypeName = this.templatetype.nativeElement.value;
 
 159     this.edittemplate.topicName = this.topic.nativeElement.value;
 
 160     this.edittemplate.dbs = this.tempSeletedDbs;
 
 161     this.passEntry.emit(this.edittemplate);