You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Toasts.js 5.9KB

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