MDL-69166 core_payment: Use a custom modal type for gateway selector
[moodle.git] / payment / amd / build / gateways_modal.min.js.map
1 {"version":3,"sources":["../src/gateways_modal.js"],"names":["registerEventListenersBySelector","nodeSelector","document","querySelectorAll","forEach","element","registerEventListeners","rootNode","addEventListener","e","preventDefault","show","focusOnClose","target","ModalFactory","ModalGateways","TYPE","Templates","render","type","title","body","create","modal","getRoot","on","ModalEvents","hidden","destroy","focus","PaymentEvents","proceed","root","gateway","querySelector","Selectors","values","value","processPayment","dataset","amount","currency","component","componentid","description","success","message","hide","Notification","addNotification","location","reload","alert","then","gateways","context","renderForPromise","html","js","replaceNodeContents","find","regions","gatewaysContainer","callback","paymentMethod","process"],"mappings":"+XAwBA,OACA,OAGA,OACA,OACA,OAEA,OACA,O,ggBAOgD,QAAnCA,CAAAA,gCAAmC,CAACC,CAAD,CAAkB,CAC9DC,QAAQ,CAACC,gBAAT,CAA0BF,CAA1B,EAAwCG,OAAxC,CAAgD,SAACC,CAAD,CAAa,CACzDC,CAAsB,CAACD,CAAD,CACzB,CAFD,CAGH,C,CAOM,GAAMC,CAAAA,CAAsB,CAAG,SAACC,CAAD,CAAc,CAChDA,CAAQ,CAACC,gBAAT,CAA0B,OAA1B,CAAmC,SAACC,CAAD,CAAO,CACtCA,CAAC,CAACC,cAAF,GACAC,CAAI,CAACJ,CAAD,CAAW,CAACK,YAAY,CAAEH,CAAC,CAACI,MAAjB,CAAX,CACP,CAHD,CAIH,CALM,C,8BAcDF,CAAAA,CAAI,4CAAG,WAAMJ,CAAN,wJAET,EAFS,KACTK,YADS,CACTA,CADS,YACM,IADN,QAGWE,SAHX,MAICC,UAAcC,IAJf,gBAKQ,iBAAU,mBAAV,CAA+B,cAA/B,CALR,mCAMOC,WAAUC,MAAV,CAAiB,6BAAjB,CAAgD,EAAhD,CANP,0BAILC,IAJK,MAKLC,KALK,MAMLC,IANK,6BAGwBC,MAHxB,yBAGHC,CAHG,QAST,qBAAeA,CAAK,CAACC,OAAN,GAAgB,CA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,UAASA,CAAT,CAAJ,CAA3D,CACH,CAEDrC,CAAC,CAACC,cAAF,EACH,CAjCD,EAmCM+B,CA1DG,CA0DQlC,CAAQ,CAACgC,OAAT,CAAiBE,QA1DzB,iBA2Dc,oCAA8BA,CAA9B,CA3Dd,SA2DHa,CA3DG,QA4DHC,CA5DG,CA4DO,CACZD,QAAQ,CAARA,CADY,CA5DP,iBAgEgBrC,WAAUuC,gBAAV,CAA2B,uBAA3B,CAAoDD,CAApD,CAhEhB,kBAgEFE,CAhEE,GAgEFA,IAhEE,CAgEIC,CAhEJ,GAgEIA,EAhEJ,CAiETzC,UAAU0C,mBAAV,CAA8BpC,CAAK,CAACC,OAAN,GAAgBoC,IAAhB,CAAqBzB,UAAU0B,OAAV,CAAkBC,iBAAvC,CAA9B,CAAyFL,CAAzF,CAA+FC,CAA/F,EAjES,yCAAH,uD,CAgFJpB,CAAc,4CAAG,WAAML,CAAN,CAAeO,CAAf,CAAuBC,CAAvB,CAAiCC,CAAjC,CAA4CC,CAA5C,CAAyDC,CAAzD,CAAsEmB,CAAtE,oMACsB9B,CADtB,mOACsBA,CADtB,sDACsBA,CADtB,6BACb+B,CADa,QAGnBA,CAAa,CAACC,OAAd,CAAsBzB,CAAtB,CAA8BC,CAA9B,CAAwCC,CAAxC,CAAmDC,CAAnD,CAAgEC,CAAhE,CAA6EmB,CAA7E,EAHmB,wCAAH,uD","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 <http://www.gnu.org/licenses/>.\n\n/**\n * Contain the logic for the gateways modal.\n *\n * @module     core_payment/gateways_modal\n * @package    core_payment\n * @copyright  2019 Shamim Rezaie <shamim@moodle.com>\n * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport ModalFactory from 'core/modal_factory';\nimport Templates from 'core/templates';\nimport {get_string as getString} from 'core/str';\nimport {getGatewaysSupportingCurrency} from './repository';\nimport Selectors from './selectors';\nimport ModalEvents from 'core/modal_events';\nimport PaymentEvents from 'core_payment/events';\nimport {add as addToast, addToastRegion} from 'core/toast';\nimport Notification from 'core/notification';\nimport ModalGateways from './modal_gateways';\n\n/**\n * Register event listeners for the module.\n *\n * @param {string} nodeSelector The root to listen to.\n */\nexport const registerEventListenersBySelector = (nodeSelector) => {\n    document.querySelectorAll(nodeSelector).forEach((element) => {\n        registerEventListeners(element);\n    });\n};\n\n/**\n * Register event listeners for the module.\n *\n * @param {HTMLElement} rootNode The root to listen to.\n */\nexport const registerEventListeners = (rootNode) => {\n    rootNode.addEventListener('click', (e) => {\n        e.preventDefault();\n        show(rootNode, {focusOnClose: e.target});\n    });\n};\n\n/**\n * Shows the gateway selector modal.\n *\n * @param {HTMLElement} rootNode\n * @param {Object} options - Additional options\n * @param {HTMLElement} options.focusOnClose The element to focus on when the modal is closed.\n */\nconst show = async(rootNode, {\n    focusOnClose = null,\n} = {}) => {\n    const modal = await ModalFactory.create({\n        type: ModalGateways.TYPE,\n        title: await getString('selectpaymenttype', 'core_payment'),\n        body: await Templates.render('core_payment/gateways_modal', {}),\n    });\n\n    addToastRegion(modal.getRoot()[0]);\n\n    modal.show();\n\n    modal.getRoot().on(ModalEvents.hidden, () => {\n        // Destroy when hidden.\n        modal.destroy();\n        try {\n            focusOnClose.focus();\n        } catch (e) {\n            // eslint-disable-line\n        }\n    });\n\n    modal.getRoot().on(PaymentEvents.proceed, (e) => {\n        const root = modal.getRoot()[0];\n        const gateway = (root.querySelector(Selectors.values.gateway) || {value: ''}).value;\n\n        if (gateway) {\n            processPayment(\n                gateway,\n                rootNode.dataset.amount,\n                rootNode.dataset.currency,\n                rootNode.dataset.component,\n                rootNode.dataset.componentid,\n                rootNode.dataset.description,\n                ({success, message = ''}) => {\n                    modal.hide();\n                    if (success) {\n                        Notification.addNotification({\n                            message: message,\n                            type: 'success',\n                        });\n                        location.reload();\n                    } else {\n                        Notification.alert('', message);\n                    }\n                },\n            );\n        } else {\n            // We cannot use await in the following line.\n            // The reason is that we are preventing the default action of the save event being triggered,\n            // therefore we cannot define the event handler function asynchronous.\n            getString('nogatewayselected', 'core_payment').then(message => addToast(message));\n        }\n\n        e.preventDefault();\n    });\n\n    const currency = rootNode.dataset.currency;\n    const gateways = await getGatewaysSupportingCurrency(currency);\n    const context = {\n        gateways\n    };\n\n    const {html, js} = await Templates.renderForPromise('core_payment/gateways', context);\n    Templates.replaceNodeContents(modal.getRoot().find(Selectors.regions.gatewaysContainer), html, js);\n};\n\n/**\n * Process payment using the selected gateway.\n *\n * @param {string} gateway The gateway to be used for payment\n * @param {number} amount Amount of payment\n * @param {string} currency The currency in the three-character ISO-4217 format\n * @param {string} component Name of the component that the componentid belongs to\n * @param {number} componentid An internal identifier that is used by the component\n * @param {string} description Description of the payment\n * @param {processPaymentCallback} callback The callback function to call when processing is finished\n * @returns {Promise<void>}\n */\nconst processPayment = async(gateway, amount, currency, component, componentid, description, callback) => {\n    const paymentMethod = await import(`pg_${gateway}/gateways_modal`);\n\n    paymentMethod.process(amount, currency, component, componentid, description, callback);\n};\n\n/**\n * The callback definition for processPayment.\n *\n * @callback processPaymentCallback\n * @param {bool} success\n * @param {string} message\n */\n"],"file":"gateways_modal.min.js"}