Allows sorting items by last used

This commit is contained in:
Alicia Sykes 2021-09-05 01:00:44 +01:00
parent 955985071f
commit 11f5f8c9df
3 changed files with 20 additions and 1 deletions

View File

@ -107,7 +107,9 @@ export default {
} else {
this.$emit('itemClicked');
}
// Update the most/ last used ledger, for smart-sorting
this.incrementMostUsedCount(this.id);
this.incrementLastUsedCount(this.id);
},
/* Open custom context menu, and set position */
openContextMenu(e) {
@ -200,7 +202,7 @@ export default {
default: window.open(url, '_blank');
}
},
/* Used for smart-sort when sorting items by most/ last used */
/* Used for smart-sort when sorting items by most used apps */
incrementMostUsedCount(itemId) {
const mostUsed = JSON.parse(localStorage.getItem(localStorageKeys.MOST_USED) || '{}');
let counter = mostUsed[itemId] || 0;
@ -208,6 +210,12 @@ export default {
mostUsed[itemId] = counter;
localStorage.setItem(localStorageKeys.MOST_USED, JSON.stringify(mostUsed));
},
/* Used for smart-sort when sorting by last used apps */
incrementLastUsedCount(itemId) {
const lastUsed = JSON.parse(localStorage.getItem(localStorageKeys.LAST_USED) || '{}');
lastUsed[itemId] = new Date().getTime();
localStorage.setItem(localStorageKeys.LAST_USED, JSON.stringify(lastUsed));
},
},
mounted() {
// If ststus checking is enabled, then check service status

View File

@ -75,6 +75,7 @@ export default {
sortOrder() {
return this.displayData.sortBy || defaultSortOrder;
},
/* If the sortBy attribute is specified, then return sorted data */
sortedItems() {
let { items } = this;
if (this.sortOrder === 'alphabetical') {
@ -83,6 +84,8 @@ export default {
items.sort((a, b) => (a.title < b.title ? 1 : -1));
} else if (this.sortOrder === 'most-used') {
items = this.sortByMostUsed(items);
} else if (this.sortOrder === 'last-used') {
items = this.sortBLastUsed(items);
}
return items;
},
@ -134,6 +137,13 @@ export default {
items.reverse().sort((a, b) => (gmu(a) < gmu(b) ? 1 : -1));
return items;
},
/* Sorts items by most recently used */
sortBLastUsed(items) {
const usageCount = JSON.parse(localStorage.getItem(localStorageKeys.LAST_USED) || '{}');
const glu = (item) => usageCount[this.makeId(item.title)] || 0;
items.reverse().sort((a, b) => (glu(a) < glu(b) ? 1 : -1));
return items;
},
},
};
</script>

View File

@ -94,6 +94,7 @@ module.exports = {
HIDE_SETTINGS: 'hideSettings',
USERNAME: 'username',
MOST_USED: 'mostUsed',
LAST_USED: 'lastUsed',
},
/* Key names for cookie identifiers */
cookieKeys: {