launch-dialog.component.ts 5.55 KB
Newer Older
Chris Hines's avatar
Chris Hines committed
1
import { Component, OnInit, ViewChild, ElementRef, Inject, Input } from '@angular/core';
2
3
import { DomSanitizer} from '@angular/platform-browser';
import { Renderer2 } from '@angular/core';
Chris Hines's avatar
Chris Hines committed
4
5
//import { MatDialogRef,MAT_DIALOG_DATA } from '@angular/material';
import { TesService } from '../tes.service';
Chris Hines's avatar
Chris Hines committed
6
import { Observable, merge, pipe, timer, Subscription} from 'rxjs';
Chris Hines's avatar
Chris Hines committed
7
import { BehaviorSubject } from 'rxjs';
8
import { take, skip } from 'rxjs/operators';
Chris Hines's avatar
Chris Hines committed
9
10
11
import { Identity } from '../identity';
import { Strudelapp } from '../strudelapp';
import { BatchInterface} from '../batchinterface';
Chris Hines's avatar
Chris Hines committed
12
import { fromEvent } from 'rxjs';
13
14
15
16
17
18
19
20


@Component({
  selector: 'app-launch-dialog',
  templateUrl: './launch-dialog.component.html',
  styleUrls: ['./launch-dialog.component.css']
})
export class LaunchDialogComponent implements OnInit {
Chris Hines's avatar
Chris Hines committed
21
22
  @Input() identity: Identity;
  @Input() appSubject: BehaviorSubject<Strudelapp>;
Chris Hines's avatar
Chris Hines committed
23
  //@Input() app: Strudelapp;
Chris Hines's avatar
Chris Hines committed
24
  app: Strudelapp;
25
26
27
28
29
  batchcmdurl: string;
  appconfigurl: string;
  batchcmdsafeurl: any;
  appconfigsafeurl: any;
  rmListen: any;
Chris Hines's avatar
Chris Hines committed
30
  @ViewChild('launchbtn', { read: ElementRef, static: false }) launchbtn: ElementRef;
31
  @ViewChild('batchbuilderiframe', { read: ElementRef, static: false }) batchbuilderiframe: ElementRef;
32
  setFocus: Boolean;
Chris Hines's avatar
Chris Hines committed
33
  subscriptions: Subscription[];
34
  launchNext: Boolean;
Chris Hines's avatar
Chris Hines committed
35
  width: number;
Chris Hines's avatar
Chris Hines committed
36
  appconfigwidth: number;
Chris Hines's avatar
Chris Hines committed
37
  height: number;
Chris Hines's avatar
Chris Hines committed
38
39
40
41
42
  public appData: BehaviorSubject<any>;
  public submitcmd: BehaviorSubject<string>;
  configtoggle: boolean;
  configwindow: any;
  public readyToLaunch: boolean;
Chris Hines's avatar
Chris Hines committed
43
44
45
46
47
48
49

  //constructor( public dialogRef: MatDialogRef<LaunchDialogComponent>,
  constructor( 
    private domSanitizer: DomSanitizer,
    private renderer: Renderer2,
    private tesService: TesService,
  ) {
Chris Hines's avatar
Chris Hines committed
50
51
    this.submitcmd = new BehaviorSubject<string>(null);
    this.appData = new BehaviorSubject<any>(null);
Chris Hines's avatar
Chris Hines committed
52
    this.subscriptions = [];
53
    this.launchNext = false;
Chris Hines's avatar
Chris Hines committed
54
55
56
    this.height=0;
    this.submitcmd.subscribe(() => this.canILaunch());
    this.appData.subscribe(() => this.canILaunch());
57
58
59
60

  }

  ngOnInit() {
Chris Hines's avatar
Chris Hines committed
61
    console.log('creating launch-dialog');
62
    this.rmListen = this.renderer.listen('window','message',(event) => this.receiveMessage(event));
63
    this.setFocus = false;
Chris Hines's avatar
Chris Hines committed
64
65
66
67
68
69
70
71
72
73
74
    this.updateURLs();
    this.clearInfo();
    this.subscriptions.push(this.appSubject.subscribe(() => { this.app = this.appSubject.value; this.updateURLs() ; this.clearInfo()}));
  }

