-
Notifications
You must be signed in to change notification settings - Fork 4
/
CvBacklog.js
96 lines (76 loc) · 2.71 KB
/
CvBacklog.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
/*jslint plusplus: true, white: true, browser: true */
/*global CvPlsHelper */
(function() {
'use strict';
function buildCvLink(cvRequest) {
var div, a, requestType;
requestType = (cvRequest.closed_date !== undefined) ? 'delv' : 'cv';
div = this.document.createElement('div');
div.appendChild(this.document.createTextNode('[' + requestType + '-pls] '));
a = div.appendChild(this.document.createElement('a'));
a.setAttribute('href', cvRequest.link);
a.setAttribute('target', '_blank');
a.innerHTML = cvRequest.title;
return div;
}
function processBacklogResponse(data) {
var i, l;
while (this.descriptionElement.hasChildNodes()) {
this.descriptionElement.removeChild(this.descriptionElement.lastChild);
}
for (i = 0, l = data.length; i < l && i < this.pluginSettings.getSetting('backlogAmount'); i++) {
this.descriptionElement.appendChild(buildCvLink.call(this, data[i]));
}
}
CvPlsHelper.CvBacklog = function(document, pluginSettings, backlogUrl) {
this.document = document;
this.pluginSettings = pluginSettings;
this.backlogUrl = backlogUrl;
this.descriptionElement = document.getElementById('roomdesc');
};
CvPlsHelper.CvBacklog.prototype.originalDescription = null;
CvPlsHelper.CvBacklog.prototype.timeout = null;
CvPlsHelper.CvBacklog.prototype.visible = false;
CvPlsHelper.CvBacklog.prototype.refresh = function() {
var xhr,
self = this,
interval = this.pluginSettings.getSetting('backlogRefreshInterval') * 60 * 1000;
this.timeout = null;
if (!this.pluginSettings.getSetting('backlogEnabled')) {
this.hide();
return;
}
this.show();
xhr = new XMLHttpRequest();
xhr.open("GET", this.backlogUrl, true);
xhr.setRequestHeader('Accept', 'application/json; charset=utf-8');
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
try {
processBacklogResponse.call(self, JSON.parse(xhr.responseText));
} catch(e) { /* probably a JSON parse error occured, ignore it */ }
}
};
xhr.send(null);
if (this.pluginSettings.getSetting('backlogRefresh')) {
this.timeout = setTimeout(function() {
self.refresh();
}, interval);
}
};
CvPlsHelper.CvBacklog.prototype.show = function() {
if (!this.visible) {
this.visible = true;
this.originalDescription = this.descriptionElement.innerHTML;
}
};
CvPlsHelper.CvBacklog.prototype.hide = function() {
if (this.visible) {
this.visible = false;
this.descriptionElement.innerHTML = this.originalDescription;
if (this.timeout !== null) {
clearTimeout(this.timeout);
}
}
};
}());