-
Notifications
You must be signed in to change notification settings - Fork 726
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #714 from marmelab/embedded_list
[RFR] Introducing the embedded_list field type
- Loading branch information
Showing
23 changed files
with
775 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/javascripts/ng-admin/Crud/column/maEmbeddedListColumn.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import Entry from 'admin-config/lib/Entry'; | ||
|
||
function sorter(sortField, sortDir) { | ||
return (entry1, entry2) => { | ||
// use < and > instead of substraction to sort strings properly | ||
const sortFactor = sortDir === 'DESC' ? -1 : 1; | ||
if (entry1.values[sortField] > entry2.values[sortField]) return sortFactor; | ||
if (entry1.values[sortField] < entry2.values[sortField]) return -1 * sortFactor; | ||
return 0; | ||
}; | ||
} | ||
|
||
function maEmbeddedListColumn(NgAdminConfiguration) { | ||
const application = NgAdminConfiguration(); // jshint ignore:line | ||
return { | ||
scope: { | ||
'field': '&', | ||
'value': '&', | ||
'datastore': '&' | ||
}, | ||
restrict: 'E', | ||
link: { | ||
pre: function(scope) { | ||
const field = scope.field(); | ||
const targetEntity = field.targetEntity(); | ||
const targetEntityName = targetEntity.name(); | ||
const targetFields = field.targetFields(); | ||
const sortField = field.sortField(); | ||
const sortDir = field.sortDir(); | ||
var filterFunc; | ||
if (field.permanentFilters()) { | ||
const filters = field.permanentFilters(); | ||
const filterKeys = Object.keys(filters); | ||
filterFunc = (entry) => filterKeys.reduce((isFiltered, key) => isFiltered && entry.values[key] === filters[key], true); | ||
} else { | ||
filterFunc = () => true; | ||
} | ||
let entries = Entry | ||
.createArrayFromRest(scope.value() || [], targetFields, targetEntityName, targetEntity.identifier().name()) | ||
.sort(sorter(sortField, sortDir)) | ||
.filter(filterFunc); | ||
if (!targetEntityName) { | ||
let index = 0; | ||
entries = entries.map(e => { | ||
e._identifierValue = index++; | ||
return e; | ||
}); | ||
} | ||
scope.field = field; | ||
scope.targetFields = targetFields; | ||
scope.entries = entries; | ||
scope.entity = targetEntityName ? application.getEntity(targetEntityName) : targetEntity; | ||
scope.sortField = sortField; | ||
scope.sortDir = sortDir; | ||
scope.sort = field => { | ||
let sortDir = 'ASC'; | ||
const sortField = field.name(); | ||
if (scope.sortField === sortField) { | ||
// inverse sort dir | ||
sortDir = scope.sortDir === 'ASC' ? 'DESC' : 'ASC'; | ||
} | ||
scope.entries = scope.entries.sort(sorter(sortField, sortDir)); | ||
scope.sortField = sortField; | ||
scope.sortDir = sortDir; | ||
}; | ||
} | ||
}, | ||
template: ` | ||
<ma-datagrid ng-if="::entries.length > 0" | ||
entries="entries" | ||
fields="::targetFields" | ||
list-actions="::field.listActions()" | ||
entity="::entity" | ||
datastore="::datastore()" | ||
sort-field="{{ sortField }}" | ||
sort-dir="{{ sortDir }}" | ||
sort="::sort"> | ||
</ma-datagrid>` | ||
}; | ||
} | ||
|
||
maEmbeddedListColumn.$inject = ['NgAdminConfiguration']; | ||
|
||
module.exports = maEmbeddedListColumn; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/javascripts/ng-admin/Crud/field/maEmbeddedListField.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import Entry from 'admin-config/lib/Entry'; | ||
|
||
function maEmbeddedListField() { | ||
return { | ||
scope: { | ||
'field': '&', | ||
'value': '=', | ||
'datastore': '&' | ||
}, | ||
restrict: 'E', | ||
link: { | ||
pre: function(scope) { | ||
const field = scope.field(); | ||
const targetEntity = field.targetEntity(); | ||
const targetEntityName = targetEntity.name(); | ||
const targetFields = field.targetFields(); | ||
const sortField = field.sortField(); | ||
const sortDir = field.sortDir() === 'DESC' ? -1 : 1; | ||
var filterFunc; | ||
if (field.permanentFilters()) { | ||
const filters = field.permanentFilters(); | ||
const filterKeys = Object.keys(filters); | ||
filterFunc = (entry) => { | ||
return filterKeys.reduce((isFiltered, key) => isFiltered && entry.values[key] === filters[key], true); | ||
}; | ||
} else { | ||
filterFunc = () => true; | ||
} | ||
scope.fields = targetFields; | ||
scope.entries = Entry | ||
.createArrayFromRest(scope.value || [], targetFields, targetEntityName, targetEntity.identifier().name()) | ||
.sort((entry1, entry2) => { | ||
// use < and > instead of substraction to sort strings properly | ||
if (entry1.values[sortField] > entry2.values[sortField]) return sortDir; | ||
if (entry1.values[sortField] < entry2.values[sortField]) return -1 * sortDir; | ||
return 0; | ||
}) | ||
.filter(filterFunc); | ||
scope.addNew = () => scope.entries.push(Entry.createForFields(targetFields)); | ||
scope.remove = entry => { | ||
scope.entries = scope.entries.filter(e => e !== entry); | ||
}; | ||
scope.$watch('entries', (newEntries, oldEntries) => { | ||
if (newEntries === oldEntries) return; | ||
scope.value = newEntries.map(e => e.transformToRest(targetFields)); | ||
}, true); | ||
} | ||
}, | ||
template: ` | ||
<div class="row"><div class="col-sm-12"> | ||
<ng-form ng-repeat="entry in entries track by $index" class="subentry" name="subform_{{$index}}" ng-init="formName = 'subform_' + $index"> | ||
<div class="remove_button_container"> | ||
<a class="btn btn-default btn-sm" ng-click="remove(entry)"><span class="glyphicon glyphicon-minus-sign" aria-hidden="true"></span> Remove</a> | ||
</div> | ||
<div class="form-field form-group" ng-repeat="field in ::fields track by $index"> | ||
<ma-field field="::field" value="entry.values[field.name()]" entry="entry" entity="::entity" form="formName" datastore="::datastore()"></ma-field> | ||
</div> | ||
<hr/> | ||
</ng-form> | ||
<div class="form-group"> | ||
<div class="col-sm-offset-2 col-sm-10"> | ||
<a class="btn btn-default btn-sm" ng-click="addNew()"><span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span> Add new {{ field().name() }}</a> | ||
</div> | ||
</div> | ||
</div></div>` | ||
}; | ||
} | ||
|
||
maEmbeddedListField.$inject = []; | ||
|
||
module.exports = maEmbeddedListField; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.