lexeme-view.js 10.8 KB
/* global $dj, slickgrid, edit, Slick, gettext */

var text_ops = ['eq', 'ne', 'bw', 'bn', 'ew', 'en', 'cn', 'nc', 're', 'nr'];
var eqne = ['in', '-in'];
var count_ops = ['eq', 'ge', 'le'];

var extra_attr_filter_fields = {};

$.each($dj.extra_attributes, function(i, attr) {
    "use strict";
    var filter_field = {
        name: gettext("Atrybut") + ': ' + attr.name,
        gender: 'm'
    };
    if (attr.closed) {
        filter_field.ops = eqne;
        filter_field.options = attr.options;
    } else
        filter_field.ops = text_ops;
    extra_attr_filter_fields['extra-' + attr.id] = filter_field;
});

$(function() {
    "use strict";
    $(document).on('click', '.cr-link', function() {
        var id = parseInt(this.id.substr(2), 10);
        slickgrid.row_index(id);
    });
    $('#search-by-form-dialog').dialog({
        autoOpen: false,
        width: 'auto',
        height: 'auto',
        modal: false
    });
    $(document).on('click', '.search-by-form-row', function() {
        var row = $(this).data('row');
        slickgrid.set_active_when_loaded = row;
        slickgrid.grid.scrollRowToCenter(row);
        slickgrid.ensure_data();
    });
    $(document).on('click', '.highlight-pattern', function() {
        var $t = $(this);
        var pattern_id = $t.attr('data-pattern');
        var active = $t.hasClass('active-pattern');
        $('.active-pattern').removeClass('active-pattern');
        if (!active) {
            $('.p' + pattern_id).addClass('active-pattern');
        }
    });
    edit.highlight_form = undefined;
    function unhighlight_form() {
        edit.highlight_form = undefined;
        $('.searched-form').removeClass('searched-form');
    }
    $('#text-search').change(unhighlight_form).keyup(unhighlight_form);
});

var visible_vocab_ids = $.map($dj.visible_vocabularies, function(vocab) {
    "use strict";
    return vocab[0];
});

var column_names = {
    id: gettext("Nr"),
    entry: gettext("Hasło"),
    abbr_pos: gettext("Klasa gramatyczna"),
    pos: gettext("POS"),
    patterns: gettext("Wzory"),
    genders: gettext("Rodzaj/aspekt"),
    vocabs: gettext("Słowniki"),
    owner: gettext("Sł. właściciel"),
    status: gettext("Status"),
    qualifiers: gettext("Kwal.")
};

var columns = {};

$.each(column_names, function(key, name) {
    "use strict";
    columns[key] = {id: key, field: key, name: name};
});

