{"version":3,"file":"autolinker.min.js","sources":["https:\/\/courses.fincert.org\/filter\/glossary\/amd\/src\/autolinker.js"],"sourcesContent":["\/\/ This file is part of Moodle - http:\/\/moodle.org\/\n\/\/\n\/\/ Moodle is free software: you can redistribute it and\/or modify\n\/\/ it under the terms of the GNU General Public License as published by\n\/\/ the Free Software Foundation, either version 3 of the License, or\n\/\/ (at your option) any later version.\n\/\/\n\/\/ Moodle is distributed in the hope that it will be useful,\n\/\/ but WITHOUT ANY WARRANTY; without even the implied warranty of\n\/\/ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\/\/ GNU General Public License for more details.\n\/\/\n\/\/ You should have received a copy of the GNU General Public License\n\/\/ along with Moodle. If not, see .\n\n\/**\n * Module for auto-linking glossary entries.\n *\n * @module filter_glossary\/autolinker\n * @copyright 2023 Mihail Geshoski \n * @license http:\/\/www.gnu.org\/copyleft\/gpl.html GNU GPL v3 or later\n *\/\n\nimport Ajax from 'core\/ajax';\nimport ModalCancel from \"core\/modal_cancel\";\nimport Templates from 'core\/templates';\nimport {getString} from 'core\/str';\n\n\/** @constant {Object} The object containing the relevant selectors. *\/\nconst Selectors = {\n glossaryEntryAutoLink: 'a.glossary.autolink.concept',\n};\n\n\/**\n * Register the event listeners for the glossary entry auto-linker.\n *\n * @return {void}\n *\/\nconst registerEventListeners = () => {\n document.addEventListener('click', async(e) => {\n const glossaryEntryAutoLink = e.target.closest(Selectors.glossaryEntryAutoLink);\n if (glossaryEntryAutoLink) {\n e.preventDefault();\n const entryId = glossaryEntryAutoLink.dataset.entryid;\n await showGlossaryEntry(entryId);\n await glossaryEntryViewed(entryId);\n }\n });\n};\n\n\/**\n * Show the linked glossary entry in a modal.\n *\n * @method showGlossaryEntry\n * @param {int} entryId The id of the linked glossary entry.\n * @returns {Promise} The modal promise.\n *\/\nconst showGlossaryEntry = async(entryId) => {\n const entryData = await fetchGlossaryEntry(entryId);\n \/\/ Obtain the HTML and JS used for rendering the auto-linked glossary entry.\n const {html, js} = await Templates.renderForPromise('filter_glossary\/linked_glossary_entry', {\n definition: entryData.entry.definition,\n taglistdata: await generateTagListData(entryData.entry.tags),\n hasattachments: Boolean(entryData.entry.attachment),\n attachments: entryData.entry.attachments\n });\n \/\/ Create the modal.\n const modal = await ModalCancel.create({\n title: entryData.entry.concept,\n body: html,\n isVerticallyCentered: true,\n buttons: {\n cancel: await getString('ok')\n }\n });\n \/\/ Execute the JS code returned from the template once the modal is created.\n Templates.runTemplateJS(js);\n \/\/ Display the modal.\n modal.show();\n\n return modal;\n};\n\n\/**\n * Fetch the linked glossary entry.\n *\n * @method fetchGlossaryEntry\n * @param {int} entryId The id of the linked glossary entry.\n * @returns {Promise} The glossary entry promise.\n *\/\nconst fetchGlossaryEntry = (entryId) => {\n const request = {\n methodname: 'mod_glossary_get_entry_by_id',\n args: {\n id: entryId,\n },\n };\n return Ajax.call([request])[0];\n};\n\n\/**\n * Notify that the linked glossary entry was viewed.\n *\n * @method glossaryEntryViewed\n * @param {int} entryId The id of the linked glossary entry.\n * @returns {Promise} The promise object.\n *\/\nconst glossaryEntryViewed = (entryId) => {\n const request = {\n methodname: 'mod_glossary_view_entry',\n args: {\n id: entryId,\n },\n };\n return Ajax.call([request])[0];\n};\n\n\/**\n * Generates an object that contains the data required to render a tag list.\n *\n * @method generateTagListData\n * @param {array} tags The array containing the tags related to the linked glossary entry.\n * @returns {Object} The data required to render a tag list.\n *\/\nconst generateTagListData = async(tags) => {\n \/\/ Define the number of initially displayed tags.\n const limit = 10;\n const hasOverflow = tags.length > limit;\n \/\/ If the total number of tags exceeds the defined limit, then we need to mark all the excess tags as over the limit.\n \/\/ By specifying this, these tags will be initially hidden.\n if (hasOverflow) {\n for (let i = limit; i < tags.length; i++) {\n tags[i].overlimit = true;\n }\n }\n\n return {\n tags: tags,\n tagscount: tags.length,\n overflow: hasOverflow,\n label: await getString('tags')\n };\n};\n\n\/**\n * Initialize the module.\n *\/\nexport const init = () => {\n registerEventListeners();\n};\n"],"names":["Selectors","showGlossaryEntry","async","entryData","fetchGlossaryEntry","entryId","html","js","Templates","renderForPromise","definition","entry","taglistdata","generateTagListData","tags","hasattachments","Boolean","attachment","attachments","modal","ModalCancel","create","title","concept","body","isVerticallyCentered","buttons","cancel","runTemplateJS","show","request","methodname","args","id","Ajax","call","glossaryEntryViewed","hasOverflow","length","i","overlimit","tagscount","overflow","label","document","addEventListener","glossaryEntryAutoLink","e","target","closest","preventDefault","dataset","entryid"],"mappings":";;;;;;;8NA6BMA,gCACqB,8BA2BrBC,kBAAoBC,MAAAA,gBAChBC,gBAAkBC,mBAAmBC,UAErCC,KAACA,KAADC,GAAOA,UAAYC,mBAAUC,iBAAiB,wCAAyC,CACzFC,WAAYP,UAAUQ,MAAMD,WAC5BE,kBAAmBC,oBAAoBV,UAAUQ,MAAMG,MACvDC,eAAgBC,QAAQb,UAAUQ,MAAMM,YACxCC,YAAaf,UAAUQ,MAAMO,cAG3BC,YAAcC,sBAAYC,OAAO,CACnCC,MAAOnB,UAAUQ,MAAMY,QACvBC,KAAMlB,KACNmB,sBAAsB,EACtBC,QAAS,CACLC,aAAc,kBAAU,mCAItBC,cAAcrB,IAExBY,MAAMU,OAECV,OAULf,mBAAsBC,gBAClByB,QAAU,CACZC,WAAY,+BACZC,KAAM,CACFC,GAAI5B,iBAGL6B,cAAKC,KAAK,CAACL,UAAU,IAU1BM,oBAAuB\/B,gBACnByB,QAAU,CACZC,WAAY,0BACZC,KAAM,CACFC,GAAI5B,iBAGL6B,cAAKC,KAAK,CAACL,UAAU,IAU1BjB,oBAAsBX,MAAAA,aAGlBmC,YAAcvB,KAAKwB,OADX,MAIVD,gBACK,IAAIE,EALC,GAKUA,EAAIzB,KAAKwB,OAAQC,IACjCzB,KAAKyB,GAAGC,WAAY,QAIrB,CACH1B,KAAMA,KACN2B,UAAW3B,KAAKwB,OAChBI,SAAUL,YACVM,YAAa,kBAAU,wBAOX,KA5GhBC,SAASC,iBAAiB,SAAS3C,MAAAA,UACzB4C,sBAAwBC,EAAEC,OAAOC,QAAQjD,oCAC3C8C,sBAAuB,CACvBC,EAAEG,uBACI7C,QAAUyC,sBAAsBK,QAAQC,cACxCnD,kBAAkBI,eAClB+B,oBAAoB\/B"}