-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHallType.js
83 lines (73 loc) · 2.19 KB
/
HallType.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
var root = 'https://www.mealbookings.cai.cam.ac.uk/index.php';
var HallType = function(name, id) {
this.name = name;
this.id = id;
}
HallType.loadAll = function() {
return $.defer(Object.method(chrome.extension, 'sendRequest'), {action: 'loadHallTypes'}).then(function(data) {
data.forEach(function(d) {
d.__proto__ = Object.create(HallType.prototype);
});
return data;
})
};
HallType.prototype.loadHalls = function(opts) {
opts = Object.merge({
from: new Date('1970-01-01'),
to: new Date('2038-01-19')
}, opts)
var type = this;
// request all valid unix timestamps
return $.get(root, {
'event': type.id,
'from': opts.from.toLocalISODateString(),
'to': opts.to.toLocalISODateString()
}, null, 'html').then(function(data) {
var $doc = $(data);
var tables = $doc.find('table.list');
var bookedRows = tables.eq(0).find('tr').slice(1, -1);
var unbookedRows = tables.eq(1).find('tr').slice(1, -1);
var parseDate = function(summary, str) {
if(str.trim() == "No current bookings found") {
summary.invalid = true;
return;
}
summary.date = new Date(str);
}
var parseFullness = function(summary, str) {
var fullnessParts = /\((-?\d+)\/(\d+)\)/.exec(str);
try {
summary.capacity = parseInt(fullnessParts[2]);
summary.available = parseInt(fullnessParts[1]);
} catch(e) { }
}
var parseStatus = function(summary, str) {
str = str.trim();
if(/signup deadline has passed/i.test(str))
summary.status = 'closed';
else if(/signup has not yet opened/i.test(str))
summary.status = 'unopened';
}
var parseCells = function(s, cells) {
parseDate(s, cells.eq(0).text());
var rest = cells.slice(1).text();
parseFullness(s, rest);
parseStatus(s, rest);
if(!s.invalid)
return s;
};
var results = [].concat(
bookedRows.map(function() {
var s = new HallSummary(type);
s.status = 'booked';
return parseCells(s, $(this).find('td'));
}).get(),
unbookedRows.map(function() {
var s = new HallSummary(type);
s.status = 'open';
return parseCells(s, $(this).find('td'));
}).get()
)
return results;
}).promise();
};