-
Notifications
You must be signed in to change notification settings - Fork 10
ocp mokhov #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: gh-pages
Are you sure you want to change the base?
ocp mokhov #7
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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); | ||
|
|
||
| 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()); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PS Иметь такой интерфейс Модели выгодно потому, что он может бесконечно расширяться: new Question(options)Вот такой в будущем придется дописать: new Question(label, choices)
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Да, я так и делал, а потом выпил :) |
||
| new QuestionViewInput(new Question('Два+Два?')).render(); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Класс QuestionView должен имплементировать метод
buildInput, но об этом мы будем говорить на следующей лекции по SOLID.