Skip to content

Commit

Permalink
Imoprove user input parsing and css highlights.
Browse files Browse the repository at this point in the history
  • Loading branch information
hrishikeshs committed Jun 8, 2014
1 parent a92ceb0 commit 5a8bc1d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() + '":' );
Expand All @@ -24,7 +24,7 @@ Mongoman.DocumentsController = Ember.ArrayController.extend({
str = '{}';
}
str = str.replace(/\n/g,'');
return str;
return str.replace(/\"\"/g, '"');
},

isValidDocument: function(payload) {
Expand All @@ -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: {
Expand Down Expand Up @@ -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();
});
Expand Down
12 changes: 6 additions & 6 deletions Mongoman/app/assets/javascripts/helpers/parse_json_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ Ember.Handlebars.registerBoundHelper('parseJSONString', function (json) {

case 'string' :
if(json.match(/^ObjectId*/)) {
return '<span class="special-form" style="display:none;">"</span>' + '<span style="color: #dd1144;"> ' +
Handlebars.Utils.escapeExpression(json) + '</span>' + '<span class="special-form" style="display:none;">"</span>'
return '<span class="invisible">"</span>' + '<span class="object-id">' +
Handlebars.Utils.escapeExpression(json) + '</span>' + '<span class="invisible">"</span>';
}
else if(json.match(/^ISODate*/)) {
return '<span class="special-form" style="display:none;">"</span>' + '<span style="color: rgb(176, 96, 214);"> ' +
Handlebars.Utils.escapeExpression(json) + '</span>' + '<span class="special-form" style="display:none;">"</span>'
return '<span class="invisible">"</span>' + '<span class="iso-date"> ' +
Handlebars.Utils.escapeExpression(json) + '</span>' + '<span class="invisible">"</span>'
}

return '<span style="color: rgb(11, 168, 36);">"' +
return '<span class="character-string">"' +
Handlebars.Utils.escapeExpression(json) + '"</span>';


Expand All @@ -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 = "<br /><div class='showhide'><strong style='color: rgb(51, 15, 241);'>" + k + '</strong>' + ': ' + "<span style='overflow:auto;padding-left:10px;'>" + prettyPrint(json[k]) + "</span></div>";
var html = "<br /><div class='showhide'><strong class='field-name'>" + k + '</strong>' + ': ' + "<span style='overflow:auto;padding-left:10px;'>" + prettyPrint(json[k]) + "</span></div>";
htmlArray.push(html);
}
return '{' + "<div class='collapsible'>" + htmlArray.join() + '<br />' + '</div>}';
Expand Down
2 changes: 1 addition & 1 deletion Mongoman/app/assets/stylesheets/_pages/common_classes.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

.footer {
padding-top: 50px;
padding-bottom: 80px;
padding-bottom: 40px;
}

.invisible {
Expand Down
14 changes: 14 additions & 0 deletions Mongoman/app/assets/stylesheets/_pages/documents.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
float: left;
margin-top: 38px;
margin-left: 0px;
font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;
}

.document:hover{
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions Mongoman/app/controllers/documents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5a8bc1d

Please sign in to comment.