{ "version": 3, "sources": ["../../../Vettvangur.Heilsuvera/src/scripts/components/wordTranslation.ts"], "sourcesContent": ["interface Translation {\n\tword: string;\n\tdescription: string;\n}\n\nconst wordTranslation = {\n\tlist: [] as Translation[],\n\n\tinit: (): void => {\n\t\tconst articlePage = document.querySelector(\".article\");\n\n\t\tif (articlePage != null) {\n\t\t\twordTranslation.fetchData();\n\t\t}\n\t},\n\n\tfetchData: async (): Promise => {\n\t\tfetch(`/Umbraco/Surface/Content/MolarTranslations`)\n\t\t\t.then((response) => response.json())\n\t\t\t.then((data) => {\n\t\t\t\tif (data.success === true) {\n\t\t\t\t\tObject.entries(data.data).forEach(([key, value]) => {\n\t\t\t\t\t\twordTranslation.list.push({\n\t\t\t\t\t\t\tword: key,\n\t\t\t\t\t\t\tdescription: value.toString(),\n\t\t\t\t\t\t});\n\t\t\t\t\t});\n\n\t\t\t\t\tconst testList = wordTranslation.list.filter(\n\t\t\t\t\t\t(item) => item.word !== \"\u00D3fr\u00EDsk\",\n\t\t\t\t\t);\n\t\t\t\t\twordTranslation.translate(testList);\n\t\t\t\t}\n\t\t\t});\n\t},\n\n\taddTooltipListener(element) {\n\t\telement.addEventListener(\"mouseenter\", (event) => {\n\t\t\tconst tooltipParentRect = event.currentTarget.getBoundingClientRect();\n\t\t\tconst containerParentRect =\n\t\t\t\tevent.currentTarget.parentElement.getBoundingClientRect();\n\t\t\tconst tooltipRect = event.currentTarget.getBoundingClientRect();\n\t\t\tconst containerRect =\n\t\t\t\tevent.currentTarget.parentElement.getBoundingClientRect();\n\t\t\tconst distanceToContainerBottom =\n\t\t\t\tcontainerParentRect.bottom - tooltipParentRect.bottom;\n\t\t\tconst distanceToContainerLeft = tooltipRect.left - containerRect.left;\n\t\t\tconst distanceToContainerRight = containerRect.right - tooltipRect.right;\n\t\t\tconst threshold = 90;\n\t\t\tif (distanceToContainerBottom < 55) {\n\t\t\t\tevent.currentTarget.classList.add(\"bottom-close\");\n\t\t\t} else {\n\t\t\t\tevent.currentTarget.classList.remove(\"bottom-close\");\n\t\t\t}\n\t\t\tif (distanceToContainerLeft <= threshold) {\n\t\t\t\tevent.currentTarget.classList.add(\"left-close\");\n\t\t\t} else {\n\t\t\t\tevent.currentTarget.classList.remove(\"left-close\");\n\t\t\t}\n\n\t\t\tif (distanceToContainerRight <= threshold) {\n\t\t\t\tevent.currentTarget.classList.add(\"right-close\");\n\t\t\t} else {\n\t\t\t\tevent.currentTarget.classList.remove(\"right-close\");\n\t\t\t}\n\t\t});\n\t},\n\n\tcreateWordTranslationElement(word, description) {\n\t\t// Create the outer span\n\t\tconst wordSpan = document.createElement(\"span\");\n\t\twordSpan.className = \"wordTranslation\";\n\n\t\t// Create the inner span for the tooltip text\n\t\tconst tooltipSpan = document.createElement(\"span\");\n\t\ttooltipSpan.className = \"wordTranslation__text\";\n\t\ttooltipSpan.textContent = description;\n\n\t\t// Append the tooltip span to the word span\n\t\twordSpan.appendChild(document.createTextNode(word));\n\t\twordSpan.appendChild(tooltipSpan);\n\n\t\t// Attach the tooltip listener\n\t\tthis.addTooltipListener(wordSpan);\n\n\t\treturn wordSpan;\n\t},\n\n\treplaceMatchingWords(pattern: RegExp, description: string): string {\n\t\treturn description.replace(pattern, (matchedWord) => {\n\t\t\tconst matchingItem = wordTranslation.list.find(\n\t\t\t\t(item) => item.word === matchedWord.toLowerCase(),\n\t\t\t);\n\n\t\t\tconst innerHtml =\n\t\t\t\t'$1$2' +\n\t\t\t\tdescription +\n\t\t\t\t\"$3\";\n\n\t\t\tif (matchingItem) {\n\t\t\t\treturn innerHtml;\n\t\t\t} else {\n\t\t\t\treturn matchedWord;\n\t\t\t}\n\t\t});\n\t},\n\n\ttranslateTest: (descriptionList: Translation[]): void => {\n\t\tconst pElements = document.getElementsByTagName(\"p\");\n\n\t\tfor (let x = pElements.length; x--; ) {\n\t\t\tconst currentElement = pElements[x];\n\n\t\t\tfor (let i = 0, l = descriptionList.length; i < l; i++) {\n\t\t\t\tconst reg = new RegExp(\n\t\t\t\t\t\"([^a-zA-Z-])(\" + descriptionList[i].word + \")([^a-zA-Z-])\",\n\t\t\t\t\t\"giu\",\n\t\t\t\t);\n\n\t\t\t\tconst pattern = new RegExp(\n\t\t\t\t\tdescriptionList.map((item) => item.word).join(\"|\"),\n\t\t\t\t\t\"i\",\n\t\t\t\t);\n\t\t\t\tconst patternTest = pattern.test(descriptionList[i].description);\n\n\t\t\t\tcurrentElement.innerHTML = currentElement.innerHTML.replace(\n\t\t\t\t\treg,\n\t\t\t\t\t'$1$2' +\n\t\t\t\t\t\tdescriptionList[i].description +\n\t\t\t\t\t\t\"$3\",\n\t\t\t\t);\n\n\t\t\t\tif (patternTest === true) {\n\t\t\t\t\tconst hh = wordTranslation.replaceMatchingWords(\n\t\t\t\t\t\tpattern,\n\t\t\t\t\t\tdescriptionList[i].description,\n\t\t\t\t\t);\n\t\t\t\t\tconst infoBox = currentElement.querySelectorAll(\n\t\t\t\t\t\t\".wordTranslation__text\",\n\t\t\t\t\t);\n\n\t\t\t\t\tif (infoBox && infoBox.length && infoBox.length > 1) {\n\t\t\t\t\t\tinfoBox[0].innerHTML = hh;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tconst liElements = document.querySelectorAll(\".article__content li\");\n\n\t\tif (liElements !== null) {\n\t\t\tfor (let y = liElements.length; y--; ) {\n\t\t\t\tconst currentLiElement = liElements[y];\n\n\t\t\t\tfor (let j = 0, le = descriptionList.length; j < le; j++) {\n\t\t\t\t\tconst regExpression = new RegExp(\n\t\t\t\t\t\t\"([^a-zA-Z-])(\" + descriptionList[j].word + \")([^a-zA-Z-])\",\n\t\t\t\t\t\t\"giu\",\n\t\t\t\t\t);\n\n\t\t\t\t\tcurrentLiElement.innerHTML = currentLiElement.innerHTML.replace(\n\t\t\t\t\t\tregExpression,\n\t\t\t\t\t\t'$1$2' +\n\t\t\t\t\t\t\tdescriptionList[j].description +\n\t\t\t\t\t\t\t\"$3\",\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t},\n\n\ttranslate: (descriptionList: Translation[]): void => {\n\t\tconst elements = document.querySelectorAll(\n\t\t\t\".article__content p, .article__content li\",\n\t\t);\n\n\t\telements.forEach((element) => {\n\t\t\tdescriptionList.forEach((description) => {\n\t\t\t\tconst regex = new RegExp(\n\t\t\t\t\t`([^a-zA-Z-])(${description.word})([^a-zA-Z-])`,\n\t\t\t\t\t\"giu\",\n\t\t\t\t);\n\n\t\t\t\telement.childNodes.forEach((child) => {\n\t\t\t\t\tif (child.nodeType === 3) {\n\t\t\t\t\t\t// Node type 3 is a text node\n\t\t\t\t\t\tconst newContent = [];\n\t\t\t\t\t\tchild.textContent.split(regex).forEach((part, index) => {\n\t\t\t\t\t\t\tif (index % 4 === 2) {\n\t\t\t\t\t\t\t\t// The word to be translated\n\t\t\t\t\t\t\t\tnewContent.push(\n\t\t\t\t\t\t\t\t\twordTranslation.createWordTranslationElement(\n\t\t\t\t\t\t\t\t\t\tpart,\n\t\t\t\t\t\t\t\t\t\tdescription.description,\n\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t} else if (part) {\n\t\t\t\t\t\t\t\tnewContent.push(document.createTextNode(part));\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t});\n\t\t\t\t\t\tif (newContent.length) {\n\t\t\t\t\t\t\tnewContent.forEach((newNode) =>\n\t\t\t\t\t\t\t\tchild.parentNode.insertBefore(newNode, child),\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\tchild.parentNode.removeChild(child);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t});\n\t\t});\n\t},\n};\n\nexport default wordTranslation;\n"], "mappings": "gCAKA,IAAMA,EAAkB,CACvB,KAAM,CAAC,EAEP,KAAM,IAAY,CACG,SAAS,cAAc,UAAU,GAElC,MAClBA,EAAgB,UAAU,CAE5B,EAEA,UAAW,SAA2B,CACrC,MAAM,4CAA4C,E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,OACzB,gBAAkBd,EAAgBY,CAAC,EAAE,KAAO,gBAC5C,KACD,EAEAD,EAAiB,UAAYA,EAAiB,UAAU,QACvDG,EACA,yEACCd,EAAgBY,CAAC,EAAE,YACnB,kBACF,CACD,CACD,CAEF,EAEA,UAAYZ,GAAyC,CACnC,SAAS,iBACzB,2CACD,EAES,QAASlB,GAAY,CAC7BkB,EAAgB,QAASP,GAAgB,CACxC,IAAMsB,EAAQ,IAAI,OACjB,gBAAgBtB,EAAY,IAAI,gBAChC,KACD,EAEAX,EAAQ,WAAW,QAASkC,GAAU,CACrC,GAAIA,EAAM,WAAa,EAAG,CAEzB,IAAMC,EAAa,CAAC,EACpBD,EAAM,YAAY,MAAMD,CAAK,EAAE,QAAQ,CAACG,EAAMC,IAAU,CACnDA,EAAQ,IAAM,EAEjBF,EAAW,KACV1C,EAAgB,6BACf2C,EACAzB,EAAY,WACb,CACD,EACUyB,GACVD,EAAW,KAAK,SAAS,eAAeC,CAAI,CAAC,CAE/C,CAAC,EACGD,EAAW,SACdA,EAAW,QAASG,GACnBJ,EAAM,WAAW,aAAaI,EAASJ,CAAK,CAC7C,EACAA,EAAM,WAAW,YAAYA,CAAK,EAEpC,CACD,CAAC,CACF,CAAC,CACF,CAAC,CACF,CACD,EAEOK,EAAQ9C", "names": ["wordTranslation", "response", "data", "key", "value", "testList", "item", "element", "event", "tooltipParentRect", "containerParentRect", "tooltipRect", "containerRect", "distanceToContainerBottom", "distanceToContainerLeft", "distanceToContainerRight", "threshold", "word", "description", "wordSpan", "tooltipSpan", "pattern", "matchedWord", "matchingItem", "innerHtml", "descriptionList", "pElements", "x", "currentElement", "i", "reg", "patternTest", "hh", "infoBox", "liElements", "y", "currentLiElement", "j", "le", "regExpression", "regex", "child", "newContent", "part", "index", "newNode", "wordTranslation_default"] }