﻿<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>jsGrid - Data Manipulation</title>
    <link rel="stylesheet" type="text/css" href="demos.css" />
    <link href='http://fonts.googleapis.com/css?family=Open+Sans:300,600,400' rel='stylesheet' type='text/css'>

    <link rel="stylesheet" type="text/css" href="../css/jsgrid.css" />
    <link rel="stylesheet" type="text/css" href="../css/theme.css" />

    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/cupertino/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>

    <script src="db.js"></script>

    <script src="../src/jsgrid.core.js"></script>
    <script src="../src/jsgrid.load-indicator.js"></script>
    <script src="../src/jsgrid.load-strategies.js"></script>
    <script src="../src/jsgrid.sort-strategies.js"></script>
    <script src="../src/jsgrid.field.js"></script>
    <script src="../src/jsgrid.field.text.js"></script>
    <script src="../src/jsgrid.field.number.js"></script>
    <script src="../src/jsgrid.field.select.js"></script>
    <script src="../src/jsgrid.field.checkbox.js"></script>
    <script src="../src/jsgrid.field.control.js"></script>

    <style>
        .details-form-field input,
        .details-form-field select {
            width: 250px;
            float: right;
        }

        .details-form-field {
            margin: 15px 0;
        }

        .ui-widget *, .ui-widget input, .ui-widget select, .ui-widget button {
            font-family: 'Helvetica Neue Light', 'Open Sans', Helvetica;
            font-size: 14px;
            font-weight: 300 !important;
        }
    </style>
</head>
<body>
    <h1>Data Manipulation</h1>
    <div id="jsGrid"></div>

    <div id="detailsForm">
        <div class="details-form-field">
            <label>Name: <input id="name" type="text" /></label>
        </div>
        <div class="details-form-field">
            <label>Age: <input id="age" type="number" /></label>
        </div>
        <div class="details-form-field">
            <label>Address: <input id="address" type="text" /></label>
        </div>
        <div class="details-form-field">
            <label>Country:
                <select id="country">
                    <option value="0">(Select)</option>
                    <option value="1">United States</option>
                    <option value="2">Canada</option>
                    <option value="3">United Kingdom</option>
                    <option value="4">France</option>
                    <option value="5">Brazil</option>
                    <option value="6">China</option>
                    <option value="7">Russia</option>
                </select>
            </label>
        </div>
        <div class="details-form-field">
            <label>Is Married: <input id="married" type="checkbox" /></label>
        </div>
        <div class="details-form-field">
            <button type="button" id="save">Save</button>
        </div>
    </div>

    <script>
        $(function() {

            $("#jsGrid").jsGrid({
                height: "70%",
                width: "100%",
                editing: true,
                autoload: true,
                paging: true,
                deleteConfirm: function(item) {
                    return "The client \"" + item.Name + "\" will be removed. Are you sure?";
                },
                rowClick: function(args) {
                    showDetailsDialog("Edit", args.item);
                },
                controller: db,
                fields: [
                    { name: "Name", type: "text", width: 150 },
                    { name: "Age", type: "number", width: 50 },
                    { name: "Address", type: "text", width: 200 },
                    { name: "Country", type: "select", items: db.countries, valueField: "Id", textField: "Name" },
                    { name: "Married", type: "checkbox", title: "Is Married", sorting: false },
                    {
                        type: "control",
                        modeSwitchButton: false,
                        editButton: false,
                        headerTemplate: function() {
                            return $("<button>").attr("type", "button").text("Add")
                                    .on("click", function () {
                                        showDetailsDialog("Add", {});
                                    });
                        }
                    }
                ]
            });

            $("#detailsForm").dialog({
                autoOpen: false,
                width: 400
            });

            var showDetailsDialog = function(dialogType, client) {
                $("#name").val(client.Name);
                $("#age").val(client.Age);
                $("#address").val(client.Address);
                $("#country").val(client.Country);
                $("#married").prop("checked", client.Married);

                $("#save").off("click").on("click", function() {
                    saveClient(client, dialogType === "Add");
                });

                $("#detailsForm").dialog("option", "title", dialogType + " Client")
                    .dialog("open");
            };

            var saveClient = function(client, isNew) {
                $.extend(client, {
                    Name: $("#name").val(),
                    Age: parseInt($("#age").val(), 10),
                    Address: $("#address").val(),
                    Country: parseInt($("#country").val(), 10),
                    Married: $("#married").is(":checked")
                });

                $("#jsGrid").jsGrid(isNew ? "insertItem" : "updateItem", client);

                $("#detailsForm").dialog("close");
            };

        });
    </script>
</body>
</html>
