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

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: attr.name,
        gender: attr.gender
    };
    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 $t = $(this);
        $('#by-form-container').find('.active').removeClass('active');
        $t.addClass('active');
        var row = $t.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 checked');
        if (!active) {
            $t.addClass('checked');
            $('.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];
});

// parametr width jest aktualnie ignorowany, bo jest ustawione forceFitColumns,
// ale bez tego kolumny nie dopasowują się do szerokości listy przy przeciąganiu
var column_data = {
    id: {
        name: gettext("id"),
        width: 65
    },
    entry: {
        name: gettext("entry"),
        minWidth: 100
    },
    abbr_pos: {
        name: gettext("lex. cl."),
        width: 55,
        maxWidth: 55
    },
    pos: {
        name: gettext("POS"),
        width: 60,
        maxwidth: 60
    },
    patterns: {
        name: gettext("patterns"),
        width: 60,
        minWidth: 60
    },
    genders: {
        name: gettext("gnr."),
        width: 60,
        minWidth: 40,
        maxWidth: 80
    },
    vocabs: {
        name: gettext("dictionaries"),
        width: 65
    },
    owner: {
        name: gettext("owner dict."),
        width: 65
    },
    status: {
        name: gettext("status"),
        width: 70
    },
    qualifiers: {
        name: gettext("qual."),
        width: 60
    }
};

var columns = {};
var main_field = 'entry';

$.each(column_data, function(key, data) {
    "use strict";
    columns[key] = data;
    columns[key].id = key;
    columns[key].field = key;
    if (key === main_field)
        columns[key].cssClass = 'main-column';
});

