/**
* @ngdoc fbFormService
* @name fasit.services.#fbFormService
* @fbFormService
*
* @description
* Service som hanterar gemensamma attribut på fbForm-directives
*
*/
angular.module('fbMocks')
.factory('fbFormService', ['logService', function (logService: fb.ILogService): fb.IFbFormService {
var classes = {
formGroup: function (scope) {
return { 'has-feedback': (scope.disable || (scope.model && scope.model.required)) };
},
element: function (scope) {
return { 'has-value': scope.hasValue(), 'miss-value': scope.missValue(), 'invalid-value': scope.invalidValue() };
}
};
var functions = {
preCompile: function (element: fb.IRootElementService, attrs: any) {
if (!angular.isDefined(attrs.model)) {
var errorString = 'Missing required parameter for fbForm'
if (!angular.isDefined(attrs.model)) {
errorString += '\nmodel: ' + attrs.model;
}
console.error(errorString);
logService.log(errorString);
return;
}
},
postCompile: function (element: fb.IRootElementService, attrs: any) {
if (angular.isDefined(attrs.actionType)) {
element.find('.fb-form-group').attr('fb-action', '');
element.find('.fb-form-group').attr(attrs.actionType, 'model');
}
element.find('.fb-form-group:not(.no-icon)').
append('');
element.find('.fb-form-group:not(.no-icon)').
append('');
element.find('.fb-form-group').attr('tooltip', "{{disable?disableReason:(model.invalidStr?(model.invalidStr|translate):'')}}");
element.find('.fb-form-control').attr('ng-class', "{'has-value': hasValue(), 'miss-value': missValue(), 'invalid-value': invalidValue() }");
},
preLink: function (scope: fb.IFbFormCommonScope, element: ng.IAugmentedJQuery, attrs: any) {
scope.missValue = function () {
var isNull = scope.model ? (scope.model.value === null) : true;
var isEmpty = scope.model ? (scope.model.value === '') : true;
var missValue = isNull || isEmpty;
return missValue;
};
scope.hasValue = function () {
return !scope.missValue();
};
scope.invalidValue = function () {
return scope.model && !scope.model.valid;
};
scope.$on('$destroy', function () {
element.unbind();
element.remove();
});
scope.getClass = function (element) {
return classes[element](scope);
};
},
postLink: function (scope: fb.IFbFormCommonScope, element: ng.IAugmentedJQuery, attrs: any) {
}
};
var getScopeObject = function (directiveExclusiveScope) {
var scope = directiveExclusiveScope;
if (!scope.model) { scope.model = '='; }
if (!scope.disable) { scope.disable = '=' }
if (!scope.disableReason) { scope.disableReason = '@'; }
if (!scope.actionType) { scope.actionType = '@'; }
return scope;
}
return {
functions: functions,
getScopeObject: getScopeObject
};
}]);