summaryrefslogtreecommitdiffstats
path: root/src/main/resources/ui/sqlfiddle/www/javascript/fiddle_backbone/views/UserOptions.js
blob: 15a0ae2c77f371d4b935c1a9283abada71c89885 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
define ([
    "jquery",
    "Backbone",
    "Handlebars",
    "md5",
    "utils/openidconnect",
    "text!fiddle_backbone/templates/loginButton.html",
    "text!fiddle_backbone/templates/authenticatedUserOptions.html"
],
function ($,Backbone,Handlebars,md5,openidconnect,loginButtonTemplate,authenticatedUserOptionsTemplate) {

    var UserInfoView = Backbone.View.extend({
        initialize: function (options) {
            this.options = options;
            this.authCompiledTemplate = Handlebars.compile(authenticatedUserOptionsTemplate);
            this.loginButtonCompiledTemplate = Handlebars.compile(loginButtonTemplate);
        },
        events: {
            "click #logout": "logout",
            "click #myFiddles": "showMyFiddles"
        },
        renderAnonymous: function () {
            $(this.el).html(
                this.loginButtonCompiledTemplate()
            );

            this.options.oidc.fetch();

            return this;
        },
        renderAuthenticated: function (userDetails) {

            $(this.el).html(
                this.authCompiledTemplate({
                    gravatar: md5(decodeURIComponent(userDetails.email).toLowerCase()).toLowerCase(),
                    email: decodeURIComponent(userDetails.email)
                })
            );

            return this;
        },
        showMyFiddles: function (e) {
            e.preventDefault();
            this.options.myFiddleDialog.render(true);
        },
        logout: function (e) {
            e.preventDefault();
            this.options.myFiddleDialog.setAnonymous(true);
            openidconnect.removeTokens();
            this.renderAnonymous();
        }
    });

    return UserInfoView;

});