converted tag controller to ts

This commit is contained in:
Miodec 2022-02-19 22:03:06 +01:00
parent ba992a0632
commit a2ecc054d4
2 changed files with 15 additions and 13 deletions

View file

@ -1,11 +1,11 @@
import * as DB from "../db";
import * as ModesNotice from "../elements/modes-notice";
export function saveActiveToLocalStorage() {
let tags = [];
export function saveActiveToLocalStorage(): void {
const tags: string[] = [];
try {
DB.getSnapshot().tags.forEach((tag) => {
DB.getSnapshot().tags?.forEach((tag) => {
if (tag.active === true) {
tags.push(tag._id);
}
@ -21,10 +21,10 @@ export function saveActiveToLocalStorage() {
} catch (e) {}
}
export function clear(nosave = false) {
export function clear(nosave = false): void {
const snapshot = DB.getSnapshot();
snapshot.tags = snapshot.tags.map((tag) => {
snapshot.tags = snapshot.tags?.map((tag) => {
tag.active = false;
return tag;
@ -35,10 +35,10 @@ export function clear(nosave = false) {
if (!nosave) saveActiveToLocalStorage();
}
export function set(tagid, state, nosave = false) {
export function set(tagid: string, state: boolean, nosave = false): void {
const snapshot = DB.getSnapshot();
snapshot.tags = snapshot.tags.map((tag) => {
snapshot.tags = snapshot.tags?.map((tag) => {
if (tag._id === tagid) {
tag.active = state;
}
@ -51,8 +51,8 @@ export function set(tagid, state, nosave = false) {
if (!nosave) saveActiveToLocalStorage();
}
export function toggle(tagid, nosave = false) {
DB.getSnapshot().tags.forEach((tag) => {
export function toggle(tagid: string, nosave = false): void {
DB.getSnapshot().tags?.forEach((tag) => {
if (tag._id === tagid) {
if (tag.active === undefined) {
tag.active = true;
@ -65,16 +65,18 @@ export function toggle(tagid, nosave = false) {
if (!nosave) saveActiveToLocalStorage();
}
export function loadActiveFromLocalStorage() {
export function loadActiveFromLocalStorage(): void {
// let newTags = $.cookie("activeTags");
let newTags = window.localStorage.getItem("activeTags");
let newTags: string[] | string = window.localStorage.getItem(
"activeTags"
) as string;
if (newTags != undefined && newTags !== "") {
try {
newTags = JSON.parse(newTags) ?? [];
} catch (e) {
newTags = [];
}
newTags.forEach((ntag) => {
(newTags as string[]).forEach((ntag) => {
toggle(ntag, true);
});
saveActiveToLocalStorage();

View file

@ -835,7 +835,7 @@ $(document).on(
".pageSettings .section.tags .tagsList .tag .tagButton",
(e) => {
const target = e.currentTarget;
const tagid = $(target).parent(".tag").attr("id");
const tagid = $(target).parent(".tag").attr("id") as string;
TagController.toggle(tagid);
$(target).toggleClass("active");
}