/** * @jsx React.DOM */ var namespace = NamespaceModel(); function errback(err) { console.log("error:", err); } var kind = ""; var lists = { "groups": new ResourceListModel("group"), "layers": new ResourceListModel("layer"), "variables": new VariableListModel(), "policies": new PolicyListModel(), "users": new UserListModel(), "hosts": new ResourceListModel("host") }; var conjurConfiguration; var userId; var components = {}; var router; var globalIds; var endpoints; var flash = null; function updateNamespace(ns) { namespace.currentNamespace = ns; components[kind].setState({currentNamespace: ns, members: lists[kind].members(ns)}); } function pluralize(kind) { if ( kind[kind.length-1] === 'y' ) return kind.slice(0, kind.length-1) + 'ies'; else if( kind[kind.length - 1] != 's') return kind + 's'; else return kind; } $(document).ready(function() { // http://www.quirksmode.org/js/cookies.html function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') c = c.substring(1, c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length); } return null; } conjurConfiguration = JSON.parse(decodeURIComponent(readCookie('conjur_configuration')).replace(/\+/g, ' ')); userId = decodeURIComponent(readCookie('conjur_userid')); function createEndpoints(){ endpoints = {}; function genericEndpoint(path){ return function(){ var i= 0, length = arguments.length; var localPath = [].concat(path); // clone it for(; i < length && _.isString(arguments[i]); i++){ localPath.push(arguments[i]); } var pathString = _.flatten(localPath.map(function(p){ return p.split('/').map(encodeURIComponent) })).join("/"); if(i < length){ pathString += '?' + jQuery.param(arguments[i]); } if(pathString.charAt(0) != '/') pathString = '/' + pathString; return pathString; } } endpoints.authz = genericEndpoint(['api/authz', conjurConfiguration.account]); endpoints.core = genericEndpoint('api'); } createEndpoints(); _.mixin(_.str.exports()); // Use delegation to avoid initial DOM selection and allow all matching elements to bubble $(document).delegate("a", "click", function(evt) { // Get the anchor href and protcol var href = $(this).attr("href"); var protocol = this.protocol + "//"; // Ensure the protocol is not part of URL, meaning its relative. // Stop the event bubbling to ensure the link will not cause a page refresh. if (href.slice(protocol.length) !== protocol && href[0] !== '#') { evt.preventDefault(); // Note by using Backbone.history.navigate, router events will not be // triggered. If this is a problem, change this to navigate on your // router. Backbone.history.navigate(href, {trigger:true}); } }); function error(err) { console.log("error:", err); } function activateList(componentFunction) { /* console.log("List", kind); */ setActiveNav(kind); namespace.currentNamespace = ""; lists[kind].fetch(function(list) { var component = componentFunction(list); components[kind] = component; React.renderComponent( component, document.getElementById('content') ); component.setState({currentNamespace: namespace.currentNamespace, members: list.members(namespace.currentNamespace)}); }); } function activateRecord(k, id, componentFunction) { /* console.log("Record", k, " :", id); */ $('#inlineSearchContainer').show(); kind = pluralize(k); setActiveNav(kind); var model; if ( Record[_.capitalize(k)] ) model = new Record[_.capitalize(k)](id); else model = new Record.Generic(kind, id); model.fetch(function(record) { var component = componentFunction(record); React.renderComponent( component, document.getElementById('content') ); }, function(status, text, xhr){ if(status == 403){ window.flash = "You are not authorized to view " + kind + ":" + id + "."; window.history.back() }else{ console.error("HTTP error: ", status, text); } }); } function setActiveNav(name){ $('.nav-item').removeClass('active'); $('#nav-' + name).addClass('active'); } var Workspace = Backbone.Router.extend({ routes: { "": "dashboard", "ui": "dashboard", "ui/users": "users", "ui/users/:user": "user", "ui/groups": "groups", "ui/groups/:group": "group", "ui/hosts": "hosts", "ui/hosts/:host": "host", "ui/layers": "layers", "ui/layers/:layer": "layer", "ui/variables": "variables", "ui/variables/:variable": "variable", "ui/policies": "policies", "ui/policies/:policy": "policy", "ui/audit": "audit", "ui/search/:search": "search" }, dashboard: function() { $('#inlineSearchContainer').hide(); React.renderComponent( , document.getElementById('content') ); }, user: function(user){ activateRecord("user", user, function(record){ return ; }); }, users: function() { $('#inlineSearchContainer').show(); kind = "users"; activateList(function(list) { return ; }); }, group: function(group) { activateRecord("group", group, function(record) { return ; }); }, groups: function() { $('#inlineSearchContainer').show(); kind = "groups"; activateList(function(list) { return ; }); }, host: function(host){ activateRecord("host", host, function(record) { return ; }); }, hosts: function() { $('#inlineSearchContainer').show(); kind = "hosts"; activateList(function(list) { return ; }); }, layer: function(layer) { activateRecord("layer", layer, function(record) { return ; }); }, layers: function() { $('#inlineSearchContainer').show(); kind = "layers"; activateList(function(list) { return }); }, variable: function(variable) { activateRecord("variable", variable, function(record) { return ; }); }, variables: function() { $('#inlineSearchContainer').show(); kind = "variables"; activateList(function(list) { return }); }, policies: function() { kind = "policies"; activateList(function(list) { return }); }, policy: function(policy) { kind = "policies"; setActiveNav(kind); $.ajax({ url: "/api/authz/" + conjurConfiguration.account + "/resources/policy/" + policy, success: function(result) { React.renderComponent( , document.getElementById('content') ); }, error: error }); }, audit: function(){ $('#inlineSearchContainer').show(); setActiveNav('audit') React.renderComponent( , document.getElementById('content') ); }, search: function(search){ $('#inlineSearchContainer').show(); SearchResults.search(search); } }); router = new Workspace(); router.on('all', function(route, router){ console.log("router.all", route, flash); if(route != 'route') return; if(flash){ var old = flash; flash = null; var $flash = $('#flash'); var $text = $('.text', $flash); $text.text(old); $flash.show(); }else{ $('#flash').hide(); } }); Backbone.history.start({pushState: true}); React.renderComponent( , document.getElementById("inlineSearchContainer") ); });