modal.js 2.36 KB
Newer Older
1
define(['jquery'], function($) {
2 3 4 5 6 7 8 9 10 11 12 13 14 15
    /**
     * Hides the modal and modal cover, using the standard selectors.
     * Note though that the class "is-fixed" on the modal cover
     * prevents the closing operation.
     */
    var hideModal = function(e) {
        if (e) {
            e.preventDefault();
        }

        var $modalCover = getModalCover();

        // Unit editors (module_edit) do not want the modal cover to hide when users click outside
        // of the editor. Users must press Cancel or Save to exit the editor.
16
        if (!$modalCover.hasClass('is-fixed')) {
17 18 19 20 21 22 23 24 25 26 27
            getModal().hide();
            hideModalCover($modalCover);
        }
    };

    /**
     * Hides just the modal cover. Caller can pass in a specific
     * element as the modal cover, otherwise the standard selector will be used.
     *
     * This method also unbinds the click handler on the modal cover.
     */
28
    var hideModalCover = function(modalCover) {
29 30 31 32
        if (modalCover == undefined) {
            modalCover = getModalCover();
        }
        modalCover.hide();
33
        modalCover.removeClass('is-fixed');
34 35 36 37 38 39
        modalCover.unbind('click');
    };

    /**
     * Shows the modal and modal cover, using the standard selectors.
     */
40
    var showModal = function() {
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
        getModal().show();
        showModalCover();
    };

    /**
     * Shows just the modal cover. The caller can optionally choose
     * to have the class "is-fixed" added to the cover, and
     * the user can also choose to specify a custom click handler
     * for the modal cover.
     *
     * This method returns the modal cover element.
     */
    var showModalCover = function(addFixed, clickHandler) {
        var $modalCover = getModalCover();
        $modalCover.show();
        if (addFixed) {
57
            $modalCover.addClass('is-fixed');
58 59 60 61 62 63 64 65 66 67 68
        }
        $modalCover.unbind('click');
        if (clickHandler) {
            $modalCover.bind('click', clickHandler);
        }
        else {
            $modalCover.bind('click', hideModal);
        }
        return $modalCover;
    };

69
    var getModalCover = function() {
70 71 72
        return $('.modal-cover');
    };

73 74
    var getModal = function() {
        return $('.modal, .showAsModal');
75 76 77 78 79 80 81 82 83
    };

    return {
        showModal: showModal,
        hideModal: hideModal,
        showModalCover: showModalCover,
        hideModalCover: hideModalCover
    };
});