Generate and append a unique ID to each item

This commit is contained in:
Alicia Sykes 2021-10-16 16:41:10 +01:00
parent 8c3a8e996f
commit 7a817c17d4
3 changed files with 35 additions and 14 deletions

View File

@ -17,9 +17,9 @@
:style="gridStyle" :style="gridStyle"
> >
<Item <Item
v-for="(item, index) in sortedItems" v-for="(item) in sortedItems"
:id="makeId(title, item.title, index)" :id="item.id"
:key="makeId(title, item.title, index)" :key="item.id"
:url="item.url" :url="item.url"
:title="item.title" :title="item.title"
:description="item.description" :description="item.description"
@ -115,12 +115,6 @@ export default {
}, },
}, },
methods: { methods: {
/* Returns a unique lowercase string, based on name, for section ID */
makeId(sectionStr, itemStr, index) {
const charSum = sectionStr.split('').map((a) => a.charCodeAt(0)).reduce((x, y) => x + y);
const itemTitleStr = itemStr.replace(/\s+/g, '-').replace(/[^a-zA-Z ]/g, '').toLowerCase();
return `${index}_${charSum}_${itemTitleStr}`;
},
/* Opens the iframe modal */ /* Opens the iframe modal */
triggerModal(url) { triggerModal(url) {
this.$refs[`iframeModal-${this.groupId}`].show(url); this.$refs[`iframeModal-${this.groupId}`].show(url);
@ -145,14 +139,14 @@ export default {
/* Sorts items by most used to least used, based on click-count */ /* Sorts items by most used to least used, based on click-count */
sortByMostUsed(items) { sortByMostUsed(items) {
const usageCount = JSON.parse(localStorage.getItem(localStorageKeys.MOST_USED) || '{}'); const usageCount = JSON.parse(localStorage.getItem(localStorageKeys.MOST_USED) || '{}');
const gmu = (item) => usageCount[this.makeId(this.title, item.title)] || 0; const gmu = (item) => usageCount[item.id] || 0;
items.reverse().sort((a, b) => (gmu(a) < gmu(b) ? 1 : -1)); items.reverse().sort((a, b) => (gmu(a) < gmu(b) ? 1 : -1));
return items; return items;
}, },
/* Sorts items by most recently used */ /* Sorts items by most recently used */
sortBLastUsed(items) { sortBLastUsed(items) {
const usageCount = JSON.parse(localStorage.getItem(localStorageKeys.LAST_USED) || '{}'); const usageCount = JSON.parse(localStorage.getItem(localStorageKeys.LAST_USED) || '{}');
const glu = (item) => usageCount[this.makeId(this.title, item.title)] || 0; const glu = (item) => usageCount[item.id] || 0;
items.reverse().sort((a, b) => (glu(a) < glu(b) ? 1 : -1)); items.reverse().sort((a, b) => (glu(a) < glu(b) ? 1 : -1));
return items; return items;
}, },

View File

@ -13,6 +13,7 @@ import {
layout as defaultLayout, layout as defaultLayout,
} from '@/utils/defaults'; } from '@/utils/defaults';
import ErrorHandler from '@/utils/ErrorHandler'; import ErrorHandler from '@/utils/ErrorHandler';
import { applyItemId } from '@/utils/MiscHelpers';
import conf from '../../public/conf.yml'; import conf from '../../public/conf.yml';
export default class ConfigAccumulator { export default class ConfigAccumulator {
@ -57,18 +58,24 @@ export default class ConfigAccumulator {
/* Sections */ /* Sections */
sections() { sections() {
let sections = [];
// If the user has stored sections in local storage, return those // If the user has stored sections in local storage, return those
const localSections = localStorage[localStorageKeys.CONF_SECTIONS]; const localSections = localStorage[localStorageKeys.CONF_SECTIONS];
if (localSections) { if (localSections) {
try { try {
const json = JSON.parse(localSections); const json = JSON.parse(localSections);
if (json.length >= 1) return json; if (json.length >= 1) sections = json;
} catch (e) { } catch (e) {
ErrorHandler('Malformed section data in local storage'); ErrorHandler('Malformed section data in local storage');
} }
} }
// If the function hasn't yet returned, then return the config file sections // If sections were not set from local data, then use config file instead
return this.conf ? this.conf.sections || [] : []; if (sections.length === 0) {
sections = this.conf ? this.conf.sections || [] : [];
}
// Apply a unique ID to each item
sections = applyItemId(sections);
return sections;
} }
/* Complete config */ /* Complete config */

View File

@ -25,3 +25,23 @@ export const sanitize = (string) => {
const reg = /[&<>"'/]/ig; const reg = /[&<>"'/]/ig;
return string.replace(reg, (match) => (map[match])); return string.replace(reg, (match) => (map[match]));
}; };
/* Based on section title, item name and index, return a string value for ID */
const makeItemId = (sectionStr, itemStr, index) => {
const charSum = sectionStr.split('').map((a) => a.charCodeAt(0)).reduce((x, y) => x + y);
const itemTitleStr = itemStr.replace(/\s+/g, '-').replace(/[^a-zA-Z ]/g, '').toLowerCase();
return `${index}_${charSum}_${itemTitleStr}`;
};
/* Given an array of sections, apply a unique ID to each item, and return modified array */
export const applyItemId = (inputSections) => {
const sections = inputSections || [];
sections.forEach((sec, secIdx) => {
if (sec.items) {
sec.items.forEach((item, itemIdx) => {
sections[secIdx].items[itemIdx].id = makeItemId(sec.name, item.title, itemIdx);
});
}
});
return sections;
};