$.extend(slickgrid, {
    loader: new Slick.Data.RemoteModel(
        $dj.ajax_get_page, $dj.ajax_search_index,
        {visible_vocabs: visible_vocab_ids}),
    grid_caption: gettext("Leksemy"),
    grid_element_id: 'lexeme-grid',
    main_field: 'entry',
    initial_columns: $dj.initial_columns,
    initial_sort_rules: ['a_fronte'],
    all_columns: columns,
    filter_fields: $.extend({
        entry: {name: gettext("Hasło"), gender: 'n', ops: text_ops},
        abbr_pos: {
            name: gettext("Klasa gramatyczna"),
            gender: 'f',
            ops: eqne,
            options: $dj.abbr_poses
        },
        part_of_speech: {
            name: gettext("POS"),
            gender: 'f',
            ops: eqne,
            options: $dj.parts_of_speech
        },
        pattern_name: {name: gettext("Wzór"), gender: 'm', ops: text_ops},
        pattern_count: {
            name: gettext("Liczba wzorów"),
            gender: 'f',
            ops: count_ops
        },
        pattern_type: {
            name: gettext("Typ wzoru"),
            gender: 'm',
            ops: eqne,
            optgroups: $dj.pattern_type_options
        },
        gender: {
            name: gettext("Rodzaj"),
            gender: 'm',
            ops: eqne,
            options: $dj.gender_options
        },
        gender_count: {name: gettext("Liczba rodzajów"), gender: 'f', ops: count_ops},
        form: {name: gettext("Forma"), gender: 'f', ops: text_ops},
        containing_vocabulary: {
            name: gettext("Słownik"),
            gender: 'm',
            ops: eqne,
            options: $dj.visible_vocabularies
        },
        owner_vocabulary: {
            name: gettext("Słownik właściciel"),
            gender: 'm',
            ops: eqne,
            options: $dj.visible_vocabularies
        },
        status: {
            name: gettext("Status"),
            gender: 'm',
            ops: eqne,
            options: $dj.status_options
        },
        comment: {name: gettext("Komentarz"), gender: 'm', ops: text_ops},
        lexeme_qualifier: {
            name: gettext("Kwal. leksemu"),
            gender: 'm',
            ops: eqne,
            optgroups: $dj.lexeme_qualifier_options
        },
        lip_qualifier: {
            name: gettext("Kwal. odmieniasia"),
            gender: 'm',
            ops: eqne,
            optgroups: $dj.form_qualifier_options
        },
        qualifier: {
            name: gettext("Kwal. przy dow. formie"),
            gender: 'm',
            ops: eqne,
            optgroups: $dj.qualifier_options
        },
        qualifiers_dor: {
            name: gettext("Kwal. z Dor."), gender: 'm', ops: text_ops},
        classification_value: {
            name: gettext("Wartość klasyfikacji"),
            gender: 'f',
            ops: eqne,
            optgroups: $dj.cv_options
        },
        borrowing_source: {
            name: gettext("Źródło zapożyczenia"),
            gender: 'n',
            ops: eqne,
            options: $dj.bs_options
        },
        gloss: {name: gettext("Glosa"), gender: 'f', ops: text_ops},
        note: {name: gettext("Nota"), gender: 'f', ops: text_ops},
        extended_note: {
            name: gettext("Nota rozszerzona"),
            gender: 'f',
            ops: text_ops
        },
        pronunciation: {name: gettext("Wymowa"), gender: 'f', ops: text_ops},
        valence: {name: gettext("Łączliwość"), gender: 'f', ops: text_ops},
        cr_type: {
            name: gettext("Typ odsyłacza"),
            gender: 'm',
            ops: eqne,
            options: $dj.cr_type_options
        },
        responsible: {
            name: gettext("Autor ostatniej zmiany"),
            gender: 'm',
            ops: eqne,
            options: $dj.user_options
        }
    }, extra_attr_filter_fields),

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

    update_sort_button: function(sort_rules) {
        "use strict";
        var icon = $('#sort-button').find('.ui-icon');
        var a_fronte = sort_rules[0] === 'a_fronte';
        if (a_fronte) {
            icon.removeClass('ui-icon-sort-atergo').addClass('ui-icon-sort');
        } else {
            icon.removeClass('ui-icon-sort').addClass('ui-icon-sort-atergo');
        }
        slickgrid.loader.onDataLoading.notify();
        var entry_index = slickgrid.grid.getColumnIndex('entry');
        if (entry_index !== undefined) {
            var columns = slickgrid.grid.getColumns();
            if (a_fronte)
                columns[entry_index].cssClass = '';
            else
                columns[entry_index].cssClass = 'atergo';
            slickgrid.grid.setColumns(columns);
        }
    },

    row_index: function(id) {
        "use strict";
        $.ajaxJSON({
            url: $dj.ajax_row_index,
            method: 'get',
            data: {
                id: id,
                sort_rules: slickgrid.loader.getSort(),
                filter: slickgrid.loader.getFilter(),
                visible_vocabs: visible_vocab_ids
            },
            callback: function(data) {
                var index = data.index;
                if (index === null) {
                    slickgrid.load_content(id);
                    slickgrid.last_active_row = undefined;
                    slickgrid.grid.resetActiveCell();
                } else {
                    slickgrid.set_active_when_loaded = index;
                    slickgrid.grid.scrollRowToCenter(index);
                    slickgrid.ensure_data();
                }
            }
        });
    },

    init_sort: function() {
        "use strict";
        var sort_rules = slickgrid.loader.getSort();
        slickgrid.update_sort_button(sort_rules);
        $('#sort-button').click(function() {
            if (sort_rules[0] === 'a_fronte')
                sort_rules[0] = 'a_tergo';
            else
                sort_rules[0] = 'a_fronte';
            slickgrid.update_sort_button(sort_rules);
            var active_id = slickgrid.active_id();
            slickgrid.loader.setSort(sort_rules);
            if (active_id)
                slickgrid.row_index(active_id);
        });
    },
    init: function() {
        "use strict";
        $('#lexeme-tabs').on('tabsactivate', function(e, ui) {
            if (ui.newPanel.html() === '') {
                edit.load_tab(ui.newPanel.attr('id'), edit.get_id());
            }
        });
    },

    search_enter: function() {
        // lista leksemów z daną formą
        "use strict";
        if ($dj.ajax_search_by_form) {
            $.ajaxJSON({
                url: $dj.ajax_search_by_form,
                method: 'get',
                data: {
                    filter: slickgrid.loader.getFilter(),
                    sort_rules: slickgrid.loader.getSort(),
                    exponent: slickgrid.search_string(),
                    visible_vocabs: visible_vocab_ids
                },
                callback: function(data) {
                    var table = $('#search-by-form-table');
                    table.empty();
                    if (data.rows.length === 0) {
                        slickgrid.search();
                        return;
                    }
                    edit.highlight_form = slickgrid.search_string();
                    if (data.rows.length === 1) {
                        var row = data.rows[0].row;
                        slickgrid.cancel_search = true;
                        slickgrid.set_active_when_loaded = row;
                        slickgrid.grid.scrollRowToCenter(row);
                        slickgrid.ensure_data();
                        return;
                    }
                    $.each(data.rows, function(i, row) {
                        // row: entry, id, pos, row
                        var tr = $('<tr/>');
                        tr.addClass('search-by-form-row').data('row', row.row);
                        $.each(slickgrid.grid.getColumns(), function(i, col_data) {
                            tr.append('<td>' + row[col_data.field] + '</td>');
                        });
                        table.append(tr);
                    });
                    $('#search-by-form-dialog').dialog('open');
                }
            });
        } else {
            slickgrid.search();
        }
    }
});