From c69ca372ee1d8f08316212c9b0174bea106bf7ff Mon Sep 17 00:00:00 2001 From: j Date: Sun, 13 Oct 2024 17:06:15 +0100 Subject: [PATCH] sort annotations --- static/mobile/js/item.js | 1 + static/mobile/js/utils.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/static/mobile/js/item.js b/static/mobile/js/item.js index a9942536..7f81fa23 100644 --- a/static/mobile/js/item.js +++ b/static/mobile/js/item.js @@ -129,6 +129,7 @@ async function loadData(id, args) { ${icon.down} ${layerData.title} `) + data.layers[layer] = sortBy(data.layers[layer], ["+in", "+created"]) data.layers[layer].forEach(annotation => { if (pandora.url) { annotation.value = annotation.value.replace( diff --git a/static/mobile/js/utils.js b/static/mobile/js/utils.js index cbaec6a6..860db698 100644 --- a/static/mobile/js/utils.js +++ b/static/mobile/js/utils.js @@ -161,3 +161,33 @@ const getVideoURL = function(id, resolution, part, track, streamId) { return prefix + '/' + getVideoURLName(id, resolution, part, track, streamId); }; +function getSortValue(value) { + var getSortValue = Ox.cache(function getSortValue(value) { + var sortValue = value; + return sortValue; +} + +function sortBy(array, by, map) { + return array.sort(function(a, b) { + var aValue, bValue, index = 0, key, ret = 0; + while (ret == 0 && index < by.length) { + key = by[index].key; + aValue = getSortValue( + map[key] ? map[key](a[key], a) : a[key] + ); + bValue = getSortValue( + map[key] ? map[key](b[key], b) : b[key] + ); + if ((aValue === null) != (bValue === null)) { + ret = aValue === null ? 1 : -1; + } else if (aValue < bValue) { + ret = by[index].operator == '+' ? -1 : 1; + } else if (aValue > bValue) { + ret = by[index].operator == '+' ? 1 : -1; + } else { + index++; + } + } + return ret; + }); +}