alert.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // window.confirm(text);
  2. // window.confirm(title, text);
  3. // window.confirm(title, text, options);
  4. // window.confirm(text, options);
  5. window.confirm = function(/* ... */) {
  6. var title = typeof(arguments[1]) === "string" ? arguments[0] : "Photochamber";
  7. var text = typeof(arguments[1]) === "string" ? arguments[1] : arguments[0];
  8. var optionsParams = arguments[2] || arguments[1] || {};
  9. var options = {
  10. closeButtonValue: false,
  11. displayCloseButton: true,
  12. canCancel: true,
  13. okButtonText: "Ok",
  14. cancelButtonText: "Cancel"
  15. };
  16. for (var i in options)
  17. if (optionsParams[i])
  18. options[i] = optionsParams[i];
  19. return new Promise(ok => {
  20. document.body.classList.add("overlay-visible");
  21. var modalContainer = document.createElement("div");
  22. modalContainer.className = "modal";
  23. modalContainer.tabIndex = "-1";
  24. modalContainer.role = "dialog";
  25. var modal = document.createElement("div");
  26. modal.className = "modal-dialog";
  27. var modalContent = document.createElement("div");
  28. modalContent.className = "modal-content";
  29. var modalTitle = document.createElement("div");
  30. modalTitle.className = "modal-header";
  31. var modalTitleLabel = document.createElement("h5");
  32. modalTitleLabel.textContent = title;
  33. modalTitleLabel.className = "modal-title";
  34. modalTitle.appendChild(modalTitleLabel);
  35. if (options.displayCloseButton) {
  36. var closeBt = document.createElement("button");
  37. closeBt.type = "button";
  38. closeBt.class = "close";
  39. closeBt.addEventListener("click", e => closeHandler());
  40. modalTitle.appendChild(closeBt);
  41. var span = document.createElement("span");
  42. span.ariaHidden = true;
  43. span.innerHTML = "×";
  44. closeBt.appendChild(span);
  45. }
  46. var modalBody = document.createElement("div");
  47. modalBody.className = "modal-body";
  48. modalBody.textContent = text;
  49. var modalFooter = document.createElement("div");
  50. modalFooter.className = "modal-footer";
  51. var okButton = document.createElement("button");
  52. okButton.type = "button";
  53. okButton.className = "btn";
  54. okButton.textContent = options.okButtonText;
  55. okButton.addEventListener("click", () => {
  56. confirmValue(true);
  57. });
  58. modalFooter.appendChild(okButton);
  59. if (options.canCancel) {
  60. var cancelButton = document.createElement("button");
  61. cancelButton.type = "button";
  62. cancelButton.className = "btn btn-danger";
  63. cancelButton.textContent = options.cancelButtonText;
  64. cancelButton.addEventListener("click", () => {
  65. confirmValue(false);
  66. });
  67. modalFooter.appendChild(cancelButton);
  68. }
  69. modalContainer.style.display = "initial";
  70. modalContainer.appendChild(modal);
  71. modal.appendChild(modalContent);
  72. modalContent.appendChild(modalTitle);
  73. modalContent.appendChild(modalBody);
  74. modalContent.appendChild(modalFooter);
  75. document.getElementById("pch-page").appendChild(modalContainer);
  76. let closeHandler = () => {
  77. confirmValue(options.closeButtonValue);
  78. };
  79. function confirmValue(val) {
  80. if (options.displayCloseButton)
  81. document.removeOnClosePopinRequested(closeHandler);
  82. document.getElementById("pch-page").removeChild(modalContainer);
  83. document.body.classList.remove("overlay-visible");
  84. ok(val);
  85. };
  86. if (options.displayCloseButton) {
  87. document.onClosePopinRequested(closeHandler);
  88. }
  89. });
  90. }