navmesh commited on
Commit
3960bfe
·
1 Parent(s): bb92f34

Upload progressbar.js

Browse files
Files changed (1) hide show
  1. progressbar.js +196 -0
progressbar.js ADDED
@@ -0,0 +1,196 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // code related to showing and updating progressbar shown as the image is being made
2
+ let lastTimeSuccess = 0; //epoch time in second
3
+ function rememberGallerySelection() {
4
+
5
+ }
6
+
7
+ function getGallerySelectedIndex() {
8
+
9
+ }
10
+
11
+ function request(url, data, handler, errorHandler) {
12
+ var xhr = new XMLHttpRequest();
13
+ xhr.open("POST", url, true);
14
+ xhr.setRequestHeader("Content-Type", "application/json");
15
+ xhr.onreadystatechange = function() {
16
+ if (xhr.readyState === 4) {
17
+ if (xhr.status === 200) {
18
+ try {
19
+ var js = JSON.parse(xhr.responseText);
20
+ handler(js);
21
+ } catch (error) {
22
+ console.error(error);
23
+ errorHandler();
24
+ }
25
+ } else {
26
+ errorHandler();
27
+ }
28
+ }
29
+ };
30
+ var js = JSON.stringify(data);
31
+ xhr.send(js);
32
+ }
33
+
34
+ function pad2(x) {
35
+ return x < 10 ? '0' + x : x;
36
+ }
37
+
38
+ function formatTime(secs) {
39
+ if (secs > 3600) {
40
+ return pad2(Math.floor(secs / 60 / 60)) + ":" + pad2(Math.floor(secs / 60) % 60) + ":" + pad2(Math.floor(secs) % 60);
41
+ } else if (secs > 60) {
42
+ return pad2(Math.floor(secs / 60)) + ":" + pad2(Math.floor(secs) % 60);
43
+ } else {
44
+ return Math.floor(secs) + "s";
45
+ }
46
+ }
47
+
48
+ function setTitle(progress) {
49
+ var title = 'Unity';
50
+
51
+ if (opts.show_progress_in_title && progress) {
52
+ title = '[' + progress.trim() + '] ' + title;
53
+ lastTimeSuccess = getCurTimeInSeconds();
54
+ }
55
+ else{
56
+ var curTime = getCurTimeInSeconds();
57
+ if (lastTimeSuccess !== 0 && curTime - lastTimeSuccess >= 150){
58
+ var durationSinceLastSuccess = curTime - lastTimeSuccess;
59
+ title = 'warning ' + durationSinceLastSuccess + 's';
60
+ }
61
+ }
62
+ if (document.title != title) {
63
+ document.title = title;
64
+ }
65
+ }
66
+
67
+ function getCurTimeInSeconds(){
68
+ return Math.floor(Date.now() / 1000);
69
+ }
70
+
71
+ function randomId() {
72
+ return "task(" + Math.random().toString(36).slice(2, 7) + Math.random().toString(36).slice(2, 7) + Math.random().toString(36).slice(2, 7) + ")";
73
+ }
74
+
75
+ // starts sending progress requests to "/internal/progress" uri, creating progressbar above progressbarContainer element and
76
+ // preview inside gallery element. Cleans up all created stuff when the task is over and calls atEnd.
77
+ // calls onProgress every time there is a progress update
78
+ function requestProgress(id_task, progressbarContainer, gallery, atEnd, onProgress, inactivityTimeout = 40) {
79
+ var dateStart = new Date();
80
+ var wasEverActive = false;
81
+ var parentProgressbar = progressbarContainer.parentNode;
82
+
83
+ var divProgress = document.createElement('div');
84
+ divProgress.className = 'progressDiv';
85
+ divProgress.style.display = opts.show_progressbar ? "block" : "none";
86
+ var divInner = document.createElement('div');
87
+ divInner.className = 'progress';
88
+
89
+ divProgress.appendChild(divInner);
90
+ parentProgressbar.insertBefore(divProgress, progressbarContainer);
91
+
92
+ var livePreview = null;
93
+
94
+ var removeProgressBar = function() {
95
+ if (!divProgress) return;
96
+
97
+ setTitle("");
98
+ parentProgressbar.removeChild(divProgress);
99
+ if (gallery && livePreview) gallery.removeChild(livePreview);
100
+ atEnd();
101
+
102
+ divProgress = null;
103
+ };
104
+
105
+ var funProgress = function(id_task) {
106
+ request("./internal/progress", {id_task: id_task, live_preview: false}, function(res) {
107
+ if (res.completed) {
108
+ removeProgressBar();
109
+ return;
110
+ }
111
+
112
+ let progressText = "";
113
+
114
+ divInner.style.width = ((res.progress || 0) * 100.0) + '%';
115
+ divInner.style.background = res.progress ? "" : "transparent";
116
+
117
+ if (res.progress > 0) {
118
+ progressText = ((res.progress || 0) * 100.0).toFixed(0) + '%';
119
+ }
120
+
121
+ if (res.eta) {
122
+ progressText += " ETA: " + formatTime(res.eta);
123
+ }
124
+
125
+ setTitle(progressText);
126
+
127
+ if (res.textinfo && res.textinfo.indexOf("\n") == -1) {
128
+ progressText = res.textinfo + " " + progressText;
129
+ }
130
+
131
+ divInner.textContent = progressText;
132
+
133
+ var elapsedFromStart = (new Date() - dateStart) / 1000;
134
+
135
+ if (res.active) wasEverActive = true;
136
+
137
+ if (!res.active && wasEverActive) {
138
+ removeProgressBar();
139
+ return;
140
+ }
141
+
142
+ if (elapsedFromStart > inactivityTimeout && !res.queued && !res.active) {
143
+ removeProgressBar();
144
+ return;
145
+ }
146
+
147
+ if (onProgress) {
148
+ onProgress(res);
149
+ }
150
+
151
+ setTimeout(() => {
152
+ funProgress(id_task, res.id_live_preview);
153
+ }, opts.live_preview_refresh_period || 500);
154
+ }, function() {
155
+ removeProgressBar();
156
+ });
157
+ };
158
+
159
+ var funLivePreview = function(id_task, id_live_preview) {
160
+ request("./internal/progress", {id_task: id_task, id_live_preview: id_live_preview}, function(res) {
161
+ if (!divProgress) {
162
+ return;
163
+ }
164
+
165
+ if (res.live_preview && gallery) {
166
+ var img = new Image();
167
+ img.onload = function() {
168
+ if (!livePreview) {
169
+ livePreview = document.createElement('div');
170
+ livePreview.className = 'livePreview';
171
+ gallery.insertBefore(livePreview, gallery.firstElementChild);
172
+ }
173
+
174
+ livePreview.appendChild(img);
175
+ if (livePreview.childElementCount > 2) {
176
+ livePreview.removeChild(livePreview.firstElementChild);
177
+ }
178
+ };
179
+ img.src = res.live_preview;
180
+ }
181
+
182
+ setTimeout(() => {
183
+ funLivePreview(id_task, res.id_live_preview);
184
+ }, opts.live_preview_refresh_period || 500);
185
+ }, function() {
186
+ removeProgressBar();
187
+ });
188
+ };
189
+
190
+ funProgress(id_task, 0);
191
+
192
+ if (gallery) {
193
+ funLivePreview(id_task, 0);
194
+ }
195
+
196
+ }