Improve entity select component.

This commit is contained in:
Igor Kulikov 2018-07-05 12:40:58 +03:00
parent 10e588aaf4
commit b36e8a1b88
2 changed files with 18 additions and 3 deletions

View File

@ -22,14 +22,26 @@ import entitySelectTemplate from './entity-select.tpl.html';
/* eslint-enable import/no-unresolved, import/default */ /* eslint-enable import/no-unresolved, import/default */
/*@ngInject*/ /*@ngInject*/
export default function EntitySelect($compile, $templateCache) { export default function EntitySelect($compile, $templateCache, entityService) {
var linker = function (scope, element, attrs, ngModelCtrl) { var linker = function (scope, element, attrs, ngModelCtrl) {
var template = $templateCache.get(entitySelectTemplate); var template = $templateCache.get(entitySelectTemplate);
element.html(template); element.html(template);
scope.tbRequired = angular.isDefined(scope.tbRequired) ? scope.tbRequired : false; scope.tbRequired = angular.isDefined(scope.tbRequired) ? scope.tbRequired : false;
scope.model = {};
var entityTypes = entityService.prepareAllowedEntityTypesList(scope.allowedEntityTypes, scope.useAliasEntityTypes);
if (entityTypes.length === 1) {
scope.displayEntityTypeSelect = false;
scope.defaultEntityType = entityTypes[0];
} else {
scope.displayEntityTypeSelect = true;
}
scope.model = {
entityType: scope.defaultEntityType
};
scope.updateView = function () { scope.updateView = function () {
if (!scope.disabled) { if (!scope.disabled) {
@ -54,7 +66,7 @@ export default function EntitySelect($compile, $templateCache) {
scope.model.entityType = value.entityType; scope.model.entityType = value.entityType;
scope.model.entityId = value.id; scope.model.entityId = value.id;
} else { } else {
scope.model.entityType = null; scope.model.entityType = scope.defaultEntityType;
scope.model.entityId = null; scope.model.entityId = null;
} }
initWatchers(); initWatchers();
@ -106,6 +118,7 @@ export default function EntitySelect($compile, $templateCache) {
theForm: '=?', theForm: '=?',
tbRequired: '=?', tbRequired: '=?',
disabled:'=ngDisabled', disabled:'=ngDisabled',
allowedEntityTypes: "=?",
useAliasEntityTypes: "=?" useAliasEntityTypes: "=?"
} }
}; };

View File

@ -17,10 +17,12 @@
--> -->
<div layout='row' class="tb-entity-select"> <div layout='row' class="tb-entity-select">
<tb-entity-type-select style="min-width: 100px;" <tb-entity-type-select style="min-width: 100px;"
ng-if="displayEntityTypeSelect"
the-form="theForm" the-form="theForm"
ng-disabled="disabled" ng-disabled="disabled"
tb-required="tbRequired" tb-required="tbRequired"
use-alias-entity-types="useAliasEntityTypes" use-alias-entity-types="useAliasEntityTypes"
allowed-entity-types="allowedEntityTypes"
ng-model="model.entityType"> ng-model="model.entityType">
</tb-entity-type-select> </tb-entity-type-select>
<tb-entity-autocomplete flex ng-if="model.entityType" <tb-entity-autocomplete flex ng-if="model.entityType"