function PopupController(parentView, popupView) {
if (!(popupView instanceof PopupView)) {
throw new PopupControllerException("Неправильный класс аргумента.");
}
this._popupView = popupView;
this._parentView = parentView;
this._dispatcher = new EventDispatcher();
this._attachToPopupView();
this._setEvents();
}
PopupController.prototype.getDispatcher = function() {
return this._dispatcher;
};
PopupController.prototype._attachToPopupView = function() {
var popupDispatcher = this._popupView.getDispatcher();
var parentViewDispatcher = this._parentView.getDispatcher();
popupDispatcher.addEventListener(PopupEvent.CLOSE_OPTION, function(event) {
//ВОПРОС 1
parentViewDispatcher.dispatchEvent(new PopupEvent(PopupEvent.CLOSE_OPTION));
});
popupDispatcher.addEventListener(/*ВОПРОС 2*/);
};
PopupController.prototype._setEvents = function() {
this._popupView._addLeftClickToPopupOptionsListener();
};
function PopupControllerException(message) {
this.name = "PopupException";
this.message = message;
}
/***************************************/
function PopupView(parentView, headerText, bodyText, optionsTxt, cancelTxt) {
if (!parentView.getContainer) {
throw new PopupViewException("Необходим заданный метод у аргумента-объекта.");
}
this._container = null;
this._modalWindow = null;
this._options = null;
this._cancelOption = null;
//За счет этого элемента из parentView будет позионироваться popup
this._parentContainer = parentView.getContainer();
this._parentView = parentView;
this._createContainer(headerText, bodyText);
this._createOptions(optionsTxt, cancelTxt);
this._createModalWindow();
}
PopupView.CLOSE_OPTION = "close";
PopupView.prototype.getContainer = function() {
return this._container;
};
PopupView.prototype.remove = function() {
var popupBody = this.getPopupContainer();
var popupParent = popupBody.parentNode;
popupParent.removeChild(popupBody);
var modalWindow = this.getModalWindow();
var modalWindowParent = modalWindow.parentNode;
modalWindowParent.removeChild(modalWindow);
};
PopupView.prototype.getPopupContainer = function() {
return this._parentContainer;
};
PopupView.prototype.getModalWindow = function() {
return this._modalWindow;
};
PopupView.prototype._createContainer = function() {
var template = document.querySelector(".template-popup");
if (!template) {
throw new PopupViewException("Не найдет шаблон.");
}
var templateInner = template.innerHTML;
templateInner = templateInner.replace(/\{\{header-text}}/, headerText);
templateInner = templateInner.replace(/\{\{body-text}}/, bodyText);
var container = Util.createElem("div", this._parentContainer, ["popup-container"], null, templateInner);
container.style.marginTop = -container.offsetHeight / 2 + "px";
container.style.marginLeft = -container.offsetWidth / 2 + "px";
this._container = container;
};
PopupView.prototype._createOptions = function(optionsTxt, cancelTxt) {
if (!this._container) {
throw new PopupViewException("Вначале необходимо создать контейнер!");
}
this._options = this._container.querySelector(".popup-options");
this._cancelOption = this._options.querySelector(".cancel-option");
for (var i = 0; i < optionsTxt.length; i++) {
var optionElem = document.createElement("button");
optionElem.textContent = optionsTxt[i];
optionElem.dataset.option = i + 1;
this._options.appendChild(optionElem);
}
this._cancelOption.textContent = cancelTxt;
};
PopupView.prototype._addLeftClickToPopupOptionsListener = function() {
var that = this;
this._parentContainer.addEventListener("click", function(event) {
var target = event.target;
var option = target.dataset.option;
if (!target.closest(".popup-options") ||
target.tagName != "BUTTON") {
return false;
} else if (!option) {
throw new DomGameViewException("Dataset не инициализирован у элемента.");
} else if (event.which == 1) {
that._dispatcher.dispatchEvent(new PopupEvent(option));
}
});
};
PopupView.prototype._createModalWindow = function() {
var space = this._parentView.getContainer();
this._modalWindow = Util.createElem("div", space, ["modal-window"]);
};
function PopupViewException(message) {
this.name = "PopupException";
this.message = message;
}