export.js
5.7 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* global $dj */
// straszna i zła copypasta
function selected_qualifiers_text(num, total, checked) {
"use strict";
if (num >= 4)
return num + ' kwalifikatorów';
var grouped = {};
$.each(checked, function (i, elem) {
var id = elem.value;
var vocab = $dj.vocabs[id];
if (grouped[vocab] === undefined)
grouped[vocab] = [];
grouped[vocab].push(elem.title);
});
var vocab_table = [];
var owner = $('#owner-vocabulary').text();
if (grouped[owner])
vocab_table.push(owner + ': ' + grouped[owner].join(', '));
$.each(grouped, function (vocab, qualifiers) {
if (vocab !== owner)
vocab_table.push(vocab + ': ' + qualifiers.join(', '));
});
return vocab_table.join('; ');
}
var new_row_counter = 1;
function add_qualifier_row(qualifier, regex) {
"use strict";
$.ajaxJSON({
type: 'get',
url: $dj.ajax_new_qualifier_row,
data: {num: new_row_counter},
callback: function(data) {
var new_row = $(data.html);
$('#magic-qualifiers').append(new_row);
new_row.find('button').button();
var row_id = new_row.prop('id');
$('#id_' + row_id + '-qualifier').val(qualifier);
$('#id_' + row_id + '-regex').val(regex);
new_row_counter++;
}
});
}
var remove_qualifier_row = function () {
"use strict";
$(this).closest('li').remove();
};
function save_export_data() {
"use strict";
var name, post_data;
name = window.prompt('Wybierz nazwę');
if (name) {
post_data = {
name: name,
serialized_data: $.toJSON($('#export-form').serializeArray())
};
var save_data = {
method: 'post',
url: $dj.ajax_save_export_data,
data: post_data,
description: 'Zapisanie ustawień'
};
$.ajaxJSON($.extend(save_data, {
callback: function (data) {
if (data.exists && window.confirm('Ta nazwa jest zajęta. Nadpisać?')) {
$.ajaxJSON($.extend(save_data, {
data: $.extend(post_data, {force: true})
}));
}
}
}));
}
return false;
}
function put_data(json) {
"use strict";
var data = $.parseJSON(json),
qualifier_rows = {}, vocabs = [], antivocabs = [], parts;
$('#id_refl').attr('checked', false);
$('#id_commonness').attr('checked', false);
$('#magic-qualifiers').find('li').remove();
$.each(data, function (i, item) {
if (item.name === 'vocabs') {
vocabs.push(item.value);
}
if (item.name === 'antivocabs') {
antivocabs.push(item.value);
}
if (item.name === 'variant') {
$('#id_variant').val(item.value);
}
if (item.name === 'refl' || item.name === 'commonness') {
$('#id_' + item.name).attr('checked', true);
}
if (item.name.indexOf('magic') === 0) {
parts = item.name.split('-');
if (!qualifier_rows[parts[0]]) {
qualifier_rows[parts[0]] = {};
}
qualifier_rows[parts[0]][parts[1]] = item.value;
}
if (item.name === 'copyright') {
$('#id_copyright').val(item.value);
}
});
$('#id_vocabs').val(vocabs).multiselect2("refresh");
$('#id_antivocabs').val(antivocabs).multiselect2("refresh");
$.each(qualifier_rows, function (id, item) {
add_qualifier_row(item.qualifier, item.regex);
});
}
function export_choice(ajax_data) {
"use strict";
var exports = ajax_data.data_list,
list = $('#data-list');
list.html('');
$.each(exports, function (i, data) {
var item = $('<li/>').addClass('ui-state-default').attr('id', data.id);
var delete_button = $('<span/>')
.addClass('remove ui-icon ui-icon-closethick');
delete_button.click(function () {
if (window.confirm('Usunąć eksport "' + data.name + '"?')) {
$.ajaxJSON({
method: 'post',
url: $dj.ajax_delete_export_data,
data: {data_id: data.id},
description: 'Usunięcie ustawień eksportu',
callback: function () {
item.remove();
}
});
}
return false;
});
item.append(delete_button);
item.append(data.name);
item.click(function () {
put_data(data.json);
$("#load-data-dialog").dialog('close');
});
list.append(item);
});
$("#load-data-dialog").dialog('open');
}
function load_export_data() {
"use strict";
$.ajaxJSON({
method: 'get',
url: $dj.ajax_get_export_data,
data: {},
description: 'Pobranie zapisanych eksportów',
callback: export_choice
});
return false;
}
$(function () {
"use strict";
var opts = {
noneSelectedText: 'Wybierz słowniki',
selectedText: '# słowników',
selectedList: 4,
header: false
};
$('#id_vocabs').multiselect2(opts);
$('#id_antivocabs').multiselect2(opts);
$('#id_excluding_qualifiers').multiSelect({
selectedText: selected_qualifiers_text
});
$('#add-magic-qualifier-row').click(add_qualifier_row);
$('button.save').click(save_export_data);
$('button.load').click(load_export_data);
$("#load-data-dialog").dialog({
autoOpen: false,
height: 'auto',
width: 'auto',
modal: true
});
$(document).on('click', '#magic-qualifiers .remove', remove_qualifier_row);
});