techtools
Головна / portal / assets / portal.js

portal.js

JavaScript · 63 рядків

1// Посилання на сервіси: з *.techtools.top — на домени, інакше — той самий хост із портом сервісу. Індикатори стану, вкладки й фільтр досліджень головної.
2(function () {
3  var onDomain = /(^|\.)techtools\.top$/.test(location.hostname)
4  document.querySelectorAll('a[data-port]').forEach(function (a) {
5    var d = a.dataset.domain, p = a.dataset.port, path = a.dataset.path || ''
6    if (onDomain && d) a.href = location.protocol + '//' + d + '/' + path
7    else if (onDomain) a.href = 'http://' + a.dataset.lan + ':' + p + '/' + path
8    else a.href = location.protocol + '//' + location.hostname + ':' + p + '/' + path
9  })
10
11  var tabs = document.querySelectorAll('section.tab')
12  if (tabs.length) {
13    document.documentElement.classList.add('js')
14    var show = function () {
15      var id = location.hash.slice(1)
16      if (!document.getElementById(id) || !document.getElementById(id).classList.contains('tab')) id = tabs[0].id
17      tabs.forEach(function (t) { t.classList.toggle('active', t.id === id) })
18      document.querySelectorAll('.tabs a').forEach(function (a) {
19        if (a.getAttribute('href') === '#' + id) a.setAttribute('aria-current', 'page'); else a.removeAttribute('aria-current')
20      })
21    }
22    window.addEventListener('hashchange', show)
23    show()
24  }
25
26  document.querySelectorAll('.filters button').forEach(function (b, _, all) {
27    b.onclick = function () {
28      all.forEach(function (x) { x.setAttribute('aria-pressed', x === b ? 'true' : 'false') })
29      var f = b.dataset.filter
30      document.querySelectorAll('.research-table tbody tr').forEach(function (tr) {
31        var arch = tr.dataset.archived === '1'
32        tr.classList.toggle('hidden', f === 'archive' ? !arch : arch || (f !== 'all' && tr.dataset.kind !== f))
33      })
34    }
35  })
36
37  // Колонка «Стан» у таблицях — колір за словом (відкрите, прийнято, замінено…)
38  document.querySelectorAll('article.md table').forEach(function (t) {
39    var col = Array.prototype.findIndex.call(t.querySelectorAll('thead th'), function (th) { return th.textContent.trim() === 'Стан' })
40    if (col < 0) return
41    t.querySelectorAll('tbody tr').forEach(function (tr) {
42      var td = tr.children[col]; if (!td) return
43      var v = td.textContent.trim().toLowerCase()
44      var k = /^(замінено|спростовано)/.test(v) ? 'off' : /^не підтверджено/.test(v) ? 'unconfirmed'
45        : /^(відкрите|треба|в роботі|уточнити)/.test(v) ? 'open' : /^(прийнято|закрите|зроблено|підтверджено|у doc|відповідь є)/.test(v) ? 'done' : ''
46      td.classList.add('st'); if (k) td.classList.add('st-' + k)
47    })
48  })
49
50  var dots = document.querySelectorAll('[data-status]')
51  if (!dots.length) return
52  fetch('/api/status').then(function (r) { return r.json() }).then(function (st) {
53    dots.forEach(function (el) {
54      var ok = st[el.dataset.status]
55      el.classList.add(ok ? 'up' : 'down')
56      el.title = ok ? 'Працює' : 'Не запущено'
57    })
58    var mine = document.querySelectorAll('#stand [data-status]'), up = document.querySelectorAll('#stand .dot.up').length
59    var c = document.getElementById('stand-count')
60    if (c && mine.length) { c.textContent = up + '/' + mine.length; c.title = 'Працює ' + up + ' з ' + mine.length }
61  }).catch(function () {})
62})()
63