  configdialog(){
    console.log('open app config');
    console.log(this.app.url);
    this.configwindow = window.open(this.app.url);
    this.configwindow.onunload = function () { console.log('config closed')};
    this.subscriptions.push(fromEvent(this.configwindow,'message').subscribe((event) => this.receiveAppconfig(event)));
75
76
  }

Chris Hines's avatar
Chris Hines committed
77
  updateURLs() {
Chris Hines's avatar
Chris Hines committed
78
79
80
    if (this.app == null) {
      return;
    }
Chris Hines's avatar
Chris Hines committed
81
82
83
84
85
86
87
88
89
    let theme = "";
    theme=localStorage.getItem('strudel-theme');
    if (theme == null) {
      theme = 'strudel-theme-light';
    }
    if (this.identity !== null && this.app !== null) {
      this.batchcmdurl = this.identity.site.url+"/"+this.app.name+"?theme="+theme;
    }
    this.batchcmdsafeurl = this.domSanitizer.bypassSecurityTrustResourceUrl(this.batchcmdurl);
Chris Hines's avatar
Chris Hines committed
90
    if (this.app.url !== null) {
Chris Hines's avatar
Chris Hines committed
91
      this.appconfigurl = this.app.url+"?theme="+theme;
Chris Hines's avatar
Chris Hines committed
92
93
    } else {
      this.appconfigurl = null;
Chris Hines's avatar
Chris Hines committed
94
    }
Chris Hines's avatar
Chris Hines committed
95

Chris Hines's avatar
Chris Hines committed
96
97
    if (!(this.appconfigurl == null)) {
      this.appconfigsafeurl = this.domSanitizer.bypassSecurityTrustResourceUrl(this.appconfigurl);
Chris Hines's avatar
Chris Hines committed
98
99
    } else {
      this.appconfigsafeurl = null;
Chris Hines's avatar
Chris Hines committed
100
101
    }
    console.log('updatedURLs');
102
103
104
105
  }

  ngOnDestroy() {
    this.rmListen();
Chris Hines's avatar
Chris Hines committed
106
107
108
    for (let s of this.subscriptions) {
      s.unsubscribe();
    }
109
110
111
  }


Chris Hines's avatar
Chris Hines committed
112
  setReady(ready) {
Chris Hines's avatar
Chris Hines committed
113
114
      console.log('call setReady',ready);
      this.readyToLaunch = ready;
115
      if (this.launchbtn === undefined) {
Chris Hines's avatar
Chris Hines committed
116
          console.log('launchbtn is not defined, cant set ready');
117
118
          return
      }
119
120
121
122
123
124
125
      if (ready && !(this.setFocus)) {
        this.launchbtn.nativeElement.focus();
        this.setFocus = true;
      }
  }


Chris Hines's avatar
Chris Hines committed
126
127
128
129
130
131
  receiveAppconfig(event) {
      if (event.data['appData'] !== undefined)  {
        this.appData.next(event.data['appData']);
        console.log('received appData');
      }
  }
132
  receiveMessage(event) {
Chris Hines's avatar
Chris Hines committed
133
      console.log(event);
134
      if (event.data['batchcmd'] !== undefined)  {
Chris Hines's avatar
Chris Hines committed
135
        this.submitcmd.next(event.data['batchcmd']);
136
      }
Chris Hines's avatar
Chris Hines committed
137
138
139
      if (event.data['resize'] !== undefined) {
        this.height = event.data['resize']['height'];
        this.width = event.data['resize']['width'];
140
      }
141
  }
Chris Hines's avatar
Chris Hines committed
142
143
144

  launch() {
    // tell the batchbuilder iframe that lanch was selected
145
    // This is the cue to save the values as defaults for next launch
Chris Hines's avatar
Chris Hines committed
146
    this.batchbuilderiframe.nativeElement.contentWindow.postMessage('launch selected',"*");
147
    this.submitcmd.pipe(skip(1),take(1)).subscribe( (submitcmd) => this.reallyLaunch(submitcmd))
148
149
150
    this.launchNext=true;
  }

Chris Hines's avatar
Chris Hines committed
151
  reallyLaunch(submitcmd) {
Chris Hines's avatar
Chris Hines committed
152
    let bi = new BatchInterface();
Chris Hines's avatar
Chris Hines committed
153
154
155
    bi.submitcmd = submitcmd;
    if (this.appData.value != null){ 
      let appparams = this.appData.value;
Chris Hines's avatar
Chris Hines committed
156
157
158
159
160
161
162
      this.tesService.submit(this.app,this.identity,bi,appparams);
    } else {
        this.tesService.submit(this.app,this.identity,bi);
    }
  }

  clearInfo() {
Chris Hines's avatar
Chris Hines committed
163
164
      this.appData.next(null);
      this.submitcmd.next(null);
Chris Hines's avatar
Chris Hines committed
165
166
167
  }

  canILaunch() {
Chris Hines's avatar
Chris Hines committed
168
169
170
171
172
173
174
    if (this.app == null) {
      this.setReady(false);
      return
    }
    if (this.submitcmd.value != null) {
        console.log(this.app.url);
        if (this.app.url === null || this.appData.value != null ) {
Chris Hines's avatar
Chris Hines committed
175
176
177
178
179
180
            this.setReady(true);
            return;
        }
    }
    this.setReady(false);
  }
181
}