<commit_before>/* ENUM SortCriteria noteList */
import compile = Handlebars.compile;
enum SortCriteria {
    id,
    dueDate,
    creationDate,
    importance,
}
/* ENUM FilterCriteria notelist */
enum FilterCriteria {
    id,
    noteActive,
    noteHighImportance,
    noteMediumImportance,
    noteLowImportance
}

/* Handlebars Helper */

/* Rate transformiert den Score (Ausprägung des Ratings) in HTML
 Der gültige Wertebereich beträgt 0 bis BESTRATING.
 Wird ein ungüliger Wert übergeben, wird die class error-sign generiert*/

Handlebars.registerHelper('rate', function (rating:number) {
    var RatingHTML:string = "";
    const BESTRATING:number = 5;
    if (rating >= 0 && rating <= BESTRATING) {
        for (let i = 1; i <= BESTRATING; i++) {
            if (rating >= i) {
                RatingHTML = RatingHTML + '<span class="scored-sign">&nbsp;</span>';
            } else {
                RatingHTML = RatingHTML + '<span class="unscored-sign">&nbsp;</span>';
            }
        }
    }
    else {
        const ERRORSIGNSPAN:string = '<span class="error-sign">&nbsp;</span>';
        for (let i = 1; i <= BESTRATING; i++) {
            RatingHTML = RatingHTML + ERRORSIGNSPAN;
        }
    }

    return new Handlebars.SafeString(RatingHTML);
});

/* ToDo: insert Prefix */
Handlebars.registerHelper('showdate', function (date:string, prefixText:string) {
    const DATEFORMAT:string = "LL";
    let outDate:string = "";
    if (moment(date).isValid()) {
        outDate = moment(date).format(DATEFORMAT);
    }
    else {
        outDate = "";
        console.log("Invalid Date: " + moment(date));
    }

    return new Handlebars.SafeString(outDate);
});

/* Notelistview Classes */
class Notelistview {
    noteListTemplateHTML:HandlebarsTemplateDelegate;

    constructor() {
        this.compile();
    }

    render(notelist:INote[]):void {
        let context = {
            notes: notelist
        };
        let notesHtml:string = this.noteListTemplateHTML(context);
        document.getElementById("notelist").innerHTML = notesHtml;
    }

    private compile():void {
        this.noteListTemplateHTML = Handlebars.compile(document.getElementById("notes-template").innerText);
    }

}
class NotelistController extends NoteController{
    notelist:INote[];
    notelistview:Notelistview;
    noteservice:NoteService;
    noteStorageService:NoteStorageService;

    /* HTMLSelectElement greift auf das Interface von HTMLElement zurück */
    /* Aktives Filter- und Sortierkriterium über Listboxen
     Default ist das Item, das ausgewählt wurde */
    /* ToDo: Services im constructor übergeben Hinweis Michael */
    constructor() {
        super(HREF_PREFIX_STYLE);
        this.noteservice = new NoteService();
        this.noteStorageService = new NoteStorageService();
        this.notelist = this.noteStorageService.noteList;
        this.notelistview = new Notelistview();
        this.notelistview.render(this.notelist);
        this.registerBtnEdit();
        this.registerBtnNoteNew();
        this.registerCBFinished();
        this.registerListboxSorter();
        this.registerListboxFilter();
    };

    registerCBFinished():void {
        $(":checkbox").change(function () {
            let  id = $(this).parent().attr("id");
            let finishedDate = $(this).is(':checked') ? new Date().toJSON() : " ";
            let note:INote = new Note();
            note.id = Number(id);
            new NoteStorageService().updateNote(note, finishedDate);
        })
    }

    registerBtnEdit():void {
        $(":button").on('click', function () {
            let id = $(this).parent().attr("id");
            NotelistController.changLocation(Number(id));
        });
    }

    registerBtnNoteNew():void {
        let el:HTMLElement = document.getElementById("btnNoteNew");
        el.addEventListener('click', this.createNewNote.bind(this));
    }

    registerListboxSorter():void {
        let el:HTMLElement = document.getElementById("ddlb_sorterselect");
        el.addEventListener('change', this.sort.bind(this));
    }

