//====ajay
app.directive('toggle', function () {
return {
restrict: 'A',
link: function (scope, element, attrs) {
if (attrs.toggle == "tooltip") {
$(element).tooltip({
trigger: "hover"
});
}
if (attrs.toggle == "popover") {
$(element).popover({
trigger: "hover"
});
}
if (attrs.toggle == "popover1") {
$(element).popover({
trigger: "click"
});
}
}
};
});
//=====ajay
app.directive('numbersOnly', function () {
return {
require: 'ngModel',
link: function (scope, element, attr, ngModelCtrl) {
function fromUser(text) {
if (text) {
var transformedInput = text.replace(/[^0-9]/g, '');
// var transformedInput = text.replace(/([0-9]*[.])?[0-9]/g, '');
if (transformedInput !== text) {
ngModelCtrl.$setViewValue(transformedInput);
ngModelCtrl.$render();
}
return transformedInput;
}
return undefined;
}
ngModelCtrl.$parsers.push(fromUser);
}
};
});
//===
app.directive("floatingNumberOnly", function () {
return {
require: 'ngModel',
link: function (scope, ele, attr, ctrl) {
ctrl.$parsers.push(function (inputValue) {
console.log(inputValue);
var pattern = new RegExp("(^[0-9]{1,9})+(\.[0-9]{1,4})?$", "g");
if (inputValue == '')
return '';
var dotPattern = /^[.]*$/;
if (dotPattern.test(inputValue)) {
console.log("inside dot Pattern");
ctrl.$setViewValue('');
ctrl.$render();
return '';
}
var newInput = inputValue.replace(/[^0-9.]/g, '');
// newInput=inputValue.replace(/.+/g,'.');
if (newInput != inputValue) {
ctrl.$setViewValue(newInput);
ctrl.$render();
}
//******************************************
//***************Note***********************
/*** If a same function call made twice,****
*** erroneous result is to be expected ****/
//******************************************
//******************************************
var result;
var dotCount;
var newInputLength = newInput.length;
if (result = (pattern.test(newInput))) {
console.log("pattern " + result);
dotCount = newInput.split(".").length - 1; // count of dots present
if (dotCount == 0 && newInputLength > 9) { //condition to restrict "integer part" to 9 digit count
newInput = newInput.slice(0, newInputLength - 1);
ctrl.$setViewValue(newInput);
ctrl.$render();
}
} else { //pattern failed
console.log("pattern " + result);
// console.log(newInput.length);
dotCount = newInput.split(".").length - 1; // count of dots present
console.log("dotCount : " + dotCount);
if (newInputLength > 0 && dotCount > 1) { //condition to accept min of 1 dot
console.log("length>0");
newInput = newInput.slice(0, newInputLength - 1);
console.log("newInput : " + newInput);
}
if ((newInput.slice(newInput.indexOf(".") + 1).length) > 4) { //condition to restrict "fraction part" to 4 digit count only.
newInput = newInput.slice(0, newInputLength - 1);
console.log("newInput : " + newInput);
}
ctrl.$setViewValue(newInput);
ctrl.$render();
}
return newInput;
});
}
};
});
///======
app.directive('selectWatcher', function ($timeout) {
return {
link: function (scope, element, attr) {
var last = attr.last;
if (last === "true") {
$timeout(function () {
$(element).parent().selectpicker('val', '0');
$(element).parent().selectpicker('refresh');
});
}
}
};
});
// Bottstrap Tooltip & Popover
app.directive('toggle', function () {
return {
restrict: 'A',
link: function (scope, element, attrs) {
//if (attrs.toggle == "tooltip") {
// $(element).tooltip({
// trigger: "hover"
// });
// }
// if (attrs.toggle == "popover") {
// $(element).popover({
// trigger: "hover"
// });
// }
}
};
});
// scope message convert to html
app.directive('bindHtmlCompile', ['$compile', function ($compile) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
scope.$watch(function () {
return scope.$eval(attrs.bindHtmlCompile);
}, function (value) {
// Incase value is a TrustedValueHolderType, sometimes it
// needs to be explicitly called into a string in order to
// get the HTML string.
element.html(value && value.toString());
// If scope is provided use it, otherwise use parent scope
var compileScope = scope;
if (attrs.bindHtmlScope) {
compileScope = scope.$eval(attrs.bindHtmlScope);
}
$compile(element.contents())(compileScope);
});
}
};
}]);
// Only Numeric
app.directive('numericOnly', function () {
return {
require: 'ngModel',
link: function (scope, element, attrs, modelCtrl) {
modelCtrl.$parsers.push(function (inputValue) {
var transformedInput = inputValue ? inputValue.replace(/[^\d.-]/g, '') : null;
if (transformedInput != inputValue) {
modelCtrl.$setViewValue(transformedInput);
modelCtrl.$render();
}
return transformedInput;
});
}
};
});
// No Special Cracter
app.directive('noSpecialChar', function () {
return {
require: 'ngModel',
restrict: 'A',
link: function (scope, element, attrs, modelCtrl) {
modelCtrl.$parsers.push(function (inputValue) {
if (inputValue == null)
return '';
cleanInputValue = inputValue.replace(/[^\w\s]/gi, '');
if (cleanInputValue != inputValue) {
modelCtrl.$setViewValue(cleanInputValue);
modelCtrl.$render();
}
return cleanInputValue;
});
}
};
});
// Items Reverse
app.filter('reverse', function () {
return function (items) {
return items.slice().reverse();
};
});
// Number and Dot value
app.directive('validNumber', function () {
return {
require: '?ngModel',
link: function (scope, element, attrs, ngModelCtrl) {
if (!ngModelCtrl) {
return;
}
ngModelCtrl.$parsers.push(function (val) {
if (angular.isUndefined(val)) {
var val = '';
}
var clean = val.replace(/[^0-9\.]/g, '');
var decimalCheck = clean.split('.');
//if (!angular.isUndefined(decimalCheck[1])) {
// decimalCheck[1] = decimalCheck[1].slice(0, 2);
// clean = decimalCheck[0] + '.' + decimalCheck[1];
//}
if (val !== clean) {
ngModelCtrl.$setViewValue(clean);
ngModelCtrl.$render();
}
return clean;
});
element.bind('keypress', function (event) {
if (event.keyCode === 32) {
event.preventDefault();
}
});
}
};
});
// IP Address
app.directive('ipaddress', function () {
return {
require: 'ngModel',
link: function (scope, elem, attrs, ctrl) {
ctrl.$validators.ipaddress = function (modelValue, viewValue) {
if (ctrl.$isEmpty(modelValue)) {
return false;
}
var matcher;
if ((matcher = viewValue.match(/^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/)) != null) {
var i;
var previous = "255";
for (i = 1; i < 5; i++) {
var octet = parseInt(matcher[i]);
if (octet > 255) return false;
}
return true;
} else {
return false;
}
};
}
};
});
var compareTo = function () {
return {
require: "ngModel",
scope: {
otherModelValue: "=compareTo"
},
link: function (scope, element, attributes, ngModel) {
ngModel.$validators.compareTo = function (modelValue) {
return modelValue == scope.otherModelValue;
};
scope.$watch("otherModelValue", function () {
ngModel.$validate();
});
}
};
};
app.directive("compareTo", compareTo);
//Run material design lite
app.run(function ($rootScope, $timeout) {
$rootScope.$on('$viewContentLoaded', function (event) {
$timeout(function () {
componentHandler.upgradeAllRegistered();
}, 0);
});
$rootScope.render = {
header: true,
aside: true
};
});
app.directive("tooltipx", function () {
return {
link: function (scope, element, attrs) {
$(element).on("mouseover", function () {
$(this).append("<span>" + attrs.tooltipx + "</span>");
});
$(element).on("mouseout", function () {
$(this).find("span").remove();
});
scope.$on("$destroy", function () {
$(element).off("mouseover");
$(element).off("mouseout");
});
}
};
});
app.directive('convertNumber', function () {
return {
require: 'ngModel',
link: function (scope, el, attr, ctrl) {
ctrl.$parsers.push(function (value) {
return parseInt(value, 10);
});
ctrl.$formatters.push(function (value) {
return value.toString();
});
}
};
});
app.directive('fieldMatch', ["$parse", function ($parse) {
return {
require: 'ngModel',
link: function (scope, elem, attrs, ctrl) {
var me = $parse(attrs.ngModel);
var matchTo = $parse(attrs.fieldMatch);
scope.$watchGroup([me, matchTo], function (newValues, oldValues) {
ctrl.$setValidity('fieldmatch', me(scope) === matchTo(scope));
}, true);
}
};
}]);