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.

112 lines
1.9 KiB

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