export.js
2.44 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
// straszna i zła copypasta
function selected_qualifiers_text(num, total, checked) {
if (num >= 4)
return num + ' kwalifikatorów';
grouped = {};
$.each(checked, function(i, elem) {
id = elem.value;
vocab = vocabs[id];
if (grouped[vocab] === undefined)
grouped[vocab] = [];
grouped[vocab].push(elem.title);
});
vocab_table = [];
owner = $('#owner-vocabulary').text();
if (grouped[owner])
vocab_table.push(owner + ': ' + grouped[owner].join(', '));
for (vocab in grouped) {
if (vocab != owner)
vocab_table.push(vocab + ': ' + grouped[vocab].join(', '));
}
return vocab_table.join('; ');
}
var qualifier_options = {
noneSelectedText: 'Wybierz',
selectedText: selected_qualifiers_text,
selectedList: 4,
header: false,
};
var new_row_counter = 1;
function get_new_qualifier_row_html() {
var new_row_html = $.ajax({
type: 'get',
url: ajax_new_qualifier_row,
async: false
}).responseText;
var row_html = new_row_html.replace(/NUM/g, new_row_counter);
new_row_counter++;
return row_html;
}
function add_qualifier_row() {
var new_row = $(get_new_qualifier_row_html());
$('#magic-qualifiers').append(new_row);
new_row.find('button').button();
}
function remove_qualifier_row() {
$(this).closest('li').remove();
}
function save_export_data() {
var name, post_data;
name = prompt('Wybierz nazwę')
if(name) {
post_data = {
name: name,
serialized_data: $.toJSON($('#export-form').serializeArray())
};
var save_data = {
method: 'post',
url: ajax_save_export_data,
data: post_data,
description: 'Zapisanie ustawień'
}
$.ajaxJSON($.extend(save_data, {
callback: function(data) {
if (data.exists && confirm('Ta nazwa jest zajęta. Nadpisać?')) {
$.ajaxJSON($.extend(save_data, {
data: $.extend(post_data, {force: true})
}));
}
}
}));
}
return false;
}
$(function() {
var opts = {
noneSelectedText: 'Wybierz słowniki',
selectedText: '# słowników',
selectedList: 4,
header: false
};
$('#id_vocabs').multiselect(opts);
$('#id_antivocabs').multiselect(opts);
$('#id_excluding_qualifiers').multiselect(qualifier_options);
$('#add-magic-qualifier-row').click(add_qualifier_row);
$('button.save').click(save_export_data);
$('button.load').click(load_export_data);
$(document).on('click', '#magic-qualifiers .remove', remove_qualifier_row);
})