combo/data/themes/gadjo/static/js/publik.js

90 lines
3.1 KiB
JavaScript

GADJO_DEFAULT_SIDEPAGE_STATUS = 'expanded';
$(function() {
var all_done = false;
function check_all_done() {
var local_done = true;
$(COMBO_KNOWN_SERVICES).each(function(index, service) {
if (service.data === undefined) {
local_done = false;
}
});
all_done = local_done;
if (all_done) {
window.sessionStorage.hobo_environment = JSON.stringify(COMBO_KNOWN_SERVICES);
window.sessionStorage.hobo_environment_timestamp = Date.now();
create_menu_items();
$(document).trigger('publik:environment-loaded');
}
}
function create_menu_items() {
$('#sidepage-menu').remove();
var menu_links = $('<ul id="sidepage-menu">');
var more_entries = Array();
$(COMBO_KNOWN_SERVICES).each(function(index, service) {
if (service.data === undefined || service.data.length == 0) {
return;
}
$(service.data).each(function(idx, element) {
var li = $('<li><a href="#">' + element.label + '</a></li>').appendTo(menu_links);
$(li).find('a').attr('href', element.url);
if (element.icon !== undefined) {
$(li).find('a').addClass('icon-' + element.icon);
} else if (element.slug !== undefined) {
$(li).find('a').addClass('icon-' + element.slug);
}
if (window.location.href.indexOf(element.url) == 0) {
$(li).addClass('active');
}
});
});
$(more_entries).each(function(index, entry) {
var li = $('<li><a href="#">' + entry.title + '</a></li>').appendTo(menu_links);
$(li).find('a').attr('href', entry.url);
});
menu_links.appendTo('#sidepage');
}
if (window.sessionStorage.hobo_environment &&
parseInt(window.sessionStorage.hobo_environment_timestamp) > Date.now()-600000) {
COMBO_KNOWN_SERVICES = JSON.parse(window.sessionStorage.hobo_environment);
$(document).trigger('publik:environment-loaded');
create_menu_items();
} else {
var this_hostname = window.location.hostname;
$(COMBO_KNOWN_SERVICES).each(function(index, element) {
if (element.backoffice_menu_url === null) {
element.data = Array();
check_all_done();
return;
}
if (element.service_id === 'wcs' && element.uniq === false) {
/* as wcs comes with many menu entries, if it's not the only instance
* in the environment, we simply skip it if it's not the active site.
*/
var that_hostname = $('<a>').attr('href', element.backoffice_menu_url)[0].hostname;
if (that_hostname != this_hostname) {
element.data = Array();
check_all_done();
return;
}
}
$.ajax({url: element.backoffice_menu_url,
xhrFields: { withCredentials: true },
async: true,
dataType: 'jsonp',
crossDomain: true,
success: function(data) { element.data = data; check_all_done(); },
error: function(error) { console.log('bouh', error); element.data = Array(); check_all_done(); }
}
);
});
}
var sidepage_button = $('#sidepage #applabel');
sidepage_button.text('Publik ▼');
});