(function () { "use strict"; var streamApp = angular.module('streamApp'); streamApp.directive('dateDirective', ['$timeout', DateDirective]); function DateDirective($timeout) { return { restrict: 'E', scope: { crud: '=', display: '=', callback: '=' }, templateUrl: '/App/Directives/dateDirective/dateDirective.html?v=6.0.0c', link: function (scope, element, attrs, tabsCtrl) { var date = scope.crud.date || new Date(); scope.date = new Date(date); let daysOfMonth = getDaysOfMonth(); changeType(); scope.update = function () { setValue(); setDisplay(); }; setDisplay(); scope.setDay = function () { setType(0); }; scope.setMonth = function () { setType(1); }; scope.setYear = function () { setType(2); }; scope.setDecade = function () { setType(3); }; function setType(dateType) { scope.crud.dateType = dateType; changeType(); setDisplay(); }; function changeType() { scope.step = 1; switch (scope.crud.dateType) { case 0: scope.min = 1; scope.max = daysOfMonth[scope.date.getMonth()]; if (scope.date.getMonth() === 1) { scope.max = isLeapYear(scope.date.getFullYear()) ? 29 : 28; } scope.minDisplay = scope.min; scope.maxDisplay = scope.max; $timeout(() => scope.value = scope.date.getDate(), 0); break; case 1: scope.min = 1; scope.max = 12; scope.minDisplay = "January"; scope.maxDisplay = "December"; $timeout(() => scope.value = scope.date.getMonth() + 1, 0); break; case 2: scope.min = getMinYear(scope.date.getFullYear()); scope.max = getMaxYear(scope.date.getFullYear()); scope.minDisplay = scope.min; scope.maxDisplay = scope.max; $timeout(() => scope.value = scope.date.getFullYear(), 0); break; default: scope.step = 10; scope.min = 1870; scope.max = 2020; scope.minDisplay = scope.min; scope.maxDisplay = scope.max; $timeout(() => scope.value = getDecade(scope.date.getFullYear()), 0); } } function setDisplay() { scope.display = labelDate(scope.date, scope.crud.dateType); scope.crud.date = scope.date; } function setValue() { switch (scope.crud.dateType) { case 0: scope.date.setDate(scope.value); break; case 1: scope.date.setMonth(scope.value - 1); break; case 2: scope.date.setYear(scope.value); break; default: let year = scope.date.getFullYear(); let lastDigit = parseInt(year.toString().substring(3)); year = parseInt(scope.value) + lastDigit; scope.date.setYear(year); } } function getMaxYear(year) { let intYear = parseInt(year); let currentYear = new Date().getFullYear(); if (intYear === currentYear) return currentYear; let modYear = intYear % 10; if (!modYear) return intYear + 10; return intYear + 10 - modYear; } function getMinYear(year) { let intYear = parseInt(year); let currentYear = new Date().getFullYear(); let modYear = intYear % 10; if (intYear === currentYear) return currentYear - 10 - modYear; return modYear ? intYear - modYear : intYear; } function isLeapYear(year) { let intYear = parseInt(year); return !(intYear % 4) && intYear % 100; } } }; } }());