Repo refactor and webproject hooks fixes
[astakos] / snf-astakos-app / astakos / im / static / im / cloudbar / cloudbar.js
1 $(document).ready(function(){
2     
3     /*
4     * LINKS CONFIGURATION
5     */
6
7     var PROFILE_URL = "https://accounts.cloud.grnet.gr";
8     var SERVICES_LINKS = window.CLOUDBAR_SERVICES_LINKS || {
9         'cloud':   { url:'/', name:'grnet cloud', id:'cloud', icon:'home-icon.png' },
10         'okeanos': { url:'/okeanos.html', name:'~okeanos', id:'okeanos' },
11         'pithos':  { url:'/ui/', name:'pithos+', id:'pithos' }
12     };
13     
14     var PROFILE_LINKS = window.CLOUDBAR_PROFILE_LINKS || {
15         'login': { url: '/im/login?next=' + window.location.toString(), auth:false, name: "login...", visible:false },
16         'profile': { url: '/im/profile', auth:true, name: "view your profile..." },
17         'password': { url: '/im/password', auth:true, name: "change your password..." },
18         'invitations': { url: '/im/invite', auth:true, name: "invite some friends..." },
19         'feedback': { url: '/im/feedback', auth:true, name: "feedback..." },
20         'logout': { url: '/im/logout', auth:true, name: "logout..." }
21     };
22
23
24     // cookie plugin https://raw.github.com/carhartl/jquery-cookie/master/jquery.cookie.js 
25     //  * Copyright (c) 2010 Klaus Hartl, @carhartl
26     //  * Dual licensed under the MIT and GPL licenses
27     var cookie=function(key,value,options){if(arguments.length>1&&(!/Object/.test(Object.prototype.toString.call(value))||value===null||value===undefined)){options=$.extend({},options);if(value===null||value===undefined){options.expires=-1}if(typeof options.expires==='number'){var days=options.expires,t=options.expires=new Date();t.setDate(t.getDate()+days)}value=String(value);return(document.cookie=[encodeURIComponent(key),'=',options.raw?value:encodeURIComponent(value),options.expires?'; expires='+options.expires.toUTCString():'',options.path?'; path='+options.path:'',options.domain?'; domain='+options.domain:'',options.secure?'; secure':''].join(''))}options=value||{};var decode=options.raw?function(s){return s}:decodeURIComponent;var pairs=document.cookie.split('; ');for(var i=0,pair;pair=pairs[i]&&pairs[i].split('=');i++){if(decode(pair[0])===key)return decode(pair[1]||'')}return null};
28
29     var ACTIVE_MENU = window.CLOUDBAR_ACTIVE_SERVICE || 'cloud';
30     var USER_DATA = window.CLOUDBAR_USER_DATA || {'user': 'Not logged in', 'logged_in': false};
31     var COOKIE_NAME = window.CLOUDBAR_COOKIE_NAME || '_pithos2_a';
32
33     var cssloc = window.CLOUDBAR_LOCATION || "http://127.0.0.1:8989/";
34     
35     // load css
36     var css = $("<link />");
37     css.attr({rel:'stylesheet', type:'text/css', href:cssloc + 'cloudbar.css'});
38     $("head").append(css);
39
40     // load service specific css
41     var SKIP_ADDITIONAL_CSS = window.SKIP_ADDITIONAL_CSS == undefined ? false : window.SKIP_ADDITIONAL_CSS;
42
43     if (!SKIP_ADDITIONAL_CSS) {
44         var css = $("<link />");
45         css.attr({rel:'stylesheet', type:'text/css', href:cssloc + 'service_' + ACTIVE_MENU + '.css'});
46         $("head").append(css);
47     }
48
49     var root = $('body');
50     var bar = $('<div class="servicesbar"></div>');
51     var services = $('<div class="services"></div>');
52     var profile = $('<div class="profile"></div>');
53     
54     
55     // create services links and set the active class to the current service
56     $.each(SERVICES_LINKS, function(i, el){
57         var slink = $("<a>");
58         if (el.icon) {
59             slink.append($('<img src="'+cssloc+el.icon+'"/>'));
60         } else {
61             slink.text(el.name);
62         }
63         slink.attr('href', el.url);
64         slink.attr('title', el.name);
65         services.append(slink);
66         if (el.id == ACTIVE_MENU) {
67             slink.addClass("active");
68         }
69     });
70
71     var USERNAME, LOGGED_IN;
72     var authcookie = cookie(COOKIE_NAME);
73     var anonymous = {'user': 'Login...', 'logged_in': false};
74
75     if (authcookie && authcookie.indexOf("|") > -1) {
76         USER_DATA.logged_in = true;
77         USER_DATA.user = authcookie.split("|")[0];
78     } else {
79         USER_DATA = anonymous;
80     }
81
82     USERNAME = USER_DATA.user;
83     LOGGED_IN = USER_DATA.logged_in;
84
85     // clear username
86     USERNAME = USERNAME.replace(/\\'/g,'');
87     USERNAME = USERNAME.replace(/\"/g,'');
88
89     var user = $('<div class="user"></div>');
90     var username = $('<a href="#"></a>');
91     username.text(USERNAME);
92     
93     // create profile links
94     var usermenu = $("<ul>");
95     $.each(PROFILE_LINKS, function(i,el) {
96         if (!LOGGED_IN && el.auth) { return }
97         if (LOGGED_IN && !el.auth) { return }
98         var li = $("<li />");
99         var link = $("<a />");
100         link.text(el.name);
101         link.attr({href:el.url});
102         li.append(link);
103         if (el.visible == false) {
104             li.hide();
105         }
106         usermenu.append(li);
107     });
108
109     //profile.filter(".user a").attr("href", 
110                                    //profile.find("li a").get(0).attr("href"))
111     
112
113     
114     user.append(username);
115     user.append(usermenu);
116     profile.append(user);
117     bar.append(services).append(profile);
118     
119
120     root.prepend(bar);
121     var firstlink = profile.find("ul li:first-child a").attr("href");
122     profile.find(".user > a").attr("href", firstlink);
123 });