Closes #350: better relations widget

This commit is contained in:
bgamard 2020-03-07 17:46:40 +01:00
parent 8a85830bd3
commit 3b5c27096b
4 changed files with 25 additions and 20 deletions

View File

@ -9,6 +9,7 @@ angular.module('docs').directive('selectRelation', function() {
templateUrl: 'partial/docs/directive.selectrelation.html',
replace: true,
scope: {
id: '=',
relations: '=',
ref: '@',
ngDisabled: '='
@ -18,21 +19,12 @@ angular.module('docs').directive('selectRelation', function() {
* Add a relation.
*/
$scope.addRelation = function($item) {
// Does the new relation is already in the model
var duplicate = _.find($scope.relations, function(relation) {
if ($item.id === relation.id) {
return relation;
}
});
// Add the new relation
if (!duplicate) {
$scope.relations.push({
id: $item.id,
title: $item.title,
source: true
});
}
$scope.input = '';
};
@ -42,11 +34,11 @@ angular.module('docs').directive('selectRelation', function() {
$scope.deleteRelation = function(deleteRelation) {
$scope.relations = _.reject($scope.relations, function(relation) {
return relation.id === deleteRelation.id;
})
});
};
/**
* Returns a promise for typeahead title.
* Returns a promise for typeahead document.
*/
$scope.getDocumentTypeahead = function($viewValue) {
var deferred = $q.defer();
@ -57,7 +49,15 @@ angular.module('docs').directive('selectRelation', function() {
asc: true,
search: $viewValue
}).then(function(data) {
deferred.resolve(data.documents);
deferred.resolve(_.reject(data.documents, function(document) {
var duplicate = _.find($scope.relations, function(relation) {
if (document.id === relation.id) {
return relation;
}
});
return document.id === $scope.id || duplicate;
}));
});
return deferred.promise;
};

View File

@ -0,0 +1,4 @@
<a>
<div ng-bind-html="match.label.title | uibTypeaheadHighlight: query"></div>
<div class="small">{{ match.label.create_date | date: $root.dateFormat }}</div>
</a>

View File

@ -7,6 +7,7 @@
</li>
</ul>
<input class="form-control" type="text" id="{{ ref }}" ng-attr-placeholder="{{ 'directive.selectrelation.typeahead' | translate }}" ng-model="input" ng-disabled="ngDisabled"
autocomplete="off" uib-typeahead="document.title for document in getDocumentTypeahead($viewValue)"
autocomplete="off" uib-typeahead="document for document in getDocumentTypeahead($viewValue)"
typeahead-template-url="partial/docs/directive.selectionrelation.typeahead.html"
typeahead-wait-ms="200" typeahead-on-select="addRelation($item)" />
</div>

View File

@ -191,7 +191,7 @@
<div class="form-group">
<label class="col-sm-2 control-label" for="inputRelations">{{ 'document.relations' | translate }}</label>
<div class="col-sm-10">
<select-relation relations="document.relations" ref="inputRelations" ng-disabled="fileIsUploading"></select-relation>
<select-relation id="document.id" relations="document.relations" ref="inputRelations" ng-disabled="fileIsUploading"></select-relation>
</div>
</div>
</div>