-
Notifications
You must be signed in to change notification settings - Fork 542
/
catalogue.ts
101 lines (80 loc) · 2.93 KB
/
catalogue.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import * as Adaptive from "adaptivecards";
import { Downloader } from "./downloader";
export class CatalogueEntry {
private _cardPayload: string;
private _cardPayloadDownloaded: boolean = false;
private _sampleData: string;
private _sampleDataDownloaded: boolean = false;
private downloadCompleted() {
if (this._cardPayloadDownloaded && this._sampleDataDownloaded && this.onDownloaded) {
this.onDownloaded(this);
}
}
sampleDataUrl?: string;
onDownloaded: (sender: CatalogueEntry) => void = null;
constructor(readonly displayName: string, readonly cardPayloadUrl: string, sampleDataUrl?: string) {
this.sampleDataUrl = sampleDataUrl;
}
download() {
let payloadDownloader = new Downloader(this.cardPayloadUrl);
payloadDownloader.onSuccess = () => {
this._cardPayload = payloadDownloader.data;
this._cardPayloadDownloaded = true;
this.downloadCompleted();
}
payloadDownloader.onError = () => {
this._cardPayloadDownloaded = true;
this.downloadCompleted();
}
payloadDownloader.download();
if (!Adaptive.isNullOrEmpty(this.sampleDataUrl)) {
let sampleDataDownloader = new Downloader(this.sampleDataUrl);
sampleDataDownloader.onSuccess = () => {
this._sampleData = sampleDataDownloader.data;
this._sampleDataDownloaded = true;
this.downloadCompleted();
}
sampleDataDownloader.onError = () => {
this._sampleDataDownloaded = true;
this.downloadCompleted();
}
sampleDataDownloader.download();
}
else {
this._sampleDataDownloaded = true;
this.downloadCompleted();
}
}
get cardPayload(): string {
return this._cardPayload;
}
get sampleData(): string {
return this._sampleData;
}
}
export function parseCatalogue(input: any): CatalogueEntry[] {
let entries: any[] = null;
if (Array.isArray(input)) {
entries = input;
}
else {
entries = Array.isArray(input["entries"]) ? input["entried"] : null;
}
let result: CatalogueEntry[] = [];
if (entries != null) {
for (let entry of entries) {
if (typeof entry === "object") {
let displayName = Adaptive.getStringValue(entry["displayName"]);
let cardPayloadUrl = Adaptive.getStringValue(entry["cardPayloadUrl"]);
if (!Adaptive.isNullOrEmpty(displayName) && !Adaptive.isNullOrEmpty(cardPayloadUrl)) {
result.push(
new CatalogueEntry(
displayName,
cardPayloadUrl,
Adaptive.getStringValue(entry["dataSampleUrl"])));
}
}
}
}
return result;
}