confirmDialog.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * @constructor
  3. * @param {string} title
  4. * @param {string|Element} content
  5. **/
  6. function ConfirmDialog(title, content) {
  7. /** @const @type {string} */
  8. this.title = title;
  9. /** @const @type {string|Element} */
  10. this.content = content;
  11. /** @const @type {Element} */
  12. this.dom = this.createDom();
  13. /** @const @type {Element} */
  14. this.domOverlay = this.createDomOverlay();
  15. /** @type {Array.<function()>} */
  16. this.confirmHandlers = [];
  17. /** @type {Array.<function()>} */
  18. this.dissmissHandler = [];
  19. }
  20. /**
  21. * @return {Element}
  22. **/
  23. ConfirmDialog.prototype.createDom = function() {
  24. var
  25. dom = document.createElement("div")
  26. ,domTitle = document.createElement("header")
  27. ,titleLabel = document.createElement("span")
  28. ,closeButton = document.createElement("span")
  29. ,domBody = document.createElement("div")
  30. ,domButtons = document.createElement("footer");
  31. var _this = this;
  32. dom.confirmButton = document.createElement("span");
  33. dom.dissmissButton = document.createElement("span");
  34. titleLabel.textContent = this.title;
  35. if (typeof this.content == "string")
  36. domBody.innerHTML = this.content;
  37. else
  38. domBody.appendChild(this.content);
  39. domTitle.className = R.klass.dialog.title;
  40. titleLabel.className = R.klass.dialog.titleLabel;
  41. closeButton.className = R.klass.dialog.closeButton;
  42. closeButton.textContent = 'x';
  43. domTitle.appendChild(titleLabel);
  44. domTitle.appendChild(closeButton);
  45. dom.appendChild(domTitle);
  46. domBody.className = R.klass.dialog.body;
  47. dom.appendChild(domBody);
  48. dom.dissmissButton.className = R.klass.button;
  49. dom.dissmissButton.textContent = locale.dissmiss;
  50. dom.dissmissButton.addEventListener("click", function() {
  51. _this.buttonClicked(false);
  52. });
  53. closeButton.addEventListener("click", function() {
  54. _this.buttonClicked(false);
  55. });
  56. dom.confirmButton.addEventListener("click", function() {
  57. _this.buttonClicked(true);
  58. });
  59. domButtons.appendChild(dom.dissmissButton);
  60. dom.confirmButton.className = R.klass.button;
  61. dom.confirmButton.textContent = locale.ok;
  62. domButtons.appendChild(dom.confirmButton);
  63. domButtons.className = R.klass.buttonContainer +' ' +R.klass.dialog.buttonBar;
  64. dom.appendChild(domButtons);
  65. dom.className = R.klass.dialog.container;
  66. return dom;
  67. };
  68. /**
  69. * @param {boolean} confirmed
  70. **/
  71. ConfirmDialog.prototype.buttonClicked = function(confirmed) {
  72. (confirmed ? this.confirmHandlers : this.dissmissHandler).forEach(function(handler) {
  73. handler();
  74. });
  75. this.close();
  76. };
  77. /**
  78. * @return {Element}
  79. **/
  80. ConfirmDialog.prototype.createDomOverlay = function() {
  81. var dom = document.createElement("div");
  82. dom.className = R.klass.dialog.overlay;
  83. dom.addEventListener("click", (function() {
  84. this.buttonClicked(false);
  85. }).bind(this));
  86. return dom;
  87. };
  88. /**
  89. * @param {string} confirmText
  90. * @param {string} dissmissText
  91. * @return {ConfirmDialog}
  92. **/
  93. ConfirmDialog.prototype.setButtonText = function(confirmText, dissmissText) {
  94. this.dom.confirmButton.textContent = confirmText;
  95. this.dom.dissmissButton.textContent = dissmissText;
  96. return this;
  97. };
  98. /**
  99. * @param {Element=} domParent
  100. * @return {ConfirmDialog}
  101. **/
  102. ConfirmDialog.prototype.spawn = function(domParent) {
  103. domParent = domParent || document.body;
  104. domParent.appendChild(this.domOverlay);
  105. domParent.appendChild(this.dom);
  106. return this;
  107. };
  108. /**
  109. * @return {ConfirmDialog}
  110. **/
  111. ConfirmDialog.prototype.close = function() {
  112. this.dom.remove();
  113. this.domOverlay.remove();
  114. return this;
  115. };
  116. /**
  117. * @param {function()} handler
  118. * @return {ConfirmDialog}
  119. **/
  120. ConfirmDialog.prototype.onConfirm = function(handler) {
  121. this.confirmHandlers.push(handler);
  122. return this;
  123. };
  124. /**
  125. * @param {function()} handler
  126. * @return {ConfirmDialog}
  127. **/
  128. ConfirmDialog.prototype.onDissmiss = function(handler) {
  129. this.dissmissHandler.push(handler);
  130. return this;
  131. };