js_header_functions.naml

<override_macro name="js_header_functions" requires="servlet">
    var t_login = "<t>Login</t>";
    var t_register = "<t>Register</t>";
    var t_logout = "<t>Log out</t>";
    var t_my_posts = "<t>My posts</t>";
    var t_account_settings = "<t>Account settings</t>";
    var t_show_nabble_notice = "<t>Show Nabble notice</t>";
    var t_permalink = "<t>Link</t>";
    var t_refresh = "<t>Refresh</t>";
    var login_path = "<n.login_path/>";
    var logout_path = "<n.logout_path/>";
    var register_path = "<n.register_path/>";
    var user_profile_path = "<n.user_profile_path/>";
    <![CDATA[
    Nabble.userId = Nabble.getCookie("userId");
    var encodedUsername = Nabble.getCookie("username");
    if (encodedUsername) {
        Nabble.username = decodeURIComponent(encodedUsername)
            .replace(new RegExp('\\+','g'),' ');
    }

    if (!Nabble.userId) {
        Nabble.anonymousId = Nabble.getCookie("anonymousId");
        var encodedName = Nabble.getCookie("anonymousName");
        if (encodedName) {
            Nabble.anonymousName = decodeURIComponent(encodedName)
                .replace(new RegExp('\\+','g'),' ')
                .replace(new RegExp('<','g'),'&lt;')
                .replace(new RegExp('>','g'),'&gt;');
        }
    }

    Nabble.embedPermalink = function(url) {
        if (!Nabble.isEmbedded)
            return url;
        return url.replace(/(;|&)cid=(\d|\-)+/g, '').replace(/\?cid=(\d|\-)+(&)?/g, '?');
    };

    Nabble.getPermalink = function() {
        return Nabble.embedPermalink(
            Nabble.overridePermalink? Nabble.overridePermalink : Nabble.getParent().nabbleiframe.location.href);
    };

    Nabble.permalinkLabel = function() {
        if (!Nabble.isEmbedded)
            return '';
        var p = "<script>function openPermalink() { prompt('Copy this:', Nabble.getPermalink()); };</script>";
        p += "<a href='javascript: void openPermalink();'>"+t_permalink+"</a>&nbsp;&nbsp;";
        return p;
    };

    Nabble.refreshLabel = function() {
        if (Nabble.isEmbedded)
            return "<a href='javascript: location.reload(true);'>"+t_refresh+"</a>&nbsp;&nbsp;";
        return '';
    };

    Nabble.siteHeader = function() {
        var s = Nabble.refreshLabel();
        s += Nabble.permalinkLabel();
        if (!Nabble.userId) {
            var nextUrl = typeof loginNextUrl == 'undefined'? window.location.href : loginNextUrl;
            s += '<a href="'+login_path+'&nextUrl='+encodeURIComponent(nextUrl)+'">'+t_login+'</a>';
            s += '&nbsp;&nbsp;';
            s += '<a href="'+register_path+'&nextUrl='+encodeURIComponent(nextUrl)+'">'+t_register+'</a>';
            if (Nabble.anonymousId && Nabble.anonymousName) {
                s += '&nbsp;&nbsp;';
                s += '<a href="/template/NamlServlet.jtp?macro=user_nodes&user=' + Nabble.anonymousId + '~' + Nabble.anonymousName + '">';
                s += Nabble.anonymousName;
                s += '</a>';
            }
        } else
            s += '<span id="user-dd"></span>';
        return s;
    };

    Nabble.userDropdown = function() {
        var $t = $('#user-dd');
        if ($t.size() == 0)
            return;
        $t.empty();
        var dropdown = new NabbleDropdown('usrdd', Nabble.escapeHTML(Nabble.username));
        dropdown.add('myPosts','<a href="/template/NamlServlet.jtp?macro=user_nodes&user='+Nabble.userId + '">'+t_my_posts+'</a>');
        dropdown.add('personalSettings','<a href="'+user_profile_path+'">'+t_account_settings+'</a>');
        dropdown.add('adminNotice','<a href="javascript: void Nabble.showAdminNotice()">'+t_show_nabble_notice+'</a>','display:none');
        dropdown.add('logout','<a href="javascript: void Nabble.logout()">'+t_logout+'</a>');
        $t.html(dropdown.getHtml());
        dropdownInit('usrdd');
    };

    Nabble.addUserHeaderListener(function() {
        Nabble.userDropdown();
    });

    Nabble.showAdminNotice = function() {
        Nabble.deleteCookie('admin-notice');
        window.location.reload();
    };

    Nabble.logout = function() {
        window.location = '/template/NamlServlet.jtp?macro=logout_page' + Nabble.getClientID();
    };
    ]]>
</override_macro>