Skip to content

Commit

Permalink
fix dncrews#7: date format parsed from query to builder
Browse files Browse the repository at this point in the history
  • Loading branch information
oguennec committed Jun 24, 2016
1 parent 36665b1 commit 42c4f56
Show file tree
Hide file tree
Showing 13 changed files with 235 additions and 99 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ It's still pretty early on, as it doesn't support a whole lot of use-cases, but
Notice: this plugin requires:
- the [Angular Recursion](https://github.com/marklagendijk/angular-recursion) module.
- the [Angular directives for Bootstrap](https://github.com/angular-ui/bootstrap) module to display the Calendar (ui.bootstrap.datepicker)
- the [Angular Moment](https://github.com/urish/angular-moment) module.

### Installation
First you'll need to download the [dist](https://github.com/dncrews/angular-elastic-builder/tree/master/dist) files and include this JS file to your app (don't forget to substitute `x.x.x` with the current version number), along with the RecursionHelper, if you're not already using it.
```html
<script type="text/javascript" src="angular-ui-bootstrap/ui-bootstrap-tpls.js"></script>
<script src="moment/moment.js"></script>
<script src="angular-moment/angular-moment.js"></script>
<script type="text/javascript" src="angular-recursion/angular-recursion.min.js"></script>
<script type="text/javascript" src="angular-elastic-builder/angular-elastic-builder.min.js"></script>
```
Expand Down
19 changes: 16 additions & 3 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
{
"name": "angular-elastic-builder",
"version": "1.5.1",
"description": "Angular Module for building an Elasticsearch Query",
"homepage": "https://github.com/dncrews/angular-elastic-builder",
"authors": [
"Dan Crews <[email protected]>"
{ "name": "Dan Crews", "email": "[email protected]" },
{ "name": "Olivier Guennec", "email": "[email protected]" }
],
"description": "Angular Module for building an Elasticsearch Query",
"repository": {
"type": "git",
"url": "https://github.com/dncrews/angular-elastic-builder.git"
},
"main": "dist/angular-elastic-builder.js",
"keywords": [
"elasticsearch",
Expand All @@ -17,5 +23,12 @@
"bower_components",
"test",
"tests"
]
],
"devDependencies": {
"angular-moment": "^0.10.3",
"angular-recursion": "^1.0.5",
"angular-ui-bootstrap": "^1.3.3",
"express": "^4.13.4",
"moment": "^2.13.0"
}
}
140 changes: 97 additions & 43 deletions dist/angular-elastic-builder.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/angular-elastic-builder.min.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ <h3>Query</h3>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="angular-ui-bootstrap/ui-bootstrap-tpls.js"></script>
<script src="moment/moment.js"></script>
<script src="angular-moment/angular-moment.js"></script>
<script src="angular-recursion/angular-recursion.min.js"></script>
<script src="js/angular-elastic-builder.js"></script>
<script src="js/exampleApp.js"></script>
Expand Down
2 changes: 2 additions & 0 deletions examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ app.use(express.static(__dirname));
app.use('/js', express.static(path.join(__dirname, '../dist')));
app.use('/angular-recursion', express.static(path.join(__dirname, '../node_modules/angular-recursion')));
app.use('/angular-ui-bootstrap', express.static(path.join(__dirname, '../node_modules/angular-ui-bootstrap/dist')));
app.use('/moment', express.static(path.join(__dirname, '../node_modules/moment')));
app.use('/angular-moment', express.static(path.join(__dirname, '../node_modules/angular-moment')));

app.listen(process.env.PORT || 3000, function() {
console.log('listening on ' + ( process.env.PORT || 3000));
Expand Down
12 changes: 7 additions & 5 deletions examples/js/exampleApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
'and': [
{
'term': {
'test.date': '2016-04-08T09:16:48'
"test.date": "2016-04-07T23:59:59",
"format": "yyyy-MM-ddTHH:mm:ss"
}
},
{
Expand All @@ -25,8 +26,9 @@
},
{
'range': {
'test.number': {
'lt': 850
'test.otherdate': {
"lt": "2016/04/07",
"format": "yyyy/MM/dd"
}
}
}
Expand Down Expand Up @@ -59,8 +61,8 @@
{
'range': {
'test.otherdate': {
'gte': 'now',
'lte': 'now+7d'
"gte": "now-7d",
"lte": "now"
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{
"name": "angular-elastic-builder",
"version": "1.5.0",
"version": "1.5.1",
"description": "Angular Module for building an Elasticsearch Query",
"author": "Dan Crews <[email protected]>",
"contributors": [{
"name": "Olivier Guennec",
"email": "[email protected]"
}],
"license": "MIT",
"keywords": [
"angular",
Expand All @@ -13,6 +17,7 @@
"url": "https://github.com/dncrews/angular-elastic-builder.git"
},
"devDependencies": {
"angular-moment": "^0.10.3",
"angular-recursion": "^1.0.5",
"angular-ui-bootstrap": "^1.2.5",
"del": "^1.2.1",
Expand All @@ -24,7 +29,8 @@
"gulp-eslint": "^2.0.0",
"gulp-header": "^1.2.2",
"gulp-rename": "^1.2.2",
"gulp-uglifyjs": "^0.6.1"
"gulp-uglifyjs": "^0.6.1",
"moment": "^2.13.0"
},
"scripts": {
"example": "node examples",
Expand Down
45 changes: 33 additions & 12 deletions src/directives/RuleTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

app.directive('elasticType', [

function() {
'$filter', 'moment',
function($filter, moment) {
return {
scope: {
type: '=elasticType',
Expand Down Expand Up @@ -60,18 +61,13 @@
return ~needs.indexOf(scope.rule.subType);
};

scope.today = function() {
scope.rule.date = new Date();
};
scope.today();

scope.clear = function() {
scope.rule.date = null;
};

scope.dateOptions = {
dateDisabled: disabled,
formatYear: 'yy',
formatYear: 'yyyy',
maxDate: new Date(2018, 1, 13),
minDate: new Date(),
startingDay: 1,
Expand All @@ -88,20 +84,45 @@
scope.popup1.opened = true;
};

scope.setDate = function(year, month, day) {
scope.rule.date = new Date(year, month - 1, day);

scope.formatDate = function (date, format) {
if (!!scope.rule.format) {
var inputFormat = scope.rule.format;
//moment requires yyyy and DD in upper case but ui.datepicker and Elastic require lower case
var momentFormat = inputFormat.replace(/(yyyy|yy|d|dd|ddd)/g, function(str) {
return str.toUpperCase();
});

var fDate = moment(date, momentFormat);

return fDate.toDate(momentFormat);

}
}

scope.formatDate($filter, scope.rule.value, scope.rule.format);

scope.setDate = function() {
if (!!scope.rule.format) {
scope.rule.date = scope.formatDate(scope.rule.value, scope.rule.format);
scope.date = scope.rule.date;
}
};

scope.setDate();

scope.formats = [
'yyyy-MM-ddTHH:mm:ss',
'yyyy-MM-ddTHH:mm:ssZ',
'yyyy-MM-dd',
'yyyyMMdd',
'dd-MMMM-yyyy',
'yyyy/MM/dd',
'shortDate',
];
scope.rule.dateFormat = scope.formats[0];
scope.format = scope.rule.dateFormat;

if (scope.rule.hasOwnProperty('format')) {
scope.format = scope.rule.format;
}

scope.altInputFormats = ['M!/d!/yyyy'];

Expand Down
2 changes: 2 additions & 0 deletions src/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
angular.module('angular-elastic-builder', [
'RecursionHelper',
'ui.bootstrap',
'angularMoment'
]);
//angular.module('angular-elastic-builder').constant('moment', require('moment-timezone'));

})(window.angular);
89 changes: 60 additions & 29 deletions src/services/QueryService.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@
delete obj.value;
break;
case 'term':
obj.field = Object.keys(group[key])[0];
obj.value = group[key][obj.field];
var fieldData = fieldMap[Object.keys(group[key])[0]];

if (fieldData.type === 'date') {
obj.subType = 'equals';
obj.format = group.term.format;
} else {
obj.subType = truthy ? 'equals' : 'notEquals';
obj.value = group[key][obj.field];

if (typeof obj.value === 'number') {
obj.subType = 'boolean';
}
}
break;

case 'terms':
obj.field = Object.keys(group[key])[0];
var fieldData = fieldMap[Object.keys(group[key])[0]];
Expand All @@ -88,39 +105,49 @@
var date, parts;
obj.field = Object.keys(group[key])[0];
obj.subType = Object.keys(group[key][obj.field])[0];
var fieldData = fieldMap[Object.keys(group[key])[0]];

if (angular.isNumber(group[key][obj.field][obj.subType])) {
obj.value = group[key][obj.field][obj.subType];
break;
}
if (fieldData.type === 'date') {

if (group[key][obj.field].format) {
for (var subType in group[key][obj.field]) {
if (subType != "format") {
date = group[key][obj.field][subType];
obj.value = date;
} else {
obj.format = group[key][obj.field].format;
}
}
break;

if (angular.isDefined(Object.keys(group[key][obj.field])[1])) {
date = group[key][obj.field].gte;
} else {

if (~date.indexOf('now-')) {
obj.subType = 'last';
obj.value = parseInt(date.split('now-')[1].split('d')[0]);
break;
if (angular.isDefined(Object.keys(group[key][obj.field])[1])) {
date = group[key][obj.field].gte;

if (~date.indexOf('now-')) {
obj.subType = 'last';
obj.value = parseInt(date.split('now-')[1].split('d')[0]);
break;
}

if (~date.indexOf('now')) {
obj.subType = 'next';
date = group[key][obj.field].lte;
obj.value = parseInt(date.split('now+')[1].split('d')[0]);
break;
}
}
}
}

if (~date.indexOf('now')) {
obj.subType = 'next';
date = group[key][obj.field].lte;
obj.value = parseInt(date.split('now+')[1].split('d')[0]);
if (fieldData.type === 'number') {
if (angular.isNumber(group[key][obj.field][obj.subType])) {
obj.value = group[key][obj.field][obj.subType];
break;
}

obj.subType = 'equals';
parts = date.split('T')[0].split('-');
obj.date = parts[2] + '/' + parts[1] + '/' + parts[0];
break;
}

date = group[key][obj.field][obj.subType];
parts = date.split('T')[0].split('-');
obj.date = parts[2] + '/' + parts[1] + '/' + parts[0];
break;

case 'not':
obj = parseQueryGroup(fieldMap, group[key].filter, false);
break;
Expand Down Expand Up @@ -188,21 +215,24 @@
case 'equals':
if (!angular.isDate(group.date)) return;
obj.term = {};
obj.term[fieldName] = formatDate($filter, group.date, group.dateFormat);
obj.term[fieldName] = formatDate($filter, group.date, group.format);
obj.term.format = group.format;
break;
case 'lt':
case 'lte':
if (!angular.isDate(group.date)) return;
obj.range = {};
obj.range[fieldName] = {};
obj.range[fieldName][group.subType] = formatDate($filter, group.date, group.dateFormat);
obj.range[fieldName][group.subType] = formatDate($filter, group.date, group.format);
obj.range[fieldName]["format"] = group.format;
break;
case 'gt':
case 'gte':
if (!angular.isDate(group.date)) return;
obj.range = {};
obj.range[fieldName] = {};
obj.range[fieldName][group.subType] = formatDate($filter, group.date, group.dateFormat);
obj.range[fieldName][group.subType] = formatDate($filter, group.date, group.format);
obj.range[fieldName]["format"] = group.format;
break;
case 'last':
if (!angular.isNumber(group.value)) return;
Expand Down Expand Up @@ -256,6 +286,7 @@
field: '',
subType: '',
value: '',
format: '',
},
number: {
field: '',
Expand All @@ -267,9 +298,9 @@
return angular.copy(templates[type]);
}

function formatDate($filter, date, dateFormat) {
function formatDate($filter, date, format) {
if (!angular.isDate(date)) return false;
var fDate = $filter('date')(date, dateFormat);
var fDate = $filter('date')(date, format);
return fDate;
}

Expand Down
Loading

0 comments on commit 42c4f56

Please sign in to comment.