Toasts.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // noinspection EqualityComparisonWithCoercionJS
  2. /**
  3. * --------------------------------------------
  4. * AdminLTE Toasts.js
  5. * License MIT
  6. * --------------------------------------------
  7. */
  8. import $ from 'jquery'
  9. /**
  10. * Constants
  11. * ====================================================
  12. */
  13. const NAME = 'Toasts'
  14. const DATA_KEY = 'lte.toasts'
  15. const EVENT_KEY = `.${DATA_KEY}`
  16. const JQUERY_NO_CONFLICT = $.fn[NAME]
  17. const EVENT_INIT = `init${EVENT_KEY}`
  18. const EVENT_CREATED = `created${EVENT_KEY}`
  19. const EVENT_REMOVED = `removed${EVENT_KEY}`
  20. const SELECTOR_CONTAINER_TOP_RIGHT = '#toastsContainerTopRight'
  21. const SELECTOR_CONTAINER_TOP_LEFT = '#toastsContainerTopLeft'
  22. const SELECTOR_CONTAINER_BOTTOM_RIGHT = '#toastsContainerBottomRight'
  23. const SELECTOR_CONTAINER_BOTTOM_LEFT = '#toastsContainerBottomLeft'
  24. const CLASS_NAME_TOP_RIGHT = 'toasts-top-right'
  25. const CLASS_NAME_TOP_LEFT = 'toasts-top-left'
  26. const CLASS_NAME_BOTTOM_RIGHT = 'toasts-bottom-right'
  27. const CLASS_NAME_BOTTOM_LEFT = 'toasts-bottom-left'
  28. const POSITION_TOP_RIGHT = 'topRight'
  29. const POSITION_TOP_LEFT = 'topLeft'
  30. const POSITION_BOTTOM_RIGHT = 'bottomRight'
  31. const POSITION_BOTTOM_LEFT = 'bottomLeft'
  32. const Default = {
  33. position: POSITION_TOP_RIGHT,
  34. fixed: true,
  35. autohide: false,
  36. autoremove: true,
  37. delay: 1000,
  38. fade: true,
  39. icon: null,
  40. image: null,
  41. imageAlt: null,
  42. imageHeight: '25px',
  43. title: null,
  44. subtitle: null,
  45. close: true,
  46. body: null,
  47. class: null
  48. }
  49. /**
  50. * Class Definition
  51. * ====================================================
  52. */
  53. class Toasts {
  54. constructor(element, config) {
  55. this._config = config
  56. this._prepareContainer()
  57. $('body').trigger($.Event(EVENT_INIT))
  58. }
  59. // Public
  60. create() {
  61. const toast = $('<div class="toast" role="alert" aria-live="assertive" aria-atomic="true"/>')
  62. toast.data('autohide', this._config.autohide)
  63. toast.data('animation', this._config.fade)
  64. if (this._config.class) {
  65. toast.addClass(this._config.class)
  66. }
  67. if (this._config.delay && this._config.delay != 500) {
  68. toast.data('delay', this._config.delay)
  69. }
  70. const toastHeader = $('<div class="toast-header">')
  71. if (this._config.image != null) {
  72. const toastImage = $('<img />').addClass('rounded mr-2').attr('src', this._config.image).attr('alt', this._config.imageAlt)
  73. if (this._config.imageHeight != null) {
  74. toastImage.height(this._config.imageHeight).width('auto')
  75. }
  76. toastHeader.append(toastImage)
  77. }
  78. if (this._config.icon != null) {
  79. toastHeader.append($('<i />').addClass('mr-2').addClass(this._config.icon))
  80. }
  81. if (this._config.title != null) {
  82. toastHeader.append($('<strong />').addClass('mr-auto').html(this._config.title))
  83. }
  84. if (this._config.subtitle != null) {
  85. toastHeader.append($('<small />').html(this._config.subtitle))
  86. }
  87. if (this._config.close == true) {
  88. const toastClose = $('<button data-dismiss="toast" />').attr('type', 'button').addClass('ml-2 mb-1 close').attr('aria-label', 'Close').append('<span aria-hidden="true">&times;</span>')
  89. if (this._config.title == null) {
  90. toastClose.toggleClass('ml-2 ml-auto')
  91. }
  92. toastHeader.append(toastClose)
  93. }
  94. toast.append(toastHeader)
  95. if (this._config.body != null) {
  96. toast.append($('<div class="toast-body" />').html(this._config.body))
  97. }
  98. $(this._getContainerId()).prepend(toast)
  99. const $body = $('body')
  100. $body.trigger($.Event(EVENT_CREATED))
  101. toast.toast('show')
  102. if (this._config.autoremove) {
  103. toast.on('hidden.bs.toast', function () {
  104. $(this).delay(200).remove()
  105. $body.trigger($.Event(EVENT_REMOVED))
  106. })
  107. }
  108. }
  109. // Static
  110. _getContainerId() {
  111. if (this._config.position === POSITION_TOP_RIGHT) {
  112. return SELECTOR_CONTAINER_TOP_RIGHT
  113. }
  114. if (this._config.position === POSITION_TOP_LEFT) {
  115. return SELECTOR_CONTAINER_TOP_LEFT
  116. }
  117. if (this._config.position === POSITION_BOTTOM_RIGHT) {
  118. return SELECTOR_CONTAINER_BOTTOM_RIGHT
  119. }
  120. if (this._config.position === POSITION_BOTTOM_LEFT) {
  121. return SELECTOR_CONTAINER_BOTTOM_LEFT
  122. }
  123. }
  124. _prepareContainer() {
  125. if ($(this._getContainerId()).length === 0) {
  126. const container = $('<div />').attr('id', this._getContainerId().replace('#', ''))
  127. if (this._config.position == POSITION_TOP_RIGHT) {
  128. container.addClass(CLASS_NAME_TOP_RIGHT)
  129. } else if (this._config.position == POSITION_TOP_LEFT) {
  130. container.addClass(CLASS_NAME_TOP_LEFT)
  131. } else if (this._config.position == POSITION_BOTTOM_RIGHT) {
  132. container.addClass(CLASS_NAME_BOTTOM_RIGHT)
  133. } else if (this._config.position == POSITION_BOTTOM_LEFT) {
  134. container.addClass(CLASS_NAME_BOTTOM_LEFT)
  135. }
  136. $('body').append(container)
  137. }
  138. if (this._config.fixed) {
  139. $(this._getContainerId()).addClass('fixed')
  140. } else {
  141. $(this._getContainerId()).removeClass('fixed')
  142. }
  143. }
  144. // Static
  145. static _jQueryInterface(option, config) {
  146. return this.each(function () {
  147. const _options = $.extend({}, Default, config)
  148. const toast = new Toasts($(this), _options)
  149. if (option === 'create') {
  150. toast[option]()
  151. }
  152. })
  153. }
  154. }
  155. /**
  156. * jQuery API
  157. * ====================================================
  158. */
  159. $.fn[NAME] = Toasts._jQueryInterface
  160. $.fn[NAME].Constructor = Toasts
  161. $.fn[NAME].noConflict = function () {
  162. $.fn[NAME] = JQUERY_NO_CONFLICT
  163. return Toasts._jQueryInterface
  164. }
  165. export default Toasts