index.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. var __rest = (this && this.__rest) || function (s, e) {
  2. var t = {};
  3. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
  4. t[p] = s[p];
  5. if (s != null && typeof Object.getOwnPropertySymbols === "function")
  6. for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  7. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
  8. t[p[i]] = s[p[i]];
  9. }
  10. return t;
  11. };
  12. import { MessageType } from './message.interface';
  13. import { getInstance } from '../common/utils';
  14. const showMessage = function (options, theme = MessageType.info) {
  15. const { context, selector = '#t-message' } = options, otherOptions = __rest(options, ["context", "selector"]);
  16. const instance = getInstance(context, selector);
  17. if (instance) {
  18. instance.resetData(() => {
  19. instance.setData(Object.assign({ theme }, otherOptions), instance.show.bind(instance));
  20. });
  21. return instance;
  22. }
  23. console.error('未找到组件,请确认 selector && context 是否正确');
  24. };
  25. export default {
  26. info(options) {
  27. return showMessage(options, MessageType.info);
  28. },
  29. success(options) {
  30. return showMessage(options, MessageType.success);
  31. },
  32. warning(options) {
  33. return showMessage(options, MessageType.warning);
  34. },
  35. error(options) {
  36. return showMessage(options, MessageType.error);
  37. },
  38. hide(options) {
  39. const { context, selector = '#t-message' } = Object.assign({}, options);
  40. const instance = getInstance(context, selector);
  41. if (!instance) {
  42. return;
  43. }
  44. instance.hide();
  45. },
  46. };