paginer.js
4.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
/*
paginer - mapa paginowanych list na stronie, indeksowana idami
listę reprezentuje obiekt z polami:
* type - typ listy
* order_by - nazwa pola po którym jest porządek
* filters - słownik/obiekt odpowiadający (mniej więcej) parametrom filter
* lista kolumnn - potrzebna dla ajaxa (nie zmienia się)
*/
// 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 page_numbers = {};
function get_page_number(id) {
"use strict";
if (!page_numbers[id])
return 1;
return page_numbers[id];
}
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)
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);
};
// TODO przerobić
var update_filters = function() {
"use strict";
var list_id, val;
list_id = this.id.replace('_filter-button', '');
$.each(this.form.elements, function() {
if (this.name !== '') {
val = $(this).value();
if (val !== null) {
if (val !== '')
paginer[list_id][1].filters[this.name] = val;
else
delete paginer[list_id][1].filters[this.name];
}
}
});
change_page(list_id, 1);
};
var update_order = function() {
"use strict";
var sort_choices = this.parentNode;
var list_id = sort_choices.id.replace('_sort-choices', '');
var sign = $('.sort-direction', this).text() > 0? '' : '-';
var field_name = $('.sort-field-name', this).text();
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 = 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]);
}
// TODO całkiem przerobić (dynamiczne wiersze)
function reload_panel(list_id) {
"use strict";
var panel = $('#' + list_id + '_filter-panel');
var filters = paginer[list_id][1].filters;
if (panel.length === 0)
return;
$.each(panel[0].elements, function() {
if (this.name) {
if ($(this).is(':checkbox')) {
if (filters[this.name])
this.checked = 'checked';
else
this.checked = '';
} else {
if (filters[this.name] !== undefined)
$(this).val(filters[this.name]); // źle dla radio
else
$(this).val('');
}
}
});
}
$(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);
});
function paginer_bind_events(id) {
"use strict";
$('div#'+id+' a.link').click(paginer_link_click);
$('div#'+id+' .group-action').click(perform_group_action);
}
function paginer_ajax_callback(data, id) {
"use strict";
paginer_bind_events(id);
var list_callback = paginer[id][0]+'_bind_events';
if (window[list_callback])
window[list_callback](id);
}
function change_page(id, nr, protect_hash) {
"use strict";
var param = {
id: id,
type: paginer[id][0],
params: paginer[id][1],
page_nr: nr,
action: paginer[id][2],
group_actions: paginer[id][3]
};
$.ajaxJSON({
method: 'post',
url: ajax_paginer,
data: param,
description: _("Pobranie strony"),
dest: $('#'+id+'.paginer'),
callback: paginer_ajax_callback,
callback_args: [id]
});
page_numbers[id] = nr;
if (!protect_hash)
update_hash();
}
function update_list(id, protect_hash) {
"use strict";
change_page(id, get_page_number(id), protect_hash);
}