2014-02-17 13:07:28 +01:00
|
|
|
/* ----------------------------------------------------------
|
|
|
|
Utilities
|
|
|
|
---------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* Console log fix
|
|
|
|
-------------------------- */
|
|
|
|
if (typeof(console) === 'undefined') {
|
|
|
|
var console = {};
|
|
|
|
console.log = console.error = console.info = console.debug = console.warn = console.trace = console.dir = console.dirxml = console.group = console.groupEnd = console.time = console.timeEnd = console.assert = console.profile = function() {};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Array utilities
|
|
|
|
https://github.com/Darklg/JavaScriptUtilities/blob/master/assets/js/vanilla-js/libs/vanilla-arrays.js
|
|
|
|
-------------------------- */
|
|
|
|
Array.contains = function(needle, haystack) {
|
|
|
|
var i = 0,
|
|
|
|
length = haystack.length;
|
|
|
|
|
|
|
|
for (; i < length; i++) {
|
|
|
|
if (haystack[i] === needle) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
Array.each = function(arrayToParse, callback) {
|
|
|
|
var i = 0,
|
|
|
|
length = arrayToParse.length;
|
|
|
|
for (; i < length; i++) {
|
|
|
|
callback(arrayToParse[i]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* CSS classes utilities
|
|
|
|
https://github.com/Darklg/JavaScriptUtilities/blob/master/assets/js/vanilla-js/libs/vanilla-classes.js
|
|
|
|
-------------------------- */
|
|
|
|
Element.getClassNames = function(element) {
|
|
|
|
var classNames = [],
|
|
|
|
elementClassName = element.className;
|
|
|
|
if (elementClassName !== '') {
|
|
|
|
elementClassName = elementClassName.replace(/\s+/g, ' ');
|
|
|
|
classNames = elementClassName.split(' ');
|
|
|
|
}
|
|
|
|
return classNames;
|
|
|
|
};
|
|
|
|
Element.hasClass = function(element, className) {
|
|
|
|
if (element.classList) {
|
|
|
|
return element.classList.contains(className);
|
|
|
|
}
|
|
|
|
return Array.contains(className, Element.getClassNames(element));
|
|
|
|
};
|
|
|
|
Element.addClass = function(element, className) {
|
|
|
|
if (element.classList) {
|
|
|
|
element.classList.add(className);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!Element.hasClass(element, className)) {
|
|
|
|
var elementClasses = Element.getClassNames(element);
|
|
|
|
elementClasses.push(className);
|
|
|
|
element.className = elementClasses.join(' ');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Element.removeClass = function(element, className) {
|
|
|
|
if (element.classList) {
|
|
|
|
element.classList.remove(className);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var elementClasses = Element.getClassNames(element);
|
|
|
|
var newElementClasses = [];
|
|
|
|
var i = 0,
|
|
|
|
arLength = elementClasses.length;
|
|
|
|
for (; i < arLength; i++) {
|
|
|
|
if (elementClasses[i] !== className) {
|
|
|
|
newElementClasses.push(elementClasses[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
element.className = newElementClasses.join(' ');
|
|
|
|
};
|
|
|
|
Element.toggleClass = function(element, className) {
|
|
|
|
if (!Element.hasClass(element, className)) {
|
|
|
|
Element.addClass(element, className);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Element.removeClass(element, className);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Add Event
|
|
|
|
https://github.com/Darklg/JavaScriptUtilities/blob/master/assets/js/vanilla-js/libs/vanilla-events.js
|
|
|
|
-------------------------- */
|
|
|
|
window.addEvent = function(el, eventName, callback) {
|
|
|
|
if (el.addEventListener) {
|
|
|
|
el.addEventListener(eventName, callback, false);
|
|
|
|
}
|
|
|
|
else if (el.attachEvent) {
|
|
|
|
el.attachEvent("on" + eventName, function(e) {
|
|
|
|
return callback.call(el, e);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
window.eventPreventDefault = function(event) {
|
|
|
|
return (event.preventDefault) ? event.preventDefault() : event.returnValue = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Smallest DOMReady
|
|
|
|
http://dustindiaz.com/smallest-domready-ever
|
|
|
|
-------------------------- */
|
2014-02-04 21:41:53 +01:00
|
|
|
function domReady(cb) {
|
|
|
|
/in/.test(document.readyState) // in = loadINg
|
|
|
|
? setTimeout('domReady('+cb+')', 9)
|
|
|
|
: cb();
|
|
|
|
}
|
|
|
|
|
2014-02-17 13:07:28 +01:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------
|
|
|
|
Main
|
|
|
|
---------------------------------------------------------- */
|
2014-02-04 21:41:53 +01:00
|
|
|
domReady(function(){
|
2014-02-17 13:48:04 +01:00
|
|
|
// Don't do this in iframe
|
|
|
|
if (window.self !== window.top) {return false;}
|
2014-02-04 21:41:53 +01:00
|
|
|
|
2014-02-17 13:07:28 +01:00
|
|
|
// Add portal stylesheet
|
|
|
|
var portalStyle = document.createElement("link");
|
|
|
|
portalStyle.setAttribute("rel", "stylesheet");
|
|
|
|
portalStyle.setAttribute("type", "text/css");
|
|
|
|
portalStyle.setAttribute("href", '/ynhpanel.css');
|
|
|
|
document.getElementsByTagName("head")[0].insertBefore(portalStyle, null);
|
|
|
|
|
|
|
|
// Create portal link
|
2014-02-04 21:41:53 +01:00
|
|
|
var portal = document.createElement('a');
|
2014-02-17 13:07:28 +01:00
|
|
|
portal.setAttribute('id', 'ynhportal');
|
2014-05-14 18:42:20 +02:00
|
|
|
portal.setAttribute('href', '/yunohost/sso/');
|
2014-05-20 11:58:46 +02:00
|
|
|
portal.setAttribute('class', 'disableAjax');
|
2014-02-17 13:07:28 +01:00
|
|
|
document.body.insertBefore(portal, null);
|
2014-02-04 21:41:53 +01:00
|
|
|
|
2014-05-14 01:42:31 +02:00
|
|
|
// Create overlay element
|
|
|
|
var overlay = document.createElement("div");
|
|
|
|
overlay.setAttribute("id","ynhoverlay");
|
|
|
|
overlay.setAttribute("style","display:none");
|
|
|
|
|
|
|
|
document.body.insertBefore(overlay, null);
|
|
|
|
|
|
|
|
//Color Application
|
|
|
|
var colors = ['bluebg','purplebg','redbg','orangebg','greenbg','darkbluebg','lightbluebg','yellowbg','lightpinkbg'];
|
|
|
|
|
2014-02-17 13:07:28 +01:00
|
|
|
// Get user's app
|
|
|
|
var r = new XMLHttpRequest();
|
|
|
|
r.open("GET", "/ynhpanel.json", true);
|
|
|
|
r.onreadystatechange = function () {
|
|
|
|
// Die if error
|
|
|
|
if (r.readyState != 4 || r.status != 200) return;
|
|
|
|
|
|
|
|
// Response is JSON
|
2014-05-14 01:42:31 +02:00
|
|
|
document.querySelector('body').classList.add('ynh-panel-active');
|
2014-02-17 13:07:28 +01:00
|
|
|
response = JSON.parse(r.responseText);
|
|
|
|
|
|
|
|
// Add overlay header
|
2014-05-20 11:58:46 +02:00
|
|
|
overlay.innerHTML += '<div id="yuno-user" class="wrapper info">' +
|
|
|
|
'<ul class="ul-reset user-menu"><li><a class="icon icon-connexion disableAjax" href="'+ response.portal_url +'?action=logout">'+response.t_logout+'</a></li></ul>'+
|
|
|
|
'<a class="user-container user-container-info disableAjax" href="'+ response.portal_url +'edit.html">' +
|
2014-05-14 19:23:52 +02:00
|
|
|
'<h2 class="user-username">'+ response.uid +'</h2>' +
|
|
|
|
'<small class="user-fullname">'+ response.givenName + ' ' + response.sn +'</small>' +
|
|
|
|
'<span class="user-mail">'+ response.mail +'</span>' +
|
|
|
|
'</a>'
|
2014-02-17 13:07:28 +01:00
|
|
|
'</div>';
|
2014-02-04 21:41:53 +01:00
|
|
|
|
2014-05-14 19:23:52 +02:00
|
|
|
|
2014-02-17 13:07:28 +01:00
|
|
|
// Add application links
|
|
|
|
var links = [];
|
2014-05-14 01:42:31 +02:00
|
|
|
Array.prototype.forEach.call(response.app, function(app, n){
|
2014-05-20 11:58:46 +02:00
|
|
|
links.push('<li><a class="'+colors[n]+' disableAjax" href="//'+app.url+'"><span class="first-letter" data-first-letter="'+ app.name.substr(0,2) +'"></span><span class="sourcePro">'+app.name+'</span></a></li>');
|
2014-02-17 13:07:28 +01:00
|
|
|
});
|
2014-05-14 01:42:31 +02:00
|
|
|
overlay.innerHTML += '<div id="yuno-apps" class="wrapper apps"><ul class="ul-reset listing-apps col colNomarge sourceProBold">'+ links.join('') +'</ul></div>';
|
2014-02-17 13:07:28 +01:00
|
|
|
|
|
|
|
// Add overlay to DOM
|
2014-05-14 01:42:31 +02:00
|
|
|
var btn = document.getElementById('logo'),
|
|
|
|
yunoverlay = document.getElementById('ynhoverlay'),
|
|
|
|
user = document.getElementById('yuno-user'),
|
|
|
|
apps = document.getElementById('yuno-apps');
|
|
|
|
|
|
|
|
var pfx = ["webkit", "moz", "MS", "o", ""];
|
|
|
|
function PrefixedEvent(element, type, callback) {
|
|
|
|
for (var p = 0; p < pfx.length; p++) {
|
|
|
|
if (!pfx[p]) type = type.toLowerCase();
|
|
|
|
element.addEventListener(pfx[p]+type, callback, false);
|
|
|
|
}
|
|
|
|
}
|
2014-02-17 13:07:28 +01:00
|
|
|
|
|
|
|
// Bind YNH Button
|
|
|
|
window.addEvent(portal, 'click', function(e){
|
|
|
|
// Prevent default click
|
|
|
|
window.eventPreventDefault(e);
|
|
|
|
// Toggle overlay on YNHPortal button
|
2014-05-14 01:42:31 +02:00
|
|
|
//Element.toggleClass(overlay, 'visible');
|
2014-02-17 14:36:51 +01:00
|
|
|
Element.toggleClass(portal, 'visible');
|
2014-05-14 01:42:31 +02:00
|
|
|
|
|
|
|
if(yunoverlay.classList.contains('yuno-active')) {
|
|
|
|
yunoverlay.classList.add('yuno-fadeOut');
|
|
|
|
PrefixedEvent(yunoverlay, "AnimationEnd", function(){
|
|
|
|
if(yunoverlay.classList.contains('yuno-fadeOut')) {
|
|
|
|
yunoverlay.classList.remove('yuno-active');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
apps.classList.remove('yuno-fadeIn', 'yuno-delay');
|
|
|
|
apps.classList.remove('yuno-fadeInLeft', 'yuno-delay');
|
|
|
|
user.classList.remove('yuno-slideintop');
|
|
|
|
}else {
|
|
|
|
yunoverlay.classList.remove('yuno-fadeOut');
|
|
|
|
yunoverlay.classList.add('yuno-active');
|
|
|
|
|
|
|
|
apps.classList.add('yuno-fadeInLeft', 'yuno-delay');
|
|
|
|
user.classList.add('yuno-slideintop');
|
|
|
|
}
|
2014-02-17 13:07:28 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
r.send();
|
|
|
|
|
|
|
|
});
|