-
Notifications
You must be signed in to change notification settings - Fork 11
/
search_page_vm.js
99 lines (79 loc) · 2.43 KB
/
search_page_vm.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
function MatchViewModel(data) {
var self = this;
this.isExpanded = ko.observable(false);
this.expand = function() {
self.isExpanded(true);
}
this.collapse = function() {
self.isExpanded(false);
}
ko.mapping.fromJS(data, {}, this);
}
function SearchPageViewModel(settingsVM, qrServer) {
var self = this;
this.name = ko.observable();
this.results = ko.observable();
this.settingsPageViewModel = settingsVM;
this.server = qrServer;
this.lastCheckInResultModel = new CheckInResultsModel();
this.resultsMapping = {
'matches': {
create: function(options) {
return new MatchViewModel(options.data);
}
}
}
this.searchForName = function(name) {
if (self.server.isMakingRequest()) {
return;
}
if (self.name() == null || self.name().length < 3) {
alertWrapper("Please enter at least three characters.");
return;
}
self.server.progressMessage("Searching for Tickets...");
self.lastCheckInResultModel.haveResult(false);
self.server.isMakingRequest(true);
url = self.settingsPageViewModel.endpoint() + "/qr_check_in/search/" +
self.settingsPageViewModel.apiKey() + "/" +
encodeURIComponent(self.settingsPageViewModel.selectedEvent()) + "/" + encodeURIComponent(self.name());
$.ajax({
type: 'GET',
url: url,
dataType: 'json',
error: function(xhr, ajaxOptions, thrownError) {
alertWrapper("Error contacting server");
self.server.isMakingRequest(false);
},
success: function(event, data, status, xhr) {
self.results(ko.mapping.fromJS(event, self.resultsMapping));
self.server.isMakingRequest(false);
}
});
}
self.clearSearchTerm = function() {
self.name(null);
self.results(null);
self.lastCheckInResultModel.haveResult(false);
}
this.checkInTicket = function(ticket) {
self.server.checkInTicket(
self.settingsPageViewModel.endpoint(),
self.settingsPageViewModel.apiKey(),
self.settingsPageViewModel.selectedEvent(),
ticket.ticket_token(),
self.lastCheckInResultModel,
ticket.checked_in
);
}
this.checkOutTicket = function(ticket) {
self.server.checkOutTicket(
self.settingsPageViewModel.endpoint(),
self.settingsPageViewModel.apiKey(),
self.settingsPageViewModel.selectedEvent(),
ticket.ticket_token(),
self.lastCheckInResultModel,
ticket.checked_in
);
}
}