export function SortcodeDirectiveFactory() { return new SortcodeDirective(); } class SortcodeDirective implements ng.IDirective { restrict = 'E'; bindToController = true; controller = SortcodeController; controllerAs = 'ctrl'; template = require('./Sortcode.html'); require = '^^form'; scope = { name: '@', model: '=ngModel' } link = (scope: ng.IScope, element: JQuery, attrs: ng.IAttributes, form: ng.IFormController) => { form[attrs.name].$validators.sortcode = function (modelValue, viewValue) { form[attrs.name].$setDirty(); return /^[0-9]{6}$/.test(viewValue); } } } export class SortcodeController { model: string; part = new Array(3); update() { this.model = this.part.join(''); } keyPress(event) { // No override logic for tabs, shift, ctrl, alt if ([9, 16, 17, 18].indexOf(event.keyCode) !== -1) { } // Cleared out this part (i.e. pressed backspace) else if (event.keyCode === 8 && event.currentTarget.value.length == 0 && event.currentTarget.parentElement.previousElementSibling) event.currentTarget.parentElement.previousElementSibling.firstElementChild.focus(); // Completed this part else if (event.currentTarget.value.length >= event.currentTarget.maxLength && event.currentTarget.parentElement.nextElementSibling) event.currentTarget.parentElement.nextElementSibling.firstElementChild.focus(); } }