MDL-69166 core_payment: Use a custom modal type for gateway selector
[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
25define([
26 'jquery',
27 'core/notification',
28 'core/custom_interaction_events',
29 'core/modal',
30 'core/modal_events',
31 'core_payment/events',
32 'core/modal_registry'
33],
34function(
35 $,
36 Notification,
37 CustomEvents,
38 Modal,
39 ModalEvents,
40 PaymentEvents,
41 ModalRegistry
42) {
43
44 var registered = false;
45 var SELECTORS = {
46 PROCEED_BUTTON: '[data-action="proceed"]',
47 CANCEL_BUTTON: '[data-action="cancel"]',
48 };
49
50 /**
51 * Constructor for the Modal.
52 *
53 * @param {object} root The root jQuery element for the modal
54 */
55 var ModalGateways = function(root) {
56 Modal.call(this, root);
57 };
58
59 ModalGateways.TYPE = 'core_payment-modal_gateways';
60 ModalGateways.prototype = Object.create(Modal.prototype);
61 ModalGateways.prototype.constructor = ModalGateways;
62
63 /**
64 * Set up all of the event handling for the modal.
65 *
66 * @method registerEventListeners
67 */
68 ModalGateways.prototype.registerEventListeners = function() {
69 // Apply parent event listeners.
70 Modal.prototype.registerEventListeners.call(this);
71
72 this.getModal().on(CustomEvents.events.activate, SELECTORS.PROCEED_BUTTON, function(e, data) {
73 var proceedEvent = $.Event(PaymentEvents.proceed);
74 this.getRoot().trigger(proceedEvent, this);
75
76 if (!proceedEvent.isDefaultPrevented()) {
77 this.hide();
78 data.originalEvent.preventDefault();
79 }
80 }.bind(this));
81
82 this.getModal().on(CustomEvents.events.activate, SELECTORS.CANCEL_BUTTON, function(e, data) {
83 var cancelEvent = $.Event(ModalEvents.cancel);
84 this.getRoot().trigger(cancelEvent, this);
85
86 if (!cancelEvent.isDefaultPrevented()) {
87 this.hide();
88 data.originalEvent.preventDefault();
89 }
90 }.bind(this));
91 };
92
93 // Automatically register with the modal registry the first time this module is imported so that you can create modals
94 // of this type using the modal factory.
95 if (!registered) {
96 ModalRegistry.register(ModalGateways.TYPE, ModalGateways, 'core_payment/modal_gateways');
97 registered = true;
98 }
99
100 return ModalGateways;
101});