all files / src/modal/ modalDialog.js

36.21% Statements 21/58
13.64% Branches 3/22
8.33% Functions 1/12
36.36% Lines 20/55
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121                                                                                                                                          43× 43×                                                         43×        
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
var Action = require('../actionNames');
var initialState = {
    dialogId: null,
    isVisible: false,
    data: {}
};
function showModalSideeffect(id) {
    var $modal = $('.ui.modal.' + id);
    if (!$modal.length) {
        throw new Error("Can not find Modal with id", id, $modal);
    }
    $modal.modal('show');
}
function closeModalSideeffect(id) {
    $('.ui.modal.' + id).modal('hide');
}
function updateModalVisibility(stateAfter, stateBefore) {
    var dialogBefore = stateBefore.modalDialog;
    var dialogAfter = stateAfter.modalDialog;
    if (dialogBefore.isVisible !== dialogAfter.isVisible) {
        if (stateAfter.modalDialog.isVisible) {
            showModalSideeffect(dialogAfter.dialogId);
        }
        else {
            closeModalSideeffect(dialogBefore.dialogId);
        }
    }
    else if (dialogBefore.dialogId && dialogAfter.dialogId && dialogBefore.dialogId !== dialogAfter.dialogId) {
        closeModalSideeffect(dialogBefore.dialogId);
        showModalSideeffect(dialogAfter.dialogId);
    }
}
function showModal(id, data) {
    if (data === void 0) { data = {}; }
    return function (dispatch, getState) {
        var stateBefore = getState();
        dispatch({
            type: Action.SHOW_MODAL,
            dialogId: id,
            data: data
        });
        var stateAfter = getState();
        updateModalVisibility(stateAfter, stateBefore);
    };
}
exports.showModal = showModal;
function closeModal() {
    return function (dispatch, getState) {
        var stateBefore = getState();
        dispatch({
            type: Action.HIDE_MODAL
        });
        var stateAfter = getState();
        updateModalVisibility(stateAfter, stateBefore);
    };
}
exports.closeModal = closeModal;
function addError(message) {
    return {
        type: Action.MODAL_ADD_USER_MESSAGE,
        kind: "error",
        message: message
    };
}
exports.addError = addError;
function addInfo(message) {
    return {
        type: Action.MODAL_ADD_USER_MESSAGE,
        kind: "info",
        message: message
    };
}
exports.addInfo = addInfo;
function deleteUserMessage(userMessage) {
    return {
        type: Action.MODAL_DELETED_USER_MESSAGE,
        message: userMessage
    };
}
exports.deleteUserMessage = deleteUserMessage;
function modalDialog(state, action) {
    if (state === void 0) { state = initialState; }
    switch (action.type) {
        case Action.SHOW_MODAL:
            return Object.assign({}, state, {
                dialogId: action.dialogId,
                data: action.data,
                isVisible: true,
                errors: []
            });
        case Action.HIDE_MODAL:
            return Object.assign({}, state, {
                dialogId: null,
                data: null,
                isVisible: false,
                errors: []
            });
        case Action.MODAL_ADD_USER_MESSAGE: {
            var stateErrors = state.errors || [];
            var errors = stateErrors.concat([{ text: action.message, kind: action.kind }]);
            return Object.assign({}, state, {
                errors: errors
            });
        }
        case Action.MODAL_DELETED_USER_MESSAGE: {
            var errors = _.filter(state.errors.slice(), function (e) { return e.text != action.message.text; });
            return Object.assign({}, state, {
                errors: errors
            });
        }
        default:
            return state;
    }
}
exports.modalDialog = modalDialog;