export.js 5.84 KB
/* global $dj, gettext */

// straszna i zła copypasta
function selected_qualifiers_text(num, total, checked) {
    "use strict";
    if (num >= 4)
        return num + ' ' + gettext("kwalifikatorów");
    var grouped = {};
    $.each(checked, function (i, elem) {
        var id = elem.value;
        var vocab = $dj.vocabs[id];
        if (grouped[vocab] === undefined)
            grouped[vocab] = [];
        grouped[vocab].push(elem.title);
    });
    var vocab_table = [];
    var owner = $('#owner-vocabulary').text();
    if (grouped[owner])
        vocab_table.push(owner + ': ' + grouped[owner].join(', '));
    $.each(grouped, function (vocab, qualifiers) {
        if (vocab !== owner)
            vocab_table.push(vocab + ': ' + qualifiers.join(', '));
    });
    return vocab_table.join('; ');
}

var new_row_counter = 1;

function add_qualifier_row(qualifier, regex) {
    "use strict";
    $.ajaxJSON({
        type: 'get',
        url: $dj.ajax_new_qualifier_row,
        data: {num: new_row_counter},
        callback: function(data) {
            var new_row = $(data.html);
            $('#magic-qualifiers').append(new_row);
            new_row.find('button').button();
            var row_id = new_row.prop('id');
            $('#id_' + row_id + '-qualifier').val(qualifier);
            $('#id_' + row_id + '-regex').val(regex);
            new_row_counter++;
        }
    });
}

var remove_qualifier_row = function () {
    "use strict";
    $(this).closest('li').remove();
};

function save_export_data() {
    "use strict";
    var name, post_data;
    name = window.prompt(gettext("Wybierz nazwę"));
    if (name) {
        post_data = {
            name: name,
            serialized_data: $.toJSON($('#export-form').serializeArray())
        };
        var save_data = {
            method: 'post',
            url: $dj.ajax_save_export_data,
            data: post_data,
            description: gettext("Zapisanie ustawień")
        };
        $.ajaxJSON($.extend(save_data, {
            callback: function (data) {
                if (data.exists && window.confirm(
                        gettext("Ta nazwa jest zajęta. Nadpisać?"))) {
                    $.ajaxJSON($.extend(save_data, {
                        data: $.extend(post_data, {force: true})
                    }));
                }
            }
        }));
    }
    return false;
}

function put_data(json) {
    "use strict";
    var data = $.parseJSON(json),
        qualifier_rows = {}, vocabs = [], antivocabs = [], parts;
    $('#id_refl').attr('checked', false);
    $('#id_commonness').attr('checked', false);
    $('#magic-qualifiers').find('li').remove();
    $.each(data, function (i, item) {
        if (item.name === 'vocabs') {
            vocabs.push(item.value);
        }
        if (item.name === 'antivocabs') {
            antivocabs.push(item.value);
        }
        if (item.name === 'variant') {
            $('#id_variant').val(item.value);
        }
        if (item.name === 'refl' || item.name === 'commonness') {
            $('#id_' + item.name).attr('checked', true);
        }
        if (item.name.indexOf('magic') === 0) {
            parts = item.name.split('-');
            if (!qualifier_rows[parts[0]]) {
                qualifier_rows[parts[0]] = {};
            }
            qualifier_rows[parts[0]][parts[1]] = item.value;
        }
        if (item.name === 'copyright') {
            $('#id_copyright').val(item.value);
        }
    });
    $('#id_vocabs').val(vocabs).multiselect2('refresh');
    $('#id_antivocabs').val(antivocabs).multiselect2('refresh');
    $.each(qualifier_rows, function (id, item) {
        add_qualifier_row(item.qualifier, item.regex);
    });
}

function export_choice(ajax_data) {
    "use strict";
    var exports = ajax_data.data_list,
        list = $('#data-list');
    list.html('');
    $.each(exports, function (i, data) {
        var item = $('<li/>').addClass('ui-state-default').attr('id', data.id);
        var delete_button = $('<span/>')
            .addClass('remove ui-icon ui-icon-closethick');
        delete_button.click(function () {
            if (window.confirm(
                    gettext("Usunąć eksport") + ' "' + data.name + '"?')) {
                $.ajaxJSON({
                    method: 'post',
                    url: $dj.ajax_delete_export_data,
                    data: {data_id: data.id},
                    description: gettext("Usunięcie ustawień eksportu"),
                    callback: function () {
                        item.remove();
                    }
                });
            }
            return false;
        });
        item.append(delete_button);
        item.append(data.name);
        item.click(function () {
            put_data(data.json);
            $('#load-data-dialog').dialog('close');
        });
        list.append(item);
    });
    $('#load-data-dialog').dialog('open');
}

function load_export_data() {
    "use strict";
    $.ajaxJSON({
        method: 'get',
        url: $dj.ajax_get_export_data,
        data: {},
        description: gettext("Pobranie zapisanych eksportów"),
        callback: export_choice
    });
    return false;
}

$(function () {
    "use strict";
    var opts = {
        noneSelectedText: gettext("Wybierz słowniki"),
        selectedText: gettext("# słowników"),
        selectedList: 4,
        header: false
    };
    $('#id_vocabs').multiSelect(opts);
    $('#id_antivocabs').multiSelect(opts);
    $('#id_excluding_qualifiers').multiSelect({
        selectedText: selected_qualifiers_text
    });
    $('#add-magic-qualifier-row').click(add_qualifier_row);
    $('button.save').click(save_export_data);
    $('button.load').click(load_export_data);
    $('#load-data-dialog').dialog({
        autoOpen: false,
        height: 'auto',
        width: 'auto',
        modal: true
    });
    $(document).on('click', '#magic-qualifiers .remove', remove_qualifier_row);
});