weex-nuke
Version:
基于 Rax 、Weex 的高性能组件体系 ~~
135 lines (116 loc) • 3.75 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _rax = require('rax');
var _nukeEnv = require('nuke-env');
var _customToast = require('./custom-toast');
var _customToast2 = _interopRequireDefault(_customToast);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var durationObj = {
LONG: 3500,
SHORT: 2000
};
var styles = {
toastContainer: {
backgroundColor: 'rgba(0,0,0,0.7)',
boxSizing: 'border-box',
maxWidth: '80%',
opacity: 1,
color: ' #ffffff',
padding: '4px 8px',
position: 'fixed',
left: '50%',
bottom: '50%',
fontSize: '14px',
height: 'auto',
lineHeight: '18px',
borderRadius: '2px',
textAlign: 'center',
transition: 'all 0.4s ease-in-out',
'-webkit-transition': 'all 0.4s ease-in-out',
transform: 'translateX(-50%)',
'-webkit-transform': 'translateX(-50%)',
'z-index': 9999
}
};
var queue = [];
var isProcessing = false;
var toastWin = void 0;
function showToastWindow(message, durationTime, cb, containerStyle) {
if (!toastWin) {
toastWin = document.createElement('div');
for (var key in containerStyle) {
toastWin.style[key] = containerStyle[key];
}
document.body.appendChild(toastWin);
}
toastWin.innerHTML = message;
setTimeout(function () {
// document.body.remove(toastWin);
toastWin.parentNode.removeChild(toastWin);
toastWin = null;
cb();
// }, 1000000);
}, durationTime);
}
var toast = {
push: function push(message, durationTime, containerStyle) {
queue.push({
message: message,
durationTime: durationTime,
containerStyle: containerStyle
});
this.show(durationTime, containerStyle);
},
show: function show(durationTime, containerStyle) {
var self = this;
// All messages had been toasted already, so remove the toast window,
if (!queue.length) {
if (toastWin) {
toastWin.parentNode.removeChild(toastWin);
}
toastWin = null;
return;
}
// the previous toast is not ended yet.
if (isProcessing) {
return;
}
isProcessing = true;
var toastInfo = queue.shift();
showToastWindow(toastInfo.message, durationTime, function () {
isProcessing = false;
if (queue.length) {
self.show(durationTime, containerStyle);
}
}, containerStyle);
}
};
function Toast(message) {
var duration = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'SHORT';
var userStyle = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var durationTime = duration ? durationObj[duration] : durationObj.SHORT;
if ((typeof message === 'undefined' ? 'undefined' : _typeof(message)) === 'object') {
// 自定义用法 todo in weex
// return newToast(message,Number(durationTime),userStyle);
return (0, _customToast2.default)(message, Number(durationTime));
}
var containerStyle = Object.assign({}, styles.toastContainer, userStyle);
// console.log(containerStyle);
if (_nukeEnv.isWeex) {
// let moduleName = '@weex-module/modal';
var weexModal = require('@weex-module/modal');
if (weexModal.toast) {
weexModal.toast({
message: message,
duration: Number(durationTime) / 1000
});
}
} else {
toast.push(message, durationTime, containerStyle);
}
}
exports.default = Toast;
module.exports = exports['default'];
;