(function() { Sammy("#main", function(app) { var api, commonScope, mergeScope, noticeTimeout, withCommonScope; this.use(Sammy.Tmpl); this.use(Sammy.Session); this.use(Sammy.Title); this.setTitle("OAuth Admin - "); this.use(Sammy.OAuth2); this.authorize = document.location.pathname + "/authorize"; $(document).ajaxError(function(evt, xhr) { if (xhr.status === 401) { app.loseAccessToken(); } return app.trigger("notice", xhr.responseText); }); $(document).ajaxStart(function(evt) { return $("#throbber").show(); }); $(document).ajaxStop(function(evt) { return $("#throbber").hide(); }); this.requireOAuth(); this.bind("oauth.denied", function(evt, error) { return app.partial("admin/views/no_access.tmpl", { error: error.message }); }); this.bind("oauth.connected", function() { $("#header .signin").hide(); return $("#header .signout").show(); }); this.bind("oauth.disconnected", function() { $("#header .signin").show(); return $("#header .signout").hide(); }); api = "" + document.location.pathname + "/api"; mergeScope = function(scope) { if ($.isArray(scope)) { scope = scope.join(" "); } scope = (scope || "").trim().split(/\s+/); if (scope.length === 1 && scope[0] === "") { return []; } else { return _.uniq(scope).sort(); } }; commonScope = null; withCommonScope = function(cb) { if (commonScope) { return cb(commonScope); } else { return $.getJSON("" + api + "/clients", function(json) { return cb(commonScope = json.scope); }); } }; this.get("#/", function(context) { context.title("All Clients"); return $.getJSON("" + api + "/clients", function(clients) { commonScope = clients.scope; return context.partial("admin/views/clients.tmpl", { clients: clients.list, tokens: clients.tokens }).load(clients.history).then(function(json) { return $("#fig").chart(json.data, "granted"); }); }); }); this.get("#/client/:id", function(context) { return $.getJSON("" + api + "/client/" + context.params.id, function(client) { context.title(client.displayName); client.notes = (client.notes || "").split(/\n\n/); return context.partial("admin/views/client.tmpl", client).load(client.history).then(function(json) { return $("#fig").chart(json.data, "granted"); }); }); }); this.get("#/client/:id/page/:page", function(context) { return $.getJSON("" + api + "/client/" + context.params.id + "?page=" + context.params.page, function(client) { context.title(client.displayName); client.notes = client.notes.split(/\n\n/); return context.partial("admin/views/client.tmpl", client).load(client.history).then(function(json) { return $("#fig").chart(json.data, "granted"); }); }); }); this.post("#/token/:id/revoke", function(context) { return $.post("" + api + "/token/" + context.params.id + "/revoke", function() { return context.redirect("#/"); }); }); this.get("#/client/:id/edit", function(context) { return $.getJSON("" + api + "/client/" + context.params.id, function(client) { context.title(client.displayName); return withCommonScope(function(scope) { client.common = scope; return context.partial("admin/views/edit.tmpl", client); }); }); }); this.put("#/client/:id", function(context) { context.params.scope = mergeScope(context.params.scope); return $.ajax({ type: "put", url: "" + api + "/client/" + context.params.id, data: { displayName: context.params.displayName, link: context.params.link, imageUrl: context.params.imageUrl, redirectUri: context.params.redirectUri, notes: context.params.notes, scope: context.params.scope }, success: function(client) { context.redirect("#/client/" + context.params.id); return app.trigger("notice", "Saved your changes"); }, error: function(xhr) { return withCommonScope(function(scope) { context.params.common = scope; return context.partial("admin/views/edit.tmpl", context.params); }); } }); }); this.del("#/client/:id", function(context) { return $.ajax({ type: "post", url: "" + api + "/client/" + context.params.id, data: { _method: "delete" }, success: function() { return context.redirect("#/"); } }); }); this.post("#/client/:id/revoke", function(context) { return $.post("" + api + "/client/" + context.params.id + "/revoke", function() { return context.redirect("#/"); }); }); this.get("#/new", function(context) { context.title("Add New Client"); return withCommonScope(function(scope) { return context.partial("admin/views/edit.tmpl", { common: scope, scope: scope }); }); }); this.post("#/clients", function(context) { context.title("Add New Client"); context.params.scope = mergeScope(context.params.scope); return $.ajax({ type: "post", url: "" + api + "/clients", data: { displayName: context.params.displayName, link: context.params.link, imageUrl: context.params.imageUrl, redirectUri: context.params.redirectUri, notes: context.params.notes, scope: context.params.scope }, success: function(client) { app.trigger("notice", "Added new client application " + client.displayName); return context.redirect("#/"); }, error: function(xhr) { return withCommonScope(function(scope) { context.params.common = scope; return context.partial("admin/views/edit.tmpl", context.params); }); } }); }); this.get("#/signout", function(context) { context.loseAccessToken(); return context.redirect("#/"); }); $("a[data-method]").live("click", function(evt) { var form, link, method; evt.preventDefault; link = $(this); if (link.attr("data-confirm") && !confirm(link.attr("data-confirm"))) { return false; } method = link.attr("data-method") || "get"; form = $("