/* @ author Kamal*/
'use strict';
commonsApp
.directive(
'olrSelectBox',
[
'$document','$timeout',
function($document,$timeout) {
return {
restrict : 'E',
scope : {
name:'@',
labelName : '@',
defaultName : '@',
listValue : '=',
required : '@',
selectedValue : '@',
infocontrol : '='
},
template :'
{{labelName}}'+
'
'+
'
{{errorMessage}} ',
link : function(scope, elem, attrs) {
scope.valid = false;
var initializing = true;
scope.$watch('selectedValue', function () {
if (initializing) {
$timeout(function() { initializing = false; },1000);
} else {
if (scope.selectedValue != ""){
scope.updateValidity();
}
}
});
scope.updateValidity = function() {
angular.forEach(scope.listValue, function (value, key) {
if(value == scope.selectedValue) {
scope.selectedIndex = key;
}
})
validateField();
if (scope.infocontrol
&& scope.infocontrol.publish) {
var publishValue = scope.selectedValue;
if(!angular.isDefined(scope.selectedValue)) {
publishValue = scope.defaultName;
}
scope.infocontrol.publish(
scope.name,
publishValue,
scope.valid,scope.selectedIndex);
}
return scope.valid;
};
if (scope.infocontrol) {
scope.infocontrol.validateField = scope.updateValidity;
scope.infocontrol.resetDefault = function(defaultVal){
scope.selectedValue = defaultVal;
}
}
if (scope.selectedValue == undefined) {
scope.selectedValue = scope.defaultName;
}
var validateField = function() {
if (scope.required) {
if (scope.selectedValue && scope.selectedValue != "") {
scope.valid = true;
scope.errorMessage = "";
} else {
scope.valid = false;
scope.errorMessage = scope.name
+ " is a required field.";
}
}
}
}
}
} ]);