This repository has been archived by the owner on Mar 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathquestion.coffee
79 lines (67 loc) · 2.39 KB
/
question.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
Spine = require 'spine'
class Question extends Spine.Model
@configure 'Question', 'tree', 'text', 'answers', 'checkboxes', 'leadsTo', 'helpText'
@findByTreeAndText: (tree, text) ->
@select (q) -> q.tree is tree and q.text is text
@firstForTree: (tree) ->
@find "#{ tree }-0"
constructor: (hash) ->
count = Question.findAllByAttribute('tree', hash.tree).length
@id or= "#{ hash.tree }-#{ count }"
@helpText or= ''
@answers or= { }
@checkboxes or= { }
@text or= hash.text
@title or= I18n.t 'questions', @i18nId(), 'title'
hash.answerWith?.apply @
super
isTalk: =>
@id in ['candels-17', 'sloan-11', 'ukidss-11']
help: (text) ->
@helpText = I18n.t 'questions', @i18nId(), 'help'
answer: (text, { leadsTo: leadsTo, icon: icon, examples: examples, talk: talk } = { leadsTo: null, icon: null, examples: 0, talk: false }) ->
id = "a-#{ _(@answers).keys().length }"
text = I18n.t 'questions', @i18nId(), 'answers', id
@answers[id] = { text, leadsTo, icon, examples, talk }
examples: =>
_({ }).tap (examples) =>
answers = $.extend { }, @answers, @checkboxes
for key, answer of answers
if answer.examples
_(answer.examples).times (i) =>
examples[key] or= []
examples[key].push "#{ @id }_#{ key }_#{ i }"
else
examples[key] = ['blank']
checkbox: (text, { icon: icon, examples: examples } = { icon: null, examples: 0 }) ->
checkbox = true
id = "x-#{ _(@checkboxes).keys().length }"
text = I18n.t 'questions', @i18nId(), 'checkboxes', id
@checkboxes[id] = { checkbox, text, icon, examples }
nextQuestionFrom: (answer) ->
text = @answers[answer]?.leadsTo or @leadsTo
question = @constructor.findByTreeAndText @tree, text
question[0] or null
i18nId: =>
# This is *getting* ridiculous.
if @id.match /ukidss/
@id.replace 'ukidss', 'sloan'
else if @id.match /sloan_singleband/
@id.replace 'sloan_singleband', 'sloan'
else if @id.match /candels_2epoch/
@id.replace 'candels_2epoch', 'candels'
else if @id.match /ferengi/
n = @id.split('-')[1]
if n is '10'
'sloan-8'
else if n is '16'
'candels-17'
else if n is '17'
'sloan-5'
else if n is '18'
'sloan-6'
else
@id.replace 'ferengi', 'candels'
else
@id
module.exports = Question