script.js
9.44 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
var hash_interpreted = false;
$(function(){
$('.tabs').tabs({
show: function() {
if (hash_interpreted)
update_hash();
},
});
$('button').button();
$('input.datepicker').datepicker($.datepicker.regional["pl"]);
$(document).on('mouseover', '.ui-state-default', function() {
$(this).addClass('ui-state-hover');
});
$(document).on('mouseout', '.ui-state-default', function() {
$(this).removeClass('ui-state-hover');
});
if (window.paginer || $('.tabs').length > 0) {
$(window).bind('hashchange', function(e) {
var hash = location.hash.substr(1);
interpret_hash(hash);
hash_interpreted = true;
});
$(window).trigger('hashchange');
}
});
$.fn.disable = function() {
return this.prop('disabled', true);
};
$.fn.enable = function() {
return this.prop('disabled', false);
};
$.fn.value = function() {
if (this.is(':checkbox'))
return this.prop('checked');
else if (this.is(':radio'))
return this.prop('checked')? this.val() : null;
else
return this.val();
};
$.fn.bind_hover = function(c) {
var elem = this;
this.hover(
function() {elem.addClass(c);},
function() {elem.removeClass(c);}
);
};
function multiselect_toggle(select, option_value, enable) {
select = $(select);
widget = select.multiselect2("widget");
option = widget.find('[value="' + option_value + '"]');
if (enable)
option.enable();
else
option.disable();
option.parent().toggleClass('ui-state-disabled', !enable);
option.parent().parent().toggleClass('ui-multiselect-disabled', !enable);
};
function multiselect_enable(select, option_value) {
select = $(select);
widget = select.multiselect2("widget");
option = widget.find('[value="' + option_value + '"]');
option.enable();
option.parent().removeClass('ui-state-disabled');
option.parent().parent().removeClass('ui-multiselect-disabled');
};
function _(text) {
return text;
};
function error_alert(text) {
alert(text);
};
// parametry: method, url, data | description, dest, callback, callback_args
$.ajaxJSON = function(params) {
var encoded_data = {};
if (params.save && !before_save())
return false;
$.each(params.data, function(key, value) {
encoded_data[key] = $.toJSON(value);
});
$.ajax({
type: params.method,
url: params.url,
dataType: 'json',
data: encoded_data,
success: parse_result(params),
error: params.error_callback
});
return true;
};
function blank(str) {
re = /^[\t\n ]*$/;
return re.test(str);
}
function strip(str) {
return str.replace(/^\s+|\s+$/g, '');
}
// parametry: description, dest, callback, callback_args
function parse_result(params) {
return function(data, status) {
if(data.result != 'ok' || status == 'error') {
if(data.result=='logout')
error_alert(_("Sesja wygasła - zaloguj się i spróbuj ponownie."));
else
if (!params.bad_data_callback || params.bad_data_callback(data.result)) {
var msg = params.description + ' ' + _("nie powiodło się:\n");
msg += data.result;
error_alert(msg);
}
}
else { // OK
if (params.dest) {
params.dest.html(data.html);
}
if (data.js) {
eval(data.js); // nie używane
}
if (params.callback) {
if (!params.callback_args)
params.callback_args = [];
args = [data].concat(params.callback_args);
params.callback.apply({}, args);
}
}
}
};
function debug(content) {
var doc = $('#debug').contents().get(0);
doc.open();
doc.writeln(content);
doc.close();
};
$(function() {
$('#show-debug').click(function() {
$('#debug').toggle();
});
$('#debug').height($(window).height() - $('#main_menu').height());
$('#debug').css('top', $('#main_menu').height())
});
function parse_error(event, request, settings) {
debug(request.responseText);
$('#show-debug').show();
}
//$.ajaxSetup({global: true, error: parse_error});
$(function() {$('#debug').ajaxError(parse_error);});
$('html').ajaxSend(function(event, xhr, settings) {
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
// Only send the token to relative URLs i.e. locally.
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
}
});
// http://stackoverflow.com/questions/118241/calculate-text-width-with-javascript/2117698#2117698
(function($) {
$.fn.textMetrics = function() {
var h = 0, w = 0, el = this;
var div = document.createElement('div');
document.body.appendChild(div);
$(div).css({
position: 'absolute',
left: -1000,
top: -1000,
display: 'none'
});
$(div).html($(el).html());
var styles = ['font-size','font-style', 'font-weight', 'font-family','line-height', 'text-transform', 'letter-spacing'];
$(styles).each(function() {
var s = this.toString();
$(div).css(s, $(el).css(s));
});
h = $(div).outerHeight();
w = $(div).outerWidth();
$(div).remove();
var ret = {
height: h,
width: w
};
return ret;
}
})(jQuery);
function update_hash() {
var new_hash = make_hash();
old_hash = new_hash;
location.href = '#' + new_hash;
}
// nie dodawać, jeśli wartości są domyślne?
function make_hash() {
var bottom_tabs, tabs, tab_path = [], query, id, data, original_data;
var paginer_el;
var are_tabs = $('.tabs').length > 0;
if (are_tabs) {
tabs = $('.tabs:visible').first();
while (tabs.find('.tabs:visible').length > 0) {
tabs = tabs.find('.tabs:visible').first();
}
bottom_tabs = tabs;
do {
tab_path.unshift(tabs.children('.ui-tabs-panel:visible')[0].id);
tabs = tabs.parents('.tabs').first();
} while (tabs.length > 0);
} else
tab_path = [''];
paginer_el = $('.paginer:visible', bottom_tabs);
if (paginer_el.length > 0) {
var id = paginer_el[0].id;
data = paginer[id][1];
original_data = original_paginer[id][1];
// TODO sprawdzać, czy nie domyślny!
query = [];
if (data.order_by != original_data.order_by)
query.push('order_by=' + data.order_by);
$.each(data.filters, function(field, value) {
if (original_data.filters[field] === undefined)
query.push(field + '=' + value);
});
if (get_page_number(id) > 1)
query.push('page=' + get_page_number(id));
tab_path.push(query.join('&'));
} else
tab_path.push('');
return tab_path.join('/');
}
var old_hash = 'not gonna happen';
function interpret_hash(hash) {
var data = hash.split('/'), options, children;
if (hash == old_hash)
return;
var id, paginer_data, original_data, new_order_by, new_filters, new_page;
var paginer_container, paginer_el;
if (hash != '')
$.each(data.slice(0, data.length - 1), function(i, part) {
if (part)
$('#' + part).parents('.tabs').first().tabs('select', '#' + part);
});
else {
paginer_container = $('.ui-tabs-panel').first();
hash_interpreted = false; // HACK
while (paginer_container.length > 0) {
paginer_container.parents('.tabs').first().tabs(
'select', '#' + paginer_container[0].id);
children = $('.ui-tabs-panel', paginer_container);
if (children.length > 0)
paginer_container = children.first();
else
paginer_container = children;
}
hash_interpreted = true; // HACK, może zadziała
}
if (window.paginer) {
if (data[0] != '')
paginer_container = $('#' + data[data.length - 2]);
else { // nie ma tabów (lub pusty hash...)
paginer_container = $(document.body);
do {
children = $('.ui-tabs-panel:visible', paginer_container);
if (children.length)
paginer_container = children[0];
} while(children.length);
}
paginer_el = $('.paginer', paginer_container)
if (paginer_el.length > 0) {
id = paginer_el[0].id;
paginer_data = paginer[id][1];
original_data = original_paginer[id][1];
new_order_by = original_data.order_by;
new_page = 1;
new_filters = $.extend({}, original_data.filters);
if (data[data.length - 1])
$.each(data[data.length - 1].split('&'), function(i, pair) {
if (pair) {
var p = pair.split('=', 2);
var field = p[0], value = p[1];
if (value == 'false')
value = false;
if (value == 'true')
value = true; // nieodporne na tekstowe filtry (jeśli będą)
if (field == 'order_by') {
new_order_by = value;
} else if (field == 'page') {
new_page = parseInt(value);
} else { // jakieś sprawdzanie poprawności? w sumie serwer to zrobi...
new_filters[field] = value;
}
}
});
paginer_data.order_by = new_order_by;
paginer_data.filters = new_filters;
page_numbers[id] = new_page;
update_list(id, true); // protect hash
show_order(id);
reload_panel(id);
}
}
old_hash = hash;
}