input_date.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. define([
  2. "angular",
  3. "lodash",
  4. "moment",
  5. ],function (angular, _, moment) {
  6. 'use strict';
  7. angular.
  8. module("grafana.directives").
  9. directive('inputDatetime', function () {
  10. return {
  11. restrict: 'A',
  12. require: 'ngModel',
  13. link: function ($scope, $elem, attrs, ngModel) {
  14. var format = 'YYYY-MM-DD HH:mm:ss';
  15. // $elem.after('<div class="input-datetime-format">' + format + '</div>');
  16. // What should I make with the input from the user?
  17. var fromUser = function (text) {
  18. console.log('fromUser: ' + text);
  19. return text;
  20. // if (_.isString(text)) {
  21. // }
  22. // var parsed = moment(text, format);
  23. // return parsed.isValid() ? parsed : undefined;
  24. };
  25. // How should I present the data back to the user in the input field?
  26. var toUser = function (currentValue) {
  27. if (moment.isMoment(currentValue)) {
  28. return currentValue.format(format);
  29. } else {
  30. return currentValue;
  31. }
  32. };
  33. ngModel.$parsers.push(fromUser);
  34. ngModel.$formatters.push(toUser);
  35. }
  36. };
  37. });
  38. });