application.js
17.1 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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function noosfero_init() {
// focus_first_field(); it is moving the page view when de form is down.
}
/* If applicable, find the first field in which the user can type and move the
* keyboard focus to it.
*
* ToDo: focus only inside the view box to do not roll the page.
*/
function focus_first_field() {
form = document.forms[0];
if (form == undefined) {
return;
}
for (var i = 0; i < form.elements.length; i++) {
field = form.elements[i];
if (field.type == 'text' || field.type == 'textarea') {
try {
field.focus();
return;
} catch(e) { }
}
}
}
/* * * Convert a string to a valid login name * * */
function convToValidLogin( str ) {
return convToValidIdentifier(str, '')
}
/* * * Convert a string to a valid login name * * */
function convToValidIdentifier( str, sep ) {
return str.toLowerCase()
.replace( /@.*$/, "" )
.replace( /á|à|ã|â/g, "a" )
.replace( /é|ê/g, "e" )
.replace( /í/g, "i" )
.replace( /ó|ô|õ|ö/g, "o" )
.replace( /ú|ũ|ü/g, "u" )
.replace( /ñ/g, "n" )
.replace( /ç/g, "c" )
.replace( /[^-_a-z0-9.]+/g, sep )
}
function updateUrlField(name_field, id) {
url_field = $(id);
old_url_value = url_field.value;
new_url_value = convToValidIdentifier(name_field.value, "-");
url_field.value = new_url_value;
if (!/^\s*$/.test(old_url_value)
&& old_url_value != new_url_value
) {
warn_value_change(url_field);
}
}
function show_warning(field, message) {
new Effect.Highlight(field, {duration:3});
$(message).show();
}
function hide_warning(field) {
$(field).hide();
}
function enable_button(button) {
button.enable();
button.removeClassName("disabled");
}
function disable_button(button) {
button.disable();
button.addClassName("disabled");
}
function toggleDisabled(enable, element) {
if (enable) {
enable_button(element);
}
else {
disable_button(element);
}
}
/* ICON SELECTOR - LinkListBlock */
function showIconSelector(main_div) {
iconSelector = jQuery(main_div).children('.icon-selector')[0];
jQuery(iconSelector).toggle();
}
function changeIcon(iconSelected, iconName) {
iconSelector = iconSelected.parentNode;
setTimeout('iconSelector.style.display = "none"', 100);
main_div = iconSelector.parentNode;
span = main_div.getElementsByTagName('span')[0];
span.className = iconSelected.className;
iconInput = main_div.getElementsByTagName('input')[0];
iconInput.value = iconName;
}
function hideOthersIconSelector(current_div) {
jQuery('.icon-selector').not(jQuery(current_div).children('.icon-selector')).hide();
}
function loading(element_id, message) {
$(element_id).addClassName('loading');
if (message) {
$(element_id).update(message);
}
}
function small_loading(element_id, message) {
$(element_id).addClassName('small-loading');
if (message) {
$(element_id).update(message);
}
}
function loading_done(element_id) {
$(element_id).removeClassName('loading');
$(element_id).removeClassName('small-loading');
$(element_id).removeClassName('small-loading-dark');
}
function open_loading(message) {
jQuery('body').append("<div id='overlay_loading' class='ui-widget-overlay' style='display: none'/><div id='overlay_loading_modal' style='display: none'><p>"+message+"</p><img src='/images/loading-dark.gif'/></div>");
jQuery('#overlay_loading').show();
jQuery('#overlay_loading_modal').fadeIn('slow');
}
function close_loading() {
jQuery('#overlay_loading_modal').fadeOut('slow', function() {
jQuery('#overlay_loading_modal').remove();
jQuery('#overlay_loading').remove();
});
}
function update_loading(message) {
jQuery('#overlay_loading_modal p').text(message);
}
function redirect_to(url) {
document.location=url;
}
/* Products edition */
function numbersonly(e, separator) {
var key;
var keychar;
if (window.event) {
key = window.event.keyCode;
}
else if (e) {
key = e.which;
}
else {
return true;
}
keychar = String.fromCharCode(key);
if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) ) {
return true;
}
else if ((("0123456789").indexOf(keychar) > -1)) {
return true;
}
else if (keychar == separator) {
if (e.target.value.indexOf(separator) > -1) {
return false;
}
return true;
}
else
return false;
}
// transform all element with class ui_button in a jQuery UI button
function render_jquery_ui_buttons(element_id) {
if (element_id) {
element_id = '#' + element_id
jQuery(element_id).button({
icons: {
primary: jQuery(element_id).attr('data-primary-icon'),
secondary: jQuery(element_id).attr('data-secondary-icon')
}
}
)
}
else {
jQuery('.ui_button').each(function() {
jQuery(this).button({
icons: {
primary: this.getAttribute('data-primary-icon'),
secondary: this.getAttribute('data-secondary-icon')
}
}
)
})
}
}
function render_all_jquery_ui_widgets() {
jQuery(function() {
render_jquery_ui_buttons();
jQuery('.ui-tabs').each(function(){
jQuery(this).tabs({
cookie: { name: this.id }
});
});
});
}
function expandCategory(block, id) {
var link = jQuery('#block_' + block + '_category_' + id);
if (category_expanded['block'] > 0 && category_expanded['category'] > 0 && category_expanded['block'] == block && category_expanded['category'] != id && link.hasClass('category-root')) {
expandCategory(category_expanded['block'], category_expanded['category']);
category_expanded['category'] = id;
category_expanded['block'] = block;
}
if (category_expanded['block'] == 0) category_expanded['block'] = block;
if (category_expanded['category'] == 0) category_expanded['category'] = id;
jQuery('#block_' + block + '_category_content_' + id).slideToggle('slow');
link.toggleClass('category-expanded');
if (link.hasClass('category-expanded')) link.html(expanded_icon);
else {
link.html(collapsed_icon);
if (link.hasClass('category-root')) {
category_expanded['block'] = 0;
category_expanded['category'] = 0;
}
}
}
function toggleSubmenu(trigger, title, link_list) {
trigger.onclick = function() {
var submenu = jQuery(trigger).siblings('.menu-submenu');
var hide = false;
if (submenu.length > 0 && submenu.is(':visible')) hide = true;
hideAllSubmenus();
// Hide or show this submenu if it already exists
if (submenu.length > 0) {
if (!hide) {
var direction = 'down';
if (submenu.hasClass('up')) direction = 'up';
submenu.show('slide', { 'direction' : direction }, 'slow');
}
}
return false;
}
hideAllSubmenus();
// Build and show this submenu if it doesn't exist yet
var direction = 'down';
if (jQuery(trigger).hasClass('up')) direction = 'up';
var submenu = jQuery('<div></div>').attr('class', 'menu-submenu ' + direction).attr('style', 'display: none');
var header = jQuery('<div></div>').attr('class', 'menu-submenu-header');
var content = jQuery('<div></div>').attr('class', 'menu-submenu-content');
var list = jQuery('<ul></ul>').attr('class', 'menu-submenu-list');
var footer = jQuery('<div></div>').attr('class', 'menu-submenu-footer');
content.append('<h4>' + title + '</h4>');
jQuery.each(link_list, function(index, link_hash) {
for (label in link_hash) {
list.append('<li><a href="' + link_hash[label] + '">' + label + '</a></li>');
}
});
content.append(list);
submenu.append(header).append(content).append(footer);
jQuery(trigger).before(submenu);
submenu.show('slide', { 'direction' : direction }, 'slow');
}
function toggleMenu(trigger) {
hideAllSubmenus();
jQuery(trigger).siblings('.simplemenu-submenu').toggle('slide', {direction: 'up'}, 'slow').toggleClass('opened');
}
function hideAllSubmenus() {
jQuery('.menu-submenu.up:visible').hide('slide', { 'direction' : 'up' }, 'slow');
jQuery('.simplemenu-submenu:visible').hide('slide', { 'direction' : 'up' }, 'slow').toggleClass('opened');
jQuery('.menu-submenu.down:visible').hide('slide', { 'direction' : 'down' }, 'slow');
}
// Hide visible ballons when clicked outside them
jQuery(document).ready(function() {
jQuery('body').click(function() { hideAllSubmenus(); });
jQuery('.menu-submenu-trigger').click(function(e) { e.stopPropagation(); });
jQuery('.simplemenu-trigger').click(function(e) { e.stopPropagation(); });
});
function input_javascript_ordering_stuff() {
jQuery(function() {
jQuery(".input-list").sortable({
placeholder: 'ui-state-highlight',
axis: 'y',
opacity: 0.8,
tolerance: 'pointer',
forcePlaceholderSize: true,
update: function(event, ui) {
jQuery.post(jQuery(this).next('.order-inputs').attr('href'), jQuery(this).sortable('serialize'));
}
});
jQuery(".input-list li").disableSelection();
jQuery(".input-list li").hover(
function() {
jQuery(this).addClass('editing-input');
jQuery(this).css('cursor', 'move');
},
function() {
jQuery(this).removeClass('editing-input');
jQuery(this).css('cursor', 'pointer');
}
);
jQuery("#display-add-input-button > .hint").show();
});
}
function display_input_stuff() {
jQuery(function() {
jQuery("#add-input-button").click(function() {
jQuery("#display-add-input-button").find('.loading-area').addClass('small-loading');
url = jQuery(this).attr('href');
jQuery.get(url, function(data){
jQuery("#" + "new-product-input").html(data);
jQuery("#display-add-input-button").find('.loading-area').removeClass('small-loading');
jQuery("#add-input-button").hide();
});
return false;
});
});
}
function add_input_stuff() {
jQuery(function() {
jQuery(".cancel-add-input").click(function() {
jQuery("#new-product-input").html('');
jQuery("#add-input-button").show();
return false;
});
jQuery("#input-category-form").submit(function() {
id = "product-inputs";
jQuery(this).find('.loading-area').addClass('small-loading');
jQuery("#input-category-form,#input-category-form *").css('cursor', 'progress');
jQuery.post(this.action, jQuery(this).serialize(), function(data) {
jQuery("#" + id).html(data);
});
return false;
});
jQuery('body').scrollTo('50%', 500);
});
}
function input_javascript_stuff(id) {
jQuery(function() {
id = 'input-' + id;
jQuery("#add-"+ id +"-details,#edit-"+id).click(function() {
target = '#' + id + '-form';
jQuery('#' + id + ' ' + '.input-details').hide();
jQuery(target).show();
// make request only if the form is not loaded yet
if (jQuery(target + ' form').length == 0) {
small_loading(id);
jQuery(target).load(jQuery(this).attr('href'), function() {
loading_done(id);
jQuery('#' + id + ' .input-informations').removeClass('input-form-closed').addClass('input-form-opened');
});
}
else {
jQuery('#' + id + ' .input-informations').removeClass('input-form-closed').addClass('input-form-opened');
}
return false;
});
jQuery("#remove-" + id).unbind('click').click(function() {
if (confirm(jQuery(this).attr('data-confirm'))) {
url = jQuery(this).attr('href');
small_loading("product-inputs");
jQuery.post(url, function(data){
jQuery("#" + "product-inputs").html(data);
loading_done("product-inputs");
});
}
return false;
});
});
}
function edit_input_stuff(id, currency_separator) {
id = "input-" + id;
jQuery(function() {
jQuery("#" + "edit-" + id + "-form").ajaxForm({
target: "#" + id,
beforeSubmit: function(a,f,o) {
o.loading = small_loading('edit-' + id + '-form');
o.loaded = loading_done(id);
}
});
jQuery("#cancel-edit-" + id).click(function() {
jQuery("#" + id + ' ' + '.input-details').show();
jQuery("#" + id + '-form').hide();
jQuery('#' + id + ' .input-informations').removeClass('input-form-opened').addClass('input-form-closed');
return false;
});
jQuery(".numbers-only").keypress(function(event) {
return numbersonly(event, currency_separator)
});
add_input_unit(id, jQuery("#" + id + " select :selected").val())
jQuery("#" + id + ' select').change(function() {
add_input_unit(id, jQuery("#" + id + " select :selected").val())
});
jQuery("#" + id).enableSelection();
});
}
function add_input_unit(id, selected_unit) {
if (selected_unit != '') {
jQuery("#" + id + ' .price-by-unit').show();
jQuery("#" + id + ' .selected-unit').text(jQuery("#" + id + " select :selected").text());
} else {
jQuery("#" + id + ' .price-by-unit').hide();
}
}
function loading_for_button(selector) {
jQuery(selector).append("<div class='small-loading' style='width:16px; height:16px; position:absolute; top:0; right:-20px;'></div>");
jQuery(selector).css('cursor', 'progress');
}
function new_qualifier_row(selector, select_qualifiers) {
index = jQuery(selector + ' tr').size() - 1;
jQuery(selector).append("<tr><td>" + select_qualifiers + "</td><td id='certifier-area-" + index + "'><select></select></td></tr>");
}
// controls the display of the login/logout stuff
jQuery(function($) {
$.getJSON('/account/user_data', function(data) {
if (data.login) {
// logged in
$('#user .logged-in, .login-block .logged-user-info').each(function() {
$(this).find('a[href]').each(function() {
var new_href = $(this).attr('href').replace('%{login}', data.login);
if (data.email_domain) {
new_href = new_href.replace('%{email_domain}', data.email_domain);
}
$(this).attr('href', new_href);
});
var html = $(this).html().replace(/%{login}/g, data.login).replace('%{month}', data.since_month).replace('%{year}', data.since_year);
$(this).html(html).fadeIn();
if (data.is_admin) {
$('#user .admin-link').show();
}
if (data.email_domain) {
$('#user .webmail-link').show();
}
});
} else {
// not logged in
$('#user .not-logged-in, .login-block .not-logged-user').fadeIn();
}
if (data.notice) {
display_notice(data.notice);
}
});
});
// controls the display of contact list
function check_contact_list(contact_list) {
jQuery(function($) {
var verify_url = $('#verify-contact-list').attr('href');
var add_contacts_url = $('#add-contact-list').attr('href');
var cancel_contacts_fetching_url = $('#cancel-fetching-emails').attr('href');
var interval = setInterval(function() {
$.getJSON(verify_url, function(data) {
if (data.fetched) {
clearInterval(interval);
if (data.error) {
$("#loading-dialog").dialog('close');
$.get(cancel_contacts_fetching_url);
redirect_to($('#invitation_back_button').attr('href'));
display_notice(data.error);
} else {
$.get(add_contacts_url, function(data){
$("#contacts-list").html(data);
});
};
$("#loading-dialog").dialog('close');
}
});
}, 5000);
setTimeout(function() {
clearInterval(interval);
$("#loading-dialog").dialog('close');
$.get(cancel_contacts_fetching_url);
redirect_to($('#invitation_back_button').attr('href'));
}, 600000);
});
}
function display_notice(message) {
var $noticeBox = jQuery('<div id="notice"></div>').html(message).appendTo('body').fadeTo('fast', 0.8);
$noticeBox.click(function() { $(this).hide(); });
setTimeout(function() { $noticeBox.fadeOut('fast'); }, 5000);
}
function log(msg) {
jQuery('#log').append('<div></div>').append(document.createTextNode(msg));
}
/* XMPP */
var noosfero_chat_window = null;
function open_chat_window(self_link) {
if (!noosfero_chat_window || noosfero_chat_window.closed) {
noosfero_chat_window = window.open('/chat','noosfero_chat','width=900,height=500');
}
noosfero_chat_window.focus();
return false;
}
jQuery(function($) {
/* Adds a class to "msie" to the body element if a Microsoft browser is
* detected. This is needed to workaround several of their limitations.
*/
if ( navigator.appVersion.indexOf("MSIE") > -1 ) {
document.body.className += " msie msie" +
navigator.appVersion.replace(/^.*MSIE\s+([0-9]+).*$/, "$1");
}
/* Adds a class to "webkit" to the body element if a Webkit based browser
* detected.
*/
if (window.devicePixelRatio) {
$('body').addClass('webkit');
}
});