    registerListboxFilter():void {
        let el:HTMLElement = document.getElementById("ddlb_filterselect");
        el.addEventListener('change', this.filter.bind(this));

    }

    createNewNote(event:Event):void {
        let nextID:number = 1;
        console.log("NewNote", Event);

        nextID = this.noteStorageService.getNextId();
        if (typeof nextID === "number") {
            /* notedetail Editor mit neuer ID aufrufen */
            NotelistController.changLocation(nextID);
        }
        else {
            console.log("Error:CreateNewNote: Wrong ID", nextID);
        }
    }

    private static changLocation(id:number):void {
        window.location.replace("notedetail\\notedetail.html?id=" + id);
    }

    sort(event:Event):void {
        /* found no type for event.target therefore any as type */
        let target:any = event.target;
        let SelectedSortOption:string = target.value;
        switch (SelectedSortOption) {
            case "id":
                this.noteservice.sortBy(this.notelist, SortCriteria.id);
                break;
            case "due-date":
                this.noteservice.sortBy(this.notelist, SortCriteria.dueDate);
                break;
            case "creation-date":
                this.noteservice.sortBy(this.notelist, SortCriteria.creationDate);
                break;
            case "importance":
                this.noteservice.sortBy(this.notelist, SortCriteria.importance);
                break;
            default:
                console.log("Switch SelectedSortOption: default");
                break;
        }
        this.notelistview.render(this.notelist);
        console.log("change LBSort");
    }

