{"size": 6318, "ext": "js", "max_stars_repo_path": "source/MyExpenses.Web_Experience2/client/app/modules/directives/mc-datepicker.js", "max_stars_repo_name": "lalaithan/developer-immersion-data", "max_stars_repo_stars_event_min_datetime": "2017-05-24T22:55:14.000Z", "max_stars_repo_stars_event_max_datetime": "2019-03-31T00:56:05.000Z", "max_issues_repo_path": "source/MyExpenses.Web_Experience2/client/app/modules/directives/mc-datepicker.js", "max_issues_repo_name": "lalaithan/developer-immersion-data", "max_issues_repo_issues_event_min_datetime": "2017-05-20T16:10:54.000Z", "max_issues_repo_issues_event_max_datetime": "2018-09-30T18:04:46.000Z", "max_forks_repo_path": "source/MyExpenses.Web_Experience2/client/app/modules/directives/mc-datepicker.js", "max_forks_repo_name": "lalaithan/developer-immersion-data", "max_forks_repo_forks_event_min_datetime": "2017-05-20T12:42:19.000Z", "max_forks_repo_forks_event_max_datetime": "2019-03-26T16:38:16.000Z"}
{"max_stars_count": 82, "max_issues_count": 7, "max_forks_count": 55, "avg_line_length": 46.8, "max_line_length": 117, "alphanum_fraction": 0.4599556822}
﻿(function () {
    'use strict';
    angular.module('expensesApp').directive('mcDatepicker', [
        'settings',
        datePicker
    ]);

    function datePicker(settings) {

        function firstIsLessOrEqualSecond(first, second) {
            var firstDate = new Date(first);
            var secondDate = new Date(second);
            return firstDate <= secondDate;
        }

        function firstIsEqualOrGreaterSecond(first, second) {
            var firstDate = new Date(first);
            var secondDate = new Date(second);
            return firstDate >= secondDate;
        }

        return {
            restrict: 'A',
            scope: {
                dateValue: '=ngModel',
                form: '=mcForm',
                lessOrEqual: '@mcIsLessOrEqual',
                whenLessOrEqual: '=mcIsLessOrEqualWhen',
                equalOrGreater: '@mcIsEqualOrGreater',
                whenEqualOrGreater: '=mcIsEqualOrGreaterWhen'               
            },
            require: 'ngModel',
            link: function (scope, element, attrs, controller) {
                element.datepicker({
                    format: 'mm/dd/yyyy',
                    language: 'en',
                    autoclose: true
                });

                element.datepicker('setDate', scope.dateValue);

                element.on('changeDate', function (ev) {
                    scope.dateValue = moment(ev.date).format(settings.dateFormat);
                });

                scope.$on('$destroy', function () {
                    var datepicker = element.data('datepicker');
                    if (datepicker) {
                        datepicker.picker.remove();
                        element.data('datepicker', null);
                    }
                });

                if (scope.whenLessOrEqual !== undefined) {
                    scope.$watch('whenLessOrEqual', function (newValue, oldValue) {
                        var lessOrEqualValue = scope.form[scope.lessOrEqual];
                        var shouldReset = newValue !== oldValue && newValue === false;
                        if (shouldReset) {
                            controller.$setValidity('lessOrEqual', true);
                            lessOrEqualValue.$setValidity('equalOrGreater', true);
                        }
                        var shouldReevaluate = newValue !== oldValue && newValue === true;
                        if (shouldReevaluate) {
                            if (firstIsLessOrEqualSecond(controller.$viewValue, lessOrEqualValue.$viewValue)) {
                                // it is valid
                                controller.$setValidity('lessOrEqual', true);
                                lessOrEqualValue.$setValidity('equalOrGreater', true);
                            } else {
                                // it is invalid, return undefined (no model update)
                                controller.$setValidity('lessOrEqual', false);
                                lessOrEqualValue.$setValidity('equalOrGreater', false);
                            }
                        }
                    }, true);
                }

                if (scope.whenEqualOrGreater !== undefined) {
                    scope.$watch('whenEqualOrGreater', function (newValue, oldValue) {
                        var equalOrGreaterValue = scope.form[scope.equalOrGreater];
                        var shouldReset = newValue !== oldValue && newValue === false;
                        if (shouldReset) {
                            controller.$setValidity('equalOrGreater', true);
                            equalOrGreaterValue.$setValidity('lessOrEqual', true);
                        }
                        var shouldReevaluate = newValue !== oldValue && newValue === true;
                        if (shouldReevaluate) {

                            if (firstIsEqualOrGreaterSecond(controller.$viewValue, equalOrGreaterValue.$viewValue)) {
                                // it is valid
                                controller.$setValidity('equalOrGreater', true);
                                equalOrGreaterValue.$setValidity('lessOrEqual', true);
                            } else {
                                // it is invalid, return undefined (no model update)
                                controller.$setValidity('equalOrGreater', false);
                                equalOrGreaterValue.$setValidity('lessOrEqual', false);
                            }
                        }
                    }, true);
                }

                controller.$parsers.unshift(function (viewValue) {
                    if (scope.whenLessOrEqual) {
                        var lessOrEqualValue = scope.form[scope.lessOrEqual];
                        if (firstIsLessOrEqualSecond(viewValue, lessOrEqualValue.$viewValue)) {
                            // it is valid
                            controller.$setValidity('lessOrEqual', true);
                            lessOrEqualValue.$setValidity('equalOrGreater', true);
                        } else {
                            // it is invalid, return undefined (no model update)
                            controller.$setValidity('lessOrEqual', false);
                            lessOrEqualValue.$setValidity('equalOrGreater', false);
                        }
                    }

                    if (scope.whenEqualOrGreater) {
                        var equalOrGreaterValue = scope.form[scope.equalOrGreater];
                        if (firstIsEqualOrGreaterSecond(viewValue, equalOrGreaterValue.$viewValue)) {
                            // it is valid
                            controller.$setValidity('equalOrGreater', true);
                            equalOrGreaterValue.$setValidity('lessOrEqual', true);
                        } else {
                            // it is invalid, return undefined (no model update)
                            controller.$setValidity('equalOrGreater', false);
                            equalOrGreaterValue.$setValidity('lessOrEqual', false);
                        }
                    }

                    return viewValue;
                });

                element.val(scope.dateValue);
            }
        };
    }
}());