{"version":3,"file":"bfnewwindow.min.js","sources":["https:\/\/2023.qmplus.qmul.ac.uk\/local\/bfnewwindow\/amd\/src\/bfnewwindow.js"],"sourcesContent":["\/* eslint-disable no-unused-vars *\/\n\/* eslint-disable promise\/catch-or-return *\/\n\/\/ 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 * @copyright 2021 Brickfield Education Labs\n * @license http:\/\/www.gnu.org\/copyleft\/gpl.html GNU GPL v3 or later\n *\/\n\nimport $ from 'jquery';\nimport ModalFactory from 'core\/modal_factory';\nimport CustomEvents from 'core\/custom_interaction_events';\nimport ModalBf from 'local_bfnewwindow\/modal_bf';\nimport ModalBfOld from 'local_bfnewwindow\/modal_bf_old';\nimport Ajax from \"core\/ajax\";\n\n\/** @type {Object.} Mapping of used data actions. *\/\nconst SELECTORS = {\n SAMEWINDOW_BUTTON: '[data-action=\"samewindow\"]',\n NEWWINDOW_BUTTON: '[data-action=\"newwindow\"]',\n CANCEL_BUTTON: '[data-action=\"cancel\"]'\n};\n\n\/**\n * Initialises the module.\n *\n * Catches clicks on anchor tags and determines if they should be warned if the\n * link is set to open in a new window.\n * @param {number} courseid\n * @param {number} moodleversion\n *\/\nexport const initialise = (courseid, moodleversion) => {\n moodleversion = parseInt(moodleversion);\n \/\/ Getting all links in the editor. We don't want to trigger on these.\n \/\/ This is faster than scanning the DOM for every link click.\n let editorLinks = $('div[data-fieldtype=\"editor\"] a[target]');\n \/\/ Attaching event to anchor tags that open in a new window.\n $(document).on('click', 'a[target]', (event) => {\n let anchor = event.target;\n \/\/ Don't trigger on editor links.\n for (let i = 0; i < editorLinks.length; i++) {\n if (anchor == editorLinks[i]) {\n return;\n }\n }\n \/\/ Trigger on anything set to blank or an invalid frame name.\n if (anchor.target != '_blank') {\n \/\/ Alwyas valid non blank names.\n if (['_self', '_parent', '_top'].includes(anchor.target)) {\n return;\n }\n \/\/ Check for valid iframe name.\n let iframes = document.getElementsByTagName('iframe');\n let names = [];\n for (let i = 0; i < iframes.length; i++) {\n names.push(iframes[i].name);\n }\n if (names.includes(anchor.target)) {\n return;\n }\n \/\/ The frame name is invalid. Continue.\n }\n \/\/ Don't follow.\n event.preventDefault();\n \/\/ Reading anchor tag.\n let href = $(anchor).attr('href');\n let parent = event.target.parentNode;\n\n while (href === undefined) {\n href = $(parent).attr('href');\n parent = parent.parentNode;\n if (parent === undefined) {\n break;\n }\n }\n\n if (href !== undefined) {\n \/\/ Or if on a form.\n if ($(anchor).closest('.mform').length == '1') {\n window.open(href, '_blank');\n return;\n }\n\n let trigger = $('#login');\n let activateEvent = CustomEvents.events.activate;\n\n let modalPromise;\n if (moodleversion >= 2023100900) {\n modalPromise = ModalBf.create({type: ModalBf.TYPE});\n } else {\n modalPromise = ModalFactory.create({type: ModalBfOld.TYPE}, trigger);\n }\n modalPromise.then((modal) => {\n let root = modal.getRoot();\n \/\/ Attach modal button events.\n root.on(activateEvent, SELECTORS.SAMEWINDOW_BUTTON, () => {\n modal.hide();\n window.open(href, '_self');\n });\n root.on(activateEvent, SELECTORS.NEWWINDOW_BUTTON, () => {\n modal.hide();\n window.open(href, '_blank');\n });\n root.on(activateEvent, SELECTORS.CANCEL_BUTTON, () => {\n modal.hide();\n });\n modal.show();\n });\n }\n });\n\n var buttons = document.getElementsByClassName(\"bfnewwindow_modal\");\n\n Array.from(buttons).forEach(function(element) {\n element.addEventListener('click', () => addLog(courseid));\n });\n};\n\n\/**\n * Handles logging.\n *\n * @param {integer} courseid\n *\/\nfunction addLog(courseid) {\n Ajax.call([{\n methodname: 'tool_bfplus_add_log_data',\n args: {\n 'courseid': courseid,\n 'categoryid': 0,\n 'component': 'local_bfnewwindow',\n 'eventname': 'bfnewwindow_modal_used'\n }\n }]);\n}\n"],"names":["SELECTORS","courseid","moodleversion","parseInt","editorLinks","document","on","event","anchor","target","i","length","includes","iframes","getElementsByTagName","names","push","name","preventDefault","href","attr","parent","parentNode","undefined","closest","window","open","modalPromise","trigger","activateEvent","CustomEvents","events","activate","ModalBf","create","type","TYPE","ModalFactory","ModalBfOld","then","modal","root","getRoot","hide","show","buttons","getElementsByClassName","Array","from","forEach","element","addEventListener","call","methodname","args","addLog"],"mappings":";;;;8YA8BMA,4BACiB,6BADjBA,2BAEgB,4BAFhBA,wBAGa,6CAWO,CAACC,SAAUC,iBACjCA,cAAgBC,SAASD,mBAGrBE,aAAc,mBAAE,8DAElBC,UAAUC,GAAG,QAAS,aAAcC,YAC9BC,OAASD,MAAME,WAEd,IAAIC,EAAI,EAAGA,EAAIN,YAAYO,OAAQD,OAChCF,QAAUJ,YAAYM,aAKT,UAAjBF,OAAOC,OAAoB,IAEvB,CAAC,QAAS,UAAW,QAAQG,SAASJ,OAAOC,mBAI7CI,QAAUR,SAASS,qBAAqB,UACxCC,MAAQ,OACP,IAAIL,EAAI,EAAGA,EAAIG,QAAQF,OAAQD,IAChCK,MAAMC,KAAKH,QAAQH,GAAGO,SAEtBF,MAAMH,SAASJ,OAAOC,eAM9BF,MAAMW,qBAEFC,MAAO,mBAAEX,QAAQY,KAAK,QACtBC,OAASd,MAAME,OAAOa,qBAEVC,IAATJ,OACHA,MAAO,mBAAEE,QAAQD,KAAK,QACtBC,OAASA,OAAOC,gBACDC,IAAXF,kBAKKE,IAATJ,KAAoB,IAEsB,MAAtC,mBAAEX,QAAQgB,QAAQ,UAAUb,mBAC5Bc,OAAOC,KAAKP,KAAM,cAOlBQ,aAHAC,SAAU,mBAAE,UACZC,cAAgBC,mCAAaC,OA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