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.

111 lines
1.9 KiB

  1. 'use strict'
  2. /* global Vue, _ */
  3. /**
  4. * Alerts
  5. */
  6. class Alerts { // eslint-disable-line no-unused-vars
  7. /**
  8. * Constructor
  9. *
  10. * @class
  11. */
  12. constructor () {
  13. let self = this
  14. self.mdl = new Vue({
  15. el: '#alerts',
  16. data: {
  17. children: []
  18. },
  19. methods: {
  20. acknowledge: (uid) => {
  21. self.close(uid)
  22. }
  23. }
  24. })
  25. self.uidNext = 1
  26. }
  27. /**
  28. * Show a new Alert
  29. *
  30. * @param {Object} options Alert properties
  31. * @return {null} Void
  32. */
  33. push (options) {
  34. let self = this
  35. let nAlert = _.defaults(options, {
  36. _uid: self.uidNext,
  37. class: 'info',
  38. message: '---',
  39. sticky: false,
  40. title: '---'
  41. })
  42. self.mdl.children.push(nAlert)
  43. if (!nAlert.sticky) {
  44. _.delay(() => {
  45. self.close(nAlert._uid)
  46. }, 5000)
  47. }
  48. self.uidNext++
  49. }
  50. /**
  51. * Shorthand method for pushing errors
  52. *
  53. * @param {String} title The title
  54. * @param {String} message The message
  55. */
  56. pushError (title, message) {
  57. this.push({
  58. class: 'error',
  59. message,
  60. sticky: false,
  61. title
  62. })
  63. }
  64. /**
  65. * Shorthand method for pushing success messages
  66. *
  67. * @param {String} title The title
  68. * @param {String} message The message
  69. */
  70. pushSuccess (title, message) {
  71. this.push({
  72. class: 'success',
  73. message,
  74. sticky: false,
  75. title
  76. })
  77. }
  78. /**
  79. * Close an alert
  80. *
  81. * @param {Integer} uid The unique ID of the alert
  82. */
  83. close (uid) {
  84. let self = this
  85. let nAlertIdx = _.findIndex(self.mdl.children, ['_uid', uid])
  86. let nAlert = _.nth(self.mdl.children, nAlertIdx)
  87. if (nAlertIdx >= 0 && nAlert) {
  88. nAlert.class += ' exit'
  89. Vue.set(self.mdl.children, nAlertIdx, nAlert)
  90. _.delay(() => {
  91. self.mdl.children.splice(nAlertIdx, 1)
  92. }, 500)
  93. }
  94. }
  95. }