forked from Shopify/shipit-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_status.js
More file actions
155 lines (131 loc) · 4.74 KB
/
merge_status.js
File metadata and controls
155 lines (131 loc) · 4.74 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
// Generated by CoffeeScript 1.12.7
var AjaxAction, MergeStatusPoller, poller,
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
MergeStatusPoller = (function() {
var POLL_INTERVAL, previousLastModified;
POLL_INTERVAL = 3000;
function MergeStatusPoller() {
this.mergeStatus = bind(this.mergeStatus, this);
this.isMergeQueueEnabled = bind(this.isMergeQueueEnabled, this);
this.updateDocument = bind(this.updateDocument, this);
this.refreshPage = bind(this.refreshPage, this);
this.onPageChange = bind(this.onPageChange, this);
this.request = {
abort: function() {}
};
this.previousLastModified = null;
this.timeoutId = null;
}
MergeStatusPoller.prototype.start = function() {
this.timeoutId = setTimeout(this.refreshPage, POLL_INTERVAL);
return this;
};
MergeStatusPoller.prototype.stop = function() {
this.request.abort();
clearTimeout(this.timeoutId);
return this;
};
MergeStatusPoller.prototype.onPageChange = function() {
window.parent.postMessage({
event: 'hctw:height:change',
height: document.body.clientHeight,
service: 'shipit'
}, '*');
return window.parent.postMessage({
event: 'hctw:stack:info',
queue_enabled: this.isMergeQueueEnabled(),
status: this.mergeStatus(),
service: 'shipit'
}, '*');
};
MergeStatusPoller.prototype.fetchPage = function(url, callback) {
var request;
request = this.request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (request.readyState === XMLHttpRequest.DONE) {
return callback(request.status === 200 && request.responseText, request);
}
};
request.open('GET', url, true);
request.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
return request.send();
};
previousLastModified = null;
MergeStatusPoller.prototype.refreshPage = function() {
return this.fetchPage(window.location.toString(), (function(_this) {
return function(html, response) {
_this.updateDocument(html, response);
return setTimeout(_this.refreshPage, POLL_INTERVAL);
};
})(this));
};
MergeStatusPoller.prototype.updateDocument = function(html, response) {
var container, lastModified;
lastModified = response.getResponseHeader('last-modified');
if (!lastModified || lastModified !== this.previousLastModified) {
this.previousLastModified = lastModified;
if (html && (container = document.querySelector('[data-layout-content]'))) {
container.innerHTML = html;
return this.onPageChange();
}
}
};
MergeStatusPoller.prototype.isMergeQueueEnabled = function() {
var ref;
return (ref = document.querySelector('.merge-status-container .js-details-container')) != null ? ref.hasAttribute('data-queue-enabled') : void 0;
};
MergeStatusPoller.prototype.mergeStatus = function() {
var ref;
return ((ref = document.querySelector('.merge-status-container .js-details-container')) != null ? ref.getAttribute('data-merge-status') : void 0) || 'unknown';
};
return MergeStatusPoller;
})();
AjaxAction = (function() {
function AjaxAction(poller1) {
this.poller = poller1;
this.submit = bind(this.submit, this);
document.addEventListener('submit', this.submit, false);
}
AjaxAction.prototype.submit = function(event) {
if (event.target.getAttribute('data-remote') !== 'true') {
return;
}
event.preventDefault();
this.poller.stop();
this.disableButtons(event.target);
return this.submitFormAsynchronously(event.target, (function(_this) {
return function(html, request) {
_this.poller.updateDocument(html, request);
return _this.poller.start();
};
})(this));
};
AjaxAction.prototype.submitFormAsynchronously = function(form, callback) {
var request;
request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (request.readyState === XMLHttpRequest.DONE) {
return callback(request.status === 200 && request.responseText, request);
}
};
request.open(form.method.toLocaleUpperCase(), form.action, true);
request.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
return request.send(new FormData(form));
};
AjaxAction.prototype.disableButtons = function(form) {
var button, i, len, ref, results;
ref = form.querySelectorAll('[data-disable-with]');
results = [];
for (i = 0, len = ref.length; i < len; i++) {
button = ref[i];
button.disabled = true;
results.push(button.textContent = button.getAttribute('data-disable-with'));
}
return results;
};
return AjaxAction;
})();
poller = new MergeStatusPoller;
poller.onPageChange();
poller.start();
new AjaxAction(poller);