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

147 lines
5.6 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', {services: COMBO_KNOWN_SERVICES});
}
}
function create_menu_items() {
$('#sidepage-menu').remove();
var menu_links = $('<ul id="sidepage-menu">');
var more_entries = Array();
var service_order = Array('authentic', 'wcs', 'combo', 'passerelle', 'hobo');
COMBO_KNOWN_SERVICES.sort(function(a, b) {
a_service_order = service_order.indexOf(a.service_id);
b_service_order = service_order.indexOf(b.service_id);
if (a_service_order == b_service_order) {
return a.service_id.localeCompare(b.service_id);
}
if (a_service_order < 0) return 1;
if (b_service_order < 0) return -1;
return a_service_order - b_service_order;
});
$(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', {services: COMBO_KNOWN_SERVICES});
create_menu_items();
} else {
var this_hostname = window.location.hostname;
var look_for_wcs = false;
var authentic_url = undefined;
$(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) {
look_for_wcs = true;
element.data = Array();
check_all_done();
return;
}
}
if (element.service_id === 'authentic') {
authentic_url = element.url;
}
$.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(); }
}
);
});
if (look_for_wcs && authentic_url) {
/* if there is several wcs instances, we ask authentic for details on the
* user, to get the services where the user has some roles
*/
$.ajax({url: authentic_url + 'user_info/',
xhrFields: { withCredentials: true },
async: true,
dataType: 'jsonp',
crossDomain: true,
success: function(data) {
var services_to_consider = Array();
/* iterate over all services, to get those to consider */
$(COMBO_KNOWN_SERVICES).each(function(index, element) {
if (element.service_id !== 'wcs') return;
$(data.services).each(function(auth_index, auth_element) {
if (auth_element.slug !== element.slug) return;
if (auth_element.roles.length == 0) return;
services_to_consider.push(element);
});
});
if (services_to_consider.length == 1) {
/* only handle the case with a single service, for now */
var element = services_to_consider[0];
$.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(); }
}
);
}
},
error: function(error) { console.log('bouh', error); }
}
);
}
}
var sidepage_button = $('#sidepage #applabel');
sidepage_button.text('Publik ▼');
});