-
Notifications
You must be signed in to change notification settings - Fork 0
/
at-intent-card.html
142 lines (113 loc) · 3.48 KB
/
at-intent-card.html
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<link rel="import" href="../at-core-activity-queue/at-core-activity-queue.html" />
<link rel="import" href="../at-app-card/at-app-card.html" />
<dom-module id="at-intent-card">
<template>
<style>
:host {
}
</style>
<at-app-card id="card"></at-app-card>
<at-core-activity-queue id="carbonChatActivityQueue" on-response="_receiveResponseFromApi" on-error="_handleApiError"></at-core-activity-queue>
</template>
<script>
Polymer({
is: 'at-intent-card',
properties: {
serverUrl: {
type: String,
value: 'service://chatbot'
},
cardContainerType: {
type: String,
value: 'chat'
},
eventName: {
type: String,
value: 'change'
},
eventSourceSelector: {
type: String,
value: ''
},
searchTerm: {
type: String,
value: '',
observer:'_searchTermChanged'
},
box: {
type: String,
value: "card",
xtype: "enum",
xvaluelist: [
{ title: "None", value: "none" },
{ title: "Plain", value: "plain" },
{ title: "Card", value: "card" }
]
}
},
observers: [
'_eventChanged(eventName, eventSourceSelector)'
],
ready: function () {
},
_eventChanged: function(eventName, eventSourceSelector) {
if (!eventName || !eventSourceSelector) return;
var el = document.querySelector(eventSourceSelector);
if (!el) return;
if (!eventName) return;
// listen for events
el.addEventListener(eventName, function(e) {
this.searchTerm = e.target.value;
}.bind(this));
// search with current value
this.searchTerm = el.value;
},
_searchTermChanged: function (n, o) {
if (!this.searchTerm) {
this.$.card.name = "";
return;
}
// create request
var jsonContent = JSON.stringify({ query: this.searchTerm });
var requestOptions = {
method: "POST",
url: this.serverUrl,
contentType: "application/json",
body: jsonContent,
}
this.$.carbonChatActivityQueue.addRequest(requestOptions);
},
_receiveResponseFromApi: function (e) {
var response = e.detail.response;
var responseResult = (response.result || response.Data.result);
var cardMetadataDefinition = responseResult.card;
var cardData = responseResult.intent.data;
cardData._container = this.cardContainerType;
var detail = e.detail;
// no valid card found
if (!cardMetadataDefinition) {
this.$.card.name = '';
return;
}
// push card metadata and definition to at-core-state
Polymer.signal('state-push', {
url: "service://cardDefinition?cardName=" + cardMetadataDefinition.name,
response: {
Data: cardMetadataDefinition
}
});
// push card data to at-core-state
Polymer.signal('state-push', {
url: "service://cardData/" + cardMetadataDefinition.name,
response: {
Data: cardData
}
});
this.$.card.box = this.box;
this.$.card.name = cardMetadataDefinition.name;
},
_handleApiError: function (e) {
}
});
</script>
</dom-module>