(function() { "use strict"; angular .module("agora.plus.formComponent") .directive("inputFormComp", customInput) .directive("apInclude", apInclude) .directive("apIf", apIf) .directive("ignoreMouseWheel", function() { return { restrict: "A", link: function(scope, element) { element.on("mousewheel, wheel", function() { element[0].blur(); }); } }; }); // .directive("apInputValidations", apInputValidations); customInput.$inject = ["constant", "$translate", "$parse"]; function customInput(constant, $translate, $parse) { return { restrict: "E", require: ['^form', '?ngModel'], scope: { apType: "@", apName: "@", apPlaceholder: "@?", apHasValidation: "=", apAllowSpaces: "=", apLabel: "@?", apValidations: "=?", apClass: "@?", apAriaLabel: "@?", apIsColumnInput: "=", apMinLength: "@?", apMaxLength: "@?", apHasIcon: "=?", apIcon: "@?", apValue: "@?", apFunc: "&?", apInputId: "@?", apBlur: "&?", apChange: "&?", apAllowNegative: "=", apParser: "@?", apIsTextArea: "=", apIsDisabled: "=?", apManageFocus: "=?", apOnlyInteger: "=?", hasEmailValidation: "=?", apOnlyText: "=?", apAllowSpecialChars: "=?" }, controllerAs: "custominput", bindToController: true, controller: function() { var vm = this; vm.imgPath = constant.imgPath; vm.required = false; vm.disabled = false; vm.maxlength = undefined; vm.minlength = undefined; vm.validations = []; vm.inputColumn = PCK_GLOBAL_VARIABLES.input_form_comp_column; vm.inputRow = PCK_GLOBAL_VARIABLES.input_form_comp_row; vm.inputTextArea = PCK_GLOBAL_VARIABLES.input_form_comp_textarea; vm.manageFocus; }, link: function (scope, elem, attrs, ctrls) { elem.bind("keydown", function (event) { if (scope.custominput.apType === "number") { if (scope.custominput.apOnlyInteger && (event.keyCode == 110 || event.keyCode == 190 || event.keyCode == 188 || event.keyCode == 107)) { event.preventDefault(); } //prevent from adding negative numbers if ((scope.custominput.generic == 0 || typeof (scope.custominput.generic) == "undefined" || isNaN(scope.custominput.generic)) && event.keyCode == 40) { event.preventDefault(); }else if((event.keyCode == 189 && scope.custominput.apAllowNegative != "undefined" && !scope.custominput.apAllowNegative) || (event.keyCode == 109 && scope.custominput.apAllowNegative != "undefined" && !scope.custominput.apAllowNegative)){ event.preventDefault(); } //prevent special characters if it is number if (event.keyCode == 69 || event.keyCode == 187 ) { event.preventDefault(); } } if (scope.custominput.apType === "text" && scope.custominput.apOnlyText) { if (!isNaN(parseInt(event.key))){ event.preventDefault(); } } //Dont Let Input have Spaces if (scope.custominput.apAllowSpaces === false && event.keyCode === 32) { event.preventDefault(); } //IF THE INPUT ALLOWS SPECIAL CHARACTERS if (typeof (scope.custominput.apAllowSpecialChars) === "undefined") { scope.custominput.apAllowSpecialChars = true }; if (!scope.custominput.apAllowSpecialChars && ( event.key === "." || event.key === "/" || event.key === "*" //|| event.key === "-" || event.key === "+" || event.key === "," || event.key === "<" || event.key === ">" || event.key === "?" || event.key === "!" || event.key === "^" || event.key === "~" || event.key === ":" || event.key === ";" || event.key === "_" || event.key === "\\" || event.key === "«" || event.key === "»" || event.key === "=" || event.key === ")" || event.key === "(" || event.key === "}" || event.key === "{" || event.key === "]" || event.key === "[" || event.key === "|" || event.key === "#" || event.key === "$" || event.key === "%" || event.key === "&" || event.key === '"' || (event.keyCode === 48 && event.altKey) || (event.keyCode === 57 && event.altKey) || (event.keyCode === 56 && event.altKey) || (event.keyCode === 55 && event.altKey) || (event.keyCode === 50 && event.altKey) || (event.keyCode === 51 && event.altKey) //"£" // || (event.keyCode === 50 && event.shiftKey) //""" || (event.keyCode === 52 && event.altKey) //"§" || (event.keyCode === 53 && event.altKey) //"⿬" || event.keyCode === 222 // "º" || (event.altKey && event.keyCode === 69) //prevent keys from side numbers keyboard || event.keyCode === 107 || event.keyCode === 106 || event.keyCode === 111 || event.keyCode === 110 ) ) { event.preventDefault(); } }); //IF THE INPUT IS A NUMBER REMOVE THE SCROLL ON MOUSEWHEEL if (scope.custominput.apType === "number") { elem.bind('mousewheel', function (event) { elem.blur(); }); } var form = ctrls[0], ngModel = ctrls[1], inputTypes = ["text", "password", "color", "email", "number", "range", "tel", "url"]; scope.custominput.form = form; if (scope.custominput.apParser) { var apParser = loadApParser(scope.custominput.apParser); // console.log(apParser); ngModel.$parsers.push(apParser); ngModel.$formatters.push(apParser); apParser($parse(attrs.ngModel)(scope)); } // Strategy so that we can access form properties try { // Check if input type is valid if (inputTypes.indexOf(scope.custominput.apType) < 0) { throw("Invalid input type provided!"); } if (typeof(scope.custominput.apIsColumnInput) === "undefined") { throw("Mandatory attribute 'ap-is-column-input' missing!"); } else if (typeof(scope.custominput.apIsColumnInput) !== "boolean") { throw("Invalid type for 'ap-is-column-input'. A boolean is required."); } if (typeof(scope.custominput.apHasValidation) !== "boolean") { throw("'ap-has-validation' must be a boolean"); } if (typeof(scope.custominput.apHasIcon) !== "undefined" && typeof(scope.custominput.apHasIcon) !== "boolean") { throw("'ap-has-icon' must be a boolean"); } // Check if input should have validations if so, validations should be provided /*if (scope.custominput.apHasValidation) { if (typeof(scope.custominput.apValidations) !== "undefined" && Object.keys(scope.custominput.apValidations).length !== 0 && scope.custominput.apValidations instanceof Object) { angular.forEach(scope.custominput.apValidations, function(value, key) { scope.custominput.validations.push({ngMessage: key,ngMessageValue: $translate.instant(value)}); }); } else { throw("Mandatory validation messages not found!"); } }*/ // check if type is date, if so aria-label string must be provided if (scope.custominput.apType === "date") { if (typeof(scope.custominput.apAriaLabel) === "undefined" || scope.custominput.apAriaLabel === "") { throw("Input type date detected. Mandatory aria label missing!!"); } } if (scope.custominput.apHasIcon) { if (typeof(scope.custominput.apIcon) === "undefined" ) { throw("An icon must be provided"); } } scope.custominput.apPlaceholder = (typeof(scope.custominput.apPlaceholder) !== "undefined" && scope.custominput.apPlaceholder !== "") ? scope.custominput.apPlaceholder : ""; scope.custominput.apLabel = (typeof(scope.custominput.apLabel) !== "undefined" && scope.custominput.apLabel !== "") ? scope.custominput.apLabel : ""; scope.custominput.apClass = (typeof(scope.custominput.apClass) !== "undefined" && scope.custominput.apClass !== "") ? scope.custominput.apClass : ""; scope.custominput.apInputId = (typeof(scope.custominput.apInputId) !== "undefined" && scope.custominput.apInputId !== "") ? scope.custominput.apInputId : ""; scope.custominput.apBlur = (typeof (scope.custominput.apBlur) !== "undefined") ? scope.custominput.apBlur : function () { }; scope.custominput.apChange = (typeof (scope.custominput.apChange) !== "undefined") ? scope.custominput.apChange : function () { }; if (typeof(attrs.required) !== "undefined") { scope.custominput.required = true; } if (typeof(attrs.apStep) !== "undefined") { scope.custominput.step = attrs.apStep; } else { scope.custominput.step = "any"; } if (typeof(attrs.apMaxLength) !== "undefined") { scope.custominput.maxlength = scope.custominput.apMaxLength; } if (typeof(attrs.apMinLength) !== "undefined") { scope.custominput.minlength = scope.custominput.apMinLength; } } catch(error) { console.log(error); } scope.$watch(function() { return scope.custominput.apHasValidation || scope.custominput.hasEmailValidation; }, function(newValue, oldValue) { if (typeof(newValue) === 'boolean' && newValue) { if (typeof(scope.custominput.apValidations) !== "undefined" && Object.keys(scope.custominput.apValidations).length !== 0 && scope.custominput.apValidations instanceof Object) { angular.forEach(scope.custominput.apValidations, function(value, key) { var params = {}; switch(key) { case "maxlength": params = {'numchars': scope.custominput.maxlength}; break; // A translation needs to be added!! There's no dynamic translation for this case!!! /*case "minlength": params = {'numchars': scope.custominput.minlength}; break; */ } scope.custominput.validations.push({ngMessage: key,ngMessageValue: $translate.instant(value, params)}); }); } } else { scope.custominput.validations.length = 0; } }); scope.$watch(function() { return scope.custominput.apIsDisabled; }, function(newValue, oldValue) { if (typeof(newValue) === "boolean" || typeof(newValue) === "number") { scope.custominput.disabled = scope.custominput.apIsDisabled; } else { scope.custominput.disabled = false; } }); scope.$watch("custominput.generic", function() { if (typeof(scope.custominput.generic) !== "undefined") { // Set the model value if one is provided, If type date convert the string to an object date if (scope.custominput.apType === "number"){ ngModel.$setViewValue(parseFloat(scope.custominput.generic)); } else { ngModel.$setViewValue(scope.custominput.generic); } } }); scope.$watch(function() { return form[scope.custominput.apName]; }, function(newValue, oldValue) { if (typeof(newValue) !== "undefined") { scope.custominput.field = form[scope.custominput.apName]; } }); scope.$watch("custominput.manageFocus", function(newValue, oldValue) { if (typeof(newValue) !== "undefined" && typeof(newValue) === "boolean") { if (scope.custominput.hasOwnProperty("apManageFocus")) { scope.custominput.apManageFocus = newValue; } } }); scope.$watch(function() { return ngModel.$viewValue; }, function(newValue, oldValue) { if (typeof(newValue) !== "undefined") { // Set the model value if one is provided, If type date convert the string to an object date if (scope.custominput.apType === "number"){ scope.custominput.generic = parseFloat(newValue); } else { scope.custominput.generic = newValue; } //scope.custominput.generic = newValue; ngModel.$setViewValue(newValue); ngModel.$render(); } }); function loadApParser(parser) { if (parser == "capitalize") { return function (inputValue) { if (inputValue === undefined) { inputValue = ""; } var capitalized = inputValue.toUpperCase(); if(capitalized !== inputValue) { ngModel.$setViewValue(capitalized); ngModel.$render(); } return capitalized; }; } else if (parser == "capitalizeFirst") { return function (inputValue) { if (inputValue === undefined) { inputValue = ""; } var capitalized = inputValue.charAt(0).toUpperCase() + inputValue.substring(1); if(capitalized !== inputValue) { ngModel.$setViewValue(capitalized); ngModel.$render(); } return capitalized; }; } else if (parser === "removeNegative") { return function (inputValue) { var inputVal = 0; if (inputValue < 0) { inputVal = -inputValue; ngModel.$setViewValue(inputVal); ngModel.$render(); } else { inputVal = inputValue; } return inputVal; } } } }, templateUrl: PCK_GLOBAL_VARIABLES.input_form_comp, replace: true, }; } apInclude.$inject = ["$parse", "$compile", "$http"]; function apInclude($parse, $compile, $http) { return { restrict: "A", scope:false, link: function (scope, elem, attrs) { $http.get($parse(attrs.apInclude)(scope)) .success(function(templateContent) { elem.replaceWith($compile(templateContent)(scope)); }); } }; } apIf.$inject = ["ngIfDirective", "$parse", "$compile", "$http"]; function apIf(ngIfDirective, $parse, $compile, $http) { var ngIf = ngIfDirective[0]; return { transclude: ngIf.transclude, priority: ngIf.priority, terminal: ngIf.terminal, restrict: ngIf.restrict, link: function (scope,element,attr) { var value = attr["apIf"]; var yourCustomValue = scope.$eval(value); attr.ngIf = function() { return yourCustomValue; }; ngIf.link.apply(ngIf, arguments); } // restrict: "A", // scope:false, // transclude: true, // link: function (scope, elem, attrs) { // // console.log(scope); // $http.get($parse(attrs.apInclude)(scope)) // .success(function(templateContent) { // elem.replaceWith($compile(templateContent)(scope)); // }); // } } } apInputValidations.$inject = ["$translate"]; function apInputValidations($translate) { // console.log("yey"); return { restrict: "A", require: "ngModel", scope: { apInputValidations: "=" }, link: linking } linking.$inject = ["scope", "elem", "attrs", "ngModelController"]; function linking(scope, elem, attrs, ngModelController) { // console.log(ngModelController.$name); // console.log(ngModelController.$error); // console.log(scope.apInputValidations); ngModelController.apInputValidations = {}; for (var i = 0; i < scope.apInputValidations.length; i++) { ngModelController.apInputValidations[scope.apInputValidations[i].ngMessage] = $translate.instant(scope.apInputValidations[i].ngMessageValue); // console.log(scope.apInputValidations[i]); } // for (var err in scope.apInputValidations) { // if (scope.apInputValidations.hasOwnProperty(err)) { // console.log(err); // } // } // for (var i = 0; i < scope.apInputValidations.length; i++) { // ngModelController.apInputValidations[] // } } } }());