import Store from "../../store.js"; import dayjs from "dayjs"; import Helpers from "../../composables/helpers.js"; /** * Represents a comment on a webpage. */ class Comment { /** * Create a Comment. * @param {Object} position - The positions where the comment should be displayed. */ constructor( position, author, message, id, date, time, windowWidth, userAgent, team ) { this.position = position; this.author = author; this.message = message; this.id = id; this.date = date; this.time = time; this.windowWidth = windowWidth; this.userAgent = userAgent; this.team = team; this.node; } toBubble() { const bubble = this._injectNode(``); if (this.team) { bubble.classList.add("fc__team-icon"); bubble.classList.add(`fc__team-icon--${this.team}`); } bubble.addEventListener("mouseenter", () => { setTimeout(() => { this.expand(); }, 10); }); this.node = bubble; return bubble; } /** * Show the comment message. */ expand() { const windowWidthInfoElement = this.windowWidth ? `
  • Largeur fenêtre : ${this.windowWidth}px
  • ` : ""; const userAgentInfoElement = this.userAgent ? `
  • Agent utilisateur :
    ${this.userAgent}
  • ` : ""; const contextInfoElement = this.userAgent || this.windowWidth ? `
    ` : ""; const comment = this._injectNode(`
    ${this.author}
    ${this.date} à ${this.time}

    ${this.message}

    ${contextInfoElement}
    `); Helpers.fixOffscreen(comment); const deleteBtn = comment.querySelector(".fc__comment-delete"); deleteBtn.addEventListener("click", () => { this.remove(); }); const openWindowBtn = document.querySelector(".fc__open-window"); if (openWindowBtn) { openWindowBtn.addEventListener("click", () => { window.open( window.location.href, "", `width=${this.windowWidth}, height=800` ); }); } comment.addEventListener("mouseleave", () => { setTimeout(() => { this.toBubble(); }, 10); }); this.node = comment; return comment; } _injectNode(htmlString) { if (this.node) { document.body.removeChild(this.node); } const div = document.createElement("div"); div.innerHTML = htmlString; const node = div.firstChild; document.body.appendChild(node); return node; } /** * Hide the comment message. */ async remove() { const init = { method: "PATCH", }; fetch(`/comments/delete/${this.id}/${Store.page.uri}.json`, init) .then((res) => res.json()) .then((json) => { Store.comments = Store.comments.filter((item) => item.id != this.id); document.body.removeChild(this.node); }) .catch((error) => { console.log(error); }); } } export default Comment;