/* @ author Kamal*/
'use strict';
commonsApp.directive(
'olrInputBox',
[
'$document','$filter','$timeout',
function($document,$filter,$timeout) {
var uniqueId = 1;
return {
restrict : "E",
scope : {
name: '@',
labelName : "@",
onFocusText:"@",// Prefill text when the text box gains focus.
autoFocus:"@",
inputValue : '@',
type : "=",
maxLength:'@',
showCurrency:'@',
format:'@',
undisclosed : '@',
undisclosedModel : '@',
undisclosedText : '@',
validators : '=',
infocontrol : '=',
placeholder : '@'
},
template : function(element, attrs) {
if(angular.isDefined(attrs.type) && attrs.type == "number"){
return '
';
} else {
return '';
}
},
link : function(scope, elem, attrs) {
var item = 'item' + uniqueId++;
elem.find('[type=checkbox]').attr('id',
item);
elem.find('label').attr('for', item);
scope.checked = scope.undisclosedModel == "true";
scope.valid = true;
scope.currentValue = scope.inputValue;
var numberDenomination = function() {
scope.formattedValue = scope.inputValue.replace(/\D/g,'');
scope.currentValue = $filter('formatAndSplitDenomination')(scope.formattedValue);
}
scope.setFocusText = function(){
if(scope.onFocusText){
if(scope.inputValue == ''){
scope.inputValue = scope.onFocusText;
setTimeout(function(){
$(elem.find('.input-field')).setCursorPosition(0);
}, 1);
}
}
}
scope.removeFocusText = function(){
if(scope.onFocusText == scope.inputValue){
scope.inputValue = '';
}
}
var formatValueOnStart = function() {
if(scope.inputValue != "") {
scope.formattedValue = scope.inputValue.replace(/\D/g,'');
scope.currentValue = $filter('formatAndSplitDenomination')(scope.formattedValue);
scope.inputValue = ($filter('number')(scope.inputValue));
}
}
$timeout(function(){
if(scope.format) {
formatValueOnStart();
}
});
scope.updateValidity = function() {
if(scope.format) {
numberDenomination();
}
validateField();
if (scope.infocontrol
&& scope.infocontrol.publish) {
var publishValue = "";
if(scope.format) {
publishValue = scope.inputValue.replace(/\D/g,'');
} else {
publishValue = scope.inputValue
}
scope.infocontrol.publish(
scope.name,
publishValue,
scope.valid);
}
return scope.valid;
}
var validateField = function() {
scope.valid = true;
if (scope.checked) {
scope.valid = true;
scope.errorMessage = "";
scope.inputValue = "";
scope.currentValue = "";
} else {
var validators = scope.validators;
var fieldValue = scope.inputValue;
var keepGoing = true;
for (var i = 0; i < validators.length; i++) {
var validator = validators[i];
if (keepGoing) {
if (validator == "REQUIRED") {
if (fieldValue
&& fieldValue != "") {
scope.valid = true;
scope.errorMessage = "";
} else {
scope.valid = false;
scope.errorMessage = scope.name
+ " is a required field.";
keepGoing = false;
}
}
if (validator == "NUMBER") {
if(fieldValue == ''){
scope.valid = true;
scope.errorMessage = "";
continue;
}
var reg = /^\d*$/;
if(scope.format) {
fieldValue = fieldValue.replace(/[^\d|\-+|\.+]/g, '');
scope.inputValue = ($filter('number')(fieldValue));
}
if (reg.test(fieldValue)) {
if(parseInt(fieldValue) > 0){
scope.valid = true;
scope.errorMessage = "";
}else{
scope.valid = false;
scope.errorMessage = "Please enter a valid number";
keepGoing = false;
}
}
else {
scope.valid = false;
scope.errorMessage = "Please enter whole numbers.";
keepGoing = false;
}
}
if(validator == "URL") {
if(fieldValue == ''){
scope.valid = true;
scope.errorMessage = "";
continue;
}
var reg = /(https?:\/\/(?:www\.|(?!www))?[^\s\.]+\.[^\s]{2,}|(www\.)?[^\s]+\.[^\s]{2,})/;
if(reg.test(fieldValue)) {
scope.valid = true;
scope.errorMessage = "";
} else {
scope.valid = false;
scope.errorMessage = "URL is invalid. (e.g. www.owler.com)";
keepGoing = false;
}
}
if(validator == "PHONE") {
if(fieldValue == ''){
scope.valid = true;
scope.errorMessage = "";
continue;
}
var reg = /^([0-9-()]{0,20})$/;
if(reg.test(fieldValue)) {
scope.valid = true;
scope.errorMessage = "";
} else {
scope.valid = false;
scope.errorMessage = "Phone number is invalid.";
keepGoing = false;
}
}
if(validator == "EMAIL"){
var isValid = validateEmail(fieldValue)
if(isValid){
isValid = validateDomain(fieldValue)
if(!isValid){
scope.valid = false;
scope.errorMessage = 'The domain is invalid. Please use a valid email address'
keepGoing = false;
}else{
scope.valid = true;
scope.errorMessage = undefined;
continue;
}
}else{
scope.valid = false;
scope.errorMessage ='Please enter a valid email address'
keepGoing = false;
}
keepGoing = true;
}
if(typeof validator === 'function'){
if(fieldValue == ''){
scope.valid = true;
scope.errorMessage = "";
continue;
}else{
var result = validator(fieldValue);
if(result.isValid){
scope.valid = true;
scope.errorMessage = "";
}else{
scope.valid = false;
scope.errorMessage = result.errMsg;
keepGoing = false;
}
}
}
}
}
}
}
if (scope.infocontrol) {
scope.infocontrol.validateField = scope.updateValidity;
}
}
}
} ]);