table-view.js
4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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');
});
});