MDL-69166 core_payment: show cost on the modal
[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
20 * @package core_payment
21 * @copyright 2020 Shamim Rezaie <shamim@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
fd187060
SR
25import $ from 'jquery';
26import CustomEvents from 'core/custom_interaction_events';
27import Modal from 'core/modal';
28import ModalEvents from 'core/modal_events';
29import PaymentEvents from 'core_payment/events';
30import ModalRegistry from 'core/modal_registry';
b23dcc37 31
fd187060
SR
32let registered = false;
33const SELECTORS = {
34 PROCEED_BUTTON: '[data-action="proceed"]',
35 CANCEL_BUTTON: '[data-action="cancel"]',
36};
37
38export default class ModalGateways extends Modal {
b23dcc37
SR
39
40 /**
41 * Constructor for the Modal.
42 *
43 * @param {object} root The root jQuery element for the modal
44 */
fd187060
SR
45 constructor(root) {
46 super(root);
47 }
b23dcc37
SR
48
49 /**
50 * Set up all of the event handling for the modal.
51 *
52 * @method registerEventListeners
53 */
fd187060 54 registerEventListeners() {
b23dcc37 55 // Apply parent event listeners.
fd187060 56 super.registerEventListeners();
b23dcc37 57
fd187060 58 this.getModal().on(CustomEvents.events.activate, SELECTORS.PROCEED_BUTTON, (e, data) => {
b23dcc37
SR
59 var proceedEvent = $.Event(PaymentEvents.proceed);
60 this.getRoot().trigger(proceedEvent, this);
61
62 if (!proceedEvent.isDefaultPrevented()) {
63 this.hide();
64 data.originalEvent.preventDefault();
65 }
fd187060 66 });
b23dcc37 67
fd187060 68 this.getModal().on(CustomEvents.events.activate, SELECTORS.CANCEL_BUTTON, (e, data) => {
b23dcc37
SR
69 var cancelEvent = $.Event(ModalEvents.cancel);
70 this.getRoot().trigger(cancelEvent, this);
71
72 if (!cancelEvent.isDefaultPrevented()) {
73 this.hide();
74 data.originalEvent.preventDefault();
75 }
fd187060 76 });
b23dcc37 77 }
fd187060
SR
78}
79
80ModalGateways.TYPE = 'core_payment-modal_gateways';
b23dcc37 81
fd187060
SR
82// Automatically register with the modal registry the first time this module is imported so that you can create modals
83// of this type using the modal factory.
84if (!registered) {
85 ModalRegistry.register(ModalGateways.TYPE, ModalGateways, 'core_payment/modal_gateways');
86 registered = true;
87}