Project

General

Profile

1
/*
2
 * Copyright 2012 SURFnet bv, The Netherlands
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

    
17

    
18
/**
19
 * A caching abstraction for Handlebars templates.
20
 *
21
 * Use as:
22
 *
23
 * var model = {"id":"123"};
24
 * Template.get(templateId, function(template) {
25
* $(containerSelector).append(template(model));
26
*
27
* });
28
 *
29
 */
30
var Template = (function() {
31
    var tplCache = [];
32

    
33
    return {
34
        /**
35
         * We support both inline templates as external templates
36
         */
37
        get: function(templateName, callback) {
38
            if (!tplCache[templateName]) {
39
                var template = $("#" + templateName);
40
                if (template.size() == 0) {
41
                    $.get("templates/" + templateName + ".html", function(data) {
42
                        tplCache[templateName] = Handlebars.compile(data);
43
                        callback(tplCache[templateName]);
44
                    });
45
                } else {
46
                    tplCache[templateName] = Handlebars.compile(template.html());
47
                    callback(tplCache[templateName]);
48
                }
49
            } else {
50
                callback(tplCache[templateName]);
51
            }
52
        }
53
    }
54
})();
55
var landingView = (function() {
56
    var templateId = "tplLanding";
57
    var handleSelector = "#landing";
58
    var containerSelector = "#contentView";
59

    
60
    return {
61
        hide: function() {
62
            $(handleSelector).remove();
63
        },
64
        show: function() {
65
            Template.get(templateId, function(template) {
66
                $(containerSelector).append(template());
67
                $("a#loginbutton").click(function(){
68
                    windowController.login();
69
                    return false;
70
                });
71

    
72
            });
73
        }
74
    }
75
})();
76
var windowController = {
77
    oauth: new OAuth({
78
        context:window,
79
        redirectUri: window.location, // Current location as redirect URI: after authorization we get back control.
80
        authorizationEndpoint:"../oauth2/authorize",
81
//        clientId:"authorization-server-admin-js-client",
82
        clientId:"user1",
83
        scope:"read,write"
84
    }),
85
    login: function() {
86
// Start the authorization. Effectively we lose control, the browser will change location and come back later.
87
        this.oauth.authorize();
88
    },
89
    onLanding: function() {
90
        landingView.show();
91
    },
92
    onLoggedIn: function() {
93
// Refresh the data grid.
94
        this.refresh();
95
        $('.user-info').html(this.oauth.principalName());
96
// On click of nav link, remove 'current' from all, add to actual current.
97
        $("div.side-nav a").click(function() {
98
            $("div.side-nav a").removeClass("cur");
99
            $(this).addClass("cur");
100
        });
101
        $("#nav-resource-servers").click(function() {
102
            windowController.refresh();
103
        });
104
        $("#nav-clients-apps").click(function() {
105
            /*
106
             * TODO scroll/animate to the start of the client section, but to be done after ajax calls
107
             */
108
            windowController.refresh();
109
        });
110
        $("#nav-access-tokens").click(function() {
111
            windowController.clearContentView();
112
            accessTokenGridController.show();
113
        });
114
        $("#nav-statistics").click(function() {
115
            windowController.clearContentView();
116
            statisticsGridController.show();
117
        });
118
    },
119
    clearContentView: function() {
120
        resourceServerFormController.hide();
121
        resourceServerGridController.hide();
122
        clientFormController.hide();
123
        clientGridController.hide();
124
        accessTokenGridController.hide();
125
        statisticsGridController.hide();
126
    },
127
    refresh: function() {
128
        this.clearContentView();
129
        data.getResourceServers(function(resourceServers) {
130
            resourceServerGridController.show(resourceServers);
131
            clientGridController.show(resourceServers);
132
        });
133
    },
134
    /**
135
     * Resource server events.
136
     */
137
    onCloseEditResourceServer: function() {
138
        this.refresh();
139
    },
140
    onEditResourceServer: function(id) {
141
        resourceServerGridController.hide();
142
        clientGridController.hide();
143
        resourceServerFormController.show("edit", id);
144
    },
145
    onAddResourceServer: function() {
146
        resourceServerGridController.hide();
147
        clientGridController.hide();
148
        resourceServerFormController.show("add");
149
    },
150
    onDeleteResourceServer: function() {
151
        this.refresh();
152
    },
153
    onDeleteAccessToken: function() {
154
        this.clearContentView();
155
        accessTokenGridController.show();
156
    },
157
    /**
158
     * Clients events.
159
     */
160
    onEditClient: function(resourceServerId, clientId) {
161
        resourceServerGridController.hide();
162
        clientGridController.hide();
163
        clientFormController.show("edit", resourceServerId, clientId);
164
    },
165
    onAddClient: function() {
166
        resourceServerGridController.hide();
167
        clientGridController.hide();
168
        clientFormController.show("add");
169
    },
170
    onCloseEditClient: function() {
171
        this.refresh();
172
    },
173
    onDeleteClient: function() {
174
        this.refresh();
175
    },
176
    onPageLoad: function() {
177
        if (this.oauth.isTokenPresent()) { // This will be true upon return from authentication step-out.
178
// The URL-hash will contain the access token
179
            data.setAccessToken(this.oauth.extractTokenInfo());
180
// Effectively we're logged in. We can do API calls now.
181
            this.onLoggedIn();
182
        } else {
183
            this.onLanding();
184
        }
185
    }
186
};
187
// On DOM ready
188
$(function() {
189
// Attach global listeners
190
    $(".alert").alert();
191

    
192
    $('body').tooltip({
193
        selector: '[rel=tooltip]'
194
    });
195

    
196
    $('body').popover({
197
        selector: '[rel=popover]',
198
//See popoverBundle.js
199
        title: function() {
200
            return popoverBundle.getTitle(this.attributes['name'].nodeValue);
201
        },
202
        content: function() {
203
            return popoverBundle.getContent(this.attributes['name'].nodeValue);
204
        }
205
    });
206
// Initialization of window controller.
207
    windowController.onPageLoad();
208
});
(2-2/12)