<commit_before>
/**
 * mongo-view.js
 **/

var App = Spine.Controller.sub({

  db_name: null,
  collection: null,
  item: null,

  // initialize app
  init: function(params) {
    var self = this;
    // setup routes
    this.routes({
      '/': function() {
        self.setupConnect()
      },
      '/database/:database': function(params) {
        self.retrieveCollections(params.database)
      },
      '/database/:database/': function(params) {
        self.retrieveCollections(params.database)
      },
      '/database/:database/collection/:collection': function(params) {
        self.retrieveCollection(params.database, params.collection);
      },
      '/database/:database/collection/:collection/': function(params) {
        self.retrieveCollection(params.database, params.collection);
      },
      '/database/:database/collection/:collection/:id': function(params) {
        self.retrieveItem(params.database, params.collection, params.id);
      },
      '/database/:database/collection/:collection/:id/': function(params) {
        self.retrieveItem(params.database, params.collection, params.id);
      },
    });
    // setup routes
    Spine.Route.setup();
    // preserve html5 history
    Spine.Route.setup({
      history: true
    });
  },

  // clear content
  clear: function() {
    $('#content').html('');
  },

  // render connect form partial
  setupConnect: function() {
    var self = this;
    self.clear();
    var json = { MONGO_DB : 'mongodb://localhost/dash' };
    $.get('/partials/connect.html', function(template) {
      var html = $.mustache(template, json);
      $('#content').append(html);
      // connection form submit
      $('#connect-btn').click(function(e) {  
        self.connect($('#connection-string').val());    
      });
    });
  },

  // connect to mongo and initialize session
  connect: function(mongoString) {
    var self = this;
    $.get('/api/connect/', $('#connection-form').serialize(), function(data) {
      var db_name = data['db_name'];
      self.db_name = db_name;
      self.navigate('/database/' + db_name + '/');
    });
  },

  // retrieve collections for db
  retrieveCollections: function(db_name) {
    var self = this;
    self.clear();
    $.get('/api/database/' + db_name, function(data) {
      var json = {
        collections : data,
        howMany     : data.length - 1,
        linker      : function() { 
          return '/database/' + db_name + '/collection/' + this.name + '/';
        }
      };
      $.get('/partials/collections.html', function(template) {
        var html = $.mustache(template, json);
        $('#content').append(html);
      });
    });
  },

  // retrieve single collection
  retrieveCollection: function(db_name, collection) {
    var self = this;
    self.clear();
    $.get('/api/database/' + db_name + '/collection/' + collection, function(data) {
      var json = {
        howMany        : data.length,
        collection     : data,
        collectionName : collection
      };
      $.get('/partials/collection.html', function(template) {
        var html = $.mustache(template, json);
        $('#content').append(html);
      });
    });
  },

  // retrieve single collection
  retrieveItem: function(db_name, collection, id) {
    var self = this;
    self.clear();
    $.get('/api/database/' + db_name + '/collection/' + collection + '/' + id, function(data) {
      var json = {
        id       : id,
        item     : data,
        collectionName : collection,
        generateRow : function() {
          var html = '';
          var props = this;
          for (key in props) {
            html += '<div class="holder">';
            html += '<div class="key left">';
            html += key
            html += '</div>'
            html += '<div class="prop right">';
            html += props[key];
            html += '</div>';
            html += '</div>';
          };
          return html;
        }
      }
      $.get('/partials/item.html', function(template) {
        var html = $.mustache(template, json);
        $('#content').append(html);
      });
    });
  }
  
});

$(document).ready(function() {
  
  // initialize app
  var app = new App({
    start : new Date().getTime()
  });

  // setup ajax indicator text
  $('body').append($('<div id="indicator"></div>'));
  $('#indicator').html('<h1>Loading...</h1>');
  $('#indicator').css({
    bottom: '150px',
    left: '20px',
    width: '200px',
    height: '50px',
    zIndex: 99,
    opacity: 1.0,
    display: 'none',
    fontSize: '13px',
    fontFamily: 'Helvetica',
    letterSpacing: '-1px',
    position: 'absolute',
  });

  // setup ajax
  $(document).ajaxStart(function() {
    $('#indicator').fadeIn(750);
  }).ajaxStop(function() {
    $('#indicator').fadeOut(750);
  }).ajaxError(function(e, jqxhr, settings, message) {
    var suppressErrorAlert = true;
    if (jqxhr.status === 599) {
      window.location = '/';
      return suppressErrorAlert;
    } else {
      window.location = '/';
      return suppressErrorAlert;
    };
  });

});

/* EOF */<commit_msg>[spine] use navTo fn to maintain state<commit_after>
/**
 * mongo-view.js
 **/

