Saturday, March 15, 2014

/// <reference path="../Views/contact/contactPage.html" />
/// <reference path="../Views/contact/contactPage.html" />
var contact = new contactcontent();       /* This is the only global variable. Everything goes inside */

function contactcontent() {
    /* these are private methods */
    function addContact($tg) {
        alert('adding');
        $("#divContact").dialog({
            title: 'Contact Form',
            width: 900,
            height: 650,
            closed: false,
            cache: false,
            href: 'Contact/Create',
            modal: true,
            //buttons: [{
            //    'class': 'save-button',
            //    text: 'Ok',
            //    handler: function () {
            //        saveContact(null)
            //    }

            //}, {
            //    'class': 'exit-button',
            //    text: 'Cancel',
            //    handler: function () {
            //        $("#divContact").dialog("close");
            //    }
            //}]
        });

    }

    function editContact($tg) {

        var node = $tg.datagrid('getSelected');
        if (node) {
            $("#divContact").dialog({
                title: 'Contact Form',
                width: 900,
                height: 650,
                closed: false,
                cache: false,
                //href: 'contactPage.html',
                href: 'Contact/Edit/' + node.Id,
                modal: true,
                //buttons: [{
                //    text: 'Ok',
                //    'class': 'save-button',
                //    handler: function () {
                //        saveContact(node.Id)
                //    }

                //}, {
                //    'class': 'exit-button',
                //    text: 'Cancel',
                //    handler: function () {
                //        $("#divContact").dialog("close");
                //    }
                //}]
            });
        }
    }

    function deleteContact($tg) {
        var node = $tg.datagrid('getSelected');
        if (node) {
            $.ajax({
                url: 'contact/Deletecontact',
                type: 'POST',
                data: { Id: node.Id },
                dataType: 'json',
                //async:false,
                success: function (result) {
                    var index = $tg.datagrid('getRowIndex', node);
                    $tg.datagrid('deleteRow', index);
                    var lIndex = $tg.datagrid('getRows').length - 1;

                    if (index > lIndex)
                        index = lIndex;

                    $tg.datagrid('selectRow', index);
                }
            });

        }
    }

    function saveContact(id) {
        //alert(id);
        $.ajax({
            url: 'Contact/SaveContact',
            type: 'POST',
            data: $("form").serialize(),
            dataType: 'json',
            //async:false,
            success: function (result) {
                $("#divContact").dialog("close");
                if (result.Success == true) {
                    refreshContacts(result);
                }
                else {
                    alert('Error occured while saving: ' + result.ErrorMessage);
                }
            }
        });
    }

    function refreshContact(result) {
        var grd = $('#grid-Contact');
        if (result.NewModel) {
            grd.datagrid('appendRow', {
                Id: result.Id,
                FristName: result.Response.FristName,
                LastName: result.Response.LastName,
                CompanyName: result.Response.CompanyName,
                IsActive: result.Response.IsActive,
            });

            grd.datagrid('selectRow', grd.datagrid('getRows').length - 1);
            //grd.datagrid('acceptChanges');
        }
        else {
            var node = grd.datagrid('getSelected');

            grd.datagrid('updateRow', {
                index: grd.datagrid('getRowIndex', node),
                row: {
                    Id: result.Id,
                    FristName: result.Response.FristName,
                    LastName: result.Response.LastName,
                    CompanyName: result.Response.CompanyName,
                    IsActive: result.Response.IsActive,
                }
            });
        }
    }
    /*following are public methods*/
    return {
        init: function () {
            $(function () {

                $("#add-Contact").click(function () {
                    addContact();
                });

                $("#edit-Contact").click(function () {
                    editContact($('#grid-Contact'));
                });

                $("#delete-Contact").click(function () {
                    deleteContact($('#grid-Contact'));
                });

                $("#save-Contact").click(function () {
                    saveContact($('#grid-Contact').datagrid('getSelected').Id);
                });

                $("#exit-Contact").click(function () {
                    $("#divContact").dialog("close");
                });

                $('.datepicker').datepicker();
            });
        }
    }
};
contact.init();

No comments:

Post a Comment