entity-select.directive.js
Home
/
ui /
src /
app /
entity /
entity-select.directive.js
import './entity-select.scss';
import entitySelectTemplate from './entity-select.tpl.html';
export default function EntitySelect($compile, $templateCache) {
var linker = function (scope, element, attrs, ngModelCtrl) {
var template = $templateCache.get(entitySelectTemplate);
element.html(template);
scope.tbRequired = angular.isDefined(scope.tbRequired) ? scope.tbRequired : false;
scope.model = {};
scope.updateView = function () {
if (!scope.disabled) {
var value = ngModelCtrl.$viewValue;
if (scope.model && scope.model.entityType && scope.model.entityId) {
if (!value) {
value = {};
}
value.entityType = scope.model.entityType;
value.id = scope.model.entityId;
ngModelCtrl.$setViewValue(value);
} else {
ngModelCtrl.$setViewValue(null);
}
}
}
ngModelCtrl.$render = function () {
if (ngModelCtrl.$viewValue) {
var value = ngModelCtrl.$viewValue;
scope.model.entityType = value.entityType;
scope.model.entityId = value.id;
} else {
scope.model.entityType = null;
scope.model.entityId = null;
}
}
scope.$watch('model.entityType', function () {
scope.updateView();
});
scope.$watch('model.entityId', function () {
scope.updateView();
});
scope.$watch('disabled', function () {
scope.updateView();
});
$compile(element.contents())(scope);
}
return {
restrict: "E",
require: "^ngModel",
link: linker,
scope: {
theForm: '=?',
tbRequired: '=?',
disabled:'=ngDisabled'
}
};
}