fork download
  1. function PopupController(parentView, popupView) {
  2. if (!(popupView instanceof PopupView)) {
  3. throw new PopupControllerException("Неправильный класс аргумента.");
  4. }
  5. this._popupView = popupView;
  6. this._parentView = parentView;
  7. this._dispatcher = new EventDispatcher();
  8. this._attachToPopupView();
  9. this._setEvents();
  10. }
  11.  
  12. PopupController.prototype.getDispatcher = function() {
  13. return this._dispatcher;
  14. };
  15.  
  16. PopupController.prototype._attachToPopupView = function() {
  17. var popupDispatcher = this._popupView.getDispatcher();
  18. var parentViewDispatcher = this._parentView.getDispatcher();
  19. popupDispatcher.addEventListener(PopupEvent.CLOSE_OPTION, function(event) {
  20. //ВОПРОС 1
  21. parentViewDispatcher.dispatchEvent(new PopupEvent(PopupEvent.CLOSE_OPTION));
  22. });
  23. popupDispatcher.addEventListener(/*ВОПРОС 2*/);
  24. };
  25.  
  26. PopupController.prototype._setEvents = function() {
  27. this._popupView._addLeftClickToPopupOptionsListener();
  28. };
  29.  
  30. function PopupControllerException(message) {
  31. this.name = "PopupException";
  32. this.message = message;
  33. }
  34.  
  35. /***************************************/
  36.  
  37. function PopupView(parentView, headerText, bodyText, optionsTxt, cancelTxt) {
  38. if (!parentView.getContainer) {
  39. throw new PopupViewException("Необходим заданный метод у аргумента-объекта.");
  40. }
  41. this._container = null;
  42. this._modalWindow = null;
  43.  
  44. this._options = null;
  45. this._cancelOption = null;
  46.  
  47. //За счет этого элемента из parentView будет позионироваться popup
  48. this._parentContainer = parentView.getContainer();
  49. this._parentView = parentView;
  50.  
  51. this._createContainer(headerText, bodyText);
  52. this._createOptions(optionsTxt, cancelTxt);
  53. this._createModalWindow();
  54. }
  55.  
  56. PopupView.CLOSE_OPTION = "close";
  57.  
  58. PopupView.prototype.getContainer = function() {
  59. return this._container;
  60. };
  61.  
  62. PopupView.prototype.remove = function() {
  63. var popupBody = this.getPopupContainer();
  64. var popupParent = popupBody.parentNode;
  65. popupParent.removeChild(popupBody);
  66. var modalWindow = this.getModalWindow();
  67. var modalWindowParent = modalWindow.parentNode;
  68. modalWindowParent.removeChild(modalWindow);
  69. };
  70.  
  71. PopupView.prototype.getPopupContainer = function() {
  72. return this._parentContainer;
  73. };
  74.  
  75. PopupView.prototype.getModalWindow = function() {
  76. return this._modalWindow;
  77. };
  78.  
  79. PopupView.prototype._createContainer = function() {
  80. var template = document.querySelector(".template-popup");
  81. if (!template) {
  82. throw new PopupViewException("Не найдет шаблон.");
  83. }
  84. var templateInner = template.innerHTML;
  85. templateInner = templateInner.replace(/\{\{header-text}}/, headerText);
  86. templateInner = templateInner.replace(/\{\{body-text}}/, bodyText);
  87.  
  88. var container = Util.createElem("div", this._parentContainer, ["popup-container"], null, templateInner);
  89. container.style.marginTop = -container.offsetHeight / 2 + "px";
  90. container.style.marginLeft = -container.offsetWidth / 2 + "px";
  91. this._container = container;
  92. };
  93.  
  94. PopupView.prototype._createOptions = function(optionsTxt, cancelTxt) {
  95. if (!this._container) {
  96. throw new PopupViewException("Вначале необходимо создать контейнер!");
  97. }
  98. this._options = this._container.querySelector(".popup-options");
  99. this._cancelOption = this._options.querySelector(".cancel-option");
  100. for (var i = 0; i < optionsTxt.length; i++) {
  101. var optionElem = document.createElement("button");
  102. optionElem.textContent = optionsTxt[i];
  103. optionElem.dataset.option = i + 1;
  104. this._options.appendChild(optionElem);
  105. }
  106. this._cancelOption.textContent = cancelTxt;
  107. };
  108.  
  109. PopupView.prototype._addLeftClickToPopupOptionsListener = function() {
  110. var that = this;
  111. this._parentContainer.addEventListener("click", function(event) {
  112. var target = event.target;
  113. var option = target.dataset.option;
  114. if (!target.closest(".popup-options") ||
  115. target.tagName != "BUTTON") {
  116. return false;
  117. } else if (!option) {
  118. throw new DomGameViewException("Dataset не инициализирован у элемента.");
  119. } else if (event.which == 1) {
  120. that._dispatcher.dispatchEvent(new PopupEvent(option));
  121. }
  122. });
  123. };
  124.  
  125. PopupView.prototype._createModalWindow = function() {
  126. var space = this._parentView.getContainer();
  127. this._modalWindow = Util.createElem("div", space, ["modal-window"]);
  128. };
  129.  
  130.  
  131. function PopupViewException(message) {
  132. this.name = "PopupException";
  133. this.message = message;
  134. }
  135.  
Success #stdin #stdout 0.02s 122240KB
stdin
Standard input is empty
stdout
Standard output is empty