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.

114 lines
1.7 KiB

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