slickgrid.js 6.3 KB
var loader = new Slick.Data.RemoteModel(
    $dj.ajax_get_page, $dj.ajax_search_index);

function search_string() {
    "use strict";
    return $('#text-search').val();
}

var slickgrid = {
    grid: null,

    active_id: function(cell) {
        "use strict";
        if (cell === undefined)
            cell = slickgrid.grid.getActiveCell();
        var row = cell.row;
        if (row === undefined)
            return undefined;
        return slickgrid.grid.getDataItem(row).id;
    },

    scroll_search: function() {
        "use strict";
        // data = aktualne filtry i sort_rules
        loader.searchRow(search_string(), function (row) {
            slickgrid.grid.scrollRowToTop(row);
            slickgrid.grid.setActiveCell(row, 0);
        });
    },

    search: function() {
        "use strict";
        if ($dj.filtering_mode) {
            loader.setSearch(search_string());
            slickgrid.grid.scrollRowToTop(0);
            slickgrid.ensure_data();
            slickgrid.grid.setActiveCell(0, 0);
        } else {
            slickgrid.scroll_search();
        }
    },

    ensure_data: function() {
        "use strict";
        var vp = slickgrid.grid.getViewport();
        loader.ensureData(vp.top, vp.bottom);
    },

    reload_data: function() {
        "use strict";
        var vp = slickgrid.grid.getViewport();
        loader.reloadData(vp.top, vp.bottom);
    }
};

var options = {
    editable: false,
    enableAddRow: false,
    enableCellNavigation: true
};

var loadingIndicator = null;

function set_columns() {
    "use strict";
    var chosen_column_ids = $('#column-list').val();
    var chosen_columns = $.map(chosen_column_ids, function(id) {
        return slickgrid.all_columns[id];
    });
    slickgrid.grid.setColumns(chosen_columns);
    slickgrid.grid.autosizeColumns();
    /*$.ajaxJSON({
        method: 'post',
        url: $dj.ajax_save_columns,
        data: {columns: chosen_columns}
    });*/ //TODO
}

$(function() {
    "use strict";
    slickgrid.element = $('#' + slickgrid.grid_element_id);
    slickgrid.grid = new Slick.Grid(
        slickgrid.element, loader.data, slickgrid.columns, options);

    //slickgrid.grid.setSelectionModel(new Slick.RowSelectionModel());

    slickgrid.grid.onViewportChanged.subscribe(function (e, args) {
        slickgrid.ensure_data();
    });

    slickgrid.grid.onSort.subscribe(function (e, args) {
        loader.setSort(args.sortCol.field, args.sortAsc ? 1 : -1);
        slickgrid.ensure_data();
    });

    var timeout_handler;
    var last_active_row;
    slickgrid.grid.onActiveCellChanged.subscribe(function(e, cell) {
        if (cell.row === last_active_row)
            return;
        if (last_active_row !== undefined && edit.changed) {
            if (!window.confirm('Czy chcesz porzucić niezapisane zmiany?')) {
                slickgrid.grid.setActiveCell(last_active_row, 0);
                return;
            }
        }
        last_active_row = cell.row;
        if (cell.row !== undefined) {
            clearTimeout(timeout_handler);
            timeout_handler = setTimeout(function() {
                common.busy_on();
                slickgrid.load_content(slickgrid.active_id(cell));
            }, 500);
        }
    });

    loader.onDataLoading.subscribe(function () {
        if (!loadingIndicator) {
            loadingIndicator = $(
                "<span class='loading-indicator'><label>Ładowanie..." +
                "</label></span>").appendTo(document.body);
            var $g = slickgrid.element;

            loadingIndicator
                .css("position", "absolute")
                .css("top", $g.position().top + $g.height() / 2 - loadingIndicator.height() / 2)
                .css("left", $g.position().left + $g.width() / 2 - loadingIndicator.width() / 2);
        }

        loadingIndicator.show();
    });

    loader.onDataLoaded.subscribe(function (e, args) {
        for (var i = args.from; i <= args.to; i++) {
            slickgrid.grid.invalidateRow(i);
        }

        slickgrid.grid.updateRowCount();
        slickgrid.grid.render();

        loadingIndicator.fadeOut();
        layout.adjust_grid_size();
        var num = slickgrid.grid.getDataLength();
        document.title = slickgrid.grid_caption + ' (' + num + ')';

        if (!slickgrid.grid.getActiveCell()) {
            var vp = slickgrid.grid.getViewport();
            slickgrid.grid.setActiveCell(vp.top, 0);
        }
    });

    var search_timeout_handler;
    $("#text-search").keyup(function (e) {
        if (e.which === 13) {
            slickgrid.search();
        } else if ($dj.auto_search) {
            clearTimeout(search_timeout_handler);
            search_timeout_handler = setTimeout(slickgrid.search, 500);
        }
    });

    if ($dj.filtering_mode) {
        loader.setSearch(search_string());
    }

    loader.setSort($dj.sort_rules || slickgrid.initial_sort_rules);

    loader.setFilters([]);

    $('#search-button').click(slickgrid.search);

    // load the first page
    slickgrid.grid.onViewportChanged.notify();

    $('#choose-columns-dialog').dialog({
        autoOpen: false,
        width: 'auto',
        modal: true,
        buttons: [
            {
                text: "Anuluj",
                click: function () {
                    $(this).dialog("close");
                }
            },
            {
                text: "Zatwierdź",
                click: function() {
                    set_columns();
                    $(this).dialog("close");
                }
            }
        ]
    });
    var column_list = $('#column-list');
    $('#show-columns-button').click(function() {
        $('#choose-columns-dialog').dialog('open');
        column_list.multiselect('refresh');
    });
    var column_ids = [];
    function add_column_option(column, selected) {
        var option = $('<option/>');
        option.attr('value', column.id);
        option.prop('selected', selected);
        option.text(column.name);
        column_list.append(option);
    }
    $.each(slickgrid.columns, function(i, column) {
        add_column_option(column, true);
        column_ids.push(column.id);
    });
    $.each(slickgrid.all_columns, function(id, column) {
        if (column_ids.indexOf(id) === -1)
            add_column_option(column, false);
    });
    column_list.multiselect({
        sortable: true
    });

    layout.adjust_tabs();
});