reader-view.js 5.21 KB
/* global $dj, getBusyOverlay, common, slickgrid, gettext */

var AJAX_URLS = {
    'variant0': $dj.ajax_inflection_tables,
    'variant1': $dj.ajax_inflection_tables
};

(function() {
    "use strict";
    var narrowed_filter_fields = [
        'entry',
        'form',
        'abbr_pos',
        'pattern_name',
        'pattern_count',
        'pattern_type',
        'qualifier',
        'classification_value',
        'pronunciation',
        'gender',
        'gender_count'
    ];

    var narrowed_columns = [
        'entry',
        'abbr_pos',
        'patterns',
        'genders',
        'qualifiers'
    ];

    var new_filter_fields = {};

    $.each(narrowed_filter_fields, function (i, field) {
        new_filter_fields[field] = slickgrid.filter_fields[field];
    });

    $.each($dj.narrowed_attr_ids, function (i, attr_id) {
        var key = 'extra-' + attr_id;
        new_filter_fields[key] = slickgrid.filter_fields[key];
    });

    slickgrid.filter_fields = new_filter_fields;

    slickgrid.filter_fields.qualifier.name = gettext("Kwalifikator");
    slickgrid.filter_fields.classification_value.name = gettext("Pospolitość");

    var new_columns = {};

    $.each(narrowed_columns, function (i, column) {
        new_columns[column] = slickgrid.all_columns[column];
    });
    slickgrid.all_columns = new_columns;
})();

$(function () {
    "use strict";
    var sort_rules = slickgrid.loader.getSort();
    $('#variant0').hide();
    $('[data-tab=variant1]').addClass('active');
    var divs = $('#right').children('div');
    $('.switch-tab').click(function() {
        var $t = $(this);
        var tab_id = $t.attr('data-tab');
        var tab = $('#' + tab_id);
        if (tab.html() === '') {
            edit.load_tab(tab_id, edit.get_id());
        }
        divs.hide();
        tab.show();
        $('.switch-tab').removeClass('active');
        $t.addClass('active');
    });
    function set_sort() {
        // copypasta z lexeme-view.js
        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);
    }
    $('#afronte-button').click(function() {
        var $t = $(this);
        if (!$t.hasClass('menu-button-active')) {
            sort_rules[0] = 'a_fronte';
            set_sort();
        }
    });
    $('#atergo-button').click(function() {
        var $t = $(this);
        if (!$t.hasClass('menu-button-active')) {
            sort_rules[0] = 'a_tergo';
            set_sort();
        }
    });
});

var edit = {
    reader: true,
    changed: false,

    get_id: function() {
        "use strict";
        return edit.active_id;
    },
    load_tab: function(tab_id, lexeme_id, check_callback) {
        "use strict";
        var key, callback;
        key = 'lexeme_id';
        callback = function() {
            edit.busy_off();
            if (tab_id === 'variant0' || tab_id === 'variant1') {
                $('#' + tab_id).find('.form').each(function() {
                    if ($(this).text().trim() === edit.highlight_form) {
                        $(this).addClass('searched-form');
                    }
                });
            }
        };
        var data = {};
        data[key] = lexeme_id;
        if (tab_id === 'variant0') {
            data.variant = 0;
        }
        if (tab_id === 'variant1') {
            data.variant = 1;
        }
        $.ajaxJSON({
            method: 'get',
            url: AJAX_URLS[tab_id],
            dest: $('#' + tab_id),
            data: data,
            callback: callback,
            check_callback: check_callback,
            error_callback: function() {
                edit.busy_off();
            }
        });
    },
    load_content: function(id, is_created, check_callback) {
        "use strict";
        $.ajaxJSON({
            url: $dj.ajax_get_entry,
            method: 'get',
            data: {
                lexeme_id: id
            },
            callback: function(data) {
                common.update_hash('' + id + '/' + data.entry);
            }
        });
        var divs = $('#right').children('div');
        divs.empty();
        var current_div = divs.filter(':visible');
        edit.load_tab(current_div.attr('id'), id, check_callback);
        edit.active_id = id;
    },

    // busy
    busy_ctrl: null,
    busy_on: function() {
        "use strict";
        if (!edit.busy_ctrl) {
            edit.busy_ctrl = getBusyOverlay(
                $('#right')[0],
                {color: 'black', opacity: 0.2},
                {size: 30});
        }
    },
    busy_off: function() {
        "use strict";
        if (edit.busy_ctrl) edit.busy_ctrl.remove();
        edit.busy_ctrl = null;
    }
};

var backup_update_sort_button = slickgrid.update_sort_button;

slickgrid.update_sort_button = function(sort_rules) {
    "use strict";
    var a_fronte = sort_rules[0] === 'a_fronte';
    if (a_fronte) {
        $('#afronte-button').addClass('menu-button-active');
        $('#atergo-button').removeClass('menu-button-active');
    } else {
        $('#atergo-button').addClass('menu-button-active');
        $('#afronte-button').removeClass('menu-button-active');
    }
    backup_update_sort_button(sort_rules);
};