Home Reference Source

cables_dev/cables_ui/src/ui/elements/notification.js

  1. import { utils } from "cables";
  2. import { gui } from "../gui.js";
  3. let lastNotify = "";
  4. let lastText = "";
  5. let lastNotifyErr = "";
  6. let lastTextErr = "";
  7. let lastNotifyWarn = "";
  8. let lastTextWarn = "";
  9. /**
  10. * configuration object for loading a patch
  11. * @typedef {Object} NotificationDisplayOptions
  12. * @hideconstructor
  13. * @property {Number|Boolean} [timeout=2000] fade out notification after x ms, `false` to disable
  14. * @property {Boolean} [closeable=false] show closing button on notification
  15. * @property {Boolean} [force=false] force showing of notification even if last one was the same
  16. */
  17. /**
  18. * notifyError displays an error as a toast-notification
  19. *
  20. *
  21. * @param title
  22. * @param text
  23. * @param {NotificationDisplayOptions} options The option object.
  24. * @example
  25. * notifyError("error", "something broke",
  26. * {
  27. * "timeout": false,
  28. * "closeable": true,
  29. * });
  30. */
  31. export function notifyError(title, text, options = {})
  32. {
  33. const timeout = options.hasOwnProperty("timeout") ? options.timeout : 2000;
  34. const closeable = options.closeable || false;
  35. const force = options.force;
  36. if (!force)
  37. {
  38. if (title === lastNotifyErr && text === lastTextErr)
  39. {
  40. setTimeout(function ()
  41. {
  42. lastNotifyErr = "";
  43. lastTextErr = "";
  44. }, 1000);
  45. return;
  46. }
  47. }
  48. lastNotifyErr = title;
  49. lastTextErr = text;
  50. const toastId = utils.uuid();
  51. iziToast.error(
  52. {
  53. "id": toastId,
  54. "position": "bottomRight", // bottomRight, bottomLeft, topRight, topLeft, topCenter, bottomCenter, center
  55. "theme": "dark",
  56. "title": title,
  57. "message": text || "",
  58. "progressBar": false,
  59. "animateInside": false,
  60. "close": closeable,
  61. "timeout": timeout
  62. });
  63. return toastId;
  64. }
  65. export function notifyWarn(title, text = "", options = {})
  66. {
  67. const timeout = options.hasOwnProperty("timeout") ? options.timeout : 2000;
  68. const closeable = options.closeable || false;
  69. const force = options.hasOwnProperty("force") ? options.force : true;
  70. if (!force)
  71. {
  72. if (title === lastNotifyWarn && text === lastTextWarn)
  73. {
  74. setTimeout(function ()
  75. {
  76. lastNotifyWarn = "";
  77. lastTextWarn = "";
  78. }, 1000);
  79. return;
  80. }
  81. }
  82. lastNotifyWarn = title;
  83. lastTextWarn = text;
  84. const toastId = utils.uuid();
  85. iziToast.warning(
  86. {
  87. "id": toastId,
  88. "position": "bottomRight", // bottomRight, bottomLeft, topRight, topLeft, topCenter, bottomCenter, center
  89. "theme": "dark",
  90. "title": title,
  91. "message": text || "",
  92. "progressBar": false,
  93. "animateInside": false,
  94. "close": closeable,
  95. "timeout": timeout
  96. });
  97. return toastId;
  98. }
  99. /**
  100. * notify displays a toast-notification
  101. *
  102. *
  103. * @param title
  104. * @param text
  105. * @param {NotificationDisplayOptions} options The option object.
  106. * @example
  107. * notify("update", "cables has been updated",
  108. * {
  109. * "timeout": 1000,
  110. * "closeable": false
  111. * });
  112. */
  113. export function notify(title, text = "", options = {})
  114. {
  115. if (gui.isRemoteClient) return;
  116. const timeout = options.timeout || 2000;
  117. const closeable = options.closeable || false;
  118. const force = options.force || true;
  119. if (!force)
  120. {
  121. if (title == lastNotify && text == lastText)
  122. {
  123. setTimeout(function ()
  124. {
  125. lastNotify = "";
  126. lastText = "";
  127. }, 1000);
  128. return;
  129. }
  130. }
  131. lastNotify = title;
  132. lastText = text;
  133. const toastId = utils.uuid();
  134. iziToast.show(
  135. {
  136. "id": toastId,
  137. "position": "bottomRight", // bottomRight, bottomLeft, topRight, topLeft, topCenter, bottomCenter, center
  138. "theme": "dark",
  139. "title": title,
  140. "message": text || "",
  141. "progressBar": false,
  142. "animateInside": false,
  143. "close": closeable,
  144. "timeout": timeout,
  145. "buttons": options.buttons || []
  146. });
  147. return toastId;
  148. }
  149. export function hideNotificaton(toastId)
  150. {
  151. let toast = document.getElementById(toastId);
  152. if (toast) iziToast.hide({}, toast);
  153. }