Toasts.js 6.0 KB

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