input_date.ts 915 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import moment from 'moment';
  3. export function inputDateDirective() {
  4. return {
  5. restrict: 'A',
  6. require: 'ngModel',
  7. link: function ($scope, $elem, attrs, ngModel) {
  8. var format = 'YYYY-MM-DD HH:mm:ss';
  9. var fromUser = function (text) {
  10. if (text.indexOf('now') !== -1) {
  11. return text;
  12. }
  13. var parsed;
  14. if ($scope.ctrl.isUtc) {
  15. parsed = moment.utc(text, format);
  16. } else {
  17. parsed = moment(text, format);
  18. }
  19. return parsed.isValid() ? parsed : undefined;
  20. };
  21. var toUser = function (currentValue) {
  22. if (moment.isMoment(currentValue)) {
  23. return currentValue.format(format);
  24. } else {
  25. return currentValue;
  26. }
  27. };
  28. ngModel.$parsers.push(fromUser);
  29. ngModel.$formatters.push(toUser);
  30. }
  31. };
  32. }