File size: 11,458 Bytes
e4fd6e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
(function () {
  function initUserMenu() {
    var menu = document.querySelector('.user-menu');
    var toggleButton = document.querySelector('[data-user-menu-toggle="true"]');
    var dropdown = document.getElementById('userMenuDropdown');

    if (!menu || !toggleButton || !dropdown) {
      return;
    }

    function closeMenu() {
      dropdown.classList.remove('active');
    }

    function toggleMenu(event) {
      if (event) {
        event.preventDefault();
        event.stopPropagation();
      }
      dropdown.classList.toggle('active');
    }

    toggleButton.addEventListener('click', toggleMenu);
    document.addEventListener('click', function (event) {
      if (!menu.contains(event.target)) {
        closeMenu();
      }
    });
  }

  function initPasswordModal() {
    var openButton = document.querySelector('.js-open-password-modal');
    var closeButtons = document.querySelectorAll('.js-close-password-modal');
    var modal = document.querySelector('.js-password-modal');
    var form = document.getElementById('changePasswordForm');
    var message = document.getElementById('passwordMessage');

    if (!openButton || !closeButtons.length || !modal || !form || !message) {
      return;
    }

    function openModal() {
      modal.style.display = 'block';
      form.reset();
      message.innerHTML = '';
    }

    function closeModal() {
      modal.style.display = 'none';
    }

    openButton.addEventListener('click', openModal);
    closeButtons.forEach(function (button) {
      button.addEventListener('click', closeModal);
    });

    document.addEventListener('click', function (event) {
      if (event.target === modal) {
        closeModal();
      }
    });

    form.addEventListener('submit', async function (event) {
      event.preventDefault();

      var currentPassword = document.getElementById('currentPassword').value;
      var newPassword = document.getElementById('newPassword').value;
      var confirmPassword = document.getElementById('confirmPassword').value;
      var endpoint = form.dataset.changePasswordUrl;

      if (newPassword !== confirmPassword) {
        message.innerHTML = '<div class="alert alert-error">Passwords do not match</div>';
        return;
      }

      try {
        var response = await fetch(endpoint, {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json'
          },
          body: JSON.stringify({
            current_password: currentPassword,
            new_password: newPassword,
            confirm_password: confirmPassword
          })
        });

        var data = await response.json();

        if (response.ok) {
          message.innerHTML = '<div class="alert alert-success">' + data.message + '</div>';
          setTimeout(closeModal, 2000);
        } else {
          message.innerHTML = '<div class="alert alert-error">' + (data.error || 'Unable to update password') + '</div>';
        }
      } catch (error) {
        message.innerHTML = '<div class="alert alert-error">An error occurred</div>';
      }
    });
  }

  function initUploadPage() {
    var tabs = document.querySelectorAll('.upload-tab');
    var panels = document.querySelectorAll('.tab-panel');

    if (!tabs.length || !panels.length) {
      return;
    }

    tabs.forEach(function (tab) {
      tab.addEventListener('click', function () {
        tabs.forEach(function (item) {
          item.classList.remove('active');
        });
        panels.forEach(function (panel) {
          panel.classList.remove('active');
        });

        tab.classList.add('active');
        var target = document.getElementById('tab-' + tab.dataset.tab);
        if (target) {
          target.classList.add('active');
        }
      });
    });

    function wireDropzone(options) {
      var zone = document.getElementById(options.zoneId);
      var input = document.getElementById(options.inputId);
      var info = document.getElementById(options.infoId);
      var label = document.getElementById(options.labelId);
      var clearButton = document.querySelector(options.clearSel);
      var submit = document.getElementById(options.submitId);
      var form = document.getElementById(options.formId);
      var overlay = document.getElementById(options.overlayId);

      if (!zone || !input || !info || !label || !submit) {
        return;
      }

      function showFiles(files) {
        var validFiles = [];
        for (var i = 0; i < files.length; i++) {
          var name = files[i].name.toLowerCase();
          if (name.endsWith('.dcm') || name.endsWith('.zip')) {
            validFiles.push(files[i]);
          }
        }

        if (!validFiles.length) {
          return;
        }

        if (options.multi) {
          var totalSizeMB = 0;
          for (var j = 0; j < validFiles.length; j++) {
            totalSizeMB += validFiles[j].size / (1024 * 1024);
          }
          label.textContent = validFiles.length + ' file' + (validFiles.length > 1 ? 's' : '') + ' (' + totalSizeMB.toFixed(1) + ' MB)';
        } else {
          label.textContent = validFiles[0].name;
        }

        info.style.display = 'flex';
        zone.style.display = 'none';
        submit.disabled = false;
      }

      function reset() {
        input.value = '';
        info.style.display = 'none';
        zone.style.display = 'flex';
        submit.disabled = true;
      }

      zone.addEventListener('click', function () {
        input.click();
      });

      zone.addEventListener('dragover', function (event) {
        event.preventDefault();
        zone.classList.add('dragover');
      });

      zone.addEventListener('dragleave', function () {
        zone.classList.remove('dragover');
      });

      zone.addEventListener('drop', function (event) {
        event.preventDefault();
        zone.classList.remove('dragover');
        if (event.dataTransfer.files.length) {
          input.files = event.dataTransfer.files;
          showFiles(event.dataTransfer.files);
        }
      });

      input.addEventListener('change', function () {
        if (input.files.length) {
          showFiles(input.files);
        }
      });

      if (clearButton) {
        clearButton.addEventListener('click', reset);
      }

      if (form && overlay) {
        form.addEventListener('submit', function () {
          overlay.style.display = 'flex';
          submit.disabled = true;
        });
      }
    }

    wireDropzone({
      zoneId: 'dropzoneSingle',
      inputId: 'singleInput',
      infoId: 'singleInfo',
      labelId: 'singleFileName',
      clearSel: '.js-clear-single',
      submitId: 'singleSubmit',
      formId: 'singleForm',
      overlayId: 'singleOverlay',
      multi: false
    });

    wireDropzone({
      zoneId: 'dropzoneMulti',
      inputId: 'multiInput',
      infoId: 'multiInfo',
      labelId: 'multiFileName',
      clearSel: '.js-clear-multi',
      submitId: 'multiSubmit',
      formId: 'multiForm',
      overlayId: 'multiOverlay',
      multi: true
    });

    var dirInput = document.getElementById('dirPath');
    var dirSubmit = document.getElementById('dirSubmit');

    if (dirInput && dirSubmit) {
      function checkDir() {
        dirSubmit.disabled = !dirInput.value.trim();
      }

      dirInput.addEventListener('input', checkDir);
      checkDir();
    }
  }

  function initBatchProgress() {
    var page = document.querySelector('.batch-page');

    if (!page) {
      return;
    }

    var statusUrl = page.dataset.statusUrl;
    var reportsUrl = page.dataset.reportsUrl;
    var pollMs = 1000;

    var title = document.getElementById('batchTitle');
    var subtitle = document.getElementById('batchSubtitle');
    var fill = document.getElementById('progressFill');
    var pctLabel = document.getElementById('progressPct');
    var currentFile = document.getElementById('currentFile');
    var statTotal = document.getElementById('statTotal');
    var statProc = document.getElementById('statProcessed');
    var statOK = document.getElementById('statSucceeded');
    var statFail = document.getElementById('statFailed');
    var feedPanel = document.getElementById('feedPanel');
    var feedList = document.getElementById('batchFeed');
    var donePanel = document.getElementById('donePanel');
    var doneSummary = document.getElementById('doneSummary');
    var failPanel = document.getElementById('failPanel');
    var failList = document.getElementById('failList');
    var prevIds = [];

    if (!statusUrl || !title || !subtitle || !fill || !pctLabel || !currentFile || !statTotal || !statProc || !statOK || !statFail || !feedPanel || !feedList || !donePanel || !doneSummary || !failPanel || !failList) {
      return;
    }

    function poll() {
      fetch(statusUrl)
        .then(function (response) {
          return response.json();
        })
        .then(function (data) {
          var pct = data.total > 0 ? Math.round(data.processed / data.total * 100) : 0;

          statTotal.textContent = data.total;
          statProc.textContent = data.processed;
          statOK.textContent = data.succeeded;
          statFail.textContent = data.failed_count;

          fill.style.width = pct + '%';
          pctLabel.textContent = pct + '%';

          if (data.current_file) {
            currentFile.textContent = 'Processing: ' + data.current_file;
          } else {
            currentFile.textContent = '';
          }

          if (data.image_ids && data.image_ids.length) {
            feedPanel.style.display = 'block';
            data.image_ids.forEach(function (imageId) {
              if (prevIds.indexOf(imageId) === -1) {
                prevIds.push(imageId);
                var li = document.createElement('li');
                var link = document.createElement('a');
                link.href = '/case/' + imageId;
                link.textContent = imageId;
                li.appendChild(link);
                feedList.insertBefore(li, feedList.firstChild);
                while (feedList.children.length > 20) {
                  feedList.removeChild(feedList.lastChild);
                }
              }
            });
          }

          if (data.status === 'completed' || data.status === 'failed') {
            title.textContent = 'Batch Complete';
            subtitle.textContent = '';
            donePanel.style.display = 'block';
            doneSummary.textContent = data.succeeded + ' of ' + data.total + ' files processed successfully' + (data.failed_count > 0 ? ', ' + data.failed_count + ' failed' : '') + '.';

            if (data.failed_ids && data.failed_ids.length) {
              failPanel.style.display = 'block';
              data.failed_ids.forEach(function (failedId) {
                var li = document.createElement('li');
                li.textContent = failedId;
                failList.appendChild(li);
              });
            }

            if (reportsUrl) {
              return;
            }
            return;
          }

          setTimeout(poll, pollMs);
        })
        .catch(function () {
          setTimeout(poll, pollMs * 3);
        });
    }

    poll();
  }

  function initPages() {
    initUserMenu();
    initPasswordModal();
    initUploadPage();
    initBatchProgress();
  }

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', initPages);
  } else {
    initPages();
  }
})();