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
77 changes: 37 additions & 40 deletions tasks/ocp/refactoring/question.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,68 @@
var AnswerType = {
Choice: 0,
Input: 1
};

/**
* @param {String} label
* @param {Number} answerType
* @param {String[]} [choices]
* @constructor
*/
function Question(label, answerType, choices) {
function Question(label, choices) {
this.label = label;
this.answerType = answerType;
this.choices = choices;
}

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

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

var questionLabel = document.createElement('div');
questionLabel.className = 'question__label';
questionLabel.className = this._class('label');

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

var answer = document.createElement('div');
answer.className = 'question__input';

var input;
answer.className = this._class('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;
}
var input = this.buildInput(question);
Copy link
Owner

Choose a reason for hiding this comment

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

Класс QuestionView должен имплементировать метод buildInput, но об этом мы будем говорить на следующей лекции по SOLID.


answer.appendChild(input);
questionWrapper.appendChild(questionLabel);
questionWrapper.appendChild(answer);
target.appendChild(questionWrapper);
},
_class: function (className) {
return this._innerClass + (className ? '__' + className : '');
}
};

var questions = [
new Question('НЛО?', AnswerType.Choice, ['Да', 'Неа']),
new Question('Два+Два?', AnswerType.Input)
];
function QuestionViewInput() {};
QuestionViewInput.prototype = new QuestionView();
QuestionViewInput.prototype.buildInput = function (){
var input = document.createElement('input');
input.type = 'text';

return input;
}


function QuestionViewChoice() {};
QuestionViewChoice.prototype = new QuestionView();
QuestionViewChoice.prototype.buildInput = 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;
}

var questionRegion = document.getElementById('questions');
new QuestionView().render(questionRegion, questions);
new QuestionViewChoice(new Question('НЛО?', ['Да', 'Неа']).render());
Copy link
Owner

Choose a reason for hiding this comment

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

PS Иметь такой интерфейс Модели выгодно потому, что он может бесконечно расширяться:

new Question(options)

Вот такой в будущем придется дописать:

new Question(label, choices)

Copy link
Author

Choose a reason for hiding this comment

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

Да, я так и делал, а потом выпил :)

new QuestionViewInput(new Question('Два+Два?')).render();