Permalink
Browse files

feat(project): add support for enumerations

  • Loading branch information...
jeremyvergnas committed Jan 26, 2017
1 parent 1bab30d commit 5f8766b071a2c385063dc2008919bdac03d6818f
Showing with 52 additions and 11 deletions.
  1. +3 −1 .eslintrc.json
  2. +1 −1 package.json
  3. +15 −1 src/bootstrap/filter.html
  4. +33 −8 src/filter.js
@@ -1,4 +1,6 @@
{
"extends": "./node_modules/spoonx-tools/.eslintrc.json",
"rules": {}
"rules": {
"max-lines": ["error", 400]
}
}
@@ -72,4 +72,4 @@
]
}
}
}
}
@@ -23,10 +23,17 @@
<div class="input-group">
<div class="${field.data.hasError ? 'has-error' : ''}">
<form-field
keydown.delegate="onChange($parent.$index, $index, true) & debounce:50"
if.bind="field.value.type !== 'select'"
keypress.delegate="onChange($parent.$index, $index, true) & debounce:50"
value.bind="field.data.value"
element.bind="field.value">
</form-field>
<form-field
change.delegate="onChange($parent.$index, $index, true) & debounce:50"
if.bind="field.value.type === 'select'"
value.bind="field.data.value"
element.bind="field.value">
</form-field>
</div>

<div class="input-group-btn">
@@ -58,10 +65,17 @@

<div class="col-md-4" style="margin-bottom: 15px;">
<form-field
if.bind="field.value.type !== 'select'"
keypress.delegate="onChange($parent.$index, $index, true) & debounce:500"
value.bind="field.data.between"
element.bind="field.value">
</form-field>
<form-field
change.delegate="onChange($parent.$index, $index, true) & debounce:500"
if.bind="field.value.type === 'select'"
value.bind="field.data.between"
element.bind="field.value">
</form-field>
</div>
</div>
</div>
@@ -11,8 +11,9 @@ export class Filter extends CriteriaBuilder {
@bindable showIdColumns = true;
@bindable excludeColumns;

filters = [];
fieldTypes = [];
filters = [];
fieldTypes = [];
fieldEnumerations = {};

fieldElement = {
key : 'field',
@@ -59,7 +60,11 @@ export class Filter extends CriteriaBuilder {

//eslint-disable-next-line array-callback-return
this.fieldElement.options.map(filter => {
this.fieldTypes[filter.name] = (filter.type === 'datetime') ? 'datetime-local' : filter.type;
this.fieldTypes[filter.value] = (filter.type === 'datetime') ? 'datetime-local' : filter.type;

if (filter.type === 'select') {
this.fieldEnumerations[filter.value] = filter.options || [];
}
});

// Do we need to set pre-defined values for the filter?
@@ -143,10 +148,14 @@ export class Filter extends CriteriaBuilder {

// determine the `type` of the field
let valueElement = Object.create(this.valueElement);
let fieldName = data ? data.field : this.columns[0].name;
let fieldName = data ? data.field : this.columns[0].value;

valueElement.type = this.fieldTypes[fieldName] || 'string';

if (valueElement.type === 'select') {
valueElement.options = this.fieldEnumerations[fieldName];
}

let filter = {
field : this.fieldElement,
operator: this.operatorElement,
@@ -208,9 +217,18 @@ export class Filter extends CriteriaBuilder {

this.filters[blockIndex][index].value.type = type || 'string';

if (type === 'select') {
this.filters[blockIndex][index].value.options = field.options;

this.filters[blockIndex][index].data.value = field.options.length ? field.options[0].value : undefined;
}

break;
}
}
if (typeof filterValue !== 'undefined') {
this.updateCriteria();
}
}

getEntityFields() {
@@ -222,7 +240,7 @@ export class Filter extends CriteriaBuilder {
columns = this.entity.asObject();
}

this.generateFields(columns);
this.generateFields(columns, null, metaData);

if (Object.keys(metaData.associations).length < 1) {
return;
@@ -252,7 +270,7 @@ export class Filter extends CriteriaBuilder {
}
}

generateFields(columns, entityName) {
generateFields(columns, entityName, metaData) {
let excludeColumns = (this.excludeColumns) ? this.excludeColumns.replace(/\s/g, '').split(',') : [];

if (this.showIdColumns) {
@@ -271,11 +289,18 @@ export class Filter extends CriteriaBuilder {
continue;
}

this.columns.push({
let filterColumn = {
name : columnName,
value: columnName,
type : columns[column] || 'string'
});
};

if (metaData.enumerations && column in metaData.enumerations) {
filterColumn.type = 'select';
filterColumn.options = metaData.enumerations[column];
}

this.columns.push(filterColumn);
}
}
}

0 comments on commit 5f8766b

Please sign in to comment.