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;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-11-19 10:13:02 +01:00
|
|
|
/* Draggable
|
|
|
|
|
|
|
|
Sources :
|
|
|
|
http://jsfiddle.net/5t3Ju/
|
|
|
|
http://stackoverflow.com/questions/9334084/moveable-draggable-div
|
|
|
|
http://jsfiddle.net/tovic/Xcb8d/light/
|
|
|
|
-------------------------- */
|
|
|
|
|
|
|
|
var dragg = function(id) {
|
|
|
|
|
|
|
|
// Variables
|
|
|
|
this.elem = document.getElementById(id),
|
|
|
|
this.selected = null, // Selected element
|
|
|
|
this.dragged = false, // Dragging status
|
|
|
|
this.x_pos = 0, this.y_pos = 0, // Stores x & y coordinates of the mouse pointer
|
|
|
|
this.x_elem = 0, this.y_elem = 0; // Stores top, left values (edge) of the element
|
|
|
|
|
2018-05-09 18:34:00 +02:00
|
|
|
var _initDrag = function(e){
|
|
|
|
if (e.type === "touchstart"){
|
|
|
|
x_pos = e.touches[0].clientX;
|
|
|
|
y_pos = e.touches[0].clientY;
|
|
|
|
}
|
|
|
|
|
2014-11-19 10:13:02 +01:00
|
|
|
selected = elem;
|
|
|
|
x_elem = x_pos - selected.offsetLeft;
|
|
|
|
y_elem = y_pos - selected.offsetTop;
|
2018-05-09 18:34:00 +02:00
|
|
|
};
|
2014-11-19 10:13:02 +01:00
|
|
|
|
2018-05-09 18:34:00 +02:00
|
|
|
var _shutDrag = function(e){
|
|
|
|
selected = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
var _onMove = function(e){
|
2014-11-19 10:13:02 +01:00
|
|
|
// Get position
|
2018-05-09 18:34:00 +02:00
|
|
|
x_pos = document.all ? window.event: e.pageX;
|
|
|
|
y_pos = document.all ? window.event : e.pageY;
|
|
|
|
|
|
|
|
if (e.type === "touchmove"){
|
|
|
|
x_pos = e.touches[0].clientX;
|
|
|
|
y_pos = e.touches[0].clientY;
|
|
|
|
}
|
2014-11-19 10:13:02 +01:00
|
|
|
|
|
|
|
if (selected !== null) {
|
|
|
|
dragged = true;
|
|
|
|
selected.style.left = (x_pos - x_elem) + 'px';
|
|
|
|
selected.style.top = (y_pos - y_elem) + 'px';
|
|
|
|
}
|
2018-05-09 18:34:00 +02:00
|
|
|
};
|
|
|
|
|
2018-09-01 10:35:55 +02:00
|
|
|
// Prevent native D'n'D behavior
|
|
|
|
window.addEvent(elem, 'dragstart', function(e){
|
|
|
|
window.eventPreventDefault(e);
|
|
|
|
});
|
|
|
|
|
2018-05-09 18:34:00 +02:00
|
|
|
// Start dragging
|
|
|
|
window.addEvent(elem, 'mousedown', _initDrag);
|
|
|
|
window.addEvent(elem, 'touchstart', _initDrag);
|
|
|
|
|
|
|
|
// Will be called when user dragging an element
|
|
|
|
window.addEvent(window, 'mousemove', _onMove);
|
|
|
|
window.addEvent(window, 'touchmove', _onMove);
|
2014-11-19 10:13:02 +01:00
|
|
|
|
|
|
|
// Destroy the object when we are done
|
2018-05-09 18:34:00 +02:00
|
|
|
window.addEvent(window, 'mouseup', _shutDrag);
|
|
|
|
window.addEvent(window, 'touchend', _shutDrag);
|
|
|
|
window.addEvent(window, 'touchcancel', _shutDrag);
|
2014-11-19 10:13:02 +01:00
|
|
|
|
|
|
|
// Handle click event
|
|
|
|
window.addEvent(elem, 'click', function(e){
|
|
|
|
// Prevent default event
|
|
|
|
window.eventPreventDefault(e);
|
|
|
|
|
2018-09-01 10:35:55 +02:00
|
|
|
// Do not propagate to other click event if dragged out
|
2014-11-19 10:13:02 +01:00
|
|
|
if (dragged) {
|
|
|
|
e.stopImmediatePropagation();
|
|
|
|
}
|
|
|
|
// Reset dragging status
|
|
|
|
dragged = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-17 13:07:28 +01:00
|
|
|
/* 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-05-20 13:15:12 +02:00
|
|
|
// Set and store meta viewport
|
|
|
|
var meta_viewport = document.querySelector('meta[name="viewport"]');
|
|
|
|
if (meta_viewport === null) {
|
|
|
|
meta_viewport = document.createElement('meta');
|
|
|
|
meta_viewport.setAttribute('name', "viewport");
|
|
|
|
meta_viewport.setAttribute('content', "");
|
|
|
|
document.getElementsByTagName('head')[0].insertBefore(meta_viewport, null);
|
|
|
|
}
|
|
|
|
meta_viewport = document.querySelector('meta[name="viewport"]');
|
|
|
|
meta_viewport_content = meta_viewport.getAttribute('content');
|
|
|
|
|
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');
|
2015-10-07 09:50:55 +02:00
|
|
|
portal.setAttribute('id', 'ynh-overlay-switch');
|
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-11-19 10:13:02 +01:00
|
|
|
// Portal link is draggable, for user convenience
|
2015-08-31 17:24:26 +02:00
|
|
|
dragg('ynh-overlay-switch');
|
2014-11-19 10:13:02 +01:00
|
|
|
|
|
|
|
|
2014-05-14 01:42:31 +02:00
|
|
|
// Create overlay element
|
|
|
|
var overlay = document.createElement("div");
|
2015-08-31 17:22:05 +02:00
|
|
|
overlay.setAttribute("id","ynh-overlay");
|
2014-05-14 01:42:31 +02:00
|
|
|
overlay.setAttribute("style","display:none");
|
|
|
|
|
|
|
|
document.body.insertBefore(overlay, null);
|
|
|
|
|
|
|
|
//Color Application
|
2015-06-14 12:53:16 +02:00
|
|
|
var colors = ['redbg','purpledarkbg','darkbluebg','orangebg','greenbg','darkbluebg','purpledarkbg','yellowbg','lightpinkbg','pinkbg','turquoisebg','yellowbg','lightbluebg','purpledarkbg', 'bluebg'];
|
2014-05-14 01:42:31 +02:00
|
|
|
|
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
|
|
|
|
response = JSON.parse(r.responseText);
|
|
|
|
|
|
|
|
// Add overlay header
|
2015-08-31 17:20:36 +02:00
|
|
|
overlay.innerHTML += '<div id="ynh-user" class="ynh-wrapper info">' +
|
2014-05-20 11:58:46 +02:00
|
|
|
'<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>' +
|
2015-09-03 17:31:54 +02:00
|
|
|
'</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){
|
2015-06-14 12:53:16 +02:00
|
|
|
randomColorNumber = parseInt(app.name, 36) % colors.length;
|
|
|
|
links.push('<li><a class="'+colors[randomColorNumber]+' disableAjax" href="//'+app.url+'"><span class="first-letter" data-first-letter="'+ app.name.substr(0,2) +'"></span><span class="name">'+app.name+'</span></a></li>');
|
2014-02-17 13:07:28 +01:00
|
|
|
});
|
2015-08-31 17:20:36 +02:00
|
|
|
overlay.innerHTML += '<div id="ynh-apps" class="ynh-wrapper apps"><ul class="listing-apps">'+ links.join("\n") +'</ul></div>';
|
2014-06-01 14:23:29 +02:00
|
|
|
|
|
|
|
// Add footer links
|
2015-08-31 17:20:36 +02:00
|
|
|
overlay.innerHTML += '<div id="ynh-footer" class="ynh-wrapper footer"><nav>' + "\n" +
|
2014-06-01 14:23:29 +02:00
|
|
|
'<a class="link-profile-edit" href="/yunohost/sso/edit.html">'+ response.t_footerlink_edit +'</a>' + "\n" +
|
|
|
|
'<a class="link-documentation" href="//yunohost.org/docs" target="_blank">'+ response.t_footerlink_documentation +'</a>' + "\n" +
|
|
|
|
'<a class="link-documentation" href="//yunohost.org/support" target="_blank">'+ response.t_footerlink_support +'</a>' + "\n" +
|
2014-11-13 20:11:13 +01:00
|
|
|
'<a class="link-admin" href="/yunohost/admin/" target="_blank">'+ response.t_footerlink_administration +'</a>' + "\n" +
|
2014-06-01 14:23:29 +02:00
|
|
|
'</nav></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'),
|
2015-08-31 17:22:05 +02:00
|
|
|
yunoverlay = document.getElementById('ynh-overlay'),
|
2015-08-31 17:14:18 +02:00
|
|
|
user = document.getElementById('ynh-user'),
|
|
|
|
apps = document.getElementById('ynh-apps');
|
2014-07-21 22:49:31 +02:00
|
|
|
|
2014-05-14 01:42:31 +02:00
|
|
|
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-20 13:15:12 +02:00
|
|
|
Element.toggleClass(document.querySelector('html'), 'ynh-panel-active');
|
|
|
|
|
2014-05-14 01:42:31 +02:00
|
|
|
|
2015-08-31 17:14:18 +02:00
|
|
|
if(yunoverlay.classList.contains('ynh-active')) {
|
2014-05-20 13:15:12 +02:00
|
|
|
meta_viewport.setAttribute('content', meta_viewport_content);
|
2015-08-31 17:14:18 +02:00
|
|
|
yunoverlay.classList.add('ynh-fadeOut');
|
2014-05-14 01:42:31 +02:00
|
|
|
PrefixedEvent(yunoverlay, "AnimationEnd", function(){
|
2015-08-31 17:14:18 +02:00
|
|
|
if(yunoverlay.classList.contains('ynh-fadeOut')) {
|
|
|
|
yunoverlay.classList.remove('ynh-active');
|
2014-05-14 01:42:31 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}else {
|
2014-05-20 13:15:12 +02:00
|
|
|
meta_viewport.setAttribute('content', "width=device-width");
|
2015-08-31 17:14:18 +02:00
|
|
|
yunoverlay.classList.remove('ynh-fadeOut');
|
|
|
|
yunoverlay.classList.add('ynh-active');
|
2014-05-14 01:42:31 +02:00
|
|
|
}
|
2014-02-17 13:07:28 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
r.send();
|
|
|
|
|
|
|
|
});
|