From d9422005280b8f100c32abb8c247932b8536f207 Mon Sep 17 00:00:00 2001 From: Arseniy Forshtreter Date: Wed, 2 Apr 2014 16:55:06 +0600 Subject: [PATCH] OCP refactoring --- tasks/ocp/refactoring/question.js | 99 +++++++++++++++++++++---------- 1 file changed, 68 insertions(+), 31 deletions(-) diff --git a/tasks/ocp/refactoring/question.js b/tasks/ocp/refactoring/question.js index 7ccd6f8..73e34ef 100644 --- a/tasks/ocp/refactoring/question.js +++ b/tasks/ocp/refactoring/question.js @@ -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); } }; @@ -68,4 +101,8 @@ var questions = [ ]; var questionRegion = document.getElementById('questions'); -new QuestionView().render(questionRegion, questions); +var renderers = {}; +renderers[AnswerType.Choice] = new choiceQuestionRenderer(); +renderers[AnswerType.Input] = new inputQuestionRenderer(); + +new QuestionView(renderers).render(questionRegion, questions);