"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _slider = require("antd/es/slider");

var _slider2 = _interopRequireDefault(_slider);

var _icon = require("antd/es/icon");

var _icon2 = _interopRequireDefault(_icon);

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

require("antd/es/slider/style");

require("antd/es/icon/style");

var _react = require("react");

var _react2 = _interopRequireDefault(_react);

var _dayjs = require("dayjs");

var _dayjs2 = _interopRequireDefault(_dayjs);

var _duration = require("dayjs/plugin/duration");

var _duration2 = _interopRequireDefault(_duration);

var _utc = require("dayjs/plugin/utc");

var _utc2 = _interopRequireDefault(_utc);

require("./index.less");

var _loading = require("../../assert/loading.gif");

var _loading2 = _interopRequireDefault(_loading);

var _btn_audio_play = require("../../assert/btn_audio_play.png");

var _btn_audio_play2 = _interopRequireDefault(_btn_audio_play);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

_dayjs2.default.extend(_duration2.default);
_dayjs2.default.extend(_utc2.default);

var AudioPlayer = function (_React$Component) {
  _inherits(AudioPlayer, _React$Component);

  function AudioPlayer(props) {
    _classCallCheck(this, AudioPlayer);

    var _this = _possibleConstructorReturn(this, (AudioPlayer.__proto__ || Object.getPrototypeOf(AudioPlayer)).call(this, props));

    _this.state = {
      duration: 0,
      currentDuration: 0,
      isPlaying: false,
      isLoading: false
    };
    return _this;
  }

  _createClass(AudioPlayer, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this2 = this;

      if (this.player) {
        var src = this.props.src;

        this.player.oncanplay = function (event) {
          return _this2.setState({ duration: event.target.duration });
        };
        this.player.onended = function () {
          return _this2.setState({ isPlaying: false, currentDuration: 0 });
        };
        this.player.onpause = function () {
          return _this2.setState({ isPlaying: false, isLoading: false });
        };
        this.player.onplay = function () {
          return _this2.setState({ isPlaying: false, isLoading: true });
        };
        this.player.onplaying = function () {
          return _this2.setState({ isPlaying: true, isLoading: false });
        };
        this.player.ontimeupdate = function (event) {
          return _this2.setState({ currentDuration: event.target.currentTime });
        };
        this.player.src = src;
      }
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      var _this3 = this;

      if (this.props.src !== prevProps.src) {
        this.player.oncanplay = function (event) {
          return _this3.setState({ duration: event.target.duration });
        };
        this.player.onended = function () {
          return _this3.setState({ isPlaying: false, currentDuration: 0 });
        };
        this.player.onpause = function () {
          return _this3.setState({ isPlaying: false, isLoading: false });
        };
        this.player.onplay = function () {
          return _this3.setState({ isPlaying: false, isLoading: true });
        };
        this.player.onplaying = function () {
          return _this3.setState({ isPlaying: true, isLoading: false });
        };
        this.player.ontimeupdate = function (event) {
          return _this3.setState({ currentDuration: event.target.currentTime });
        };
        this.player.src = this.props.src;
      }
    }
  }, {
    key: "clickPlayOrPause",
    value: function clickPlayOrPause(isPlaying) {
      if (!this.player) {
        return;
      }
      if (isPlaying) {
        this.player.pause();
      } else {
        // 如果是播放，先暂停其他的播放
        var playerList = document.getElementsByTagName("audio");
        if (playerList) {
          Array.from(playerList).forEach(function (player) {
            if (!player.paused) {
              player.pause();
            }
          });
        }
        this.player.play();
      }
    }
  }, {
    key: "getPlayIcon",
    value: function getPlayIcon() {
      var _state = this.state,
          isPlaying = _state.isPlaying,
          isLoading = _state.isLoading;

      var playIconElem = void 0;

      if (isLoading) {
        playIconElem = _react2.default.createElement("img", { className: "icon-loading", src: _loading2.default, alt: "iconLoading" });
      } else if (isPlaying) {
        playIconElem = _react2.default.createElement(_icon2.default, { className: "pause", type: "pause" });
      } else {
        playIconElem = _react2.default.createElement("img", { className: "icon-loading", src: _btn_audio_play2.default, alt: "iconPlaying" });
      }
      return playIconElem;
    }
  }, {
    key: "render",
    value: function render() {
      var _this4 = this;

      var _state2 = this.state,
          currentDuration = _state2.currentDuration,
          isPlaying = _state2.isPlaying,
          duration = _state2.duration;

      return _react2.default.createElement(
        "div",
        { className: "comment-item-speak-audio-container" },
        _react2.default.createElement("audio", {
          ref: function ref(_ref) {
            _this4.player = _ref;
          },
          style: { display: "none" }
        }),
        _react2.default.createElement(
          "span",
          {
            className: "icon",
            onClick: function onClick() {
              _this4.clickPlayOrPause(isPlaying);
            }
          },
          this.getPlayIcon()
        ),
        _react2.default.createElement(_slider2.default, {
          step: 0.001,
          className: "slider",
          tooltipVisible: false,
          value: currentDuration,
          max: duration,
          onChange: function onChange(value) {
            if (_this4.player) {
              _this4.player.currentTime = value;
            }
          }
        }),
        _react2.default.createElement(
          "span",
          { className: "time" },
          _dayjs2.default.utc(_dayjs2.default.duration(currentDuration, "seconds").asMilliseconds()).format("mm:ss"),
          "/",
          _dayjs2.default.utc(_dayjs2.default.duration(duration, "seconds").asMilliseconds()).format("mm:ss")
        )
      );
    }
  }]);

  return AudioPlayer;
}(_react2.default.Component);

exports.default = AudioPlayer;
//# sourceMappingURL=index.js.map