1 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 * Contain the logic for modal backdrops.
19 * @module core/modal_backdrop
20 * @class modal_backdrop
22 * @copyright 2016 Ryan Wyllie <ryan@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 define(['jquery', 'core/templates', 'core/notification'],
26 function($, Templates, Notification) {
29 ROOT: '[data-region="modal-backdrop"]',
33 * Constructor for ModalBackdrop.
35 * @param {object} root The root element for the modal backdrop
37 var ModalBackdrop = function(root) {
39 this.isAttached = false;
41 if (!this.root.is(SELECTORS.ROOT)) {
42 Notification.exception({message: 'Element is not a modal backdrop'});
47 * Get the root element of this modal backdrop.
50 * @return {object} jQuery object
52 ModalBackdrop.prototype.getRoot = function() {
57 * Add the modal backdrop to the page, if it hasn't already been added.
61 ModalBackdrop.prototype.attachToDOM = function() {
62 if (this.isAttached) {
66 $('body').append(this.root);
67 this.isAttached = true;
71 * Set the z-index value for this backdrop.
74 * @param {int} value The z-index value
76 ModalBackdrop.prototype.setZIndex = function(value) {
77 this.root.css('z-index', value);
81 * Check if this backdrop is visible.
86 ModalBackdrop.prototype.isVisible = function() {
87 return this.root.hasClass('show');
91 * Check if this backdrop has CSS transitions applied.
93 * @method hasTransitions
96 ModalBackdrop.prototype.hasTransitions = function() {
97 return this.getRoot().hasClass('fade');
101 * Display this backdrop. The backdrop will be attached to the DOM if it hasn't
106 ModalBackdrop.prototype.show = function() {
107 if (this.isVisible()) {
111 if (!this.isAttached) {
115 this.root.removeClass('hide').addClass('show');
119 * Hide this backdrop.
123 ModalBackdrop.prototype.hide = function() {
124 if (!this.isVisible()) {
128 if (this.hasTransitions()) {
129 // Wait for CSS transitions to complete before hiding the element.
130 this.getRoot().one('transitionend webkitTransitionEnd oTransitionEnd', function() {
131 this.getRoot().removeClass('show').addClass('hide');
134 this.getRoot().removeClass('show').addClass('hide');
139 * Remove this backdrop from the DOM.
143 ModalBackdrop.prototype.destroy = function() {
147 return ModalBackdrop;