Countdown.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _babelHelperVueJsxMergeProps = require('babel-helper-vue-jsx-merge-props');
  6. var _babelHelperVueJsxMergeProps2 = _interopRequireDefault(_babelHelperVueJsxMergeProps);
  7. var _extends2 = require('babel-runtime/helpers/extends');
  8. var _extends3 = _interopRequireDefault(_extends2);
  9. var _moment = require('moment');
  10. var moment = _interopRequireWildcard(_moment);
  11. var _interopDefault = require('../_util/interopDefault');
  12. var _interopDefault2 = _interopRequireDefault(_interopDefault);
  13. var _propsUtil = require('../_util/props-util');
  14. var _Statistic = require('./Statistic');
  15. var _Statistic2 = _interopRequireDefault(_Statistic);
  16. var _utils = require('./utils');
  17. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }
  18. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  19. var REFRESH_INTERVAL = 1000 / 30;
  20. function getTime(value) {
  21. return (0, _interopDefault2['default'])(moment)(value).valueOf();
  22. }
  23. exports['default'] = {
  24. name: 'AStatisticCountdown',
  25. props: (0, _propsUtil.initDefaultProps)(_Statistic.StatisticProps, {
  26. format: 'HH:mm:ss'
  27. }),
  28. created: function created() {
  29. this.countdownId = undefined;
  30. },
  31. mounted: function mounted() {
  32. this.syncTimer();
  33. },
  34. updated: function updated() {
  35. this.syncTimer();
  36. },
  37. beforeDestroy: function beforeDestroy() {
  38. this.stopTimer();
  39. },
  40. methods: {
  41. syncTimer: function syncTimer() {
  42. var value = this.$props.value;
  43. var timestamp = getTime(value);
  44. if (timestamp >= Date.now()) {
  45. this.startTimer();
  46. } else {
  47. this.stopTimer();
  48. }
  49. },
  50. startTimer: function startTimer() {
  51. var _this = this;
  52. if (this.countdownId) return;
  53. this.countdownId = window.setInterval(function () {
  54. _this.$refs.statistic.$forceUpdate();
  55. _this.syncTimer();
  56. }, REFRESH_INTERVAL);
  57. },
  58. stopTimer: function stopTimer() {
  59. var value = this.$props.value;
  60. if (this.countdownId) {
  61. clearInterval(this.countdownId);
  62. this.countdownId = undefined;
  63. var timestamp = getTime(value);
  64. if (timestamp < Date.now()) {
  65. this.$emit('finish');
  66. }
  67. }
  68. },
  69. formatCountdown: function formatCountdown(_ref) {
  70. var value = _ref.value,
  71. config = _ref.config;
  72. var format = this.$props.format;
  73. return (0, _utils.formatCountdown)(value, (0, _extends3['default'])({}, config, { format: format }));
  74. },
  75. valueRenderHtml: function valueRenderHtml(node) {
  76. return node;
  77. }
  78. },
  79. render: function render() {
  80. var h = arguments[0];
  81. return h(_Statistic2['default'], (0, _babelHelperVueJsxMergeProps2['default'])([{
  82. ref: 'statistic'
  83. }, {
  84. props: (0, _extends3['default'])({}, this.$props, {
  85. valueRender: this.valueRenderHtml,
  86. formatter: this.formatCountdown
  87. }),
  88. on: (0, _propsUtil.getListeners)(this)
  89. }]));
  90. }
  91. };