table-view.js 4.73 KB
var tt_id, busy_ctrl;

function busy_on() {
    "use strict";
    busy_ctrl = getBusyOverlay(
        $('#table-edit')[0],
        {color: 'black', opacity: 0.2},
        {size: 30});
}

function busy_off() {
    "use strict";
    if (busy_ctrl) busy_ctrl.remove();
    busy_ctrl = null;
}

function template_form_init() {
    "use strict";
    var edit_form = $('#template-edit-form');
    edit_form.find('button').button();
    edit_form.find('select[multiple]').multiSelect({
        minWidth: 120,
        selectedList: 3
    });
    busy_off();
}

function load_template() {
    "use strict";
    tt_id = parseInt($('#id_table_template').val(), 10);
    busy_on();
    $.ajaxJSON({
        method: 'get',
        url: $dj.ajax_load_template,
        data: {template_id: tt_id},
        dest: $('#template-edit-form'),
        callback: template_form_init,
        error_callback: busy_off
    });
    $.ajaxJSON({
        method: 'get',
        url: $dj.ajax_preview_form,
        data: {template_id: tt_id},
        dest: $('#template-preview')
    });
}

function load_preview() {
    "use strict";
    var form = $('#preview-params');
    var attr_data = form.find('.attr-value-select').map(function(i, elem) {
        return $(elem).val();
    }).toArray();
    $.ajaxJSON({
        method: 'get',
        url: $dj.ajax_load_preview,
        data: {
            template_id: tt_id,
            pattern_type_id: form.find('.pattern-type-select').val(),
            gender_id: form.find('.gender-select').val() || null,
            attr_data: attr_data
        },
        dest: $('#preview-table')
    });
}

function save_template() {
    "use strict";
    var table_cells = [], export_cells = [], headers = [];
    busy_on();
    $('#template-edit-form').find('tr').each(function(i, elem) {
        var $e = $(elem);
        if ($e.find('select').length === 0)
            return;
        var row, col, rspan, cspan, index, bfl, prefix, suffix, pts, tag,
            label, css_class, attr_vals, genders;
        row = $e.find('.row').val();
        col = $e.find('.col').val();
        rspan = $e.find('.rspan').val();
        cspan = $e.find('.cspan').val();
        bfl = $e.find('.bfl-select').val();
        prefix = $e.find('.pre').val();
        suffix = $e.find('.suf').val();
        pts = $e.find('.pattern-types-select').val();
        genders = $e.find('.gender-select').val();
        attr_vals = $e.find('.attrs-select').map(function(i, elem) {
            return $(elem).val();
        }).toArray();
        if ($e.hasClass('table')) {
            index = $e.find('.ind').val();
            table_cells.push(
                [row, col, rspan, cspan, index, bfl, prefix, suffix, pts,
                 genders, attr_vals]);
        } else if ($e.hasClass('header')) {
            label = $e.find('.label').val();
            css_class = $e.find('.css-class-select').val();
            headers.push(
                [row, col, rspan, cspan, label, css_class, pts,
                 genders, attr_vals]);
        } else if ($e.hasClass('export')) {
            tag = $e.find('.tag').val();
            export_cells.push(
                [bfl, prefix, suffix, tag, pts, genders, attr_vals]);
        }
    });
    $.ajaxJSON({
        method: 'post',
        url: $dj.ajax_save_template,
        data: {
            template_id: tt_id,
            table_cells: table_cells,
            headers: headers,
            export_cells: export_cells
        },
        callback: function() {
            busy_off();
        },
        error_callback: function() {
            busy_off();
        }
    });
}

var new_row_counter = 1;
function add_row(type) {
    "use strict";
    $.ajaxJSON({
        method: 'get',
        url: $dj.ajax_template_row,
        data: {
            template_id: tt_id,
            row_type: type,
            num: new_row_counter
        },
        callback: function(data) {
            var new_row = $(data.html);
            $('#template-edit-form').find('thead').after(new_row);
            new_row.find('select[multiple]').multiSelect({
                minWidth: 120,
                selectedList: 3
            });
        }
    });
    new_row_counter++;
}

$(function() {
    "use strict";
    $('#load-template').click(load_template);
    $(document).on('click', '#cancel', load_template);
    $(document).on('click', '#load-preview', load_preview);
    $(document).on('click', '#save-template', save_template);
    $(document).on('click', '.remove', function() {
        $(this).closest('tr').remove();
    });
    $(document).on('click', '#add-table-cell', function() {
        add_row('table');
    });
    $(document).on('click', '#add-header', function() {
        add_row('header');
    });
    $(document).on('click', '#add-export-cell', function() {
        add_row('export');
    });
});