Toasts.js 5.3 KB

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