Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 68 additions & 31 deletions tasks/ocp/refactoring/question.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,50 +15,83 @@ function Question(label, answerType, choices) {
this.choices = choices;
}

function QuestionView() {}
QuestionView.prototype = {
render: function (target, questions) {
for (var i = 0; i < questions.length; i++) {
this.renderQuestion(target, questions[i]);
}
},
function abstractQuestionRenderer() {}

renderQuestion: function (target, question) {
abstractQuestionRenderer.prototype = {
render: function (question) {
var questionWrapper = document.createElement('div');
questionWrapper.className = 'question';

var questionLabel = this.renderQuestionLabel(question);

var answer = this.renderAnswer(question);

questionWrapper.appendChild(questionLabel);
questionWrapper.appendChild(answer);

return questionWrapper;
},

renderInput: function (question) {
},

renderQuestionLabel: function (question) {
var questionLabel = document.createElement('div');
questionLabel.className = 'question__label';

var labelText = document.createTextNode(question.label);
questionLabel.appendChild(labelText);
return questionLabel;
},

renderAnswer: function (question) {
var answer = document.createElement('div');
answer.className = 'question__input';
var input = this.renderInput(question);
answer.appendChild(input);
return answer;
}
}

function choiceQuestionRenderer() {}

var input;

switch (question.answerType) {
case AnswerType.Choice:
input = document.createElement('select');
var len = question.choices.length;
for (var i = 0; i < len; i++) {
var option = document.createElement('option');
option.text = question.choices[i];
option.value = question.choices[i];
input.appendChild(option);
}
break;
case AnswerType.Input:
input = document.createElement('input');
input.type = 'text';
break;
choiceQuestionRenderer.prototype = new abstractQuestionRenderer();

choiceQuestionRenderer.prototype.renderInput = function(question) {
var input = document.createElement('select');
var len = question.choices.length;
for (var i = 0; i < len; i++) {
var option = document.createElement('option');
option.text = question.choices[i];
option.value = question.choices[i];
input.appendChild(option);
}
return input;
}

function inputQuestionRenderer() {}

inputQuestionRenderer.prototype = new abstractQuestionRenderer();

inputQuestionRenderer.prototype.renderInput = function (question) {
var input = document.createElement('input');
input.type = 'text';
return input;
}


function QuestionView(renderers) {
this.renderers = renderers;
}

QuestionView.prototype = {
render: function (target, questions) {
for (var i = 0; i < questions.length; i++) {
target.appendChild(this.renderQuestion(questions[i]));
}
},

answer.appendChild(input);
questionWrapper.appendChild(questionLabel);
questionWrapper.appendChild(answer);
target.appendChild(questionWrapper);
renderQuestion: function (question) {
return this.renderers[question.answerType].render(question);
}
};

Expand All @@ -68,4 +101,8 @@ var questions = [
];

var questionRegion = document.getElementById('questions');
new QuestionView().render(questionRegion, questions);
var renderers = {};
renderers[AnswerType.Choice] = new choiceQuestionRenderer();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно было совсем избавиться от AnswerType. И вместо Question сделать 2 класса:

var questions = [
    new QuestionChoice('НЛО?', ['Да', 'Неа']),
    new QuestionInput('Два+Два?')
];

Оба они могли иметь интерфейс IDomRender:

{
    /**
     * @retrun {HTMLElement}
     */
    render: function () {}
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

в целом идея таже самая ведь, думали просто что question может не только рендерить, а что-от еще делать.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ну он и так сможет что-то делать. Он же имплементирует интерфейс, а не наследуется от класса.

renderers[AnswerType.Input] = new inputQuestionRenderer();

new QuestionView(renderers).render(questionRegion, questions);