lexeme-slickgrid-view.js 7.84 KB
function list_searchoptions(list) {
    "use strict";
    return {
        searchhidden: true,
        sopt: ['eq', 'ne'],
        value: list
    };
}

function list_column(name, displayed, list) {
    "use strict";
    return {
        name: name,
        index: name,
        hidden: true,
        hidedlg: !displayed,
        sortable: false,
        searchoptions: list_searchoptions(list),
        stype: 'select'
    };
}

function text_column(name) {
    "use strict";
    return {
        name: name,
        index: name,
        hidden: true,
        hidedlg: true,
        sortable: false,
        searchoptions: {searchhidden: true}
    };
}

function lip_column(name) {
    "use strict";
    return {
        name: name,
        index: name,
        hidden: true,
        sortable: false,
        searchoptions: {searchhidden: true, sopt: ['eq', 'ne']}
    };
}

function count_column(name) {
    "use strict";
    return {
        name: name,
        index: name,
        hidden: true,
        hidedlg: true,
        sortable: false,
        searchoptions: {searchhidden: true, sopt: ['eq', 'ge', 'le']}
    };
}

$.extend(slickgrid, {
    grid_element_id: 'lexeme-grid',
    main_field: 'entry',
    columns: [
        {id: "entry", name: "Hasło", field: "entry"},
        {id: "pos", name: "Część mowy", field: "pos"}
    ],
    all_columns: {
        id: {id: "id", name: "Nr", field: "id"},
        entry: {id: "entry", name: "Hasło", field: "entry"},
        pos: {id: "pos", name: "Część mowy", field: "pos"},
        patterns: {id: "patterns", name: "Wzory", field: "patterns"},
        ics: {id: "ics", name: "Char. fleks.", field: "ics"},
        vocabs: {id: "vocabs", name: "Słowniki", field: "vocabs"},
        owner: {id: "owner", name: "Sł. właściciel", field: "owner"},
        status: {id: "status", name: "Status", field: "status"}
    },
    initialColModel: [
        {name: 'id', index: 'id', search: false, hidden: true},
        {name: 'entry', index: 'entry'},
        {
            name: 'part_of_speech',
            index: 'part_of_speech',
            searchoptions: list_searchoptions($dj.parts_of_speech),
            stype: 'select'
        },
        lip_column('pattern_name'),
        count_column('pattern_count'),
        lip_column('inflection_characteristic'),
        count_column('ic_count'),
        {
            name: 'form',
            index: 'form',
            hidden: true,
            hidedlg: true,
            sortable: false,
            searchoptions: {searchhidden: true}
        },
        list_column('containing_vocabulary', true, $dj.visible_vocabularies),
        list_column('owner_vocabulary', true, $dj.visible_vocabularies),
        list_column('status', true, $dj.status_options),
        text_column('comment'),
        list_column('lexeme_qualifier', false, $dj.qualifier_options),
        list_column('lip_qualifier', false, $dj.qualifier_options),
        list_column('qualifier', false, $dj.qualifier_options),
        list_column('classification_value', false, $dj.cv_options),
        text_column('gloss'),
        text_column('note'),
        list_column('cr_type', false, $dj.cr_type_options)
    ],
    initialColNames: [
        'Id', 'Hasło', 'Część mowy', 'Wzór', 'Liczba wzorów', 'Char. fleks.',
        'Liczba char. fleks.', 'Forma', 'Słownik używający', 'Słownik właściciel',
        'Status', 'Komentarz', 'Kwal. leksemu', 'Kwal. odmieniasia',
        'Kwal. przy dow. formie', 'Wartość klasyfikacji', 'Glosa', 'Nota',
        'Typ odsyłacza'
    ],
    initial_sort_rules: [
        {field: 'entry', order: 'asc', a_tergo: false},
        {field: 'part_of_speech', order: 'asc'},
        {field: 'id', order: 'asc'}
    ],
    grid_caption: "Leksemy"
});

slickgrid.sort_rule_special_features = function(rule) {
    "use strict";
    $('#entries_a_tergo').prop('checked', rule.a_tergo);
};

slickgrid.prepare_sort_rules = function(sort_rules, old_sort_rules) {
    "use strict";
    if (sort_rules[0].field === 'entry') {
        var a_tergo = false;
        $.each(old_sort_rules, function (i, rule) {
            if (rule.field === 'entry') {
                a_tergo = rule.a_tergo;
                return false;
            }
            return null;
        });
        sort_rules[0].a_tergo = a_tergo;
    }
};

slickgrid.load_content = function(id, is_created) {
    "use strict";
    function check() {
        return id === slickgrid.active_id();
    }
    edit.load_content(id, is_created, check);
};

// filtry

function create_button(button_class, icon_class, text) {
    "use strict";
    var button = $('<a/>')
        .addClass('ui-state-default ui-corner-all ' + button_class)
        .addClass('fm-button fm-button-icon-left');
    var icon = $('<span/>').addClass('ui-icon ' + icon_class);
    button.append(icon).append(text);
    return button;
}

slickgrid.add_filter_buttons = function(f) {
    "use strict";
    if (f[0].p) {
        var button_save = create_button(
            'ui-save', 'ui-icon-disk', 'Zapisz filtr');
        var button_load = create_button(
            'ui-load', 'ui-icon-arrowthickstop-1-s', 'Załaduj filtr');
        f.next().find('tr').last().children().first()
            .append(button_save).append(button_load);
        button_save.click(function () {
            var filter_name, filters, post_data;
            filter_name = window.prompt("Wybierz nazwę filtru do zapisania");
            if (filter_name) {
                filters = $('.searchFilter').jqFilter('filterData');
                post_data = {
                    name: filter_name,
                    serialized_filter: $.toJSON(filters)
                };
                var save_filter_data = {
                    method: 'post',
                    url: $dj.ajax_save_filter,
                    data: post_data,
                    description: 'Zapisanie filtru'
                };
                $.ajaxJSON($.extend(save_filter_data, {
                    callback: function (data) {
                        if (data.exists && window.confirm(
                                "Filtr o tej nazwie już istnieje. Nadpisać?")) {
                            $.ajaxJSON($.extend(save_filter_data, {
                                data: $.extend(post_data, {force: true})
                            }));
                        }
                    }
                }));
            }
            return false;
        });
        button_load.click(function () {
            $.ajaxJSON({
                method: 'get',
                url: $dj.ajax_get_filters,
                data: {},
                description: 'Pobranie listy filtrów',
                callback: filter_choice
            });
            return false;
        });
    }
};

function filter_choice(ajax_data) {
    "use strict";
    var filters = ajax_data.filters;
    var list = $('#filter-list');
    list.html('');
    // napakować okienko na podstawie wyniku z ajaxa
    $.each(filters, function (i, filter) {
        var item = $('<li/>').addClass('ui-state-default').attr('id', filter.id);
        var delete_button = $('<span/>')
            .addClass('remove ui-icon ui-icon-closethick');
        delete_button.click(function () {
            if (window.confirm('Usunąć filtr "' + filter.name + '"?')) {
                $.ajaxJSON({
                    method: 'post',
                    url: $dj.ajax_delete_filter,
                    data: {id: filter.id},
                    description: 'Usunięcie filtru',
                    callback: function () {
                        item.remove();
                    }
                });
            }
            return false;
        });
        item.append(delete_button);
        item.append(filter.name);
        item.click(function () {
            slickgrid.put_filter(filter.json);
            $("#load-filter-dialog").dialog('close');
        });
        list.append(item);
    });
    $("#load-filter-dialog").dialog('open');
}