Blame view

media/js/table-view.js 4.76 KB
janek37 authored
1
2
/* global $dj, getBusyOverlay */
janek37 authored
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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;
}
janek37 authored
18
19
20

function template_form_init() {
    "use strict";
janek37 authored
21
22
23
    var edit_form = $('#template-edit-form');
    edit_form.find('button').button();
    edit_form.find('select[multiple]').multiSelect({
janek37 authored
24
        minWidth: 120,
janek37 authored
25
        selectedList: 3
janek37 authored
26
    });
janek37 authored
27
    busy_off();
janek37 authored
28
29
30
31
32
}

function load_template() {
    "use strict";
    tt_id = parseInt($('#id_table_template').val(), 10);
janek37 authored
33
    busy_on();
janek37 authored
34
35
36
37
38
    $.ajaxJSON({
        method: 'get',
        url: $dj.ajax_load_template,
        data: {template_id: tt_id},
        dest: $('#template-edit-form'),
janek37 authored
39
40
        callback: template_form_init,
        error_callback: busy_off
janek37 authored
41
42
43
44
45
46
47
48
49
50
51
    });
    $.ajaxJSON({
        method: 'get',
        url: $dj.ajax_preview_form,
        data: {template_id: tt_id},
        dest: $('#template-preview')
    });
}

function load_preview() {
    "use strict";
janek37 authored
52
    var form = $('#preview-params');
janek37 authored
53
    var attr_data = form.find('.closed-attr').map(function(i, elem) {
janek37 authored
54
55
        return $(elem).val();
    }).toArray();
janek37 authored
56
57
58
59
60
    $.ajaxJSON({
        method: 'get',
        url: $dj.ajax_load_preview,
        data: {
            template_id: tt_id,
janek37 authored
61
62
63
            pattern_type_id: form.find('.pattern-type-select').val(),
            gender_id: form.find('.gender-select').val() || null,
            attr_data: attr_data
janek37 authored
64
65
66
67
68
        },
        dest: $('#preview-table')
    });
}
janek37 authored
69
70
71
72
73
74
75
76
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;
janek37 authored
77
78
        var row, col, rspan, cspan, index, bfl, prefix, suffix, pts, tag,
            label, css_class, attr_vals, genders;
janek37 authored
79
80
81
82
83
84
85
86
        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();
janek37 authored
87
        genders = $e.find('.gender-select').val();
janek37 authored
88
89
90
        attr_vals = $e.find('.attrs-select').map(function(i, elem) {
            return $(elem).val();
        }).toArray();
janek37 authored
91
92
93
        if ($e.hasClass('table')) {
            index = $e.find('.ind').val();
            table_cells.push(
janek37 authored
94
95
                [row, col, rspan, cspan, index, bfl, prefix, suffix, pts,
                 genders, attr_vals]);
janek37 authored
96
97
98
99
        } else if ($e.hasClass('header')) {
            label = $e.find('.label').val();
            css_class = $e.find('.css-class-select').val();
            headers.push(
janek37 authored
100
101
                [row, col, rspan, cspan, label, css_class, pts,
                 genders, attr_vals]);
janek37 authored
102
103
104
        } else if ($e.hasClass('export')) {
            tag = $e.find('.tag').val();
            export_cells.push(
janek37 authored
105
                [bfl, prefix, suffix, tag, pts, genders, attr_vals]);
janek37 authored
106
107
108
109
110
111
112
113
114
115
116
117
118
        }
    });
    $.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();
janek37 authored
119
120
121
        },
        error_callback: function() {
            busy_off();
janek37 authored
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
        }
    });
}

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);
janek37 authored
140
141
            new_row.find('select[multiple]').multiSelect({
                minWidth: 120,
janek37 authored
142
                selectedList: 3
janek37 authored
143
            });
janek37 authored
144
145
146
147
148
        }
    });
    new_row_counter++;
}
janek37 authored
149
150
151
$(function() {
    "use strict";
    $('#load-template').click(load_template);
janek37 authored
152
    $(document).on('click', '#cancel', load_template);
janek37 authored
153
    $(document).on('click', '#load-preview', load_preview);
janek37 authored
154
155
156
157
158
159
160
161
162
163
164
165
166
    $(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');
    });
janek37 authored
167
});