Home Reference Source

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

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