pattern-view.js 5.49 KB
var main_field = 'name';
var qualifier_options = { // copypasta z leksemów...
    noneSelectedText: 'Wybierz kwalifikatory',
    selectedText: '# kwalifikatorów',
    selectedList: 4,
    header: false,
    create: function () {
        "use strict";
        var select = $(this);
        var selected = select.val();
        if (selected) {
            $.each(selected, function (i, value) {
                toggle_excluded(select, value, false);
            });
        }
    },
    click: function (event, ui) {
        "use strict";
        toggle_excluded(this, ui.value, !ui.checked);
    }
};
$.extend(jqgrid, {
    main_field: main_field,
    initialColModel: [
        {name: 'name', index: 'name', width: 150},
        {name: 'type', index: 'type', width: 70},
        {name: 'part_of_speech', index: 'part_of_speech', width: 200}
    ],
    initialColNames: ['Id', 'Typ', 'Cz. mowy'],
    initial_sort_rules: [
        {field: main_field, order: 'asc'}
    ],
    grid_caption: "Wzory",
    edit_form_id: 'pattern-edit-form',
    edit_form_cancel_id: 'pattern-edit-cancel',
    edit_form_submit_id: 'pattern-edit-submit'
});
var deleted;
$dj.reverse_exclusion_classes = {};

function get_id() {
    "use strict";
    return parseInt($('#' + jqgrid.edit_form_id).find('[name="id"]').val(), 10);
}

$.each($dj.exclusion_classes, function (ec, qualifiers) {
    "use strict";
    $.each(qualifiers, function (i, q_id) {
        $dj.reverse_exclusion_classes[q_id] = ec;
    });
});

// duuuużo copypasty :(
function toggle_excluded(select, value, enable) {
    "use strict";
    var ec = $dj.reverse_exclusion_classes[Number(value)],
        values = $dj.exclusion_classes[ec];
    if (values)
        $.each(values, function (i, v) {
            if (v !== Number(value))
                common.multiselect_toggle(select, v, enable);
        });
}

jqgrid.init_form_widgets = function() {
    "use strict";
    $(document).on('click', 'span.remove', function () {
        var li = $(this).closest('li');
        var name = li.find('input')[0].id;
        if (name.split('-')[1] !== 'add')
            deleted.push(name.split('-')[1]);
        li.remove();
        jqgrid.show_changed();
    });
    $(document).on('click', '.add-ending', function () {
        var id = $('input[name=id]', $(this).closest('form')).value();
        var new_row = $(get_new_row_html());
        var this_group = $(this).closest('tr').find('.ending-group');
        this_group.append(new_row);
        this_group.find('.qualifiers').last().multiselect2(qualifier_options);
        jqgrid.show_changed();
    });
};

jqgrid.load_content = function(id) {
    "use strict";
    $.ajaxJSON({
        method: 'get',
        url: $dj.ajax_edit_form,
        dest: $('#edit'),
        data: {id: id},
        callback: function () {
            jqgrid.edit_form_init();
            //created = Boolean(is_created);
        }
    });
    jqgrid.grid.jqGrid('setGridParam', {'lastSelectedId': id});
};

jqgrid.edit_form_init = function() {
    "use strict";
    $('button', '#edit').button();
    $('#ending-list.editable').find('.ending-group').sortable().sortable({
        change: function () {
            jqgrid.show_changed();
        }
    });
    //$('#ending-list').disableSelection();
    $('#ending-list').find('.qualifiers').multiselect2(qualifier_options);
    deleted = [];
    jqgrid.hide_changed();
    common.busy_off();
};

jqgrid.edit_form_submit = function () {
    "use strict";
    var this_form = $(this);
    var form_data = this_form.serializeArray();
    form_data.push({name: 'deleted', value: deleted});
    deleted = [];
    var ending_list = [];
    var rows = $('#ending-list').find('tr');
    $.each(rows, function (i, row) {
        var label = $(row).find('td:eq(0) strong').html();
        var endings = [];
        $(row).find('td:eq(1) input').map(function (i, input) {
            var id = $(input).attr('id').split('-')[1], q_id;
            if (id === 'add')
                q_id = '#id_add-' + $(input).attr('id').split('-')[2] + '-qualifiers';
            else
                q_id = '#id_end' + id + '-qualifiers';
            endings.push({
                id: id,
                string: $(input).val(),
                qualifiers: $(q_id).val() || []
            });
        });
        ending_list.push({base_form_label: label, endings: endings});
    });
    form_data.push({name: 'ending_list', value: ending_list});
    common.busy_on();
    $.ajaxJSON({
        method: 'post',
        url: $dj.ajax_update_pattern,
        data: {
            form_data: form_data
        },
        description: "Zapisanie zmian",
        callback: function () {
            jqgrid.hide_changed();
            jqgrid.grid.jqGrid('setGridParam', {'setOnComplete': true});
            jqgrid.grid.trigger("reloadGrid");
        },
        error_callback: function (xhr, status, error) {
            common.error_alert(status + ': ' + error);
            common.busy_off();
        },
        bad_data_callback: function () {
            common.busy_off();
            return true;
        }
    });
    return false;
};

var new_row_counter = 1; // potrzebne tylko dla niepowtarzalności idów
function get_new_row_html() {
    "use strict";
    var new_row_html = $.ajax({
        type: 'get',
        url: $dj.ajax_new_ending_row,
        data: {pattern_id: get_id()},
        async: false // w zasadzie się tak nie powinno robić
    }).responseText;
    var row_html = new_row_html.replace(/NUM/g, new_row_counter);
    new_row_counter++;
    return row_html;
}

jqgrid.add_buttons = function () {
};