forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCarouselPagination.js
105 lines (89 loc) · 3.91 KB
/
CarouselPagination.js
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
var builder = require('botbuilder');
var defaultSettings = {
showMoreTitle: 'title_show_more',
showMoreValue: 'show_more',
selectTemplate: 'select',
pageSize: 10,
unknownOption: 'unknown_option'
};
module.exports = {
create: function (getPageFunc, getItemFunc, itemToCardFunc, settings) {
// parameter validation
settings = Object.assign({}, defaultSettings, settings);
if (typeof getPageFunc !== 'function') {
throw new Error('getPageFunc must be a function');
}
if (typeof getItemFunc !== 'function') {
throw new Error('getItemFunc must be a function');
}
if (typeof itemToCardFunc !== 'function') {
throw new Error('itemToCardFunc must be a function');
}
// map item info into HeroCard
var asCard = function (session, cardInfo) {
var card = new builder.HeroCard()
.title(cardInfo.title)
.buttons([
new builder.CardAction()
.type('imBack')
.value(session.gettext(settings.selectTemplate) + cardInfo.title)
.title(session.gettext(cardInfo.buttonLabel))
]);
if (cardInfo.subtitle) {
card = card.subtitle(cardInfo.subtitle);
}
if (cardInfo.imageUrl) {
card = card.images([new builder.CardImage().url(cardInfo.imageUrl).alt(cardInfo.title)]);
}
return card;
};
// return dialog handler funciton
return function (session, args, next) {
var pageNumber = session.dialogData.pageNumber || 1;
var input = session.message.text;
var selectPrefix = session.gettext(settings.selectTemplate);
if (input && input.toLowerCase() === session.gettext(settings.showMoreValue).toLowerCase()) {
// next page
pageNumber++;
} else if (input && isSelection(input, selectPrefix)) {
// Validate selection
var selectedName = input.substring(selectPrefix.length);
getItemFunc(selectedName).then(function (selectedItem) {
if (!selectedItem) {
return session.send(settings.unknownOption);
}
// reset page
session.dialogData.pageNumber = null;
// return selection to dialog stack
return next({ selected: selectedItem });
});
return;
}
// retrieve from service and send items
getPageFunc(pageNumber, settings.pageSize).then(function (pageResult) {
// save current page number
session.dialogData.pageNumber = pageNumber;
// items carousel
var cards = pageResult.items
.map(itemToCardFunc)
.map(function (cardData) { return asCard(session, cardData); });
var message = new builder.Message(session)
.attachmentLayout(builder.AttachmentLayout.carousel)
.attachments(cards);
session.send(message);
// more items link
if (pageResult.totalCount > pageNumber * settings.pageSize) {
var moreCard = new builder.HeroCard(session)
.title(settings.showMoreTitle)
.buttons([
builder.CardAction.imBack(session, session.gettext(settings.showMoreValue), settings.showMoreValue)
]);
session.send(new builder.Message(session).addAttachment(moreCard));
}
});
};
}
};
function isSelection(input, selectPrefix) {
return input.toLowerCase().indexOf(selectPrefix.toLowerCase()) === 0;
}