slickgrid.js
15.4 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
var slickgrid = {
loader: new Slick.Data.RemoteModel(
$dj.ajax_get_page, $dj.ajax_search_index),
grid: null,
set_active_when_loaded: null,
skip_cell_changed: false,
last_active_row: undefined,
op_names: {
eq: ["równy", "równa", "równe"],
ne: ["różny od", "różna od", "różne od"],
le: ["mniejszy lub równy", "mniejsza lub równa", "mniejsze lub równe"],
ge: ["większy lub równy", "większa lub równa", "większe lub równe"],
bw: "zaczyna się od",
bn: "nie zaczyna się od",
ew: "kończy się na",
en: "nie kończy się na",
cn: "zawiera",
nc: "nie zawiera",
re: "pasuje do wzorca",
nr: "nie pasuje do wzorca"
},
search_string: function() {
"use strict";
return $('#text-search').val();
},
set_active: function(index, load) {
"use strict";
if (load && edit.changed && !edit.confirm_discard()) {
return;
}
if (slickgrid.grid.getData().length > 0) {
slickgrid.skip_cell_changed = true;
slickgrid.grid.setActiveCell(index, 0);
slickgrid.skip_cell_changed = false;
slickgrid.last_active_row = index;
if (load) {
slickgrid.load_content(slickgrid.active_id());
}
} else
slickgrid.last_active_row = undefined;
},
active_id: function() {
"use strict";
var cell = slickgrid.grid.getActiveCell();
if (cell === null || cell === undefined)
return undefined;
var row = slickgrid.grid.getActiveCell().row;
if (row === undefined)
return undefined;
if (slickgrid.grid.getDataItem(row) === undefined)
return undefined;
return slickgrid.grid.getDataItem(row).id;
},
scroll_search: function() {
"use strict";
slickgrid.loader.searchRow(slickgrid.search_string(), function (row) {
slickgrid.set_active_when_loaded = row;
slickgrid.grid.scrollRowToCenter(row);
});
},
search: function() {
"use strict";
if ($dj.filtering_mode) {
slickgrid.loader.setSearch(slickgrid.search_string());
slickgrid.grid.scrollRowToTop(0);
slickgrid.ensure_data();
slickgrid.set_active(0, true);
} else {
slickgrid.scroll_search();
}
},
ensure_data: function() {
"use strict";
$('#filter-button').children().first().toggleClass('ui-state-highlight',
slickgrid.loader.getFilter().rules.length !== 0);
var vp = slickgrid.grid.getViewport();
slickgrid.loader.ensureData(vp.top, vp.bottom);
},
reload_data: function() {
"use strict";
var vp = slickgrid.grid.getViewport();
slickgrid.loader.reloadData(vp.top, vp.bottom);
}
};
var options = {
editable: false,
enableAddRow: false,
enableCellNavigation: true
};
function set_columns() {
"use strict";
var chosen_column_ids = $('#column-list').val();
var chosen_columns = columns_from_ids(chosen_column_ids);
slickgrid.grid.setColumns(chosen_columns);
slickgrid.grid.autosizeColumns();
$.ajaxJSON({
method: 'post',
url: $dj.ajax_save_columns,
data: {columns: chosen_column_ids}
});
}
function columns_from_ids(column_ids) {
"use strict";
return $.map(column_ids, function(id) {
return slickgrid.all_columns[id];
});
}
function update_filter_row(row) {
"use strict";
var field = row.find('.rule-field').val();
var field_info = slickgrid.filter_fields[field];
var op_select = $('<select/>').addClass('rule-op');
var value_input;
$.each(field_info.ops, function(i, op) {
var op_name;
var op_name_info = slickgrid.op_names[op];
if (typeof op_name_info !== "string") {
var gender = 'mfn'.indexOf(field_info.gender);
op_name = op_name_info[gender];
} else {
op_name = op_name_info;
}
$('<option/>').text(op_name).val(op).appendTo(op_select);
});
if (field_info.options) {
value_input = $('<select/>').addClass('rule-data');
$.each(field_info.options, function(i, option) {
$('<option/>').text(option[1]).val(option[0]).appendTo(value_input);
});
} else if (field_info.optgroups) {
value_input = $('<select/>').addClass('rule-data');
$.each(field_info.optgroups, function(i, optgroup) {
var group = $('<optgroup/>').attr('label', optgroup[0]);
$.each(optgroup[1], function(i, option) {
$('<option/>').text(option[1]).val(option[0]).appendTo(group);
});
value_input.append(group);
});
} else {
value_input = $('<input/>').attr('type', 'text');
value_input.addClass('rule-data');
}
row.children().eq(1).empty().append(op_select);
row.children().eq(2).empty().append(value_input);
}
function add_filter_rule(field) {
"use strict";
var filter_table = $('#filter-table');
var row = $('<tr/>');
var field_select = $('<select/>').addClass('rule-field');
$.each(slickgrid.filter_fields, function(id, filter_field) {
var option = $('<option/>').text(filter_field.name).val(id);
if (id === field) {
option.prop('selected', true);
}
option.appendTo(field_select);
});
$('<td/>').append(field_select).appendTo(row);
row.append($('<td/>')).append($('<td/>'));
var remove_button = $('<input/>').attr('type', 'button').val('-')
.addClass('remove-filter');
$('<td/>').append(remove_button).appendTo(row);
update_filter_row(row);
filter_table.append(row);
return row;
}
function get_filter() {
"use strict";
return {
group_op: $('#group-op').val(),
rules: $('#filter-table').find('tr').map(function(i, row) {
var $row = $(row);
return {
'field': $row.find('.rule-field').val(),
'op': $row.find('.rule-op').val(),
'data': $row.find('.rule-data').val()
};
}).toArray()
};
}
function save_filter() {
"use strict";
var filter_name, post_data;
filter_name = window.prompt("Wybierz nazwę filtru do zapisania");
if (filter_name) {
post_data = {
name: filter_name,
serialized_filter: $.toJSON(get_filter())
};
var save_filter_data = {
method: 'post',
url: $dj.ajax_save_filter,
data: post_data,
description: 'Zapisanie filtru'
};
$.ajaxJSON($.extend(save_filter_data, {
callback: function (data) {
if (data.exists && window.confirm(
"Filtr o tej nazwie już istnieje. Nadpisać?")) {
$.ajaxJSON($.extend(save_filter_data, {
data: $.extend(post_data, {force: true})
}));
}
}
}));
}
return false;
}
function put_filter(filter) {
"use strict";
$('#group-op').val(filter.group_op);
$('#filter-table').empty();
$.each(filter.rules, function(i, rule) {
var row = add_filter_rule(rule.field);
row.find('.rule-op').val(rule.op);
row.find('.rule-data').val(rule.data);
});
}
function filter_choice(ajax_data) {
"use strict";
var filters = ajax_data.filters;
var list = $('#filter-list');
list.empty();
$.each(filters, function (i, filter) {
var item = $('<li/>').addClass('ui-state-default').attr('id', filter.id);
var delete_button = $('<span/>')
.addClass('remove ui-icon ui-icon-closethick');
delete_button.click(function () {
if (window.confirm('Usunąć filtr "' + filter.name + '"?')) {
$.ajaxJSON({
method: 'post',
url: $dj.ajax_delete_filter,
data: {id: filter.id},
description: 'Usunięcie filtru',
callback: function () {
item.remove();
}
});
}
return false;
});
item.append(delete_button);
item.append(filter.name);
item.click(function () {
put_filter($.parseJSON(filter.json));
$("#load-filter-dialog").dialog('close');
});
list.append(item);
});
$("#load-filter-dialog").dialog('open');
}
function apply_filter(filter) {
"use strict";
$('#text-search').text('');
slickgrid.loader.setFilter(filter || get_filter());
slickgrid.set_active_when_loaded = 0;
slickgrid.grid.scrollRowToTop(0);
slickgrid.ensure_data();
}
$(function() {
"use strict";
var columns = $dj.columns || slickgrid.initial_columns;
slickgrid.element = $('#' + slickgrid.grid_element_id);
slickgrid.grid = new Slick.Grid(
slickgrid.element, slickgrid.loader.data, columns_from_ids(columns),
options);
slickgrid.grid.onViewportChanged.subscribe(function (e, args) {
slickgrid.ensure_data();
});
var timeout_handler;
slickgrid.grid.onActiveCellChanged.subscribe(function(e, cell) {
if (cell.row === undefined)
return;
if (slickgrid.skip_cell_changed) {
slickgrid.skip_cell_changed = false;
return;
}
if (edit.get_id() === slickgrid.active_id())
return;
if (edit.changed && !edit.confirm_discard()) {
setTimeout(function() {
if (slickgrid.last_active_row !== undefined) {
slickgrid.grid.scrollRowToCenter(slickgrid.last_active_row);
slickgrid.set_active(slickgrid.last_active_row, false);
} else {
slickgrid.grid.resetActiveCell();
}
}, 100); // hack, bo inaczej z dziwnego powodu odpala się dwa razy
return;
}
slickgrid.last_active_row = cell.row;
clearTimeout(timeout_handler);
timeout_handler = setTimeout(function() {
var id = slickgrid.active_id();
if (id !== undefined) {
slickgrid.load_content(id);
}
}, 500);
});
var loading_ctrl = null;
slickgrid.loader.onDataLoading.subscribe(function () {
//layout.adjust_grid_size();
var $g = slickgrid.element;
if (!loading_ctrl) {
loading_ctrl = getBusyOverlay(
$('#left')[0],
{color: 'black', opacity: 0.2},
{size: 30});
}
});
function loading_off() {
if (loading_ctrl) loading_ctrl.remove();
loading_ctrl = null;
}
slickgrid.loader.onDataLoaded.subscribe(function (e, args) {
for (var i = args.from; i <= args.to; i++) {
slickgrid.grid.invalidateRow(i);
}
slickgrid.grid.updateRowCount();
slickgrid.grid.render();
loading_off();
layout.adjust_grid_size();
var num = slickgrid.grid.getDataLength();
document.title = slickgrid.grid_caption + ' (' + num + ')';
if (slickgrid.set_active_when_loaded !== null) {
slickgrid.set_active(slickgrid.set_active_when_loaded, true);
slickgrid.set_active_when_loaded = null;
}
});
slickgrid.loader.onAjaxError.subscribe(function() {
common.error_alert("Załadowanie strony nie powiodło się.");
loading_off();
});
// wyszukiwanie
var search_timeout_handler;
$("#text-search").keyup(function (e) {
if (e.which === 13) {
slickgrid.search();
} else if ($dj.auto_search) {
clearTimeout(search_timeout_handler);
search_timeout_handler = setTimeout(slickgrid.search, 500);
}
});
if ($dj.filtering_mode) {
slickgrid.loader.setSearch(slickgrid.search_string());
}
$('#search-button').click(slickgrid.search);
var sort_rules = $dj.sort_rules || slickgrid.initial_sort_rules;
// sortowanie
slickgrid.loader.setSort(sort_rules);
slickgrid.init_sort();
// filtry
add_filter_rule(slickgrid.main_field);
$('#choose-filter-dialog').dialog({
autoOpen: false,
width: 'auto',
modal: false,
buttons: [
{
text: "Czyść",
click: function() {
$('#filter-table').empty();
add_filter_rule(slickgrid.main_field);
apply_filter({group_op: "AND", rules: []});
}
},
{
text: "Zapisz filtr",
click: save_filter
},
{
text: "Załaduj filtr",
click: function () {
$.ajaxJSON({
method: 'get',
url: $dj.ajax_get_filters,
data: {},
description: 'Pobranie listy filtrów',
callback: filter_choice
});
return false;
}
},
{
text: 'Filtruj',
click: function() {
apply_filter();
}
}
]
});
$('#add-filter-button').click(function() {
add_filter_rule(slickgrid.main_field);
});
$(document).on('change', '.rule-field', function() {
update_filter_row($(this).closest('tr'));
});
$(document).on('click', '.remove-filter', function() {
$(this).closest('tr').remove();
});
$('#filter-button').click(function() {
$('#choose-filter-dialog').dialog('open');
});
$(document).on('keyup', '#filter-table input', function (e) {
if (e.which === 13) { // enter
$(this).change();
apply_filter();
}
});
if ($dj.filter && $dj.filter.rules.length > 0) {
slickgrid.loader.setFilter($dj.filter);
put_filter($dj.filter);
}
// wybór kolumn
$('#choose-columns-dialog').dialog({
autoOpen: false,
width: 'auto',
modal: true,
buttons: [
{
text: "Anuluj",
click: function () {
$(this).dialog("close");
}
},
{
text: "Zatwierdź",
click: function() {
set_columns();
$(this).dialog("close");
}
}
]
});
var column_list = $('#column-list');
$('#show-columns-button').click(function() {
$('#choose-columns-dialog').dialog('open');
column_list.multiselect('refresh');
});
function add_column_option(column, selected) {
var option = $('<option/>');
option.attr('value', column.id);
option.prop('selected', selected);
option.text(column.name);
column_list.append(option);
}
$.each(columns, function(i, column) {
add_column_option(slickgrid.all_columns[column], true);
});
$.each(slickgrid.all_columns, function(id, column) {
if (columns.indexOf(id) === -1)
add_column_option(column, false);
});
column_list.multiselect({
sortable: true
});
// load the first page
slickgrid.set_active_when_loaded = 0;
slickgrid.grid.onViewportChanged.notify();
layout.adjust_tabs();
});