index.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 (typeof otherOptions.single !== 'boolean') {
  18. otherOptions.single = true;
  19. }
  20. if (instance) {
  21. instance.setMessage(otherOptions, theme);
  22. return instance;
  23. }
  24. console.error('未找到组件,请确认 selector && context 是否正确');
  25. };
  26. export default {
  27. info(options) {
  28. return showMessage(options, MessageType.info);
  29. },
  30. success(options) {
  31. return showMessage(options, MessageType.success);
  32. },
  33. warning(options) {
  34. return showMessage(options, MessageType.warning);
  35. },
  36. error(options) {
  37. return showMessage(options, MessageType.error);
  38. },
  39. hide(options) {
  40. const { context, selector = '#t-message' } = Object.assign({}, options);
  41. const instance = getInstance(context, selector);
  42. if (!instance) {
  43. return;
  44. }
  45. instance.hide();
  46. },
  47. };