Subversion Repositories SmartDukaan

Rev

Rev 36892 | Blame | Compare with Previous | Last modification | View Log | RSS feed

$(function() {

        $(document).on('click', ".contact-us", function() {
                console.log("Contact Us Button Clicked...")
                contactUs("main-content");
        });

        // ----- Add / Edit / Delete contact entries (authorised users only) -----

        // Open the modal in "add" mode for the clicked section.
        $(document).on('click', ".contact-add-btn", function() {
                var section = $(this).data('section');
                $('#contactUsForm')[0].reset();
                $('#cuId').val(0);
                $('#cuSection').val(section);
                $('#contactUsModalTitle').text(section === 'ESCALATION' ? 'Add Escalation' : 'Add Contact');
                $('#cuBaseAreaLabel').text(section === 'ESCALATION' ? 'Region' : 'Base Area');
                buildPositionOptions(section, null, false);
                $('#contactUsModal').modal('show');
        });

        // Open the modal pre-filled in "edit" mode.
        $(document).on('click', ".contact-edit-btn", function() {
                var btn = $(this);
                var section = btn.data('section');
                var mobile = String(btn.data('mobile'));
                $('#contactUsForm')[0].reset();
                $('#cuId').val(btn.data('id'));
                $('#cuSection').val(section);
                $('#cuArea').val(btn.data('area'));
                $('#cuBaseArea').val(btn.data('basearea'));
                $('#cuName').val(btn.data('name'));
                $('#cuDesignation').val(btn.data('designation'));
                $('#cuMobile').val(mobile === '-' ? '' : mobile);
                $('#cuEmail').val(btn.data('email'));
                $('#contactUsModalTitle').text('Edit Contact');
                $('#cuBaseAreaLabel').text(section === 'ESCALATION' ? 'Region' : 'Base Area');
                buildPositionOptions(section, btn.data('id'), true);
                $('#contactUsModal').modal('show');
        });

        // Persist add / edit.
        $(document).on('click', "#contactUsSaveBtn", function() {
                var name = $.trim($('#cuName').val());
                if (name === '') {
                        alert('Name is required.');
                        return;
                }
                var isEdit = parseInt($('#cuId').val(), 10) > 0;
                var posVal = $('#cuPosition').val() || (isEdit ? 'KEEP|0' : 'BOTTOM|0');
                var posParts = posVal.split('|');
                var params = {
                        id: $('#cuId').val(),
                        section: $('#cuSection').val(),
                        area: $('#cuArea').val(),
                        baseArea: $('#cuBaseArea').val(),
                        name: name,
                        designation: $('#cuDesignation').val(),
                        mobile: $('#cuMobile').val(),
                        email: $('#cuEmail').val(),
                        position: posParts[0],
                        afterId: posParts[1] || 0
                };
                doPostAjaxRequestWithParamsHandler(context + "/contactUs/save", params, function(response) {
                        if (response && response.success) {
                                $('#contactUsModal').modal('hide');
                                contactUs("main-content");
                        } else {
                                alert((response && response.message) ? response.message : 'Could not save contact.');
                        }
                });
        });

        // Soft-delete an entry (with a styled confirmation).
        $(document).on('click', ".contact-delete-btn", function() {
                var name = $(this).data('name');
                var id = $(this).data('id');
                var doDelete = function() {
                        doPostAjaxRequestWithParamsHandler(context + "/contactUs/delete", { id: id }, function(response) {
                                if (response && response.success) {
                                        contactUs("main-content");
                                } else {
                                        alert((response && response.message) ? response.message : 'Could not remove contact.');
                                }
                        });
                };
                if (typeof bootbox !== 'undefined' && bootbox.confirm) {
                        bootbox.confirm({
                                title: "Delete contact",
                                message: "Are you sure you want to remove <b>" + name + "</b> from the contact list?",
                                buttons: {
                                        confirm: { label: '<i class="fa fa-trash"></i> Yes, delete', className: 'btn-danger' },
                                        cancel: { label: 'Cancel', className: 'btn-default' }
                                },
                                callback: function(result) { if (result) { doDelete(); } }
                        });
                } else if (confirm('Remove ' + name + ' from the contact list?')) {
                        doDelete();
                }
        });

        // Move a contact one step up/down within its section (authorised users only).
        $(document).on('click', ".contact-up-btn, .contact-down-btn", function() {
                var id = $(this).data('id');
                var direction = $(this).hasClass('contact-up-btn') ? 'UP' : 'DOWN';
                doPostAjaxRequestWithParamsHandler(context + "/contactUs/reorder", { id: id, direction: direction }, function(response) {
                        if (response && response.success) {
                                contactUs("main-content");
                        } else {
                                alert((response && response.message) ? response.message : 'Could not reorder contact.');
                        }
                });
        });

});

function contactUs(domId){
        doGetAjaxRequestHandler(context+"/contactUs", function(response){
                console.log("doGetAjaxRequest");
                // Drop any previously relocated modal to avoid duplicate #contactUsModal ids.
                $('#contactUsModal').remove();
                $('#' + domId).html(response);
                // The dashboard applies `zoom` to #main-content > .wrapper, which traps a
                // Bootstrap modal behind its body-level backdrop ("screen dims, no popup").
                // Move the modal out to <body> so it renders above the backdrop.
                $('#contactUsModal').appendTo('body');
        });
}

// Populate the "Position in list" dropdown for the given section by scanning the rendered rows.
// On edit, the contact being edited is excluded and "Keep current position" is offered as default.
function buildPositionOptions(section, excludeId, isEdit){
        var $sel = $('#cuPosition').empty();
        if (isEdit) {
                $sel.append($('<option>').val('KEEP|0').text('Keep current position'));
        }
        $sel.append($('<option>').val('TOP|0').text('Top of list'));
        $('#contact-us-table tr[data-cu-section="' + section + '"]').each(function(){
                var cid = String($(this).data('cuId'));
                if (excludeId != null && cid === String(excludeId)) {
                        return; // skip the contact being edited - can't place it after itself
                }
                $sel.append($('<option>').val('AFTER|' + cid).text('After — ' + $(this).data('cuName')));
        });
        $sel.append($('<option>').val('BOTTOM|0').text('Bottom of list'));
        $sel.val(isEdit ? 'KEEP|0' : 'BOTTOM|0');
}