Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"lodash": "^3.9.3",
"moment": "^2.10.3",
"mongodb-extended-json": "^1.3.1",
"mongodb-language-model": "^0.2.0",
"mongodb-schema": "^3.0.0",
"numeral": "^1.5.3",
"octicons": "https://github.com/github/octicons/archive/v2.2.0.tar.gz",
Expand Down
25 changes: 21 additions & 4 deletions src/refine-view/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
var AmpersandView = require('ampersand-view');
var EJSON = require('mongodb-extended-json');
var _ = require('lodash');
var debug = require('debug')('scout:refine-view:index');
var Query = require('mongodb-language-model').Query;

module.exports = AmpersandView.extend({
template: require('./index.jade'),
Expand Down Expand Up @@ -36,22 +39,36 @@ module.exports = AmpersandView.extend({
'input [data-hook=refine-input]': 'inputChanged',
'submit form': 'submit'
},
_cleanupInput: function (input) {
var output = input;
// accept whitespace-only input as empty query
if (_.trim(output) === '') {
output = '{}';
}
// replace single quotes with double quotes
output = output.replace(/'/g, '"');
// wrap field names in double quotes
output = output.replace(/([{,])\s*([^,{\s\'"]+)\s*:/g, ' $1 "$2" : ');
return output;
},
inputChanged: function() {
// validate user input on the fly
var queryStr = this.queryByHook('refine-input').value;
var queryStr = this._cleanupInput(this.queryByHook('refine-input').value);
try {
EJSON.parse(queryStr);
// is it valid eJSON?
var queryObj = EJSON.parse(queryStr);
// is it a valid parsable Query according to the language?
var languageObj = new Query(queryObj, {parse: true});
} catch (e) {
this.valid = false;
return;
}
this.valid = true;
},
buttonClicked: function() {
var queryStr = this.queryByHook('refine-input').value;
var queryStr = this._cleanupInput(this.queryByHook('refine-input').value);
var queryObj = EJSON.parse(queryStr);
this.model.query = queryObj;

// Modifying the query will reset field-list#schema and because we're using
// good ampersand, outgoing views will be removed for us automatically.
},
Expand Down