$.extend(slickgrid, {
    loader: new Slick.Data.RemoteModel(
        $dj.ajax_get_page, $dj.ajax_search_index),
    grid_caption: gettext("Lexemes"),
    counter: function(count) {
        "use strict";
        return ngettext("lexeme", "lexemes", count);
    },
    grid_element_id: 'lexeme-grid',
    main_field: main_field,
    initial_columns: $dj.initial_columns,
    initial_sort_rules: ['a_fronte'],
    all_columns: columns,
    filter_fields: $.extend({
        entry: {name: gettext("Entry"), gender: 'n', ops: text_ops},
        abbr_pos: {
            name: gettext("Lexical class"),
            gender: 'f',
            ops: eqne,
            options: $dj.abbr_poses
        },
        part_of_speech: {
            name: gettext("POS"),
            gender: 'm',
            ops: eqne,
            options: $dj.parts_of_speech
        },
        pattern_name: {name: gettext("Pattern"), gender: 'm', ops: text_ops},
        pattern_count: {
            name: gettext("Pattern count"),
            gender: 'f',
            ops: count_ops
        },
        pattern_type: {
            name: gettext("Pattern type"),
            gender: 'm',
            ops: eqne,
            optgroups: $dj.pattern_type_options
        },
        gender: {
            name: gettext("Gender"),
            gender: 'm',
            ops: eqne,
            options: $dj.gender_options
        },
        gender_count: {name: gettext("Gender count"), gender: 'f', ops: count_ops},
        form: {name: gettext("Form"), gender: 'f', ops: text_ops},
        containing_vocabulary: {
            name: gettext("Dictionary"),
            gender: 'm',
            ops: eqne,
            options: $dj.visible_vocabularies
        },
        owner_vocabulary: {
            name: gettext("Owner dictionary"),
            gender: 'm',
            ops: eqne,
            options: $dj.visible_vocabularies
        },
        status: {
            name: gettext("Status"),
            gender: 'm',
            ops: eqne,
            options: $dj.status_options
        },
        comment: {name: gettext("Comment"), gender: 'm', ops: text_ops},
        lexeme_qualifier: {
            name: gettext("Lexeme qual."),
            gender: 'm',
            ops: eqne,
            optgroups: $dj.lexeme_qualifier_options
        },
        lip_qualifier: {
            name: gettext("Inflection qual."),
            gender: 'm',
            ops: eqne,
            optgroups: $dj.form_qualifier_options
        },
        qualifier: {
            name: gettext("Any qualifier"),
            gender: 'm',
            ops: eqne,
            optgroups: $dj.qualifier_options
        },
        qualifiers_dor: {
            name: gettext("Dor. qual."), gender: 'm', ops: text_ops},
        classification_value: {
            name: gettext("Classification value"),
            gender: 'f',
            ops: eqne,
            optgroups: $dj.cv_options
        },
        borrowing_source: {
            name: gettext("Borrowing source"),
            gender: 'n',
            ops: eqne,
            options: $dj.bs_options
        },
        gloss: {name: gettext("Gloss"), gender: 'f', ops: text_ops},
        note: {name: gettext("Note"), gender: 'f', ops: text_ops},
        extended_note: {
            name: gettext("Extended note"),
            gender: 'f',
            ops: text_ops
        },
        pronunciation: {name: gettext("Pronunciation"), gender: 'f', ops: text_ops},
        valence: {name: gettext("Valence"), gender: 'f', ops: text_ops},
        cr_type: {
            name: gettext("Cross-reference type"),
            gender: 'm',
            ops: eqne,
            options: $dj.cr_type_options
        },
        responsible: {
            name: gettext("Last change's author"),
            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 =
                    columns[entry_index].cssClass
                        .replace(/(?:^|\s)atergo(?!\S)/g , '');
            else
                columns[entry_index].cssClass += ' atergo';
            slickgrid.grid.setColumns(columns);
        }
    },

    query_params: function() {
        "use strict";
        return {
            sort_rules: slickgrid.loader.getSort(),
            filter: slickgrid.loader.getFilter(),
            visible_vocabs: visible_vocab_ids,
            reader: $dj.reader
        };
    },

    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";
        $('<div/>').attr('id', 'by-form-container')
            .insertBefore($('.slick-viewport'));

        slickgrid.grid.onClick.subscribe(function () {
            slickgrid.hide_by_form();
        });
        slickgrid.grid.onKeyDown.subscribe(function () {
            slickgrid.hide_by_form();
        });
        $('#text-search').keydown(function() {
            slickgrid.hide_by_form();
        });
        $('.hide-by-form').click(function() {
            slickgrid.hide_by_form();
        });

        $('#lexeme-tabs').on('tabsactivate', function(e, ui) {
            if (ui.newPanel.html() === '') {
                edit.load_tab(ui.newPanel.attr('id'), slickgrid.get_id());
            }
        });
    },

    search_enter: function() {
        // lista leksemów z daną formą
        "use strict";
        slickgrid.search_counter++;
        slickgrid.last_search = undefined;
        if ($dj.ajax_search_by_form) {
            $.ajaxJSON({
                url: $dj.ajax_search_by_form,
                method: 'get',
                data: {
                    exponent: slickgrid.search_string(),
                    query_params: slickgrid.query_params(),
                    columns: slickgrid.loader.getColumns()
                },
                callback: function(data) {
                    var table = $('#by-form-container');
                    table.empty();
                    if (data.rows.length === 0) {
                        slickgrid.search();
                        return;
                    }
                    slickgrid.last_search = slickgrid.search_string();
                    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 = $('<div/>');
                        tr.addClass('ui-widget-content slick-row');
                        tr.addClass('search-by-form-row').data('row', row.row);
                        tr.addClass('scheme' + row.scheme);
                        $.each(slickgrid.grid.getColumns(), function(i, col_data) {
                            var td = $('<div/>').text(row[col_data.field]);
                            td.addClass('slick-cell l' + i + ' r' + i);
                            if (i === 0)
                                td.addClass('main-column');
                            tr.append(td);
                        });
                        table.append(tr);
                    });
                    table.show();
                    layout.adjust_grid_size();
                    table.children().first().click();
                    // $('#search-by-form-dialog').dialog('open');
                }
            });
        } else {
            slickgrid.search();
        }
    },

    hide_by_form: function() {
        "use strict";
        var by_form = $('#by-form-container');
        if (by_form.is(':visible')) {
            by_form.hide();
            layout.adjust_grid_size();
        }
    }
});