var App = Spine.Controller.sub({

  db_name: null,
  collection: null,
  item: null,

  // initialize app
  init: function(params) {
    var self = this;
    // setup routes
    this.routes({
      '/': function() {
        self.setupConnect()
      },
      '/database/:database': function(params) {
        self.retrieveCollections(params.database)
      },
      '/database/:database/': function(params) {
        self.retrieveCollections(params.database)
      },
      '/database/:database/collection/:collection': function(params) {
        self.retrieveCollection(params.database, params.collection);
      },
      '/database/:database/collection/:collection/': function(params) {
        self.retrieveCollection(params.database, params.collection);
      },
      '/database/:database/collection/:collection/:id': function(params) {
        self.retrieveItem(params.database, params.collection, params.id);
      },
      '/database/:database/collection/:collection/:id/': function(params) {
        self.retrieveItem(params.database, params.collection, params.id);
      },
    });
    // setup routes
    Spine.Route.setup();
    // preserve html5 history
    Spine.Route.setup({
      history: true
    });
    // navigate via spine
    $.fn.navTo = function(path) {
      self.navigate(path);
    }
  },

  // clear content
  clear: function() {
    $('#content').html('');
  },

  // render connect form partial
  setupConnect: function() {
    var self = this;
    self.clear();
    var json = { MONGO_DB : 'mongodb://localhost/dash' };
    $.get('/partials/connect.html', function(template) {
      var html = $.mustache(template, json);
      $('#content').append(html);
      // connection form submit
      $('#connect-btn').click(function(e) {  
        self.connect($('#connection-string').val());    
      });
    });
  },

  // connect to mongo and initialize session
  connect: function(mongoString) {
    var self = this;
    $('#indicator h1').text('connecting to database...');
    $.get('/api/connect/', $('#connection-form').serialize(), function(data) {
      var db_name = data['db_name'];
      self.db_name = db_name;
      self.navigate('/database/' + db_name + '/');
    });
  },

  // retrieve collections for db
  retrieveCollections: function(db_name) {
    var self = this;
    self.clear();
    $('#indicator h1').text('retrieving collections for ' + db_name);
    $.get('/api/database/' + db_name, function(data) {
      var json = {
        collections : data,
        howMany     : data.length - 1,
        linker      : function() { 
          return '/database/' + db_name + '/collection/' + this.name + '/';
        }
      };
      $.get('/partials/collections.html', function(template) {
        var html = $.mustache(template, json);
        $('#content').append(html);
      });
    });
  },

  // retrieve single collection
  retrieveCollection: function(db_name, collection) {
    var self = this;
    self.clear();
    $('#indicator h1').text('retrieving collection: ' + collection);
    $.get('/api/database/' + db_name + '/collection/' + collection, function(data) {
      var json = {
        howMany        : data.length,
        collection     : data,
        collectionName : collection,
        linker      : function() { 
          return '/database/' + db_name + '/collection/' + collection + '/' + this._id;
        }
      };
      $.get('/partials/collection.html', function(template) {
        var html = $.mustache(template, json);
        $('#content').append(html);
      });
    });
  },

  // retrieve single collection
  retrieveItem: function(db_name, collection, id) {
    var self = this;
    self.clear();
    $('#indicator h1').text('retrieving item: ' + id);
    $.get('/api/database/' + db_name + '/collection/' + collection + '/' + id, function(data) {
      var json = {
        id       : id,
        item     : data,
        collectionName : collection,
        generateRow : function() {
          var html = '';
          var props = this;
          for (key in props) {
            html += '<div class="holder">';
            html += '<div class="key left">';
            html += key
            html += '</div>'
            html += '<div class="prop right">';
            html += props[key];
            html += '</div>';
            html += '</div>';
          };
          return html;
        }
      }
      $.get('/partials/item.html', function(template) {
        var html = $.mustache(template, json);
        $('#content').append(html);
      });
    });
  }
  
});

$(document).ready(function() {

  // initialize app
  var app = new App({
    start : new Date().getTime()
  });

  // setup ajax indicator text
  $('body').append($('<div id="indicator"></div>'));
  $('#indicator').append('<h1></h1>');

  // setup ajax
  $(document).ajaxStart(function() {
    $('#indicator').fadeIn(750);
  }).ajaxStop(function() {
    $('#indicator').fadeOut(750);
  }).ajaxError(function(e, jqxhr, settings, message) {
    var suppressErrorAlert = true;
    if (jqxhr.status === 599) {
      window.location = '/';
      return suppressErrorAlert;
    } else {
      window.location = '/';
      return suppressErrorAlert;
    };
  });

});

/* EOF */
{'fraction_non_alphanumeric': 0.1769681822551187, 'fraction_numerical': 0.004517927520907431, 'mean_word_length': 2.2502343017806936, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3184084', 'n_tokens_mistral': 3075, 'n_tokens_neox': 3067, 'n_words': 618}