environmentEditor.component.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
  2. import { Component, Output, Input } from '@angular/core'
  3. import { Subject } from 'rxjs'
  4. /** @hidden */
  5. @Component({
  6. selector: 'environment-editor',
  7. template: require('./environmentEditor.component.pug'),
  8. styles: [require('./environmentEditor.component.scss')],
  9. })
  10. export class EnvironmentEditorComponent {
  11. @Output() modelChange = new Subject<any>()
  12. vars: { key: string, value: string }[] = []
  13. private cachedModel: any
  14. @Input() get model (): any {
  15. return this.cachedModel
  16. }
  17. set model (value) {
  18. this.vars = Object.entries(value).map(([k, v]) => ({ key: k, value: v as string }))
  19. this.cachedModel = this.getModel()
  20. }
  21. getModel () {
  22. const model = {}
  23. for (const pair of this.vars) {
  24. model[pair.key] = pair.value
  25. }
  26. return model
  27. }
  28. emitUpdate () {
  29. this.cachedModel = this.getModel()
  30. this.modelChange.next(this.cachedModel)
  31. }
  32. addEnvironmentVar () {
  33. this.vars.push({ key: '', value: '' })
  34. }
  35. removeEnvironmentVar (key: string) {
  36. this.vars = this.vars.filter(x => x.key !== key)
  37. this.emitUpdate()
  38. }
  39. }