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

169 lines
5.5 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');
$.support.cors = true; /* IE9 */
$.ajax({url: url + window.location.search,
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;
});
});