usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From snoopd...@apache.org
Subject [27/41] updating portal with latest code base
Date Mon, 24 Feb 2014 07:21:22 GMT
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/a5a98884/portal/archive/js/app/console.js
----------------------------------------------------------------------
diff --git a/portal/archive/js/app/console.js b/portal/archive/js/app/console.js
new file mode 100644
index 0000000..83974f1
--- /dev/null
+++ b/portal/archive/js/app/console.js
@@ -0,0 +1,5393 @@
+function apigee_console_app(Pages, query_params) {
+  //This code block *WILL NOT* load before the document is complete
+  window.Usergrid = window.Usergrid || {};
+  Usergrid.console = Usergrid.console || {};
+
+  // for running Apigee App Services as a local server
+  var LOCAL_STANDALONE_API_URL = "http://localhost/usergrid";
+  var LOCAL_TOMCAT_API_URL = "http://localhost:8080/ROOT";
+  var LOCAL_API_URL = LOCAL_STANDALONE_API_URL;
+  var PUBLIC_API_URL = "https://api.usergrid.com/";
+  var FORCE_PUBLIC_API = true; // Always use public API
+  if (!FORCE_PUBLIC_API && (document.domain.substring(0,9) == "localhost")) {
+    Usergrid.ApiClient.setApiUrl(LOCAL_API_URL);
+  }
+
+  String.prototype.endsWith = function (s) {
+    return (this.length >= s.length && this.substr(this.length - s.length) == s);
+  };
+
+  if (query_params.api_url) {
+    if (!query_params.api_url.endsWith('/')) {
+      query_params.api_url += '/';
+    }
+    Usergrid.ApiClient.setApiUrl(query_params.api_url);
+  } else {
+    if(window.location.host === 'localhost'){
+      //local = DIT
+      Usergrid.ApiClient.setApiUrl('http://apigee-internal-prod.jupiter.apigee.net/');
+    }if(window.location.host === 'appservices.apigee.com' && location.pathname.indexOf('/dit') >= 0){
+      //DIT
+      Usergrid.ApiClient.setApiUrl('http://apigee-internal-prod.jupiter.apigee.net/');
+    }else if(window.location.host === 'appservices.apigee.com' && location.pathname.indexOf('/mars') >= 0 ){
+      //staging
+      Usergrid.ApiClient.setApiUrl('http://apigee-internal-prod.mars.apigee.net/');
+    }
+  }
+  //Display message page in case there is a a timeout to the API
+  Usergrid.ApiClient.setCallTimeoutCallback(function(){
+    showMessagePage();
+  });
+
+  var HIDE_CONSOLE = query_params.hide_console || "";
+
+  if (HIDE_CONSOLE.indexOf("true") >= 0) {
+    $('#sidebar-menu ul li a[href="#console"]').hide();
+  }
+
+  var OFFLINE = false;
+  var OFFLINE_PAGE = "#query-page";
+
+  var self = this;
+
+  var emailRegex = new RegExp("^(([0-9a-zA-Z]+[_\+.-]?)+@[0-9a-zA-Z]+[0-9,a-z,A-Z,.,-]*(.){1}[a-zA-Z]{2,4})+$");
+  var emailAllowedCharsMessage = 'eg. example@apigee.com';
+
+  var passwordRegex = new RegExp("^([0-9a-zA-Z@#$%^&!?<>;:.,'\"~*=+_\[\\](){}/\\ |-])+$");
+  var passwordAllowedCharsMessage = 'This field only allows: A-Z, a-z, 0-9, ~ @ # % ^ & * ( ) - _ = + [ ] { } \\ | ; : \' " , . < > / ? !';
+  var passwordMismatchMessage = 'Password must match';
+
+  var usernameRegex = new RegExp("^([0-9a-zA-Z\.\_-])+$");
+  var usernameAllowedCharsMessage = 'This field only allows : A-Z, a-z, 0-9, dot, underscore and dash';
+
+  var organizationNameRegex = new RegExp ("^([0-9a-zA-Z.-])+$");
+  var organizationNameAllowedCharsMessage = 'This field only allows : A-Z, a-z, 0-9, dot, and dash';
+
+  //Regex declared differently from al the others because of the use of ". Functions exacly as if it was called from new RegExp
+  var nameRegex = /[0-9a-zA-ZáéíóúÁÉÍÓÚÑñ@#$%\^&!\?;:\.,'\"~\*-=\+_\(\)\[\]\{\}\|\/\\]+/;
+  var nameAllowedCharsMessage = "This field only allows: A-Z, a-z, áéíóúÁÉÍÓÚÑñ, 0-9, ~ @ # % ^ & * ( ) - _ = + [ ] { } \\ | ; : \' \" , . / ? !";
+
+  var titleRegex = new RegExp("[a-zA-Z0-9.!-?]+[\/]?");
+  var titleAllowedCharsMessage = 'Title field only allows : space, A-Z, a-z, 0-9, dot, dash, /, !, and ?';
+
+  var alphaNumRegex = new RegExp("[0-9a-zA-Z]+");
+  var alphaNumAllowedCharsMessage = 'Collection name only allows : a-z A-Z 0-9';
+
+  var pathRegex = new RegExp("^[^\/]*([a-zA-Z0-9\.-]+[\/]{0,1})+[^\/]$");
+  var pathAllowedCharsMessage = 'Path only allows : /, a-z, 0-9, dot, and dash, paths of the format: /path, path/, or path//path are not allowed';
+
+  var roleRegex = new RegExp("^([0-9a-zA-Z./-])+$");
+  var roleAllowedCharsMessage = 'Role only allows : /, a-z, 0-9, dot, and dash';
+
+  var intRegex = new RegExp("^([0-9])+$");
+
+  var applications = {};
+  var applications_by_id = {};
+
+  var current_application_id = "";
+  var current_application_name = {};
+  var applicationData = {};
+
+  var query_entities = null;
+  var query_entities_by_id = null;
+
+  var query_history = [];
+
+  var indexes = [];
+  var backgroundGraphColor = '#ffffff';
+
+  String.prototype.startsWith = function(s) {
+    return this.lastIndexOf(s, 0) === 0;
+  };
+
+  String.prototype.endsWith = function(s) {
+    return this.length >= s.length
+      && this.substr(this.length - s.length) == s;
+  };
+
+  $("#fixme").click(function(e){
+    e.preventDefault();
+    $('#application-panel-buttons').css('position', 'relative');
+  });
+
+  function clearBackgroundImage(){
+    $('body').css({'background-image' : 'none'});
+  }
+  Usergrid.console.clearBackgroundImage = clearBackgroundImage;
+  function setColumnBackgroundImage(){
+//    $('body').css({background : 'url(images/background_one_col.png)  repeat-y'});
+  }
+  Usergrid.console.setColumnBackgroundImage = setColumnBackgroundImage;
+
+  function initOrganizationVars() {
+    applications = {};
+    applications_by_id = {};
+
+    current_application_id = "";
+    current_application_name = "";
+
+    query_entities = null;
+    query_entities_by_id = null;
+
+    query_history = [];
+
+    indexes = [];
+  }
+
+  function keys(o) {
+    var a = [];
+    for (var propertyName in o) {
+      a.push(propertyName);
+    }
+    return a;
+  }
+
+  $('#api-activity').ajaxStart( function() {
+    $(this).show();
+  });
+
+  $('#api-activity').ajaxComplete( function() {
+    $(this).hide();
+  });
+
+  function showPanel(page) {
+    var p = $(page);
+    $("#console-panels").children().each(function() {
+      if ($(this).attr("id") == p.attr("id")) {
+        $(this).show();
+      } else {
+        $(this).hide();
+      }
+    });
+  }
+
+  function initConsoleFrame(){
+    $("#console-panel iframe").attr("src", url);
+  }
+
+  function getAccessTokenURL(){
+    var bearerToken = Usergrid.ApiClient.getToken();
+    var app_name = Usergrid.ApiClient.getApplicationName();
+    if (typeof app_name != 'string') {
+      app_name = '';
+    }
+    var org_name = Usergrid.ApiClient.getOrganizationName();
+    if (typeof org_name != 'string') {
+      org_name = '';
+    }
+    var bearerTokenJson = JSON.stringify(
+      [{
+        "type":"custom_token",
+        "name":"Authorization",
+        "value":"Bearer " + bearerToken,
+        "style":"header"
+      }, {
+        "type":"custom_token",
+        "name":"app_name",
+        "value":app_name,
+        "style":"template"
+      }, {
+        "type":"custom_token",
+        "name":"org_name",
+        "value":org_name,
+        "style":"template"
+      }]
+    );
+    var bearerTokenString = encodeURIComponent(bearerTokenJson);
+    var url = 'https://apigee.com/apigeedev/console/usergrid?v=2&embedded=true&auth=' + bearerTokenString;
+    return url;
+  }
+  Usergrid.console.getAccessTokenURL = getAccessTokenURL;
+
+  function showPanelContent(panelDiv, contentDiv) {
+    var cdiv = $(contentDiv);
+    $(panelDiv).children(".panel-content").each(function() {
+      var el = $(this);
+      if (el.attr("id") == cdiv.attr("id")) {
+        el.show();
+      } else {
+        el.hide();
+      }
+    });
+  }
+
+  function selectTabButton(link) {
+    var tab = $(link).parent();
+    tab.parent().find("li.active").removeClass('active');
+    tab.addClass('active');
+  }
+
+  function selectPillButton(link) {
+    var tab = $(link);
+    tab.parent().find("a.active").removeClass('active');
+    tab.addClass('active');
+  }
+  function selectFirstTabButton(bar){
+    selectTabButton($(bar).find("li:first-child a"));
+  }
+
+  function setNavApplicationText() {
+    var name = Usergrid.ApiClient.getApplicationName();
+    if(!name) {
+      name = "Select an Application";
+    }
+    $('#current-app-name').html('<div class="app-menu">' + name + '</div>  <span class="caret"></span>');
+    //  $('.thingy span.title span.app_title').text(" " + name);
+    $('#nav-app-name').html(name);
+  }
+
+  function escapeMe(obj) {
+    for (var property in obj) {
+      if (obj.hasOwnProperty(property)){
+        if (obj[property] && obj[property].constructor == Object || obj[property] instanceof Array) {
+          var prop = encodeURIComponent(property);
+          var value = obj[property];
+          delete obj[property];
+          obj[prop] = value;
+          escapeMe(obj[prop]);
+        }
+        else {
+          if (property === 'picture') continue;
+          var prop = escapeString(property);
+          var value = escapeString(obj[property]);
+          delete obj[property];
+          obj[prop] = value;
+          if (property === 'created' || property === 'modified') {
+            try {
+              obj[property] = parseInt(obj[property]);
+            }catch(e){}
+          }
+        }
+      }
+    }
+    return obj;
+  }
+
+  function escapeString(str) {
+    return String(str)
+      .replace(/&/g, '&amp;')
+      .replace(/"/g, '&quot;')
+      .replace(/'/g, '&#39;')
+      .replace(/</g, '&lt;')
+      .replace(/>/g, '&gt;');
+  }
+
+  /*******************************************************************
+   *
+   * Collections
+   *
+   ******************************************************************/
+
+  function pageSelectCollections() {
+    hideModal(' #collections-messages')
+    getCollections();
+  }
+  window.Usergrid.console.pageSelectCollections = pageSelectCollections;
+
+  function getCollections() {
+    //clear out the table before we start
+    var output = $('#collections-table');
+    output.empty();
+    hideCurlCommand('collections');
+    var section =$('#application-collections');
+    section.empty().html('<div class="alert alert-info">Loading...</div>');
+
+    var queryObj = new Usergrid.Query("GET",'', null, null, getCollectionsCallback,
+      function() { alertModal("Error", "There was an error getting the collections"); }
+    );
+
+    runAppQuery(queryObj);
+    return false;
+  }
+
+  function compare(a,b) {
+    if (a.name < b.name)
+      return -1;
+    if (a.name > b.name)
+      return 1;
+    return 0;
+  }
+
+  function getCollectionsCallback(response) {
+    //response = escapeMe(response);
+
+    $('#collections-pagination').hide();
+    $('#collections-next').hide();
+    $('#collections-previous').hide();
+    showEntitySelectButton();
+    if (response.entities && response.entities[0] && response.entities[0].metadata && response.entities[0].metadata.collections) {
+      applicationData.Collections = response.entities[0].metadata.collections;
+      updateApplicationDashboard();
+      updateQueryAutocompleteCollections();
+    }
+
+    var data = response.entities[0].metadata.collections;
+    var output = $('#collections-table');
+
+    var elements = [];
+    for (var key in data) {
+      if (data.hasOwnProperty(key)) {
+        elements.push(data[key])
+      }
+    }
+    var currentPath = $("#query-path").val();
+    elements.sort(compare)
+    var r = {};
+    if ($.isEmptyObject(data)) {
+      output.replaceWith('<div id="collections-table" class="collection-panel-section-message">No collections found.</div>');
+    } else {
+      output.replaceWith('<table id="collections-table" class="table"><tbody></tbody></table>');
+      var leftMenuContent = '<ul id="collections-link-buttons" class="nav nav-list" style="margin-bottom: 5px;">';
+      for (var i=0;i<elements.length;i++) {
+        r.name = elements[i];
+        r.count = data[elements[i].count];
+        var name = escapeString(elements[i].name);
+        $.tmpl('apigee.ui.collections.table_rows.html', r).appendTo('#collections-table');
+        var active = (currentPath == "/"+name)?'class="active"':'';
+        var link = "Usergrid.console.pageOpenQueryExplorer('"+name+"'); return false;";
+        leftMenuContent += '<li id="collections-link-button-'+name+'" '+active+'><a href="#" onclick="'+link+'" class="collection-nav-links"><span class="nav-menu-text">/'+name+'</span></a></li>';
+      }
+
+      leftMenuContent += '</ul>';
+      $('#left-collections-content').html(leftMenuContent);
+    }
+    showCurlCommand('collections', this.getCurl(), this.getToken());
+  }
+
+  /*******************************************************************
+   *
+   * Query Explorer
+   *
+   ******************************************************************/
+
+  function pageOpenQueryExplorer(collection) {
+
+    collection = collection || "";
+    showPanel("#collections-panel");
+    hideMoreQueryOptions();
+    //reset the form fields
+    $("#query-path").val("");
+    $("#query-source").val("");
+    $("#query-ql").val("");
+    showQueryCollectionView();
+    query_history = [];
+    //Prepare Collection Index Dropdown Menu
+    requestIndexes(collection);
+    //bind events for previous and next buttons
+    bindPagingEvents('query-response');
+    //clear out the table before we start
+    var output = $('#query-response-table');
+    output.empty();
+    //if a collection was provided, go ahead and get the default data
+    if (collection) {
+      getCollection('GET', collection);
+    }
+  }
+  window.Usergrid.console.pageOpenQueryExplorer = pageOpenQueryExplorer;
+
+  $("#query-help").click(function(e){
+    e.preventDefault();
+    $('#queryHelpModal').modal('show');
+  });
+  $("#query-method-help").click(function(e){
+    e.preventDefault();
+    $('#queryMethodHelpModal').modal('show');
+  });
+  $("#query-path-help").click(function(e){
+    e.preventDefault();
+    $('#queryPathHelpModal').modal('show');
+  });
+  $("#query-limit-help").click(function(e){
+    e.preventDefault();
+    $('#queryLimitHelpModal').modal('show');
+  });
+
+
+  //change contexts for REST operations
+  $("#button-query-get").click(function(){
+    $("#query-json-box").hide();
+    $("#query-query-box").show();
+    $("#query-limit-box").show();
+  });
+
+  $("#button-query-post").click(function(){
+    $("#query-json-box").show();
+    $("#query-query-box").hide();
+    $("#query-limit-box").hide();
+  });
+
+  $("#button-query-put").click(function(){
+    $("#query-json-box").show();
+    $("#query-query-box").show();
+    $("#query-limit-box").hide();
+  });
+
+  $("#button-query-delete").click(function(){
+    $("#query-json-box").hide();
+    $("#query-query-box").show();
+    $("#query-limit-box").hide();
+  });
+
+  $("#data-explorer-link").click(function(){
+    $('#data-explorer').show();
+    $('#query-path').val('');
+    $("#query-response-area").hide();
+  });
+
+  var queryPath = '';
+  function runCollectionQuery(){
+    var method;
+
+
+    //Select method to use
+    if($('#button-query-get').prop('checked') ){
+      method = 'GET';
+    } else if($('#button-query-post').prop('checked')){
+      method = 'POST';
+    } else if($('#button-query-put').prop('checked')){
+      method = 'PUT';
+    } else if($('#button-query-delete').prop('checked')){
+      method = 'DELETE';
+    } else {
+      alertModal("Notice", "Please select a method.");
+      return;
+    }
+
+
+    //If jsonBody is empty fill it with empty brackets
+    if($('#query-source').val() === '') {
+      $("#query-source").val('{"name":"value"}');
+    }
+    getCollection(method);
+  }
+
+  window.Usergrid.console.getCollection = getCollection;
+
+  function getCollection(method, path){
+    $("#data-explorer-status").html('Working...');
+    $("#data-explorer-status").show();
+
+    //get the data to run the query
+    if(!path){
+      var path = $("#query-path").val();
+    }
+    var path=path.replace("//","/");
+    if(method.toUpperCase() !== 'GET' && method.toUpperCase() !== 'DELETE'){
+      var data = $("#query-source").val();
+      try{
+        validateJson();
+        StatusBar.hideAlert();
+        data = JSON.parse(data);
+      } catch (e) {
+        alertModal("Error", "There is a problem with your JSON.");
+        return false;
+      }
+    }
+
+    var params = {};
+    var ql = $("#query-ql").val();
+    params.ql = ql;
+    if(method.toUpperCase() === 'GET'){
+      var limit = $("#query-limit").val();
+      params.limit = limit;
+    }
+
+    queryPath = path;
+
+    queryObj = new Usergrid.Query(method, path, data, params, getCollectionCallback, function(response) { alertModal("Error", response) });
+    runAppQuery(queryObj);
+  }
+
+  function getCollectionCallback(response) {
+    response = escapeMe(response);
+    setTimeout(function(){$("#data-explorer-status").hide();},3000);
+    $('body').scrollTop(0);
+    hidePagination('query-response');
+    $('#query-response-area').show();
+    if (response.action == 'post') {
+      pageSelectCollections();
+    }
+
+
+    $("#data-explorer-status").html('API call completed');
+
+    var path = response.path || "";
+    path = "" + path.match(/[^?]*/);
+    var path_no_slashes = "";
+    try {
+      path_no_slashes = response.path.replace(/\//g,'');
+    } catch(e) {}
+
+    $("#collections-link-buttons li").removeClass('active');
+    $("#collections-link-button-"+path_no_slashes).addClass('active');
+
+    if(response.action === ("delete")){
+      getCollection("GET", path);
+      return;
+    }
+
+    var slashes = (queryPath.split("/").length -1)
+    if (!slashes && queryPath.length > 0) {
+      queryPath = "/" + queryPath;
+    }
+    $('#query-path').val(queryPath);
+
+    $("#collection-type-field").html(response.path);
+    var output = $('#query-response-table');
+    if (response.entities) {
+      if (response.entities.length == 1 && slashes > 1){
+        generateBackToCollectionButton(response.path);
+      } else {
+        $('#back-to-collection').hide();
+      }
+      if (response.entities.length < 1) {
+        output.replaceWith('<table id="query-response-table" class="table"><tbody><tr class="zebraRows users-row"><td>No entities found</td></tr></table>');
+      } else {
+        //Inform the user of a valid query
+
+        var entity_type = response.entities [0].type;
+
+        var table = '<table id="query-response-table" class="table"><tbody><tr class="zebraRows users-row">' +
+          '<td class="checkboxo"><input type="checkbox" onclick="Usergrid.console.selectAllEntities(this);" /></td>';
+        if (entity_type === 'user') {
+          table += '<td class="gravatar50-td">&nbsp;</td>'+
+            '<td class="user-details bold-header">Username</td>'+
+            '<td class="user-details bold-header">Display Name</td>';
+        } else if (entity_type === 'group') {
+          table += '<td class="user-details bold-header">Path</td>'+
+            '<td class="user-details bold-header">Title</td>';
+        } else if (entity_type === 'role') {
+          table += '<td class="user-details bold-header">Title</td>'+
+            '<td class="user-details bold-header">Rolename</td>';
+        } else {
+          table += '<td class="user-details bold-header">Name</td>';
+        }
+        table += '<td class="user-details bold-header">UUID</td>';
+        table += '<td class="view-details">&nbsp;</td>' +
+          '</tr></tbody></table>';
+        output.replaceWith(table);
+        var this_data = {}
+        this_data.path = response.path;
+        for (i = 0; i < response.entities.length; i++) {
+          this_data.r = response.entities [i];
+          //next get a table view of the object
+          this_data.content = buildContentArea(response.entities [i]);
+
+
+          //get a json representation of the object
+          this_data.json = JSON.stringify(response.entities [i], null, 2);
+
+          if (this_data.type === 'user') {
+            if (!this_data.r.picture) {
+              this_data.r.picture = window.location.protocol+ "//" + window.location.host + window.location.pathname + "images/user-photo.png"
+            } else {
+              this_data.r.picture = get_replacementGravatar(this_data.r.picture);
+            }
+          } else {
+            if (!this_data.r.name) {
+              this_data.r.name = '[No value set]';
+            }
+          }
+          $.tmpl('apigee.ui.collection.table_rows.html', this_data).appendTo('#query-response-table');
+        }
+
+      }
+    } else if (response.list) {
+
+      var query = response.params.ql[0];
+      query = query.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
+      query = query.substr(6, query.length);
+      query = query.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
+      query = query.substring(0, query.indexOf("where"));
+      var params = query.split(",");
+
+      var table = '<table id="query-response-table" class="table"><tbody><tr class="zebraRows users-row">';
+
+      for (i = 0; i < params.length; i++) {
+        table +='<td class="user-details bold-header">'+params[i].replace(/^\s\s*/, '').replace(/\s\s*$/, '')+'</td>';
+      }
+      for (i = 0; i < response.list.length; i++) {
+        var list = response.list[i];
+        table += '<tr class="zebraRows users-row">';
+        for (j = 0; j < list.length; j++) {
+          var value = list[j];
+          if (!value) { value = '[no value]'; }
+          table +='<td class="details">'+value+'</td>';
+        }
+        table += '</tr>';
+      }
+
+      table += '</table>';
+      output.replaceWith(table);
+
+    } else {
+      output.replaceWith('<table id="query-response-table" class="table"><tbody><tr class="zebraRows users-row"><td>No entities found</td></tr></table>');
+    }
+
+    showPagination('query-response');
+  }
+
+
+  function buildContentArea(obj2) {
+    function getProperties(obj, keyType){
+      var output = '';
+      for (var property in obj) {
+        if (property == 'metadata') { keyType = 'metadata'; }
+        else if (property == 'collections') { keyType = 'collections'; }
+        else { keyType = ''; }
+
+        output += '<tr>';
+        if (obj.hasOwnProperty(property)){
+          if (obj[property] && obj[property].constructor == Object || obj[property] instanceof Array) {
+
+            var prop = (obj[property] instanceof Array)?property:'';
+            //console.log('**Object -> '+property+': ');
+            output += '<td>'+prop+'</td><td style="padding: 0"><table><tr>';
+            output += getProperties(obj[property], keyType);
+            output += '</td></tr></table>';
+          }
+          else {
+            //console.log(property + " " + obj[property]);
+            if (keyType == 'metadata' || keyType == 'collections') {
+              var link = '<a href="#" onclick="Usergrid.console.pageOpenQueryExplorer(\''+obj[property]+'\'); return false;">'+obj[property]+'</a>';
+              output += '<td>'+property+'</td><td>'+link+'</td>';
+            } else {
+              var htmlescaped = htmlEscape(obj[property]);
+              output += '<td>'+property+'</td><td>'+htmlescaped+'</td>';
+            }
+          }
+        }
+        output += '</tr>';
+      }
+      return output;
+    }
+    var output = getProperties(obj2, '');
+    return '<table>' + output + '</table>';
+  }
+  function htmlEscape(str) {
+    return String(str)
+      .replace(/&/g, '&amp;')
+      .replace(/"/g, '&quot;')
+      .replace(/'/g, '&#39;')
+      .replace(/</g, '&lt;')
+      .replace(/>/g, '&gt;');
+  }
+
+
+
+
+  function activateQueryRowJSONButton() {
+    $("#button-query-show-row-JSON").removeClass('disabled').addClass('active');
+    $("#button-query-show-row-content").removeClass('active').addClass('disabled');
+  }
+  window.Usergrid.console.activateQueryRowJSONButton = activateQueryRowJSONButton;
+
+  function activateQueryRowContentButton() {
+    $("#button-query-show-row-JSON").removeClass('active').addClass('disabled');
+    $("#button-query-show-row-content").removeClass('disabled').addClass('active');
+  }
+  window.Usergrid.console.activateQueryRowContentButton = activateQueryRowContentButton;
+
+  function showQueryCollectionView() {
+    $('#query-collection-info').show();
+    $('#query-detail-info').hide();
+    $('#query-ql-box').show();
+    $('#back-to-collection').hide();
+  }
+
+
+
+  function generateBackToCollectionButton(returnPath) {
+    var backButton = $('#back-to-collection');
+    if(backButton.attr('onclick')){
+      backButton.removeAttr('onclick');
+    }
+    backButton.attr('onclick',"Usergrid.console.getCollection('GET','" + returnPath+ "')");
+    $('#back-to-collection').show();
+  }
+
+  $.fn.loadEntityCollectionsListWidget = function() {
+    this.each(function() {
+      var entityType = $(this).dataset('entity-type');
+      var entityUIPlugin = "apigee_collections_" + entityType + "_list_item";
+      if (!$(this)[entityUIPlugin]) {
+        entityUIPlugin = "apigee_collections_entity_list_item";
+      }
+      $(this)[entityUIPlugin]();
+    });
+  };
+
+  $.fn.loadEntityCollectionsDetailWidget = function() {
+    this.each(function() {
+      var entityType = $(this).dataset('entity-type');
+      var entityUIPlugin = "apigee_collections_" + entityType + "_detail";
+      if (!$(this)[entityUIPlugin]) {
+        entityUIPlugin = "apigee_collections_entity_detail";
+      }
+      $(this)[entityUIPlugin]();
+    });
+    if (this.length === 1 ){
+      hideEntityCheckboxes();
+      hideEntitySelectButton();
+    }
+  };
+
+  function hideEntityCheckboxes(){
+    $(".listItem").hide();
+    $(".listItem").attr('checked', true);
+  }
+
+  function hideEntitySelectButton(){
+    $("#selectAllCollections").hide();
+  }
+  function showEntitySelectButton(){
+    $("#selectAllCollections").show();
+  }
+
+  function getQueryResultEntity(id) {
+    if (query_entities_by_id) {
+      return query_entities_by_id[id];
+    }
+    return null;
+  }
+  window.Usergrid.console.getQueryResultEntity = getQueryResultEntity;
+
+  function showQueryStatus(s, _type) {
+    StatusBar.showAlert(s, _type);
+  }
+
+  function expandQueryInput() {
+    $('#query-source').height(150);
+    $('#button-query-shrink').show();
+    $('#button-query-expand').hide();
+    return false;
+  }
+
+  function shrinkQueryInput() {
+    $('#query-source').height(60);
+    $('#button-query-shrink').hide();
+    $('#button-query-expand').show();
+    return false;
+  }
+
+  InitQueryPanel();
+  function InitQueryPanel(){
+    $('#query-source').focus(function(){
+      expandQueryInput();
+      prepareQueryInput(this);
+    });
+    //$('#query-source').keyup(function(){activateJSONValidator('#button-query-validate', '#query-source');})
+    $('#button-query-validate').click(function() {validateJson();return false;});
+    $('#button-query-shrink').click(shrinkQueryInput);
+    $('#button-query-expand').click(expandQueryInput);
+
+    $('#button-query').click(function(){runCollectionQuery(); return false;});
+
+  }
+
+  function prepareQueryInput(selector) {
+    var queryInput = $(selector);
+    if( queryInput.val() === ""){
+      queryInput.val("{\n\n}");
+    }
+  }
+
+  function activateJSONValidator(valButton, jsonArea) {
+    var validatorButton = $(valButton);
+    var textArea = $(jsonArea)
+    if(validatorButton.hasClass('disabled')){
+      validatorButton.removeClass('disabled');
+      validatorButton.click(function() {validateJson();return false;});
+    } else if(textArea.val() === "") {
+      validatorButton.addClass('disabled');
+      validatorButton.unbind('click');
+    }
+  }
+
+  function showMoreQueryOptions() {
+    $('.query-more-options').show();
+    $('.query-less-options').hide();
+    $('#query-ql').val("");
+    $('#query-source').val("{ }");
+  }
+
+  function hideMoreQueryOptions() {
+    $('.query-more-options').hide();
+    $('.query-less-options').show();
+    $('#query-ql').val("");
+    $('#query-source').val("");
+  }
+
+  function toggleMoreQueryOptions() {
+    $('.query-more-options').toggle();
+    $('.query-less-options').toggle();
+    $('#query-ql').val("");
+    $('#query-source').val("");
+  }
+
+  $('#button-query-more-options').click(function() {
+    toggleMoreQueryOptions();
+    return false;
+  });
+
+  $('#button-query-less-options').click(function() {
+    toggleMoreQueryOptions();
+    return false;
+  });
+
+  $('#query-source').keydown(function(e) {
+    var key = e.keyCode || e.which;
+
+    if ((key == 9 || key ===13)) {
+      e.preventDefault();
+      //Get cursor position
+      var start = this.selectionStart;
+      var end = this.selectionEnd;
+      var field = $(this);
+      var value = field.val();
+      //insert Text and indentation
+      field.val(value.substring(0, start) + '\r  ' + value.substring(end));
+      //return cursor to its position
+      this.selectionStart = this.selectionEnd = start + 1;
+    }
+  });
+
+  function validateJson() {
+    try {
+      var result = JSON.parse($('#query-source').val());
+      if (result) {
+        showQueryStatus('JSON is valid!');
+        $('#query-source').val(JSON.stringify(result, null, "  "));
+        return result;
+      }
+    } catch(e) {
+      showQueryStatus(e.toString(), "error");
+    }
+    return false;
+  };
+
+  window.Usergrid.console.doChildClick = function(event) {
+    var path = new String($('#query-path').val());
+    if (!path.endsWith("/")) {
+      path += "/";
+    }
+    path += event.target.innerText;
+    $('#query-path').val(path);
+  };
+
+  var queryQl = $('#query-ql');
+  queryQl.typeahead({source:indexes});
+
+  function doBuildIndexMenu() {
+    queryQl.data('typeahead').source = indexes;
+  }
+
+  $('#delete-entity-link').click(deleteEntity);
+
+  function deleteEntity(e) {
+    e.preventDefault();
+    var items = $('#query-response-table input[class=listItem]:checked');
+    if(!items.length){
+      alertModal("Please, first select the entities you want to delete.");
+      return;
+    }
+    var itemsCount = items.size();
+    confirmDelete(function(){
+      items.each(function() {
+        var path = $(this).attr('name');
+        runAppQuery(new Usergrid.Query("DELETE", path, null, null,
+          function(request) {
+            itemsCount--;
+            if(itemsCount==0){
+              $("#query-path").val(request.path);
+              getCollection('GET');
+            }},
+          function() { alertModal("Unable to delete: " + path); }
+        ));
+      });
+    });
+  }
+
+  function requestIndexes(path){
+    var data = {};
+    runAppQuery(new Usergrid.Query("GET", path + "/indexes", null, null,
+      function(response) {
+        if(response && response.data) {
+          data = response;
+        }
+        buildIndexDropdown('query-collections-indexes-list', data);
+
+      }));
+  }
+
+  function buildIndexDropdown(menuId, indexes) {
+    var menu = $("#" + menuId);
+    menu.empty();
+    $.tmpl('apigee.ui.collections.query.indexes.html', indexes).appendTo(menu);
+  }
+
+  function appendToCollectionsQuery(message){
+    var queryTextArea = $("#query-ql");
+    queryTextArea.val(queryTextArea.val()+ " " + message );
+  }
+  window.Usergrid.console.appendToCollectionsQuery = appendToCollectionsQuery;
+
+  /*******************************************************************
+   *
+   * Organization Home
+   *
+   ******************************************************************/
+
+
+  function pageSelectHome() {
+
+    requestAdmins();
+    displayOrganizationName(Usergrid.ApiClient.getOrganizationName());
+    requestOrganizationCredentials();
+    requestAdminFeed();
+  }
+  window.Usergrid.console.pageSelectHome = pageSelectHome;
+
+
+  function displayApplications(response) {
+    applications = {};
+    applications_by_id = {};
+    var appMenu = $('.applications-menu');
+    var appList = $('table#organization-applications-table');
+    appMenu.empty();
+    appList.empty();
+
+    if (response.data) {
+      applications = response.data;
+      var count = 0;
+      var applicationNames = keys(applications).sort();
+      var data = [];
+      var appMenuTmpl = $('<li><a >${name}</a></li>');
+
+      for (var i in applicationNames) {
+        var name = applicationNames[i];
+        var uuid = applications[name];
+        data.push({uuid:uuid, name:name.split("/")[1]});
+        count++;
+        applications_by_id[uuid] = name.split("/")[1];
+      }
+
+      if (count) {
+        $.tmpl('apigee.ui.applications.table_rows.html', data).appendTo(appList);
+        appMenuTmpl.tmpl(data).appendTo(appMenu);
+        appMenuTmpl.tmpl(data)
+        appMenu.find("a").click(function selectApp(e) {
+          var link = $(this);
+          pageSelect(link.tmplItem().data.name);
+          Usergrid.Navigation.router.navigateTo('dashboard');
+        });
+
+        appList.find("a").click(function selectApp(e) {
+          e.preventDefault();
+          var link = $(this);
+          pageSelect(link.tmplItem().data.name);
+          Usergrid.Navigation.router.navigateTo('dashboard');
+        });
+        enableApplicationPanelButtons();
+      }
+      appMenu.append('<li class="divider"></li>');
+      appMenu.append('<li><a class="" data-toggle="modal" href="#dialog-form-new-application"> <strong>+</strong> New Application</a></li>');
+    }
+
+    if(appList.is(":empty")){
+      appList.html('<div class="alert user-panel-section">No applications created.</div>');
+      appMenu.html('<li>--No Apps--</li>');
+      forceNewApp();
+    }
+
+    var appName = Usergrid.ApiClient.getApplicationName();
+    if (!appName) {
+      selectFirstApp();
+    } else {
+      setNavApplicationText();
+    }
+  }
+
+  function requestApplications() {
+    var sectionApps = $('#organization-applications-table');
+    sectionApps.empty().html('<div class="alert alert-info user-panel-section">Loading...</div>');
+    runManagementQuery(new Usergrid.Query("GET","organizations/" + Usergrid.ApiClient.getOrganizationName() + "/applications", null, null,
+      displayApplications,
+      function() {
+        sectionApps.html('<div class="alert user-panel-section">Unable to retrieve application list.</div>');
+      }
+    ));
+  }
+  Usergrid.console.requestApplications = requestApplications;
+
+  function selectFirstApp() {
+    //get the currently specified app name
+    var appName = Usergrid.ApiClient.getApplicationName();
+    //and make sure we it is in one of the current orgs
+    var app = Usergrid.organizations.getItemByName(appName);
+    if(appName && app) {
+      Usergrid.ApiClient.setApplicationName(appName);
+      pageSelect(appName);
+    } else {
+      //we need to select an app, so get the current org name
+      var orgName = Usergrid.ApiClient.getOrganizationName();
+      //get a reference to the org object by using the name
+      var org = Usergrid.organizations.getItemByName(orgName);
+      //get a handle to the first app in the org
+      app = org.getFirstItem();
+      //store the new app in the client
+      Usergrid.ApiClient.setApplicationName(app.getName());
+      pageSelect(app.getName());
+    }
+    setNavApplicationText();
+  }
+
+  function displayAdmins(response) {
+    var sectionAdmins = $('#organization-admins-table');
+    sectionAdmins.empty();
+    if (response.data) {
+      var admins = response.data;
+      admins = admins.sort();
+      for (var i in admins) {
+        var admin = admins[i];
+        admin.gravatar = get_gravatar(admin.email, 20);
+        $.tmpl('apigee.ui.admins.table_rows.html', admin).appendTo(sectionAdmins);
+      }
+    }
+    if(sectionAdmins.is(':empty')){
+      sectionAdmins.html('<div class="alert user-panel-section">No organization administrators.</div>');
+    }
+  }
+
+  function requestAdmins() {
+    var sectionAdmins =$('#organization-admins-table');
+    sectionAdmins.empty().html('<div class="alert alert-info user-panel-section">Loading...</div>');
+    runManagementQuery(new Usergrid.Query("GET","organizations/" + Usergrid.ApiClient.getOrganizationName()  + "/users", null, null,
+      displayAdmins,
+      function() {sectionAdmins.html('<div class="alert user-panel-section">Unable to retrieve admin list</div>');
+      }));
+  }
+
+  $(document).on('click', '.toggleableSP', function() {
+    $(this).parent().find('.toggleableSP').toggle();
+    return false
+  });
+
+  function get_gravatar(email, size) {
+    var size = size || 50;
+    return 'https://secure.gravatar.com/avatar/' + MD5(email) + '?s=' + size + encodeURI("&d=https://apigee.com/usergrid/img/user_profile.png");
+  }
+
+  function get_replacementGravatar(picture) {
+    picture = picture.replace(/^http:\/\/www.gravatar/i, 'https://secure.gravatar');
+    //note: changing this to use the image on apigee.com - since the gravatar default won't work on any non-public domains such as localhost
+    //this_data.picture = this_data.picture + encodeURI("?d="+window.location.protocol+"//" + window.location.host + window.location.pathname + "images/user_profile.png");
+    picture = picture + encodeURI("?d=https://apigee.com/usergrid/img/user_profile.png");
+    return picture;
+  }
+
+  function displayAdminFeed(response) {
+
+    var sectionActivities = $('#organization-feed-table');
+    sectionActivities.empty();
+
+    if (response.entities && (response.entities.length > 0)) {
+      var activities = response.entities;
+      for (var i in activities) {
+        var activity = activities[i];
+
+        // Next part is a hack. The API should return the email and title cleanly.
+        var title_tmp = $("<span/>", {html: activity.title});
+        activity.actor.email  = title_tmp.find('a').attr('mailto');
+        title_tmp.find('a').remove();
+        activity.title = title_tmp.text();
+        // hack ends here
+
+        activity.actor.gravatar = get_gravatar(activity.actor.email, 20);
+        $.tmpl('apigee.ui.feed.table_rows.html', activity).appendTo(sectionActivities);
+      }
+    }
+
+    if (sectionActivities.is(":empty")) {
+      sectionActivities.html('<div class="alert user-panel-section">No activities.</div>');
+    }
+  }
+
+  function requestAdminFeed() {
+    var section =$('#organization-activities');
+    section.empty().html('<div class="alert alert-info">Loading...</div>');
+    runManagementQuery(new Usergrid.Query("GET","orgs/" + Usergrid.ApiClient.getOrganizationName()  + "/feed", null, null, displayAdminFeed,
+      function() { section.html('<div class="alert">Unable to retrieve feed.</div>'); }));
+  }
+  window.Usergrid.console.requestAdminFeed = requestAdminFeed;
+
+  var organization_keys = { };
+
+  function requestOrganizationCredentials() {
+    $('#organization-panel-key').html('<div class="alert alert-info marginless">Loading...</div>');
+    $('#organization-panel-secret').html('<div class="alert alert-info marginless">Loading...</div>');
+    runManagementQuery(new Usergrid.Query("GET",'organizations/'+ Usergrid.ApiClient.getOrganizationName()  + "/credentials", null, null,
+      function(response) {
+        $('#organization-panel-key').html(response.credentials.client_id);
+        $('#organization-panel-secret').html(response.credentials.client_secret);
+        organization_keys = {client_id : response.credentials.client_id, client_secret : response.credentials.client_secret};
+      },
+      function() {
+        $('#organization-panel-key').html('<div class="alert marginless">Unable to load...</div>');
+        $('#organization-panel-secret').html('<div class="alert marginless">Unable to load...</div>');
+      }));
+  }
+
+  function newOrganizationCredentials() {
+    $('#organization-panel-key').html('<div class="alert alert-info marginless">Loading...</div>');
+    $('#organization-panel-secret').html('<div class="alert alert-info marginless">Loading...</div>');
+    runManagementQuery(new Usergrid.Query("POST",'organizations/' + Usergrid.ApiClient.getOrganizationName()   + "/credentials",null, null,
+      function(response) {
+        $('#organization-panel-key').html(response.credentials.client_id);
+        $('#organization-panel-secret').html(response.credentials.client_secret);
+        organization_keys = {client_id : response.credentials.client_id, client_secret : response.credentials.client_secret};
+      },
+      function() {
+        $('#organization-panel-key').html('<div class="alert marginless">Unable to load...</div>');
+        $('#organization-panel-secret').html('<div class="alert marginless">Unable to load...</div>');
+      }
+    ));
+  }
+  window.Usergrid.console.newOrganizationCredentials = newOrganizationCredentials;
+
+  function updateTips(t) {
+    tips.text(t).addClass('ui-state-highlight');
+    setTimeout(function() {
+        tips.removeClass('ui-state-highlight', 1500);
+      },
+      500);
+  }
+
+  function checkLength(o, n, min, max) {
+    if (o.val().length > max || o.val().length < min) {
+      o.addClass('ui-state-error');
+      updateTips("Length of " + n + " must be between " + min
+        + " and " + max + ".");
+      return false;
+    } else {
+      return true;
+    }
+  }
+
+  function checkRegexp(o, regexp, n) {
+    if (! (regexp.test(o.val()))) {
+      o.addClass('ui-state-error');
+      updateTips(n);
+      return false;
+    } else {
+      return true;
+    }
+  }
+
+  function checkTrue(o, t, n) {
+    if (!t) {
+      o.addClass('ui-state-error');
+      updateTips(n);
+    }
+    return t;
+  }
+
+  var tips = $('.validateTips');
+
+  /*******************************************************************
+   *
+   * Modals
+   *
+   ******************************************************************/
+
+  function alertModal(header,message) {
+    $('#alertModal h4').text(header);
+    $('#alertModal p').text(message);
+    $('#alertModal').modal('show');
+  }
+
+  //use like: alertBanner("Oh no!", "Say it isn't so!!");
+  //or like: alertBanner("Oh no!", "Say it isn't so!!", 5000); //will auto-close in 5 seconds
+  function alertBanner(header, message, timeout) {
+    $('#alert-error-header').html(header);
+    $('#alert-error-message').html(message);
+    $('#alert-error-message-container').show();
+    if (timeout) {
+      var alertTimer = setInterval(function(){
+        $('#alert-error-message-container').hide();
+        window.clearInterval(alertTimer);
+      },timeout);
+    }
+  }
+
+  function hideModal(id){
+    $(id).hide();
+  }
+
+  function confirmAction(header, message, callback){
+    var form = $('#confirmAction');
+
+    form.find('h4').text(header);
+    form.find('p').text(message);
+    form.unbind('submit');
+
+    form.submit(function(){
+      form.modal("hide");
+      callback();
+
+      return false;
+    });
+
+    form.modal('show');
+  }
+
+  function resetModal(){
+    this.reset();
+    var form = $(this);
+    formClearErrors(form);
+  }
+
+  function focusModal(){
+    $(this).find('input:first').focus();
+  }
+
+  function submitModal(e){
+    e.preventDefault();
+  }
+
+  $('form.modal').on('hidden',resetModal).on('shown',focusModal).submit(submitModal);
+  $('#dialog-form-new-application').submit(submitApplication);
+  $('#dialog-form-force-new-application').submit(submitApplication);
+  $('#dialog-form-new-admin').submit(submitNewAdmin);
+  $('#dialog-form-new-organization').submit(submitNewOrg);
+  $('#dialog-form-new-user').submit(submitNewUser);
+  $('#dialog-form-new-role').submit(submitNewRole);
+  $('#dialog-form-new-collection').submit(submitNewCollection);
+  $('#dialog-form-new-group').submit(submitNewGroup);
+  $('#dialog-form-add-group-to-user').submit(submitAddGroupToUser);
+  $('#dialog-form-add-user-to-group').submit(submitAddUserToGroup);
+  $('#dialog-form-add-user-to-role').submit(submitAddUserToRole);
+  $('#dialog-form-add-role-to-user').submit(function() { submitAddRoleToUser(current_roleName, current_roleTitle)});
+  $('#dialog-form-add-user-to-notification').submit(function() { addUserToNotification()});
+  $('#dialog-form-add-group-to-notification').submit(function() { addGroupToNotification()});
+  $('#dialog-form-add-group-to-role').submit(function() { submitAddGroupToRole(current_roleName, current_roleTitle)});
+  $('#dialog-form-add-role-to-group').submit(submitAddRoleToGroup);
+  $('#dialog-form-follow-user').submit(submitFollowUser);
+
+  function checkLength2(input, min, max) {
+    if (input.val().length > max || input.val().length < min) {
+      var tip = "Length must be between " + min + " and " + max + ".";
+      validationError(input,tip);
+      return false;
+    }
+
+    return true;
+  }
+
+  function checkRegexp2(input, regexp, tip) {
+    if (! (regexp.test(input.val()))) {
+      validationError(input,tip);
+      return false;
+    }
+    return true;
+  }
+
+  function checkTrue2(input, exp, tip) {
+    if (!exp) {
+      validationError(input,tip);
+      return false;
+    }
+
+    return true;
+  }
+
+  function confirmDelete(callback){
+    var form = $('#confirmDialog');
+    if (form.submit) {
+      form.unbind('submit');
+    }
+
+    form.submit(function(e){
+      e.preventDefault();
+      form.modal('hide');
+    }).submit(callback);
+
+    form.modal('show');
+  }
+
+  function validationError(input, tip){
+    input.focus();
+    input.parent().parent().addClass("error");
+    input.parent().parent().find(".help-block").text(tip).addClass("alert-error").addClass("alert").show();
+  }
+
+  $.fn.serializeObject = function() {
+    var o = {};
+    var a = this.serializeArray();
+
+    $.each(a, function() {
+      if (o[this.name]) {
+        if (!o[this.name].push) {
+          o[this.name] = [o[this.name]];
+        }
+        o[this.name].push(this.value || '');
+      } else {
+        o[this.name] = this.value || '';
+      }
+    });
+
+    return o;
+  };
+
+  function formClearErrors(form){
+    form.find('.ui-state-error').removeClass('ui-state-error');
+    form.find('.error').removeClass('error');
+    form.find('.help-block').empty().hide();
+  }
+
+  function submitApplication() {
+    var form = $(this);
+    formClearErrors(form);
+
+    var new_application_name = $(this).find('.new-application-name');
+
+    var bValid = checkLength2(new_application_name, 4, 80)
+      && checkRegexp2(new_application_name, usernameRegex, usernameAllowedCharsMessage);
+
+    if (bValid) {
+      runManagementQuery(new Usergrid.Query("POST","organizations/" + Usergrid.ApiClient.getOrganizationName()  + "/applications", form.serializeObject(), null,
+        function(response) {
+          for (var appName in response.data) { break; }
+          var appTitle = appName.split("/")[1];
+          var currentOrg = Usergrid.ApiClient.getOrganizationName();
+          Usergrid.organizations.getItemByName(currentOrg).addItem(new Usergrid.Application(appTitle, response.data[appName]));
+          pageSelect(appTitle);
+          requestApplications();
+        },
+        function() {
+          closeErrorMessage = function() {
+            $('#home-messages').hide();
+          };
+          var closebutton = '<a  onclick="closeErrorMessage();" class="close">&times;</a>'
+          $('#home-messages').text("Unable to create application: ").prepend(closebutton).addClass('alert-error').show();
+        }));
+      $(this).modal('hide');
+    }
+  }
+
+  function submitNewAdmin() {
+    var form = $(this);
+    formClearErrors(form);
+
+    var new_admin_email = $('#new-admin-email');
+    var bValid = checkLength2(new_admin_email, 6, 80)
+      && checkRegexp2(new_admin_email,emailRegex, emailAllowedCharsMessage);
+    if (bValid) {
+      var data = form.serializeObject();
+      runManagementQuery(new Usergrid.Query("POST","organizations/" + Usergrid.ApiClient.getOrganizationName() + "/users", data, null,
+        requestAdmins,
+        function () { alertModal("Error", "Unable to create admin"); }
+      ));
+      $(this).modal('hide');
+    }
+  }
+
+  function addOrganizationToList(orgName) {
+    runManagementQuery(new Usergrid.Query("GET","orgs/" + orgName, null, null,
+      function(response) {
+        var orgName = response.organization.name;
+        var orgUUID = response.organization.uuid;
+        organization = new Usergrid.Organization(orgName, orgUUID);
+        var apps = response.organization.applications;
+        for(app in apps) {
+          var appName = app.split("/")[1];
+          //grab the id
+          var appUUID = response.organization.applications[app];
+          //store in the new Application object
+          application = new Usergrid.Application(appName, appUUID);
+          organization.addItem(application);
+        }
+        //add organization to organizations list
+        Usergrid.organizations.addItem(organization);
+        requestAccountSettings();
+        setupOrganizationsMenu();
+      },
+      function() { alertModal("Error", "Unable to get organization" + orgName);
+      }
+    ));
+  }
+
+  function submitNewOrg() {
+    var form = $(this);
+    formClearErrors(form);
+
+    var new_organization_name = $('#new-organization-name');
+    var new_organization_name_val = $('#new-organization-name').val();
+    var bValid = checkLength2(new_organization_name, 4, 80)
+      && checkRegexp2(new_organization_name, organizationNameRegex, organizationNameAllowedCharsMessage);
+
+    if (bValid) {
+      var data = form.serializeObject();
+      runManagementQuery(new Usergrid.Query("POST","users/" + Usergrid.userSession.getUserUUID() + "/organizations", data, null,
+        function() {
+          addOrganizationToList(new_organization_name_val);
+        },
+        function() { alertModal("Error", "Unable to create organization"); }
+      ));
+      $(this).modal('hide');
+    }
+  }
+  //TODO: the organization, and required fields for this method, are hidden. There is no quick way to check variable names and order
+  /*
+   * Needed fields:
+   * username:
+   * name: FULL NAME
+   * email:
+   * password:
+   */
+  function submitNewUser() {
+    var form = $(this);
+    formClearErrors(form);
+
+    var email = $('#new-user-email');
+    var username = $('#new-user-username');
+    var fullname = $('#new-user-fullname');
+    var password = $('#new-user-password');
+    var validate_password = $('#new-user-validate-password');
+
+    var bValid =
+      //Fullname can is not required.
+      checkLength2(fullname , 0, 80)
+        && ( fullname.val() === "" || checkRegexp2(fullname, nameRegex, nameAllowedCharsMessage) )
+        //Username IS required
+        && checkRegexp2(username, usernameRegex, usernameAllowedCharsMessage)
+        //Email is NOT required
+        && ( checkLength2(email, 6, 80) )
+        && ( email.val() === "" || checkRegexp2(email,emailRegex, emailAllowedCharsMessage) )
+        && ( checkLength2(password ,0 ,0) || checkLength2(password, 1, 64) )
+        && ( password.val() === "" || checkRegexp2(password,passwordRegex, passwordAllowedCharsMessage) )
+        && ( checkTrue2(password, (password.val() === validate_password.val()), passwordMismatchMessage));
+
+    if (bValid) {
+      var data = {"email":email.val(), "username":username.val(),"name":fullname.val(), "password":password.val()}
+      runAppQuery(new Usergrid.Query("POST", 'users', data, null,
+        function() {
+          getUsers();
+          closeErrorMessage = function() {
+            $('#users-messages').hide();
+          };
+          var closebutton = '<a  onclick="closeErrorMessage();" class="close">&times;</a>';
+          $('#users-messages')
+            .text("User created successfully.")
+            .prepend(closebutton)
+            .removeClass()
+            .addClass('alert alert-warning')
+            .show();
+        },
+        function() {
+          closeErrorMessage = function() {
+            $('#users-messages').hide();
+          };
+          var closebutton = '<a  onclick="closeErrorMessage();" class="close">&times;</a>'
+          $('#users-messages')
+            .text("Unable to create user")
+            .prepend(closebutton)
+            .removeClass()
+            .addClass('alert alert-error')
+            .show();
+        }
+      ));
+
+      $(this).modal('hide');
+    }
+  }
+
+  function submitNewRole() {
+    var form = $(this);
+    formClearErrors(form);
+
+    var new_role_name = $('#new-role-name');
+    var new_role_title = $('#new-role-title');
+
+    var bValid = checkLength2(new_role_name, 1, 80)
+      && checkRegexp2(new_role_name, roleRegex, roleAllowedCharsMessage)
+      && checkLength2(new_role_title, 1, 80)
+      && checkRegexp2(new_role_title,titleRegex, titleAllowedCharsMessage);
+
+    if (bValid) {
+      var data = form.serializeObject();
+      runAppQuery(new Usergrid.Query("POST", "role", data, null,
+        function() {
+          getRoles();
+          closeErrorMessage = function() {
+            $('#roles-messages').hide();
+          };
+          var closebutton = '<a  onclick="closeErrorMessage();" class="close">&times;</a>'
+          $('#roles-messages')
+            .text("Role created successfully.")
+            .prepend(closebutton)
+            .removeClass()
+            .addClass('alert alert-warning')
+            .show();
+        },
+        function() {
+          closeErrorMessage = function() {
+            $('#roles-messages').hide();
+          };
+          var closebutton = '<a  onclick="closeErrorMessage();" class="close">&times;</a>'
+          $('#roles-messages')
+            .text("Unable to create user")
+            .prepend(closebutton)
+            .removeClass()
+            .addClass('alert alert-error')
+            .show();
+        }
+      ));
+
+      $(this).modal('hide');
+    }
+  }
+
+  function submitNewCollection() {
+    var form = $(this);
+    formClearErrors(form);
+
+    var new_collection_name = $('#new-collection-name');
+
+    var bValid = checkLength2(new_collection_name, 4, 80)
+      && checkRegexp2(new_collection_name, alphaNumRegex, alphaNumAllowedCharsMessage);
+
+    if (bValid) {
+      var data = form.serializeObject();
+      var collections = {};
+      collections[data.name] = {};
+      var metadata = {
+        metadata: {
+          collections: collections
+        }
+      }
+      runAppQuery(new Usergrid.Query("PUT", "", metadata, null,
+        function() {
+          getCollections();
+          closeErrorMessage = function() {
+            $('#collections-messages').hide();
+          };
+          var closebutton = '<a  onclick="closeErrorMessage();" class="close">&times;</a>'
+          $('#collections-messages')
+            .text("Collection created successfully.")
+            .prepend(closebutton)
+            .removeClass()
+            .addClass('alert alert-warning')
+            .show();
+          getCollection("get", data.name);
+        },
+        function() {
+          closeErrorMessage = function() {
+            $('#collections-messages').hide();
+          };
+          var closebutton = '<a  onclick="closeErrorMessage();" class="close">&times;</a>'
+          $('#collections-messages')
+            .text("Unable to create user")
+            .prepend(closebutton)
+            .removeClass()
+            .addClass('alert alert-error')
+            .show();
+        }
+      ));
+
+      $(this).modal('hide');
+    }
+  }
+
+  function submitNewGroup() {
+    var form = $(this);
+    formClearErrors(form);
+
+    var new_group_title = $('#new-group-title');
+    var new_group_path = $('#new-group-path');
+
+    var bValid = checkLength2(new_group_title, 1, 80)
+      && checkRegexp2(new_group_title, nameRegex, nameAllowedCharsMessage)
+      && checkLength2(new_group_path, 1, 80)
+      && checkRegexp2(new_group_path, pathRegex, pathAllowedCharsMessage);
+
+    if (bValid) {
+      var data = form.serializeObject();
+      runAppQuery(new Usergrid.Query("POST", "groups", data, null,
+        function() {
+          getGroups();
+          closeErrorMessage = function() {
+            $('#groups-messages').hide();
+          };
+          var closebutton = '<a  onclick="closeErrorMessage();" class="close">&times;</a>'
+          $('#groups-messages')
+            .text("Group created successfully.")
+            .prepend(closebutton)
+            .removeClass()
+            .addClass('alert alert-warning')
+            .show();
+        },
+        function() {
+          closeErrorMessage = function() {
+            $('#groups-messages').hide();
+          };
+          var closebutton = '<a  onclick="closeErrorMessage();" class="close">&times;</a>'
+          $('#groups-messages').text("Unable to create group").prepend(closebutton).addClass('alert-error').show();
+        }
+      ));
+
+      $(this).modal('hide');
+    }
+  }
+
+  function submitAddGroupToUser() {
+    var form = $(this);
+    formClearErrors(form);
+    var add_group_groupname = $('#search-group-name-input');
+    var bValid = checkLength2(add_group_groupname, 1, 80)
+      && checkRegexp2(add_group_groupname, usernameRegex, usernameAllowedCharsMessage);
+
+    if (bValid) {
+      userId = $('#search-group-userid').val();
+      groupId = $('#search-group-name-input').val();
+
+      runAppQuery(new Usergrid.Query("POST", "/groups/" + groupId + "/users/" + userId, null, null,
+        function() { requestUser(userId); },
+        function() { alertModal("Error", "Unable to add group to user"); }
+      ));
+
+      $(this).modal('hide');
+    }
+  }
+
+  function submitAddUserToGroup() {
+    var form = $(this);
+    formClearErrors(form);
+    var add_user_username = $('#search-user-name-input');
+    var bValid = checkLength2(add_user_username, 1, 80)
+      && checkRegexp2(add_user_username, usernameRegex, usernameAllowedCharsMessage);
+
+    if (bValid) {
+      userId = $('#search-user-name-input').val();
+      groupId = $('#search-user-groupid').val();
+      runAppQuery(new Usergrid.Query("POST", "/groups/" + groupId + "/users/" + userId, null, null,
+        function() { requestGroup(groupId); },
+        function() { alertModal("Error", "Unable to add user to group"); }
+      ));
+      $(this).modal('hide');
+    }
+  }
+
+  function submitFollowUser(){
+    var form = $(this);
+    formClearErrors(form);
+    var username = $('#search-follow-username-input');
+    var bValid = checkLength2(username, 1, 80) && checkRegexp2(username, usernameRegex, usernameAllowedCharsMessage);
+    if (bValid) {
+      var followingUserId = $('#search-follow-username').val();
+      var followedUserId = $('#search-follow-username-input').val();
+      runAppQuery(new Usergrid.Query("POST", "/users/" + followingUserId + "/following/user/" + followedUserId, null, null,
+        function() { pageSelectUserGraph(followingUserId)},
+        function() {alertModal("Error", "Unable to follow User");}
+      ));
+      $(this).modal('hide');
+    }
+  }
+
+  function submitAddRoleToUser(roleName, roleTitle) {
+    var form = $(this);
+    formClearErrors(form);
+    var roleIdField = $('#search-roles-user-name-input');
+    var bValid = checkLength2(roleIdField, 1, 80) && checkRegexp2(roleIdField, usernameRegex, usernameAllowedCharsMessage)
+    var username = $('#search-roles-user-name-input').val();
+    if (bValid) {
+      runAppQuery(new Usergrid.Query("POST", "/roles/" + roleName + "/users/" + username, null, null,
+        function() { pageSelectRoleUsers(roleName, roleTitle); },
+        function() { alertModal("Error", "Unable to add user to role"); }
+      ));
+      $('#dialog-form-add-role-to-user').modal('hide');
+    }
+  }
+
+  function submitAddUserToRole() {
+    var form = $(this);
+    formClearErrors(form);
+
+    var roleIdField = $('#search-role-name-input');
+    var bValid = checkLength2(roleIdField, 1, 80)
+      && checkRegexp2(roleIdField, roleRegex, roleAllowedCharsMessage)
+
+    var username = $('#role-form-username').val();
+    var roleId = $('#search-role-name-input').val();
+    // role may have a preceding or trailing slash, remove it
+    roleId = roleId.replace('/','');
+    if (bValid) {
+      runAppQuery(new Usergrid.Query("POST", "/roles/" + roleId + "/users/" + username, null, null,
+        function() { pageSelectUserPermissions(username); },
+        function() { alertModal("Error", "Unable to add user to role"); }
+      ));
+
+      $(this).modal('hide');
+    }
+  }
+
+  function deleteUsersFromRoles(username) {
+    var items = $('#users-permissions-response-table input[class^=listItem]:checked');
+    if(!items.length){
+      alertModal("Error", "Please, first select the roles you want to delete for this user.");
+      return;
+    }
+
+    confirmDelete(function(){
+      items.each(function() {
+        var roleName = $(this).attr("value");
+        runAppQuery(new Usergrid.Query("DELETE", "/roles/" + roleName + "/users/" + username, null, null,
+          function() { pageSelectUserPermissions (username); },
+          function() { alertModal("Error", "Unable to remove user from role"); }
+        ));
+      });
+    });
+  }
+  window.Usergrid.console.deleteUsersFromRoles = deleteUsersFromRoles;
+
+  function deleteRoleFromUser(roleName, roleTitle) {
+    var items = $('#role-users input[class^=listItem]:checked');
+    if(!items.length){
+      alertModal("Error", "Please, first select the users you want to delete from this role.");
+      return;
+    }
+
+    confirmDelete(function(){
+      items.each(function() {
+        var username = $(this).attr("value");
+        runAppQuery(new Usergrid.Query("DELETE", "/roles/" + roleName + "/users/" + username, null, null,
+          function() { pageSelectRoleUsers (roleName, roleTitle); },
+          function() { alertModal("Error", "Unable to remove user from role"); }
+        ));
+      });
+    });
+  }
+  window.Usergrid.console.deleteRoleFromUser = deleteRoleFromUser;
+
+  function removeUserFromGroup(userId) {
+    var items = $('#user-panel-memberships input[class^=listItem]:checked');
+    if(!items.length){
+      alertModal("Error", "Please, first select the groups you want to delete for this user.")
+      return;
+    }
+    confirmDelete(function(){
+      items.each(function() {
+        var groupId = $(this).attr("value");
+        runAppQuery(new Usergrid.Query("DELETE", "/groups/" + groupId + "/users/" + userId, null, null,
+          function() { pageSelectUserGroups (userId); },
+          function() { alertModal("Error", "Unable to remove user from group"); }
+        ));
+      });
+    });
+  }
+  window.Usergrid.console.removeUserFromGroup = removeUserFromGroup;
+
+  function removeGroupFromUser(groupId) {
+    var items = $('#group-panel-memberships input[class^=listItem]:checked');
+    if (!items.length) {
+      alertModal("Error", "Please, first select the users you want to from this group.");
+      return;
+    }
+
+    confirmDelete(function(){
+      items.each(function() {
+        var userId = $(this).attr("value");
+        runAppQuery(new Usergrid.Query("DELETE", "/groups/" + groupId + "/users/" + userId, null, null,
+          function() { pageSelectGroupMemberships (groupId); },
+          function() { alertModal("Error", "Unable to remove user from group"); }
+        ));
+      });
+    });
+  }
+  window.Usergrid.console.removeGroupFromUser = removeGroupFromUser;
+
+  function deleteRolesFromGroup(roleId, rolename) {
+    var items = $('#group-panel-permissions input[class^=listItem]:checked');
+    if(!items.length){
+      alertModal("Error", "Please, first select the roles you want to delete from this group.")
+      return;
+    }
+
+    confirmDelete(function(){
+      items.each(function() {
+        var roleId = $(this).attr("value");
+        var groupname = $('#role-form-groupname').val();
+        runAppQuery(new Usergrid.Query("DELETE", "/roles/" + roleId + "/groups/" + groupname, null, null,
+          function() { pageSelectGroupPermissions(groupname); },
+          function() { alertModal("Error", "Unable to remove role from group"); }
+        ));
+      });
+    });
+  }
+  window.Usergrid.console.deleteRolesFromGroup = deleteRolesFromGroup;
+
+  function submitAddRoleToGroup() {
+    var form = $(this);
+    formClearErrors(form);
+
+    var roleIdField = $('#search-groups-role-name-input');
+    var bValid = checkLength2(roleIdField, 1, 80)
+      && checkRegexp2(roleIdField, roleRegex, roleAllowedCharsMessage)
+
+    var groupname = $('#role-form-groupname').val();
+    var roleId = $('#search-groups-role-name-input').val();
+    // role may have a preceding or trailing slash, remove it
+    roleId = roleId.replace('/','');
+
+    if (bValid) {
+      runAppQuery(new Usergrid.Query("POST", "/groups/" + groupname + "/roles/" + roleId, null, null,
+        function() { pageSelectGroupPermissions(groupname); },
+        function() { alertModal("Error", "Unable to add user to role"); }
+      ));
+      $(this).modal('hide');
+    }
+  }
+
+  /*******************************************************************
+   *
+   * Generic page select
+   *
+   ******************************************************************/
+  function pageSelect(name) {
+    if (name) {
+      //the following 3 lines are just a safety check, we could just store the name
+      //get the current org name
+      var currentOrg = Usergrid.ApiClient.getOrganizationName();
+      //get a reference to the current org object by using the name
+      var org = Usergrid.organizations.getItemByName(currentOrg);
+      //get a reference to the specified app by name
+      var app = org.getItemByName(name);
+      //store the name
+      Usergrid.ApiClient.setApplicationName(app.getName());
+    }
+    setNavApplicationText();
+    getCollections();
+    query_history = [];
+  }
+  window.Usergrid.console.pageSelect = pageSelect;
+
+
+  /*******************************************************************
+   *
+   * Application
+   *
+   ******************************************************************/
+
+  function pageSelectApplication() {
+    pageSelect();
+    requestApplicationUsage();
+  }
+  window.Usergrid.console.pageSelectApplication = pageSelectApplication;
+
+  function updateApplicationDashboard(){
+    var data = new google.visualization.DataTable();
+    data.addColumn('string', 'Entity');
+    data.addColumn('number', 'Count');
+    var rows = [];
+    var t = '<table class="table table-bordered" id="application-panel-entity-counts">';
+    var collectionNames = keys(applicationData.Collections).sort();
+
+    var entity_count = 0;
+    for (var i in collectionNames) {
+      var collectionName = collectionNames[i];
+      var collection = applicationData.Collections[collectionName];
+      var row = [collectionName, {v: Math.abs(collection.count)}];
+      rows.push(row);
+      collectionName = escapeString(collectionName);
+      t += '<tr class="zebraRows"><td>' + collection.count + '</td><td>' + collectionName + '</td></tr>';
+      entity_count += collection.count;
+    }
+    t += '<tr id="application-panel-entity-total"><th>' + entity_count + '</th><th>entities total</th></tr>';
+    t += '</table>';
+    data.addRows(rows);
+
+    new google.visualization.PieChart(
+      document.getElementById('application-panel-entity-graph')).
+      draw(data, {
+        height: 200,
+        is3D: true,
+        backgroundColor: backgroundGraphColor
+      }
+    );
+
+    $('#dashboard-panel #application-panel-text').html(t);
+  }
+
+  function requestApplicationUsage() {
+    $('#application-entities-timeline').html("");
+    $('#application-cpu-time').html("");
+    $('#application-data-uploaded').html("");
+    $('#application-data-downloaded').html("");
+    var params = {};
+    params.start_time = Math.floor(new Date().getTime() / 1209600000) * 1209600000;
+    params.end_time = start_timestamp + 1209600000;
+    params.resolution = "day";
+    params.counter = ["application.entities", "application.request.download", "application.request.time", "application.request.upload"];
+    params.pad = true;
+
+    runAppQuery(new Usergrid.Query("GET", "counters", null, params,
+      function(response) {
+        var usage_counters = response.counters;
+
+        if (!usage_counters) {
+          $('#application-entities-timeline').html("");
+          $('#application-cpu-time').html("");
+          $('#application-data-uploaded').html("");
+          $('#application-data-downloaded').html("");
+          return;
+        }
+
+        var graph_width = 350;
+        var graph_height = 100;
+        var data = new google.visualization.DataTable();
+        data.addColumn('date', 'Time');
+        data.addColumn('number', 'Entities');
+        data.addRows(15);
+
+        for (var i in usage_counters[0].values) {
+          data.setCell(parseInt(i), 0, new Date(usage_counters[0].values[i].timestamp));
+          data.setCell(parseInt(i), 1, usage_counters[0].values[i].value);
+        }
+
+        new google.visualization.LineChart(document.getElementById('application-entities-timeline')).draw(data, {
+          title: "Entities",
+          titlePosition: "in",
+          titleTextStyle: {color: 'black', fontName: 'Arial', fontSize: 18},
+          width: graph_width,
+          height: graph_height,
+          backgroundColor: backgroundGraphColor,
+          legend: "none",
+          hAxis: {textStyle: {color:"transparent", fontSize: 1}},
+          vAxis: {textStyle: {color:"transparent", fontSize: 1}}
+        });
+
+        data = new google.visualization.DataTable();
+        data.addColumn('date', 'Time');
+        data.addColumn('number', 'CPU');
+        data.addRows(15);
+
+        for (var i in usage_counters[2].values) {
+          data.setCell(parseInt(i), 0, new Date(usage_counters[2].values[i].timestamp));
+          data.setCell(parseInt(i), 1, usage_counters[2].values[i].value);
+        }
+
+        new google.visualization.LineChart(document.getElementById('application-cpu-time')).draw(data, {
+          title: "CPU Time Used",
+          titlePosition: "in",
+          titleTextStyle: {color: 'black', fontName: 'Arial', fontSize: 18},
+          width: graph_width,
+          height: graph_height,
+          backgroundColor: backgroundGraphColor,
+          legend: "none",
+          hAxis: {textStyle: {color:"transparent", fontSize: 1}},
+          vAxis: {textStyle: {color:"transparent", fontSize: 1}}
+        });
+
+        data = new google.visualization.DataTable();
+        data.addColumn('date', 'Time');
+        data.addColumn('number', 'Uploaded');
+        data.addRows(15);
+
+        for (var i in usage_counters[3].values) {
+          data.setCell(parseInt(i), 0, new Date(usage_counters[3].values[i].timestamp));
+          data.setCell(parseInt(i), 1, usage_counters[3].values[i].value);
+        }
+
+        new google.visualization.LineChart(document.getElementById('application-data-uploaded')).draw(data, {
+          title: "Bytes Uploaded",
+          titlePosition: "in",
+          titleTextStyle: {color: 'black', fontName: 'Arial', fontSize: 18},
+          width: graph_width,
+          height: graph_height,
+          backgroundColor: backgroundGraphColor,
+          legend: "none",
+          hAxis: {textStyle: {color:"transparent", fontSize: 1}},
+          vAxis: {textStyle: {color:"transparent", fontSize: 1}}
+        });
+
+        data = new google.visualization.DataTable();
+        data.addColumn('date', 'Time');
+        data.addColumn('number', 'Downloaded');
+        data.addRows(15);
+
+        for (var i in usage_counters[1].values) {
+          data.setCell(parseInt(i), 0, new Date(usage_counters[1].values[i].timestamp));
+          data.setCell(parseInt(i), 1, usage_counters[1].values[i].value);
+        }
+
+        new google.visualization.LineChart(document.getElementById('application-data-downloaded')).draw(data, {
+          title: "Bytes Downloaded",
+          titlePosition: "in",
+          titleTextStyle: {color: 'black', fontName: 'Arial', fontSize: 18},
+          width: graph_width,
+          height: graph_height,
+          backgroundColor: backgroundGraphColor,
+          legend: "none",
+          hAxis: {textStyle: {color:"transparent", fontSize: 1}},
+          vAxis: {textStyle: {color:"transparent", fontSize: 1}}
+        });
+      },
+      function() {
+        $('#application-entities-timeline').html("");
+        $('#application-cpu-time').html("");
+        $('#application-data-uploaded').html("");
+        $('#application-data-downloaded').html("");
+      }
+    ));
+  }
+  window.Usergrid.console.requestApplicationUsage = requestApplicationUsage;
+
+  /*******************************************************************
+   *
+   * Query Object Setup
+   *
+   ******************************************************************/
+  var queryObj = {};
+
+  function hidePagination(section) {
+    $('#'+section+'-pagination').hide();
+    $('#'+section+'-next').hide();
+    $('#'+section+'-previous').hide();
+  }
+
+  function showPagination(section){
+    if (queryObj.hasNext()) {
+      $('#'+section+'-pagination').show();
+      $('#'+section+'-next').show();
+    }
+
+    if (queryObj.hasPrevious()) {
+      $('#'+section+'-pagination').show();
+      $('#'+section+'-previous').show();
+    }
+  }
+
+  function hideCurlCommand(section) {
+    $('#'+section+'-curl-container').hide();
+    $('#'+section+'-curl-token').hide();
+  }
+
+  function showCurlCommand(section, curl, token) {
+    var data = {
+      curlData: curl,
+      sectionName: section
+    };
+    var sectionId = $('#'+section+'-curl-container');
+    sectionId.html("");
+    $.tmpl('apigee.ui.curl.detail.html', data).appendTo(sectionId);
+    sectionId.show();
+    if (!token) {
+      $('#'+section+'-curl-token').hide();
+    }
+  }
+
+  function copyCurlCommand() {
+    $('#copypath', 'body')
+      .find('a')
+      .livequery('click', function() {
+        $(this)
+          .blur();
+        var nodetext = $('#'+section+'-curl').html();
+        $('#copypath input').focus();
+        $('#copypath input').select();
+        return false;
+      });
+
+  }
+
+  function bindPagingEvents(section) {
+    $(document).off('click', '#'+section+'-previous', getPrevious);
+    $(document).off('click', '#'+section+'-next', getNext);
+    //bind the click events
+    $(document).on('click', '#'+section+'-previous', getPrevious);
+    $(document).on('click', '#'+section+'-next', getNext);
+  }
+  Usergrid.console.bindPagingEvents = bindPagingEvents;
+
+  function getPrevious() { //called by a click event - for paging
+    queryObj.getPrevious();
+    runAppQuery();
+  }
+  function getNext() { //called by a click event - for paging
+    queryObj.getNext();
+    runAppQuery();
+  }
+
+  function runAppQuery(_queryObj) {
+    var obj = _queryObj || queryObj;
+    Usergrid.ApiClient.runAppQuery(obj);
+    return false;
+  }
+
+  function runManagementQuery(_queryObj) {
+    var obj = _queryObj || queryObj;
+    Usergrid.ApiClient.runManagementQuery(obj);
+    return false;
+  }
+
+  /*******************************************************************
+   *
+   * Users
+   *
+   ******************************************************************/
+  var userLetter = "*";
+  var userSortBy = "username";
+
+  function pageSelectUsers() {
+    //Hide old Alert Messages
+    hideModal('#users-messages');
+    //make a new query object
+    queryObj = new Usergrid.Query(null);
+    //bind events for previous and next buttons
+    bindPagingEvents('users');
+    //reset paging so we start at the first page
+    queryObj.resetPaging();
+    //the method to get the compile and call the query
+    getUsers();
+    //ui stuff
+    selectFirstTabButton('#users-panel-tab-bar');
+    showPanelList('users');
+    $('#search-user-username').val(''); //reset the search box
+  }
+  window.Usergrid.console.pageSelectUsers = pageSelectUsers;
+
+  function getUsers(search, searchType) {
+    //clear out the table before we start
+    hideCurlCommand('users');
+    var output = $('#users-table');
+    output.empty();
+    var query = {"ql" : "order by " + userSortBy}; //default to built in search
+    if (typeof search == 'string') {
+      if (search.length > 0) {
+        if (searchType == 'name') {
+          query = {"ql" : searchType + " contains '" + search + "*'"};
+        } else {
+          query = {"ql" : searchType + "='" + search + "*'"};
+        }
+      }
+    } else if (userLetter != "*") {
+      query = {"ql" : searchType + "='" + userLetter + "*'"};
+    }
+
+    queryObj = new Usergrid.Query("GET", "users", null, query, getUsersCallback, function() { alertModal("Error", "Unable to retrieve users."); });
+    runAppQuery(queryObj);
+  }
+
+  function getUsersCallback(response) {
+    response = escapeMe(response);
+    hidePagination('users');
+    var output = $('#users-table');
+    if (response.entities.length < 1) {
+      output.replaceWith('<div id="users-table" class="panel-section-message">No users found.</div>');
+    } else {
+      output.replaceWith('<table id="users-table" class="table"><tbody><tr class="zebraRows users-row"><td class="checkboxo"><input type="checkbox" onclick="Usergrid.console.selectAllEntities(this);" /></td><td class="gravatar50-td">&nbsp;</td><td class="user-details bold-header">username</td><td class="user-details bold-header">Display Name</td><td class="view-details">&nbsp;</td></tr></tbody></table>');
+      for (i = 0; i < response.entities.length; i++) {
+        var this_data = response.entities[i];
+        if (!this_data.picture) {
+          this_data.picture = window.location.protocol+ "//" + window.location.host + window.location.pathname + "images/user-photo.png"
+        } else {
+          this_data.picture = get_replacementGravatar(this_data.picture);
+        }
+        $.tmpl('apigee.ui.users.table_rows.html', this_data).appendTo('#users-table');
+      }
+    }
+    showPagination('users');
+    showCurlCommand('users', queryObj.getCurl(), queryObj.getToken());
+  }
+
+  function showUsersForSearch(search){
+    selectFirstTabButton('#users-panel-tab-bar');
+    $('#users-panel-search').hide();
+    selectTabButton('#button-users-list');
+    $('#users-panel-list').show();
+    userLetter = search;
+    getUsers();
+  }
+  Usergrid.console.showUsersForSearch = showUsersForSearch;
+
+  function searchUsers(){
+    var search = $('#search-user-username').val();
+    var searchType = ($('#search-user-type').val())?$('#search-user-type').val():userSortBy;
+    //make sure the input is valid:
+    if (searchType == 'name') {
+      searchType = 'name';
+    } else if (searchType == 'username') {searchType = 'username';}
+    getUsers(search, searchType);
+  }
+  Usergrid.console.searchUsers = searchUsers;
+
+  function selectAllEntities(checkbox){
+    if (checkbox.checked) {
+      $('[class=listItem]').attr('checked', true);
+    } else {
+      $('[class=listItem]').attr('checked', false);
+    }
+  }
+  window.Usergrid.console.selectAllEntities = selectAllEntities;
+
+  $('#delete-users-link').click(deleteUsers);
+  function deleteUsers(e) {
+    e.preventDefault();
+
+    var items = $('#users-table input[class^=listItem]:checked');
+    if(!items.length){
+      alertModal("Error", "Please, first select the users you want to delete.");
+      return;
+    }
+
+    confirmDelete(function(){
+      items.each(function() {
+        var userId = $(this).attr("value");
+        runAppQuery(new Usergrid.Query("DELETE", 'users/' + userId, null, null,
+          getUsers,
+          function() { alertModal("Error", "Unable to delete user - " + userId) }
+        ));
+      });
+    });
+  }
+
+  /*******************************************************************
+   *
+   * User
+   *
+   ******************************************************************/
+
+  function pageOpenUserProfile(userName) {
+    hideModal('.messages');
+    Pages.SelectPanel('user');
+    requestUser(userName);
+    selectTabButton('#button-user-profile');
+    showPanelContent('#user-panel', '#user-panel-profile');
+  }
+  window.Usergrid.console.pageOpenUserProfile = pageOpenUserProfile;
+
+  function pageOpenUserActivities(userId) {
+    Pages.SelectPanel('user');
+    requestUser(userId);
+    selectTabButton('#button-user-activities');
+    showPanelContent('#user-panel', '#user-panel-activities');
+  }
+  window.Usergrid.console.pageOpenUserActivities = pageOpenUserActivities;
+
+  function pageSelectUserPermissions(userId) {
+    Pages.SelectPanel('user');
+    requestUser(userId);
+    selectTabButton('#button-user-permissions');
+    showPanelContent('#user-panel', '#user-panel-permissions');
+  }
+  window.Usergrid.console.pageSelectUserPermissions = pageSelectUserPermissions;
+
+  function pageSelectUserGroups(userId) {
+    Pages.SelectPanel('user');
+    requestUser(userId);
+    selectTabButton('#button-user-memberships');
+    showPanelContent('#user-panel', '#user-panel-memberships');
+  }
+
+  function pageSelectUserGraph(userId) {
+    Pages.SelectPanel('user');
+    requestUser(userId);
+    selectTabButton('#button-user-graph');
+    showPanelContent('#user-panel', '#user-panel-graph');
+  }
+
+  window.Usergrid.console.pageSelectUserGroups = pageSelectUserGroups;
+
+  function saveUserProfile(uuid){
+    var payload = Usergrid.console.ui.jsonSchemaToPayload(Usergrid.console.ui.collections.vcard_schema);
+    runAppQuery(new Usergrid.Query("PUT", "users/"+uuid, payload, null,
+      completeSave,
+      function() { alertModal("Error", "Unable to update User"); }
+    ));
+  }
+  window.Usergrid.console.saveUserProfile = saveUserProfile;
+
+  function completeSave(){
+    closeMessage = function() {
+      $('.messages').hide();
+    };
+    var closebutton = '<a  onclick="closeMessage();" class="close">&times;</a>'
+    $('.messages').text("Information Saved.").prepend(closebutton).show();
+  }
+
+  function redrawUserProfile(data, curl){
+    redrawFormPanel('user-panel-profile', 'apigee.ui.panels.user.profile.html', data);
+    showCurlCommand('user-panel-profile', curl);
+  };
+
+  function redrawUserMemberships(data, curl){
+    redrawPanel('user-panel-memberships', 'apigee.ui.panels.user.memberships.html', data);
+    showCurlCommand('user-panel-memberships', curl);
+    updateGroupsAutocomplete();
+  };
+
+  function redrawUserActivities(data, curl){
+    redrawPanel('user-panel-activities', 'apigee.ui.panels.user.activities.html', data);
+    showCurlCommand('user-panel-activities', curl);
+  };
+
+  function redrawUserGraph(data, curlFollowing, curlFollowers){
+    redrawPanel('user-panel-graph', 'apigee.ui.panels.user.graph.html', data);
+    showCurlCommand('user-panel-following', curlFollowing);
+    showCurlCommand('user-panel-followers', curlFollowers);
+    updateFollowUserAutocomplete();
+  };
+
+  function redrawUserPermissions(data, curlRoles, curlPermissions){
+    redrawPanel('user-panel-permissions', 'apigee.ui.panels.user.permissions.html', data);
+    showCurlCommand('user-panel-roles', curlRoles);
+    showCurlCommand('user-panel-permissions', curlPermissions);
+    updateRolesAutocomplete();
+    updateQueryAutocompleteCollectionsUsers();
+  };
+
+  function redrawPanel(panelDiv, panelTemplate, data){
+    $("#"+panelDiv).html("");
+    $.tmpl(panelTemplate, data).appendTo($("#"+panelDiv));
+  };
+
+  function redrawGroupForm(panelDiv, panelTemplate, data){
+    $("#"+panelDiv).html("");
+    var details = $.tmpl(panelTemplate, data);
+    var formDiv = details.find('.query-result-form');
+    $(formDiv).buildForm(Usergrid.console.ui.jsonSchemaToDForm(Usergrid.console.ui.collections.group_schema, data.entity));
+    details.appendTo($("#"+panelDiv));
+    details.find('.button').button();
+  }
+
+  function redrawFormPanel(panelDiv, panelTemplate, data){
+    $("#"+panelDiv).html("");
+    var details = $.tmpl(panelTemplate, data);
+    var formDiv = details.find('.query-result-form');
+    $(formDiv).buildForm(Usergrid.console.ui.jsonSchemaToDForm(Usergrid.console.ui.collections.vcard_schema, data.entity));
+    details.appendTo($("#"+panelDiv));
+  };
+
+  function saveUserData(){
+    Usergrid.console.ui.jsonSchemaToPayload(schema, obj);
+  }
+
+  var user_data = null;
+
+  function handleUserResponse(response) {
+    if (response.entities && (response.entities.length > 0)) {
+      var entity = response.entities[0];
+      var path = response.path || "";
+      path = "" + path.match(/[^?]*/);
+      var username = entity.username;
+      var name = entity.uuid + " : " + entity.type;
+
+      if (entity.username) {
+        name = entity.username;
+      }
+
+      if (entity.name) {
+        name = name + " : " + entity.name;
+      }
+
+      var collections = $.extend({ }, (entity.metadata || { }).collections, (entity.metadata || { }).connections);
+      if ($.isEmptyObject(collections)){
+        collections = null;
+      }
+
+      var entity_contents = $.extend( false, { }, entity);
+      delete entity_contents['metadata'];
+
+      var metadata = entity.metadata;
+      if ($.isEmptyObject(metadata)){
+        metadata = null;
+      }
+
+      var entity_path = (entity.metadata || {}).path;
+      if ($.isEmptyObject(entity_path)) {
+        entity_path = path + "/" + entity.uuid;
+      }
+
+      var picture = window.location.protocol+ "//" + window.location.host + window.location.pathname + "images/user_profile.png";
+      if (entity.picture) {
+        entity.picture = entity.picture.replace(/^http:\/\/www.gravatar/i, 'https://secure.gravatar');
+        //note: changing this to use the image on apigee.com - since the gravatar default won't work on any non-public domains such as localhost
+        //this_data.picture = this_data.picture + encodeURI("?d="+window.location.protocol+"//" + window.location.host + window.location.pathname + "images/user_profile.png");
+        picture = entity.picture + encodeURI("?d=https://apigee.com/usergrid/img/user_profile.png");
+      }
+
+      var data = {
+        entity: entity_contents,
+        picture: picture,
+        name: name,
+        username: username,
+        path: entity_path,
+        collections: collections,
+        metadata: metadata,
+        uri: (entity.metadata || { }).uri,
+        followingCurl: "",
+        followersCurl: "",
+        rolesCurl: "",
+        permissionsCurl: ""
+      }
+
+      redrawUserProfile(data, this.getCurl());
+
+      //TODO: This block and the subsequent blocks could all be methods of their own
+      runAppQuery(new Usergrid.Query("GET", 'users/' + entity.username + '/groups', null, null,
+        function(response) {
+          if (data && response.entities && (response.entities.length > 0)) {
+            data.memberships = response.entities;
+          }
+          redrawUserMemberships(data, this.getCurl());
+        },
+        function() { alertModal("Error", "Unable to retrieve user's groups."); }
+      ));
+
+      runAppQuery(new Usergrid.Query("GET", 'users/' + entity.username + '/activities', null, null,
+        function(response) {
+          if (data && response.entities && (response.entities.length > 0)) {
+            data.activities = response.entities;
+            data.curl = this.getCurl();
+            $('span[id^=activities-date-field]').each( function() {
+              var created = dateToString(parseInt($(this).html()))
+              $(this).html(created);
+            });
+          }
+          redrawUserActivities(data, this.getCurl());
+        },
+        function() { alertModal("Error", "Unable to retrieve user's activities.");}
+      ));
+
+      runAppQuery(new Usergrid.Query("GET", 'users/' + entity.username + '/roles', null, null,
+        function(response) {
+          response = escapeMe(response);
+          if (data && response.entities && (response.entities.length > 0)) {
+            data.roles = response.entities;
+          } else {
+            data.roles = null;
+          }
+          data.rolesCurl = this.getCurl();
+          //Run Permissions query after roles query has been handled
+          runAppQuery(new Usergrid.Query("GET", 'users/' + entity.username + '/permissions', null, null,
+            function(response) {
+              var permissions = {};
+              if (data && response.data && (response.data.length > 0)) {
+
+                if (response.data) {
+                  var perms = response.data;
+                  var count = 0;
+
+                  for (var i in perms) {
+                    count++;
+                    var perm = perms[i];
+                    var parts = perm.split(':');
+                    var ops_part = "";
+                    var path_part = parts[0];
+
+                    if (parts.length > 1) {
+                      ops_part = parts[0];
+                      path_part = parts[1];
+                    }
+
+                    ops_part.replace("*", "get,post,put,delete")
+                    var ops = ops_part.split(',');
+                    permissions[perm] = {ops : {}, path : path_part, perm : perm};
+
+                    for (var j in ops) {
+                      permissions[perm].ops[ops[j]] = true;
+                    }
+                  }
+
+               

<TRUNCATED>

Mime
View raw message