combo/combo/public/static/js/combo.public.js

241 lines
8.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function combo_load_cell(elem) {
var $elem = $(elem);
var url = $elem.data('ajax-cell-url');
var extra_context = $elem.data('extra-context');
$.support.cors = true; /* IE9 */
var qs;
if (window.location.search) {
qs = window.location.search + '&';
} else {
qs = '?';
}
if (extra_context) {
qs += 'ctx=' + extra_context;
}
$.ajax({url: url + qs,
xhrFields: { withCredentials: true },
async: true,
dataType: 'html',
crossDomain: true,
success: function(data) {
$elem.addClass('ajax-loaded');
$elem.find('> div').nextAll().remove();
if (data == '') {
$elem.find('> div').remove();
} else {
$elem.find('> div').html(data);
}
$(document).trigger('combo:cell-loaded', $elem);
},
error: function(error) {
$elem.find('.loading').addClass('error-loading');
window.console && console.log(':(', error);
}
});
}
function combo_modify_query_string(name, value) {
var search = window.location.search.substring(1);
var parts = search.split('&');
var newparts = [];
var modified = 0;
value = encodeURIComponent(value);
for (var i = 0; i < parts.length; i++) {
if (! parts[i]) {
continue;
}
var pair = parts[i].split('=');
if (pair[0] == name) {
if (value) {
newparts.push(name + '=' + value);
}
modified = 1;
} else {
newparts.push(parts[i]);
}
}
if (! modified && value) {
newparts.push(name + '=' + value);
}
if (newparts) {
search = '?' + newparts.join('&');
} else {
search = '';
}
return search;
}
$(function() {
$('[data-ajax-cell-refresh]').each(function(idx, elem) {
var $elem = $(elem);
function refresh() {
combo_load_cell($elem);
}
$elem.timeout_id = setInterval(refresh, $elem.data('ajax-cell-refresh')*1000);
});
$('[data-ajax-cell-must-load]').each(function(idx, elem) {
combo_load_cell($(elem).parents('div.cell')[0]);
});
/* utility functions and events, for themes */
$('.togglable').on('click', function() {
$(this).toggleClass('toggled');
});
/* reload cells when parameters changes */
function combo_refresh_ajax_cells() {
$('[data-ajax-cell-url]').each(function(idx, elem) {
var $elem = $(elem);
var msg = $(elem).data('ajax-cell-loading-message');
$elem.prepend('<div><div class="loading"><span class="loading-message">' + msg + '</span></div></div>');
combo_load_cell($elem);
});
}
if (window.history.pushState && $('.parameterscell').length > 0) {
/* set initial state */
window.history.replaceState("reload", "", window.location.href);
$(window).on('popstate', function (event) {
if (event.originalEvent.state == "reload") {
combo_refresh_ajax_cells();
}
});
}
$(document).on('change', '.combo-parameters-cell-form select', function (e) {
var $target = $(e.target);
var value = $target.val();
var name = $target[0].name;
var new_qs = combo_modify_query_string(name, value);
if (window.history.pushState) {
window.history.pushState("reload", "", window.location.pathname + new_qs);
combo_refresh_ajax_cells();
} else {
window.location.search = new_qs;
}
});
$(document).on('click', '.more-items a', function() {
$(this).parent().hide();
$(this).parent().nextAll().show();
});
var menu_page_ids = $.makeArray($('[data-menu-page-id]').map(function() { return $(this).data('menu-page-id'); }));
if (menu_page_ids.length && $('body').data('check-badges') === true) {
$.ajax({url: $('body').data('api-root') + 'menu-badges/',
xhrFields: { withCredentials: true },
async: true,
dataType: 'json',
data: {'page': menu_page_ids},
crossDomain: true,
success: function(data) {
$(document).trigger('combo:menu-badges-loaded', data);
}});
}
$(document).on('combo:menu-badges-loaded', function(ctx, data) {
for (var page_id in data) {
var badge = data[page_id];
if (badge.badge) {
$('[data-menu-page-id=' + page_id + '] > a > span').append(' <span class="badge">' + badge.badge + '</span>');
}
if (badge.klass) {
$('[data-menu-page-id=' + page_id + ']').addClass(badge.klass);
}
}
});
$('.selfdeclaredinvoicepayment form').on('combo:load-invoice', window.displayPopup);
$('.selfdeclaredinvoicepayment form').on('submit', function(event) {
var url = $(this).attr('action');
var data = $(this).serialize();
var $form = $(this);
$.ajax({url: url + '?ajax=on',
xhrFields: { withCredentials: true },
data: data,
async: true,
dataType: 'json',
success: function(data) {
if (data.msg) {
var err_msg = $('<p></p>');
err_msg.text(data.msg);
$(err_msg).dialog({
dialogClass: 'alert',
modal: true,
width: 'auto',
buttons: {
'×': function() {
$(this).dialog('destroy');
}
},
});
return;
}
$form.data('url', data.url);
$form.trigger('combo:load-invoice', event);
},
error: function(error) {
window.console && console.log(':(', error);
}
});
return false;
});
function set_booking_calendar_sensitivity(table) {
if ($(table).find('input:checked').length == 0) {
/* no checked box, enable them all */
$(table).find('input').prop('disabled', false);
return;
}
/* disable every thing */
var column_index = $(table).find('input:checked').parents('td').index();
$(table).find('td').removeClass('active-column').removeClass('clickable');
$(table).find('input').prop('disabled', true);
/* enable checkboxes from the active column */
$(table).find('td:nth-child(' + (column_index+1) + ') input').prop('disabled', false);
/* mark active column */
$(table).find('td:nth-child(' + (column_index+1) + ')').addClass('active-column');
/* enable contiguous checkboxes; this is done by adding a clickable class
* to the approriate checkboxes, as all of them need to be kept as enabled
* HTML-wise to be transmitted on POST.
*/
var checkboxes = $(table).find('td:nth-child(' + (column_index+1) + ') input:checked');
/* enable the preceding one */
$(checkboxes[0]).parents('tr').prev().find('td:nth-child(' + (column_index+1) + ')').addClass('clickable');
/* enable first one, so it can be unchecked */
$(checkboxes[0]).parents('td').addClass('clickable');
/* enable last one, so it can be unchecked */
$(checkboxes[checkboxes.length-1]).parents('td').addClass('clickable');
/* enable the following one */
$(checkboxes[checkboxes.length-1]).parents('tr').next().find('td:nth-child(' + (column_index+1) + ')').addClass('clickable');
}
$('body').on('click', 'div.cell.foldable > div > h2:first-child', function() {
$(this).parents('div.foldable').toggleClass('folded');
return false;
});
$('body').on('click', 'a.calchunk', function(event){
event.preventDefault();
var $elem = $(this);
var url = $elem.data('content-url');
$.ajax({
url: url,
async: true,
dataType: 'html',
crossDomain: true,
success: function(data){
$elem.closest('div.calcontent').html(data);
},
error: function(error){
console.log(':(', error);
}
});
});
$('.bookingcalendar table').each(function(idx, elem) { set_booking_calendar_sensitivity(elem); });
$('body').on('change', '.bookingcalendar input', function() {
set_booking_calendar_sensitivity($(this).parents('table'));
});
});