paginer.js
6.59 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
// paginer[id] ma format: [typ, parametry, akcja]
// parametry mają format:
// {'columns': field_list,'order_by': field, 'filters': filter_list}
// filtry mają format [field, lookup, value]
var paginer = {};
var page_numbers = {};
paginer.page_numbers = page_numbers;
function get_page_number(id) {
"use strict";
if (!page_numbers[id])
return 1;
return page_numbers[id];
}
paginer.get_page_number = get_page_number;
function get_list_id(elem) {
"use strict";
return elem.id.split('-')[0];
}
var paginer_link_click = function() {
"use strict";
change_page($(this).parents('div.paginer')[0].id,
$('span.page_nr', this).text());
};
var perform_group_action = function() {
"use strict";
var container, id, action, ajax_url, row_ids;
container = $(this).parents('div.paginer')[0];
id = container.id;
action = this.id.replace(id + '-', '');
ajax_url = window['ajax_' + action];
row_ids = $('.checkrow:checked', container).map(function() {
return this.id.replace(id + '-', '');
}).get();
if (row_ids.length === 0)
common.error_alert("Niczego nie zaznaczono");
$.each(row_ids, function(i, row_id) {
$.ajaxJSON({
method: 'post',
url: ajax_url,
data: {row_id: row_id}
});
});
update_list(id);
};
var update_filters = function() {
"use strict";
var list_id = get_list_id(this);
var params = $dj.paginer[list_id][1];
params.filters = $.extend([], $dj.original_paginer[list_id][1].filters);
$('#' + list_id + '-filter_panel').find('li').each(function() {
var field_name = $(this).find('.field-choice').val();
var lookup = $(this).find('.lookup-choice').val();
var value = $(this).find('.value').value();
params.filters.push([field_name, lookup, value]);
});
change_page(list_id, 1);
};
var update_order = function() {
"use strict";
var sort_choices = this.parentNode;
var list_id = get_list_id(this);
var sign = $('.sort-direction', this).text() > 0? '' : '-';
var field_name = $('.sort-field-name', this).text();
$dj.paginer[list_id][1].order_by = sign + field_name;
change_page(list_id, 1);
$('.sort-direction', this).text(-(sign + '1'));
show_order(list_id);
};
function show_order(list_id) {
"use strict";
var sort_choices = $('#' + list_id + '-sort_choices');
var current_choice, icon;
var order_field = $dj.paginer[list_id][1].order_by;
var icon_classes = ['ui-icon-arrow-1-s', 'ui-icon-arrow-1-n'];
var sign = order_field[0] === '-';
if (sign)
order_field = order_field.substr(1);
current_choice = $('#' + list_id + '-' + order_field + '-sort_choice');
$('.current', sort_choices).removeClass('current');
current_choice.addClass('current');
if (sign)
icon_classes.reverse();
icon = $('.sort-icon', current_choice);
icon.removeClass(icon_classes[0]).addClass(icon_classes[1]);
}
paginer.show_order = show_order;
function reload_panel(list_id) {
"use strict";
var panel = $('#' + list_id + '-filter_panel');
var filters = $dj.paginer[list_id][1].filters;
if (panel.length === 0)
return;
$.each(filters, function(i, filter) {
if (i >= $dj.original_paginer[list_id][1].filters.length) {
var field_name = filter[0], lookup = filter[1], value = filter[2];
new_filter_row(list_id);
var row = panel.find('li').last();
row.find('.field-choice').val(field_name).change();
row.find('.lookup-choice').val(lookup);
var value_field = row.find('.value');
if (value_field.is(':checkbox')) {
if (value)
this.checked = 'checked';
else
this.checked = '';
} else {
value_field.val(value);
}
}
});
}
paginer.reload_panel = reload_panel;
function paginer_bind_events(id) {
"use strict";
$('div#' + id + ' a.link').click(paginer_link_click);
$('div#' + id + ' .group-action').click(perform_group_action);
}
paginer.bind_events = {};
function paginer_ajax_callback(data, id) {
"use strict";
paginer_bind_events(id);
var list_callback = paginer.bind_events[$dj.paginer[id][0]];
if ($.isFunction(list_callback))
list_callback(id);
}
function change_page(id, nr, protect_hash) {
"use strict";
var param = {
paginer_id: id,
paginer_type: $dj.paginer[id][0],
params: $dj.paginer[id][1],
page_nr: nr,
action: $dj.paginer[id][2],
group_actions: $dj.paginer[id][3]
};
$.ajaxJSON({
method: 'post',
url: $dj.ajax_paginer,
data: param,
description: "Pobranie strony",
dest: $('#'+id+'.paginer'),
callback: paginer_ajax_callback,
callback_args: [id]
});
page_numbers[id] = nr;
if (!protect_hash)
common.update_hash();
}
function update_list(id, protect_hash) {
"use strict";
change_page(id, get_page_number(id), protect_hash);
}
paginer.update_list = update_list;
var filter_row_counter = 1; // dla niepowtarzalności idów
function new_filter_row_html(list_id) {
"use strict";
var new_row_html = $.ajax({
type: 'get',
url: $dj.ajax_new_filter_row,
data: {list_type: $dj.paginer[list_id][0]},
async: false
}).responseText.replace(/NUM/g, filter_row_counter);
filter_row_counter++;
return new_row_html;
}
paginer.init_widget = {};
var update_filter_row = function() {
"use strict";
var num = this.id.split('-')[0].split('_')[2];
var row = $(this).closest('li');
var list_id = get_list_id($(this).closest('.filter-panel')[0]);
var list_type = $dj.paginer[list_id][0];
var field_name = $(this).val();
row.find('.dynamic-field').remove();
var dynamic_field_html = $.ajax({
type: 'get',
url: $dj.ajax_dynamic_field,
data: {list_type: list_type, field_name: field_name},
async: false
}).responseText.replace(/NUM/g, num);
$(dynamic_field_html).insertAfter(this);
var init_widget = paginer.init_widget[list_type][field_name];
if ($.isFunction(init_widget)) {
init_widget(row);
}
};
function new_filter_row(list_id) {
"use strict";
var row_html = new_filter_row_html(list_id);
var filter_li = $('<li/>');
filter_li.append($(row_html));
$('#' + list_id + '-filter_panel').append(filter_li);
filter_li.find('.field-choice').change();
}
var new_filter_click = function() {
"use strict";
new_filter_row(get_list_id(this));
};
$(function() {
"use strict";
$('div.paginer a.link').click(paginer_link_click);
$('.group-action').click(perform_group_action);
$('.filter-button').click(update_filters);
$('.sort-choices .sort-choice').click(update_order);
$('.add-filter-row').click(new_filter_click);
$(document).on('change', '.field-choice', update_filter_row);
$(document).on('click', '.delete-filter', function() {
$(this).closest('li').remove();
});
$(window).trigger('hashchange');
});