weekly release 4.2dev
[moodle.git] / payment / amd / src / modal_gateways.js
CommitLineData
b23dcc37
SR
1// This file is part of Moodle - http://moodle.org/
2//
3// Moodle is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// Moodle is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
15
16/**
17 * Contain the logic for the gateways modal: A modal with proceed and cancel buttons.
18 *
19 * @module core_payment/modal_gateways
b23dcc37
SR
20 * @copyright 2020 Shamim Rezaie <shamim@moodle.com>
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
fd187060
SR
24import $ from 'jquery';
25import CustomEvents from 'core/custom_interaction_events';
26import Modal from 'core/modal';
27import ModalEvents from 'core/modal_events';
28import PaymentEvents from 'core_payment/events';
29import ModalRegistry from 'core/modal_registry';
b23dcc37 30
fd187060
SR
31let registered = false;
32const SELECTORS = {
33 PROCEED_BUTTON: '[data-action="proceed"]',
34 CANCEL_BUTTON: '[data-action="cancel"]',
35};
36
37export default class ModalGateways extends Modal {
b23dcc37
SR
38
39 /**
40 * Constructor for the Modal.
41 *
42 * @param {object} root The root jQuery element for the modal
43 */
fd187060
SR
44 constructor(root) {
45 super(root);
46 }
b23dcc37
SR
47
48 /**
49 * Set up all of the event handling for the modal.
50 *
51 * @method registerEventListeners
52 */
fd187060 53 registerEventListeners() {
b23dcc37 54 // Apply parent event listeners.
fd187060 55 super.registerEventListeners();
b23dcc37 56
fd187060 57 this.getModal().on(CustomEvents.events.activate, SELECTORS.PROCEED_BUTTON, (e, data) => {
b23dcc37
SR
58 var proceedEvent = $.Event(PaymentEvents.proceed);
59 this.getRoot().trigger(proceedEvent, this);
60
61 if (!proceedEvent.isDefaultPrevented()) {
62 this.hide();
63 data.originalEvent.preventDefault();
64 }
fd187060 65 });
b23dcc37 66
fd187060 67 this.getModal().on(CustomEvents.events.activate, SELECTORS.CANCEL_BUTTON, (e, data) => {
b23dcc37
SR
68 var cancelEvent = $.Event(ModalEvents.cancel);
69 this.getRoot().trigger(cancelEvent, this);
70
71 if (!cancelEvent.isDefaultPrevented()) {
72 this.hide();
73 data.originalEvent.preventDefault();
74 }
fd187060 75 });
b23dcc37 76 }
fd187060
SR
77}
78
79ModalGateways.TYPE = 'core_payment-modal_gateways';
b23dcc37 80
fd187060
SR
81// Automatically register with the modal registry the first time this module is imported so that you can create modals
82// of this type using the modal factory.
83if (!registered) {
84 ModalRegistry.register(ModalGateways.TYPE, ModalGateways, 'core_payment/modal_gateways');
85 registered = true;
86}