@barteh/core
Version:
a common library for barteh based projects
304 lines (256 loc) • 11.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = withMessage;
var _react = _interopRequireDefault(require("react"));
var _Dialog = _interopRequireDefault(require("@material-ui/core/Dialog"));
var _Button = _interopRequireDefault(require("@material-ui/core/Button"));
var _Snackbar = _interopRequireDefault(require("@material-ui/core/Snackbar"));
var _DialogActions = _interopRequireDefault(require("@material-ui/core/DialogActions"));
var _DialogContent = _interopRequireDefault(require("@material-ui/core/DialogContent"));
var _DialogContentText = _interopRequireDefault(require("@material-ui/core/DialogContentText"));
var _DialogTitle = _interopRequireDefault(require("@material-ui/core/DialogTitle"));
var _Typography = _interopRequireDefault(require("@material-ui/core/Typography"));
var _Divider = _interopRequireDefault(require("@material-ui/core/Divider"));
var _Slide = _interopRequireDefault(require("@material-ui/core/Slide"));
var _withMobileDialog = _interopRequireDefault(require("@material-ui/core/withMobileDialog"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function Transition(props) {
return _react.default.createElement(_Slide.default, _extends({
direction: "up"
}, props));
}
function withMessage(Comp) {
return (0, _withMobileDialog.default)()(
/*#__PURE__*/
function (_React$PureComponent) {
_inherits(HOC, _React$PureComponent);
function HOC(props) {
var _this;
_classCallCheck(this, HOC);
_this = _possibleConstructorReturn(this, _getPrototypeOf(HOC).call(this, props));
_this.state = {
notifyOpen: false,
confirmOpen: false,
confirmObject: {}
};
_this.messageService = {};
_this.messageService.confirm = _this.confirm.bind(_assertThisInitialized(_this));
_this.messageService.notify = _this.notify.bind(_assertThisInitialized(_this));
_this.handleCloseConfirm = _this.handleCloseConfirm.bind(_assertThisInitialized(_this));
_this.handleCloseNotify = _this.handleCloseNotify.bind(_assertThisInitialized(_this));
_this.handleOk = _this.handleOk.bind(_assertThisInitialized(_this));
_this.handleCancel = _this.handleCancel.bind(_assertThisInitialized(_this));
return _this;
}
_createClass(HOC, [{
key: "confirm",
value: function confirm(inobj) {
var _this2 = this;
var obj = typeof inobj === "string" ? {
body: inobj
} : inobj;
obj.type = obj.type || "primary"; // secondary, error
obj.okTitle = obj.okTitle || "قبول";
obj.cancelTitle = obj.cancelTitle || "لغو";
obj.size = obj.size || "normal"; //responsice | fullscreen
this.setState({
confirmOpen: true,
confirmObject: obj
});
return new Promise(function (res, rej) {
_this2.confirmPromisResolve = res;
_this2.confirmPromisReject = rej;
});
}
}, {
key: "notify",
value: function notify(message, type) {
var intype = type || "primary"; // secondary, error
this.setState({
notifyOpen: true,
notifyMessage: message,
notifyType: intype
});
}
}, {
key: "handleOk",
value: function handleOk() {
var _this3 = this;
var onOk = this.state.confirmObject.onOk;
if (onOk) {
if (onOk instanceof Promise) {
onOk.then(function (a) {
_this3.confirmPromisResolve(a);
}).catch(function (e) {
_this3.confirmPromisReject(e);
}); //return onOk;
}
var result = onOk();
if (result instanceof Promise) {
result.then(function (a) {
return _this3.confirmPromisResolve(a);
}).catch(function (e) {
_this3.confirmPromisReject(e);
});
}
if (result instanceof Promise) {
result.then(function (a) {
_this3.confirmPromisResolve(a);
}).catch(function (e) {
_this3.confirmPromisReject(e);
});
} else {
this.confirmPromisResolve(result);
}
} else {
this.confirmPromisResolve(true);
}
this.handleCloseConfirm();
}
}, {
key: "handleCancel",
value: function handleCancel() {
var onCancel = this.state.confirmObject.onCancel;
if (onCancel) {
this.state.confirmObject.onCancel();
} else {
this.confirmPromisResolve(false);
}
this.handleCloseConfirm();
}
}, {
key: "handleCloseConfirm",
value: function handleCloseConfirm() {
this.setState({
confirmOpen: false
});
}
}, {
key: "handleCloseNotify",
value: function handleCloseNotify() {
this.setState({
notifyOpen: false
});
}
}, {
key: "headerColor",
value: function headerColor(type) {
var ret = null;
switch (type) {
case type = "primary":
ret = {
backgroundColor: "green",
color: "white",
minWidth: "40vh"
};
break;
case type = "secondary":
ret = {
backgroundColor: "#5e0ce7",
color: "white",
minWidth: "40vh"
};
break;
case type = "error":
ret = {
backgroundColor: "red",
color: "white",
minWidth: "40vh"
};
break;
default:
ret = {
backgroundColor: "#880000",
color: "white"
};
}
ret.fontSize = "14px";
return ret;
}
}, {
key: "render",
value: function render() {
var fulls = false;
if (this.state.confirmObject.size === "responsive") fulls = this.props.fullScreen;else if (this.state.confirmObject.size === "fullscreen") fulls = true;
return _react.default.createElement("div", null, _react.default.createElement(Comp, _extends({
messageService: this.messageService
}, this.props)), _react.default.createElement(_Snackbar.default, {
style: {
zIndex: "1455"
},
anchorOrigin: {
vertical: 'top',
horizontal: 'center'
},
onClose: this.handleCloseNotify,
autoHideDuration: 3000,
open: this.state.notifyOpen,
message: this.state.notifyMessage,
ContentProps: {
"style": this.headerColor(this.state.notifyType)
}
}), _react.default.createElement(_Snackbar.default, {
style: {
zIndex: "1455"
},
anchorOrigin: {
vertical: 'top',
horizontal: 'center'
},
onClose: this.handleCloseNotify,
autoHideDuration: 3000,
open: this.state.notifyOpen,
message: this.state.notifyMessage,
ContentProps: {
"style": this.headerColor(this.state.notifyType)
}
}), _react.default.createElement(_Dialog.default, {
style: {
zIndex: "1350"
},
disableEscapeKeyDown: this.state.confirmObject.disableEscapeKeyDown,
disableBackdropClick: this.state.confirmObject.disableBackdropClick,
fullScreen: fulls,
maxWidth: false,
TransitionComponent: Transition,
open: this.state.confirmOpen,
onClose: this.handleCancel,
"aria-labelledby": "alert-dialog-title",
"aria-describedby": "alert-dialog-description"
}, _react.default.createElement(_DialogTitle.default, null, _react.default.createElement(_Typography.default, {
noWrap: true,
color: this.state.confirmObject.type
}, this.state.confirmObject.title)), _react.default.createElement(_DialogContent.default, {
style: {
minWidth: "30vw",
minHeight: "10vh"
}
}, typeof this.state.confirmObject.body === "string" && _react.default.createElement(_DialogContentText.default, {
id: "alert-dialog-description"
}, this.state.confirmObject.body), typeof this.state.confirmObject.body !== "string" && _react.default.createElement("div", {
id: "alert-dialog-description"
}, this.state.confirmObject.body)), _react.default.createElement(_Divider.default, null), _react.default.createElement(_DialogActions.default, null, _react.default.createElement(_Button.default, {
onClick: this.handleOk,
color: "primary",
autoFocus: true
}, this.state.confirmObject.okTitle), _react.default.createElement(_Button.default, {
onClick: this.handleCancel,
color: "secondary"
}, this.state.confirmObject.cancelTitle))));
}
}]);
return HOC;
}(_react.default.PureComponent));
}