pattern-edit.js
8 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* global $dj, edit, common, slickgrid, gettext, ngettext, interpolate */
var deleted;
var new_row_counter = 1; // potrzebne tylko dla niepowtarzalności idów
function get_new_row_html() {
"use strict";
var new_row_html = $.ajax({
type: 'get',
url: $dj.ajax_new_ending_row,
data: {pattern_id: slickgrid.get_id()},
async: false // w zasadzie się tak nie powinno robić
}).responseText;
var row_html = new_row_html.replace(/NUM/g, new_row_counter.toString());
new_row_counter++;
return row_html;
}
$.extend(edit, {
form_id: 'pattern-edit-form',
form_cancel_class: 'pattern-edit-cancel',
form_submit_class: 'pattern-edit-submit',
init_form_widgets: function() {
"use strict";
$(document).on('click', 'span.remove', function () {
var li = $(this).closest('li');
var name = li.find('input')[0].id;
if (name.split('-')[1] !== 'add')
deleted.push(name.split('-')[1]);
li.remove();
edit.show_changed();
});
$(document).on('click', '.add-ending', function () {
var id = $('input[name=id]', $(this).closest('form')).value();
var new_row = $(get_new_row_html());
var this_group = $(this).closest('tr').find('.ending-group');
this_group.append(new_row);
edit.qualifier_select(this_group.find('.qualifiers').last());
edit.show_changed();
});
$('#edit').on('scroll', function() {
if (common.multiselect_is_open) {
$(this).find('select[multiple]').multiselect2('close');
}
});
$('#add-button').click(add_pattern);
$('#clone-pattern-button').on('click', function() {
clone_pattern();
$('#more-actions-dialog').dialog('close');
});
$(document).on('change', '#id_type', function () {
var $t = $(this);
var old_bfls = $dj.base_form_labels[common.previous_value($t)];
var new_bfls = $dj.base_form_labels[$t.val()];
var stale_bfls = [], fresh_bfls = [];
var confirmed;
$.each(old_bfls, function(i, bfl) {
if (new_bfls.indexOf(bfl) === -1)
stale_bfls.push(bfl);
});
$.each(new_bfls, function(i, bfl) {
if (old_bfls.indexOf(bfl) === -1)
fresh_bfls.push(bfl);
});
if (stale_bfls.length > 0) {
var fmt = ngettext(
"Label: %s will be removed.",
"Labels: %s will be removed.", stale_bfls.length);
var bfl_symbols = $.map(
stale_bfls, function(bfl_id) {
return $dj.bfl_symbols[bfl_id];
});
confirmed = window.confirm(
interpolate(fmt, [bfl_symbols.join(', ')]) +
' ' + gettext("Continue?"));
if (!confirmed) {
common.revert_selection($t);
}
}
if (confirmed || stale_bfls.length === 0) {
common.confirm_selection($t);
reload_endings(stale_bfls, fresh_bfls);
}
});
},
load_content: function(id, is_created, check_callback) {
"use strict";
$.ajaxJSON({
method: 'get',
url: $dj.ajax_edit_form,
dest: $('#edit'),
data: {id: id},
callback: function () {
edit.form_init();
edit.created = Boolean(is_created);
},
check_callback: check_callback
});
$.ajaxJSON({
method: 'get',
url: $dj.ajax_pattern_preview,
dest: $('#preview'),
data: {id: id},
check_callback: check_callback
});
$.ajaxJSON({
method: 'get',
url: $dj.ajax_history_table,
dest: $('#history'),
data: {pattern_id: id},
check_callback: check_callback
});
edit.active_id = id;
},
form_init: function() {
"use strict";
$('button', '#edit').button();
$('#ending-list.editable').find('.ending-group').sortable().sortable({
change: function () {
edit.show_changed();
}
});
//$('#ending-list').disableSelection();
edit.qualifier_select($('#ending-list').find('.qualifiers'));
deleted = [];
common.init_selection($('#id_type'));
edit.hide_changed();
edit.busy_off();
},
form_submit: function(form_data) {
"use strict";
form_data.push({name: 'deleted', value: deleted});
deleted = [];
var ending_list = [];
var rows = $('#ending-list').find('tr');
$.each(rows, function (i, row) {
var label = $(row).find('td:eq(0) strong').html();
var endings = [];
$(row).find('td:eq(1) input').map(function (i, input) {
var id_parts = $(input).attr('id').split('-');
var id = id_parts[1], q_id;
if (id === 'add')
q_id = '#id_add-' + id_parts[2] + '-qualifiers';
else
q_id = '#id_end' + id + '-qualifiers';
endings.push({
id: id,
string: $(input).val(),
qualifiers: $(q_id).val() || []
});
});
ending_list.push({base_form_label: label, endings: endings});
});
form_data.push({name: 'ending_list', value: ending_list});
var example_list = [];
$('.example-input').each(function() {
var id = parseInt(this.getAttribute('data-id'), 10);
example_list.push([id, this.value]);
});
form_data.push({name: 'example_list', value: example_list});
edit.busy_on();
var result = $.ajaxJSON({
method: 'post',
url: $dj.ajax_update_pattern,
data: {
form_data: form_data
},
description: gettext("Save"),
callback: function () {
edit.hide_changed();
// odświeżyć?
edit.load_content(slickgrid.get_id());
},
error_callback: function (xhr, status, error) {
common.error_alert(status + ': ' + error);
edit.cleanup();
},
bad_data_callback: function () {
edit.cleanup();
return true;
}
});
if (result === false) {
edit.cleanup();
}
}
});
function add_pattern() {
"use strict";
$.ajaxJSON({
method: 'post',
url: $dj.ajax_create_pattern,
data: {},
description: gettext("Pattern creation"),
callback: function(data) {
edit.load_content(data.id, true);
}
});
}
function clone_pattern() {
"use strict";
$.ajaxJSON({
method: 'post',
url: $dj.ajax_clone_pattern,
data: {pattern_id: slickgrid.get_id()},
descripton: gettext("Pattern cloning"),
callback: function(data) {
slickgrid.reload_data();
slickgrid.row_index(data.new_id);
}
});
}
function reload_endings(stale_bfls, fresh_bfls) {
"use strict";
// skasować nieświeże zakończenia
$.each(stale_bfls, function(i, bfl_id) {
var bfl_row = $('[data-bfl-id='+bfl_id+']');
bfl_row.find('.ending-row').each(function() {
$(this).find('span.remove').click();
});
bfl_row.remove();
});
$.each(fresh_bfls, function(i, bfl_id) {
// dodać wiersz w tabelce
$.ajaxJSON({
method: 'get',
url: $dj.ajax_new_ending_table_row,
data: {bfl_id: bfl_id},
callback: function(data) {
$('#ending-list').prepend(data.html);
}
});
});
}