diff --git a/Mongoman/app/assets/javascripts/controllers/documents_controller.js b/Mongoman/app/assets/javascripts/controllers/documents_controller.js index 3dbb532..81bcbe3 100644 --- a/Mongoman/app/assets/javascripts/controllers/documents_controller.js +++ b/Mongoman/app/assets/javascripts/controllers/documents_controller.js @@ -14,7 +14,7 @@ Mongoman.DocumentsController = Ember.ArrayController.extend({ jsonifyText: function(str) { - var keys = str.match(/[a-zA-Z0-9_\"\s\']+:\s+/g); + var keys = str.match(/['"]?[a-zA-Z0-9_\"\s\']+['"]?:\s+/g); if (keys) { for (var i = 0, j = keys.length; i < j; i++) { str = str.replace(keys[i],'"' + keys[i].split(':')[0].trim() + '":' ); @@ -24,7 +24,7 @@ Mongoman.DocumentsController = Ember.ArrayController.extend({ str = '{}'; } str = str.replace(/\n/g,''); - return str; + return str.replace(/\"\"/g, '"'); }, isValidDocument: function(payload) { @@ -38,6 +38,23 @@ Mongoman.DocumentsController = Ember.ArrayController.extend({ } }, + substituteFields: function(text) { + + var lines = text.split('\n'); + var substitution = ""; + lines.forEach(function(line) { + line = line.match(/ObjectId\(['"]?[0-9a-fA-F]{24}['"]?\)/) || + line.match(/ISODate\(['"]?(\d{4}\-\d\d\-\d\d([tT][\d:\.]*)?)([zZ]|([+\-])(\d\d):?(\d\d))?['"]?\)/); + if(line) { + singleQuotedline = line[0].replace(/\"/g,"'"); + substitution = '"' + singleQuotedline + '"'; + text = text.split(line[0]).join(substitution); + } + }); + + return text; + }, + actions: { @@ -83,14 +100,20 @@ Mongoman.DocumentsController = Ember.ArrayController.extend({ modal: true, buttons: { "Add Document" : function() { - var json = self.isValidDocument(self.newDocument); - if (json && (self.newDocument !== "")) { + + var valid = self.isValidDocument(self.newDocument); + + if (valid && self.newDocument) { + var url = '/documents/?'; + var json = self.substituteFields(self.newDocument.trim()); + json = json.split('\n').map(function(token) { return token.trim(); }).join(''); + Mongoman.PostRequest.post(url , { database_name : self.get('database_name'), collection_name: self.get('collection_name') - }, 'POST', self.jsonifyText(self.newDocument.trim())) + }, 'POST', self.jsonifyText(json)) .then(function() { // self.reload(); }); diff --git a/Mongoman/app/assets/javascripts/helpers/parse_json_helper.js b/Mongoman/app/assets/javascripts/helpers/parse_json_helper.js index 24eb6fb..425df53 100644 --- a/Mongoman/app/assets/javascripts/helpers/parse_json_helper.js +++ b/Mongoman/app/assets/javascripts/helpers/parse_json_helper.js @@ -8,15 +8,15 @@ Ember.Handlebars.registerBoundHelper('parseJSONString', function (json) { case 'string' : if(json.match(/^ObjectId*/)) { - return '' + ' ' + - Handlebars.Utils.escapeExpression(json) + '' + '' + return '' + '' + + Handlebars.Utils.escapeExpression(json) + '' + ''; } else if(json.match(/^ISODate*/)) { - return '' + ' ' + - Handlebars.Utils.escapeExpression(json) + '' + '' + return '' + ' ' + + Handlebars.Utils.escapeExpression(json) + '' + '' } - return '"' + + return '"' + Handlebars.Utils.escapeExpression(json) + '"'; @@ -38,7 +38,7 @@ Ember.Handlebars.registerBoundHelper('parseJSONString', function (json) { var htmlArray = []; for(var i = 0, len = keys.length; i < len; i++) { var k = keys[i]; - var html = "
" + k + '' + ': ' + "" + prettyPrint(json[k]) + "
"; + var html = "
" + k + '' + ': ' + "" + prettyPrint(json[k]) + "
"; htmlArray.push(html); } return '{' + "
" + htmlArray.join() + '
' + '
}'; diff --git a/Mongoman/app/assets/stylesheets/_pages/common_classes.scss b/Mongoman/app/assets/stylesheets/_pages/common_classes.scss index c98666c..ab24eea 100644 --- a/Mongoman/app/assets/stylesheets/_pages/common_classes.scss +++ b/Mongoman/app/assets/stylesheets/_pages/common_classes.scss @@ -16,7 +16,7 @@ .footer { padding-top: 50px; - padding-bottom: 80px; + padding-bottom: 40px; } .invisible { diff --git a/Mongoman/app/assets/stylesheets/_pages/documents.scss b/Mongoman/app/assets/stylesheets/_pages/documents.scss index 46918e3..13c17e9 100644 --- a/Mongoman/app/assets/stylesheets/_pages/documents.scss +++ b/Mongoman/app/assets/stylesheets/_pages/documents.scss @@ -16,6 +16,7 @@ float: left; margin-top: 38px; margin-left: 0px; + font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; } .document:hover{ @@ -39,6 +40,19 @@ } +.object-id { + color: #dd1144; +} +.iso-date { + color: rgb(176, 96, 214); +} +.character-string { + color: rgb(11, 168, 36); +} +.field-name { + color: rgb(51, 15, 241); +} + .edit-save-button { background-color: #f9f9f9; diff --git a/Mongoman/app/controllers/documents_controller.rb b/Mongoman/app/controllers/documents_controller.rb index 0bf955f..62dfc3e 100644 --- a/Mongoman/app/controllers/documents_controller.rb +++ b/Mongoman/app/controllers/documents_controller.rb @@ -75,7 +75,7 @@ def BsonFieldsToString(x) end def create - json_string = request.body().to_a.join() + json_string = request.body().to_a[0] json_object = JSON json_string new_document = construct_document(json_object) begin @@ -122,7 +122,7 @@ def substitutions(value) object_id = value.match /[0-9a-fA-F]{24}/ substitution = BSON::ObjectId(object_id.to_s) elsif value.match 'ISODate' - substitution = Time.parse value.match(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z/)[0] + substitution = Time.parse value.match(/(\d{4}\-\d\d\-\d\d([tT][\d:\.]*)?)([zZ]|([+\-])(\d\d):?(\d\d))?/)[0] end end substitution