    filter(event:Event):void {
        let target:any = event.target;
        let SelectedSortOption:string = target.value;
        /* Noteliste mit allen Elementen initialisieren   */
        this.notelist = this.noteservice.getNotesfromStorage();
        switch (SelectedSortOption) {
            case "id":
                /* Kein Filter */
                break;
            case "note-active":<commit_msg>hbs helper showdate now with prefixText<commit_after>/* ENUM SortCriteria noteList */
import compile = Handlebars.compile;
enum SortCriteria {
    id,
    dueDate,
    creationDate,
    importance,
}
/* ENUM FilterCriteria notelist */
enum FilterCriteria {
    id,
    noteActive,
    noteHighImportance,
    noteMediumImportance,
    noteLowImportance
}

/* Handlebars Helper */

/* Rate transformiert den Score (Ausprägung des Ratings) in HTML
 Der gültige Wertebereich beträgt 0 bis BESTRATING.
 Wird ein ungüliger Wert übergeben, wird die class error-sign generiert*/

Handlebars.registerHelper('rate', function (rating:number) {
    var RatingHTML:string = "";
    const BESTRATING:number = 5;
    if (rating >= 0 && rating <= BESTRATING) {
        for (let i = 1; i <= BESTRATING; i++) {
            if (rating >= i) {
                RatingHTML = RatingHTML + '<span class="scored-sign">&nbsp;</span>';
            } else {
                RatingHTML = RatingHTML + '<span class="unscored-sign">&nbsp;</span>';
            }
        }
    }
    else {
        const ERRORSIGNSPAN:string = '<span class="error-sign">&nbsp;</span>';
        for (let i = 1; i <= BESTRATING; i++) {
            RatingHTML = RatingHTML + ERRORSIGNSPAN;
        }
    }

    return new Handlebars.SafeString(RatingHTML);
});
/* Showdate zeigt ein Datum der Note mit einem Präfix an
 *  Mit dem Präfix kann eine Beschreibung des Datums mitgegeben werden*/

Handlebars.registerHelper('showdate', function (prefixText:string, date:string) {
    const DATEFORMAT:string = "LL";
    let outDate:string = "";
    if (!(typeof(prefixText) === "string")) {
        prefixText = "";
    }
    if (moment(date).isValid()) {
        outDate = prefixText + moment(date).format(DATEFORMAT);
    }
    else {
        outDate = "";
        console.log("Invalid Date: " + moment(date));
    }

    return new Handlebars.SafeString(outDate);
});

/* Notelistview Classes */
class Notelistview {
    noteListTemplateHTML:HandlebarsTemplateDelegate;

    constructor() {
        this.compile();
    }

    render(notelist:INote[]):void {
        let context = {
            notes: notelist
        };
        let notesHtml:string = this.noteListTemplateHTML(context);
        document.getElementById("notelist").innerHTML = notesHtml;
    }

    private compile():void {
        this.noteListTemplateHTML = Handlebars.compile(document.getElementById("notes-template").innerText);
    }

}
class NotelistController extends NoteController{
    notelist:INote[];
    notelistview:Notelistview;
    noteservice:NoteService;
    noteStorageService:NoteStorageService;

    /* HTMLSelectElement greift auf das Interface von HTMLElement zurück */
    /* Aktives Filter- und Sortierkriterium über Listboxen
     Default ist das Item, das ausgewählt wurde */
    /* ToDo: Services im constructor übergeben <NAME> */
    constructor() {
        super(HREF_PREFIX_STYLE);
        this.noteservice = new NoteService();
        this.noteStorageService = new NoteStorageService();
        this.notelist = this.noteStorageService.noteList;
        this.notelistview = new Notelistview();
        this.notelistview.render(this.notelist);
        this.registerBtnEdit();
        this.registerBtnNoteNew();
        this.registerCBFinished();
        this.registerListboxSorter();
        this.registerListboxFilter();
    };

    registerCBFinished():void {
        $(":checkbox").change(function () {
            let  id = $(this).parent().attr("id");
            let finishedDate = $(this).is(':checked') ? new Date().toJSON() : " ";
            let note:INote = new Note();
            note.id = Number(id);
            new NoteStorageService().updateNote(note, finishedDate);
        })
    }

    registerBtnEdit():void {
        $(":button").on('click', function () {
            let id = $(this).parent().attr("id");
            NotelistController.changLocation(Number(id));
        });
    }

    registerBtnNoteNew():void {
        let el:HTMLElement = document.getElementById("btnNoteNew");
        el.addEventListener('click', this.createNewNote.bind(this));
    }

    registerListboxSorter():void {
        let el:HTMLElement = document.getElementById("ddlb_sorterselect");
        el.addEventListener('change', this.sort.bind(this));
    }

    registerListboxFilter():void {
        let el:HTMLElement = document.getElementById("ddlb_filterselect");
        el.addEventListener('change', this.filter.bind(this));

    }

    createNewNote(event:Event):void {
        let nextID:number = 1;
        console.log("NewNote", Event);

        nextID = this.noteStorageService.getNextId();
        if (typeof nextID === "number") {
            /* notedetail Editor mit neuer ID aufrufen */
            NotelistController.changLocation(nextID);
        }
        else {
            console.log("Error:CreateNewNote: Wrong ID", nextID);
        }
    }

    private static changLocation(id:number):void {
        window.location.replace("notedetail\\notedetail.html?id=" + id);
    }

    sort(event:Event):void {
        /* found no type for event.target therefore any as type */
        let target:any = event.target;
        let SelectedSortOption:string = target.value;
        switch (SelectedSortOption) {
            case "id":
                this.noteservice.sortBy(this.notelist, SortCriteria.id);
                break;
            case "due-date":
                this.noteservice.sortBy(this.notelist, SortCriteria.dueDate);
                break;
            case "creation-date":
                this.noteservice.sortBy(this.notelist, SortCriteria.creationDate);
                break;
            case "importance":
                this.noteservice.sortBy(this.notelist, SortCriteria.importance);
                break;
            default:
                console.log("Switch SelectedSortOption: default");
                break;
        }
        this.notelistview.render(this.notelist);
        console.log("change LBSort");
    }

    filter(event:Event):void {
        let target:any = event.target;
        let SelectedSortOption:string = target.value;
        /* notelist mit allen Elementen initialisieren   */
        this.notelist = this.noteStorageService.noteList;
        switch (SelectedSortOption) {
            case "id":
                /* Kein Filter */
                break;
            case "note-active":
{'fraction_non_alphanumeric': 0.1198570293884035, 'fraction_numerical': 0.0009531374106433678, 'mean_word_length': 2.441104126810604, 'pattern_counts': {'":': 16, '<': 22, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '194033', 'n_tokens_mistral': 3594, 'n_tokens_neox': 3458, 'n_words': 711}