This repository has been archived by the owner on Mar 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
SAlertContent.js
175 lines (144 loc) · 7.25 KB
/
SAlertContent.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _inherits2 = require('babel-runtime/helpers/inherits');
var _inherits3 = _interopRequireDefault(_inherits2);
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
var _reactDom = require('react-dom');
var _reactDom2 = _interopRequireDefault(_reactDom);
var _sAlertTools = require('./s-alert-parts/s-alert-tools');
var _sAlertTools2 = _interopRequireDefault(_sAlertTools);
var _sAlertStore = require('./s-alert-parts/s-alert-store');
var _sAlertStore2 = _interopRequireDefault(_sAlertStore);
var _SAlertContentTmpl = require('./SAlertContentTmpl');
var _SAlertContentTmpl2 = _interopRequireDefault(_SAlertContentTmpl);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var SAlertContent = function (_React$Component) {
(0, _inherits3.default)(SAlertContent, _React$Component);
function SAlertContent(props) {
(0, _classCallCheck3.default)(this, SAlertContent);
return (0, _possibleConstructorReturn3.default)(this, (0, _getPrototypeOf2.default)(SAlertContent).call(this, props));
}
(0, _createClass3.default)(SAlertContent, [{
key: 'handleCloseAlert',
value: function handleCloseAlert() {
var closingTimeout = void 0;
var alertId = this.props.id;
var currentAlertElem = _reactDom2.default.findDOMNode(this);
var animationClose = function animationClose() {
currentAlertElem.style.display = 'none';
_sAlertStore2.default.dispatch({ type: 'REMOVE', data: { id: alertId } });
clearTimeout(closingTimeout);
};
if (document.hidden || document.webkitHidden || !currentAlertElem.classList.contains('s-alert-is-effect')) {
_sAlertStore2.default.dispatch({ type: 'REMOVE', data: { id: alertId } });
} else {
currentAlertElem.classList.remove('s-alert-show');
closingTimeout = setTimeout(function () {
currentAlertElem.classList.add('s-alert-hide');
}, 100);
currentAlertElem.removeEventListener('webkitAnimationEnd', animationClose, false);
currentAlertElem.removeEventListener('animationend', animationClose, false);
currentAlertElem.addEventListener('webkitAnimationEnd', animationClose, false);
currentAlertElem.addEventListener('animationend', animationClose, false);
}
// stop audio when closing
this.alertAudio && this.alertAudio.load();
}
}, {
key: 'componentWillMount',
value: function componentWillMount() {
var beep = this.props.beep;
var condition = this.props.condition;
if (beep && _lodash2.default.isString(beep)) {
this.alertAudio = new Audio(beep);
this.alertAudio.load();
this.alertAudio.play();
}
if (beep && _lodash2.default.isObject(beep) && condition === 'info') {
this.alertAudio = new Audio(beep.info);
this.alertAudio.load();
this.alertAudio.play();
}
if (beep && _lodash2.default.isObject(beep) && condition === 'error') {
this.alertAudio = new Audio(beep.error);
this.alertAudio.load();
this.alertAudio.play();
}
if (beep && _lodash2.default.isObject(beep) && condition === 'success') {
this.alertAudio = new Audio(beep.success);
this.alertAudio.load();
this.alertAudio.play();
}
if (beep && _lodash2.default.isObject(beep) && condition === 'warning') {
this.alertAudio = new Audio(beep.warning);
this.alertAudio.load();
this.alertAudio.play();
}
}
}, {
key: 'componentDidMount',
value: function componentDidMount() {
var _this2 = this;
if (typeof this.props.timeout === 'number') {
this.closeTimer = setTimeout(function () {
_this2.handleCloseAlert();
}, this.props.timeout);
}
if (this.props.onShow) {
this.props.onShow();
}
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
if (this.closeTimer) {
clearTimeout(this.closeTimer);
}
if (this.props.onClose) {
this.props.onClose();
}
}
}, {
key: 'render',
value: function render() {
var classNames = 's-alert-box s-alert-' + this.props.condition + ' s-alert-' + this.props.position + ' ' + (this.props.effect ? 's-alert-is-effect s-alert-effect-' + this.props.effect : '') + ' s-alert-show';
var message = this.props.html ? _react2.default.createElement('span', { dangerouslySetInnerHTML: { __html: this.props.message } }) : this.props.message;
var styles = this.props.boxPosition ? _sAlertTools2.default.styleToObj(this.props.boxPosition) : {};
var id = this.props.id;
var handleClose = this.handleCloseAlert.bind(this);
var contentTemplate = this.props.contentTemplate || _SAlertContentTmpl2.default;
var customFields = this.props.customFields || {};
return _react2.default.createElement(contentTemplate, { classNames: classNames, id: id, styles: styles, message: message, handleClose: handleClose, customFields: customFields });
}
}]);
return SAlertContent;
}(_react2.default.Component);
SAlertContent.propTypes = {
condition: _react2.default.PropTypes.string.isRequired,
message: _react2.default.PropTypes.string.isRequired,
position: _react2.default.PropTypes.string.isRequired,
boxPosition: _react2.default.PropTypes.string,
id: _react2.default.PropTypes.string.isRequired,
effect: _react2.default.PropTypes.string,
beep: _react2.default.PropTypes.oneOfType([_react2.default.PropTypes.string, _react2.default.PropTypes.object, _react2.default.PropTypes.bool]),
timeout: _react2.default.PropTypes.oneOfType([_react2.default.PropTypes.oneOf(['none']), _react2.default.PropTypes.number]),
html: _react2.default.PropTypes.bool,
onClose: _react2.default.PropTypes.func,
onShow: _react2.default.PropTypes.func,
customFields: _react2.default.PropTypes.object,
contentTemplate: _react2.default.PropTypes.func
};
exports.default = SAlertContent;