index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import Vue from 'vue'
  2. import Main from './main.vue'
  3. import {PopupManager} from 'element-ui/lib/utils/popup'
  4. import {isVNode} from 'element-ui/lib/utils/vdom'
  5. let MessageConstructor = Vue.extend(Main)
  6. let instance
  7. let instances = []
  8. let seed = 1
  9. //键:options.type+'|'+options.message,值:message实例
  10. let groups = {}
  11. const Message = function (options) {
  12. options = options || {}
  13. if (typeof options === 'string') {
  14. options = {message: options}
  15. }
  16. if (!options.type) {
  17. options.type = 'info'
  18. }
  19. const group = options.type + '|' + options.message
  20. const originalMessage = groups[group]
  21. //若不是新的message
  22. if (originalMessage) {
  23. //旧实例的badge+1,并重新计时(如果有的话)
  24. originalMessage.badge++
  25. originalMessage.clearTimer(true)
  26. originalMessage.startTimer()
  27. return originalMessage
  28. }
  29. let userOnClose = options.onClose
  30. let id = 'message_' + seed++
  31. options.onClose = function () {
  32. Message.close(id, userOnClose)
  33. }
  34. instance = new MessageConstructor({data: options})
  35. instance.id = id
  36. instance.group = group
  37. if (isVNode(instance.message)) {
  38. instance.$slots.default = [instance.message]
  39. instance.message = null
  40. }
  41. instance.$mount()
  42. document.body.appendChild(instance.$el)
  43. let verticalOffset = options.offset || 20
  44. instances.forEach(item => {
  45. verticalOffset += item.$el.offsetHeight + 16
  46. })
  47. instance.verticalOffset = verticalOffset
  48. instance.visible = true
  49. instance.$el.style.zIndex = PopupManager.nextZIndex()
  50. instances.push(instance)
  51. groups[group] = instance
  52. return instance
  53. };
  54. ['success', 'warning', 'info', 'error'].forEach(type => {
  55. Message[type] = options => {
  56. if (typeof options === 'string') {
  57. options = {message: options}
  58. }
  59. options.type = type
  60. return Message(options)
  61. }
  62. })
  63. Message.close = function (id, userOnClose) {
  64. let len = instances.length
  65. let index = -1
  66. let removedHeight
  67. for (let i = 0; i < len; i++) {
  68. if (id === instances[i].id) {
  69. removedHeight = instances[i].$el.offsetHeight
  70. index = i
  71. if (typeof userOnClose === 'function') {
  72. userOnClose(instances[i])
  73. }
  74. delete groups[instances[i].group]
  75. instances.splice(i, 1)
  76. break
  77. }
  78. }
  79. if (len <= 1 || index === -1 || index > instances.length - 1) return
  80. for (let i = index; i < len - 1; i++) {
  81. let dom = instances[i].$el
  82. dom.style.top =
  83. parseInt(dom.style.top, 10) - removedHeight - 16 + 'px'
  84. }
  85. }
  86. Message.closeAll = function () {
  87. for (let i = instances.length - 1; i >= 0; i--) {
  88. instances[i].close()
  89. }
  90. }
  91. export default Message