From c86478ec47366dfb7081cc2eb2790a0af1880eca Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 25 Jun 2020 00:23:05 +0200 Subject: [UI] Sortable Tables Header By Click (#7980) * [UI] Sortable Tables Header By Click * get rid of padding above header * restart CI * fix lint * convert getArrow JS to SortArrow go func * addopt SortArrow funct * suggestions from @silverwind - tablesort.js Co-authored-by: silverwind * Update web_src/js/features/tablesort.js Co-authored-by: silverwind * Update web_src/js/features/tablesort.js Co-authored-by: silverwind Co-authored-by: silverwind --- web_src/js/features/tablesort.js | 20 ++++++++++++++++++++ web_src/js/index.js | 2 ++ 2 files changed, 22 insertions(+) create mode 100644 web_src/js/features/tablesort.js (limited to 'web_src/js') diff --git a/web_src/js/features/tablesort.js b/web_src/js/features/tablesort.js new file mode 100644 index 0000000000..17da2985a0 --- /dev/null +++ b/web_src/js/features/tablesort.js @@ -0,0 +1,20 @@ +export default function initTableSort() { + for (const header of document.querySelectorAll('th[data-sortt-asc]') || []) { + const {sorttAsc, sorttDesc, sorttDefault} = header.dataset; + header.addEventListener('click', () => { + tableSort(sorttAsc, sorttDesc, sorttDefault); + }); + } +} + +function tableSort(normSort, revSort, isDefault) { + if (!normSort) return false; + if (!revSort) revSort = ''; + + const url = new URL(window.location); + let urlSort = url.searchParams.get('sort'); + if (!urlSort && isDefault) urlSort = normSort; + + url.searchParams.set('sort', urlSort !== normSort ? normSort : revSort); + window.location.replace(url.href); +} diff --git a/web_src/js/index.js b/web_src/js/index.js index 6b435edd0f..37cb2a3988 100644 --- a/web_src/js/index.js +++ b/web_src/js/index.js @@ -15,6 +15,7 @@ import initUserHeatmap from './features/userheatmap.js'; import initServiceWorker from './features/serviceworker.js'; import attachTribute from './features/tribute.js'; import createDropzone from './features/dropzone.js'; +import initTableSort from './features/tablesort.js'; import highlight from './features/highlight.js'; import ActivityTopAuthors from './components/ActivityTopAuthors.vue'; import {initNotificationsTable, initNotificationCount} from './features/notification.js'; @@ -2450,6 +2451,7 @@ $(document).ready(async () => { initRepoStatusChecker(); initTemplateSearch(); initContextPopups(); + initTableSort(); initNotificationsTable(); initNotificationCount(); -- cgit v1.2.3