text: ﻿// This directive is used for formatting a datetime of a model.
  var angModule = angular.module("datetime", []);
  
  angModule.directive('moDateInput', function ($window) {
      return {
          require: '^ngModel',
          restrict: 'A',
          link: function (scope, elm, attrs, ctrl) {
              var moment = $window.moment;
              var dateFormat = attrs.moMediumDate;
              attrs.$observe('moDateInput', function (newValue) {
                  if (dateFormat == newValue || !ctrl.$modelValue) return;
                  dateFormat = newValue;
                  ctrl.$modelValue = new Date(ctrl.$setViewValue);
              });
  
              ctrl.$formatters.unshift(function (modelValue) {
                  scope = scope;
                  if (!dateFormat || !modelValue) return "";
                  var retVal = moment(modelValue).format(dateFormat);
                  return retVal;
              });
  
              ctrl.$parsers.unshift(function (viewValue) {
                  scope = scope;
                  var date = moment(viewValue, dateFormat);
                  return (date && date.isValid() && date.year() > 1950) ? date.toDate() : "";
              });
          }
      };
  });
  
  angModule.directive('moChangeProxy', function ($parse) {
      return {
          require: '^ngModel',
          restrict: 'A',
          link: function (scope, elm, attrs, ctrl) {
              var proxyExp = attrs.moChangeProxy;
              var modelExp = attrs.ngModel;
              scope.$watch(proxyExp, function (nVal) {
                  if (nVal != ctrl.$modelValue)
                      $parse(modelExp).assign(scope, nVal);
              });
              elm.bind('blur', function () {
                  var proxyVal = scope.$eval(proxyExp);
                  if (ctrl.$modelValue != proxyVal) {
                      scope.$apply(function () {
                          $parse(proxyExp).assign(scope, ctrl.$modelValue);
                      });
                  }
              });
          }
      };
  });