script.js
5.93 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
var activeLemmaPanel = 'new_frames';
var selectedColor = 'rgb(176, 196, 222)'; // css backgroud color of selected table elements
$(function(){
$('button').button();
$('.ui-state-default').live('mouseover', function() {
$(this).addClass('ui-state-hover');
});
$('.ui-state-default').live('mouseout', function() {
$(this).removeClass('ui-state-hover');
});
});
$.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.is(':checked');
else if (this.is(':radio'))
return this.is(':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.multiselect("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.multiselect("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
});
};
function blank(str) {
re = /^[\t\n ]*$/;
return re.test(str);
}
// 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))
error_alert(params.description+' '+_("nie powiodło się."));
}
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();
}
$(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'));
}
});
////////////////////////////////////////////////////
function getDescendantsNodes(parent)
{
var childElements = new Array();
if(parent.childNodes)
{
for(var i=0; i<parent.childNodes.length;i++)
{
childElements.push(parent.childNodes[i]);
}
for(var i=0; i<parent.childNodes.length; i++)
{
var descendants = getDescendantsNodes(parent.childNodes[i]);
for(var j=0; j<descendants.length; j++)
{
childElements.push(descendants[j]);
}
}
}
return childElements;
}
///////////////////// funkcje ogolnego uzycia /////////////////////////////////
// czyszczenie formularza
function resetForm($form) {
$form.find('input:text, input:password, input:file, select, textarea').val('');
$form.find('input:radio, input:checkbox')
.removeAttr('checked').removeAttr('selected');
$form.find('#sem-arguments').remove();
}
// funkcja konwertujaca kody html znakow do znakow
function convertHtml(str) {
str = str.replace(/'/g, '\'');
str = str.replace(/"/g, '\"');
str = str.replace(/&/g, '&');
str = str.replace(/>/g, '>');
str = str.replace(/</g, '<');
return str;
}
String.prototype.startsWith = function(str)
{return (this.match("^"+str)==str)}
String.prototype.endsWith = function(suffix) {
if (this.length < suffix.length) return false;
return this.lastIndexOf(suffix) === this.length - suffix.length;
}