aboutsummaryrefslogtreecommitdiffstats
path: root/web_src/js/features/repo-commit.js
blob: 7e2f6fa58ead2f39846cb39905b39b1c087f147c (plain)
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
import {createTippy} from '../modules/tippy.js';
import {toggleElem} from '../utils/dom.js';
import {parseDom} from '../utils.js';
import {POST} from '../modules/fetch.js';

export function initRepoEllipsisButton() {
  for (const button of document.querySelectorAll('.js-toggle-commit-body')) {
    button.addEventListener('click', function (e) {
      e.preventDefault();
      const expanded = this.getAttribute('aria-expanded') === 'true';
      toggleElem(this.parentElement.querySelector('.commit-body'));
      this.setAttribute('aria-expanded', String(!expanded));
    });
  }
}

export async function initRepoCommitLastCommitLoader() {
  const entryMap = {};

  const entries = Array.from(document.querySelectorAll('table#repo-files-table tr.notready'), (el) => {
    const entryName = el.getAttribute('data-entryname');
    entryMap[entryName] = el;
    return entryName;
  });

  if (entries.length === 0) {
    return;
  }

  const lastCommitLoaderURL = document.querySelector('table#repo-files-table').getAttribute('data-last-commit-loader-url');

  if (entries.length > 200) {
    // For more than 200 entries, replace the entire table
    const response = await POST(lastCommitLoaderURL);
    const data = await response.text();
    document.querySelector('table#repo-files-table').outerHTML = data;
    return;
  }

  // For fewer entries, update individual rows
  const response = await POST(lastCommitLoaderURL, {data: {'f': entries}});
  const data = await response.text();
  const doc = parseDom(data, 'text/html');
  for (const row of doc.querySelectorAll('tr')) {
    if (row.className === 'commit-list') {
      document.querySelector('table#repo-files-table .commit-list')?.replaceWith(row);
      continue;
    }
    // there are other <tr> rows in response (eg: <tr class="has-parent">)
    // at the moment only the "data-entryname" rows should be processed
    const entryName = row.getAttribute('data-entryname');
    if (entryName) {
      entryMap[entryName]?.replaceWith(row);
    }
  }
}

export function initCommitStatuses() {
  for (const element of document.querySelectorAll('[data-tippy="commit-statuses"]')) {
    const top = document.querySelector('.repository.file.list') || document.querySelector('.repository.diff');

    createTippy(element, {
      content: element.nextElementSibling,
      placement: top ? 'top-start' : 'bottom-start',
      interactive: true,
      role: 'dialog',
      theme: 'box-with-header',
    });
  }
}