This repository has been archived by the owner on Oct 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclassify-page.coffee
281 lines (221 loc) · 8.81 KB
/
classify-page.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
Dialog = window.zooniverse?.controllers?.Dialog or require('zooniverse/controllers/dialog')
loginDialog = window.zooniverse?.controllers?.loginDialog or require('zooniverse/controllers/login-dialog')
signupDialog = window.zooniverse?.controllers?.signupDialog or require('zooniverse/controllers/signup-dialog')
Classifier = require './classifier'
MiniTutorial = require './mini-tutorial'
SubjectViewer = require './subject-viewer'
DecisionTree = require 'zooniverse-decision-tree'
ClassificationSummary = require './classification-summary'
DrawingTask = require './tasks/drawing'
FieldGuide = require './field-guide'
currentConfig = require 'zooniverse-readymade/current-configuration'
User = window.zooniverse?.models?.User or require('zooniverse/models/user')
$ = window.jQuery
StackOfPages = require 'stack-of-pages'
Api = window.zooniverse?.Api or require('zooniverse/lib/api')
translate = window.zooniverse?.translate or require('zooniverse/lib/translate')
class ClassifyPage extends Classifier
START_TUTORIAL: "zooniverse-readymade:classifier:start_tutorial"
targetSubjectID: ''
workflow: 'untitled_workflow'
tasks: null
firstTask: ''
tutorial: null
tutorialSteps: null
examples: null
classificationsSubmitted: 0
loginPromptEvery: 5 # Or use 0 to disable.
className: "#{Classifier::className} readymade-classify-page"
template: require './templates/classify-page'
elements:
'.readymade-no-more-subjects-message': 'noMoreSubjectsMessage'
'.readymade-subject-viewer-container': 'subjectViewerContainer'
'.readymade-decision-tree-container': 'decisionTreeContainer'
'.readymade-summary-container': 'summaryContainer'
'.readymade-field-guide-container': 'fieldGuideContainer'
constructor: ->
super
if @tutorialSteps?
@tutorial = new MiniTutorial steps: @tutorialSteps
$(@tutorial.el).on @tutorial.CLOSE_EVENT, ->
User.current?.setPreference 'tutorial_done', true
@el.append @tutorial.el
@subjectViewer = new SubjectViewer
@subjectViewerContainer.append @subjectViewer.el
@decisionTree = new DecisionTree
taskTypes:
radio: require './tasks/radio'
checkbox: require './tasks/checkbox'
button: require './tasks/button'
filter: require './tasks/filter'
drawing: DrawingTask
tasks: @tasks
firstTask: @firstTask || Object.keys(@tasks)[0]
autofocus: false
backLabel: translate 'readymade.back'
@listenTo @decisionTree.el, @decisionTree.LOAD_TASK, (e) =>
@subjectViewer.setTool null, null
@subjectViewer.setTaskIndex e.detail.index
@updateDrawingTask() if e.detail.task.type is 'drawing'
unless @decisionTree.autofocus
@decisionTree.autofocus = true
return
if e.detail.index > 0
@decisionTree.backButton.focus()
else
@decisionTree.el.querySelector('input').focus()
@listenTo @decisionTree.el, DrawingTask::SELECT_TOOL, (e) =>
# Delay so the default is preserved instead of cleared with the LOAD_TASK event.
setTimeout =>
{tool, choice} = e.detail
@subjectViewer.setTool tool, choice
@listenTo @decisionTree.el, @decisionTree.COMPLETE, =>
@finishSubject()
@summary.el.find('button[name=readymade-dont-talk]').focus()
@decisionTreeContainer.append @decisionTree.el
@listenTo @subjectViewer.markingSurface, 'marking-surface:add-tool', =>
@updateDrawingTask()
@listenTo @subjectViewer.markingSurface, 'marking-surface:remove-tool', =>
@updateDrawingTask()
@summary = new ClassificationSummary
@summary.on @summary.DISMISS, =>
@getNextSubject()
@summaryContainer.append @summary.el
if @examples?
@fieldGuide = new FieldGuide {@examples}
@fieldGuideContainer.append @fieldGuide.el
@el.on StackOfPages::activateEvent, => @onActivate arguments...
isUserScientist: ->
result = new $.Deferred
if User.current?
# TODO: Cache some of this? Pretty nasty.
project = Api.current.get "/projects/#{Api.current.project}"
talkUser = Api.current.get "/projects/#{Api.current.project}/talk/users/#{User.current.name}"
$.when(project, talkUser).then (project, talkUser) =>
projectRoles = talkUser.talk?.roles?[project.id] ? []
details =
project: project.id
roles: projectRoles
scientist: 'scientist' in projectRoles
admin: 'admin' in projectRoles
'brian-c': talkUser.name in ['brian-c', 'eatyourgreens']
console?.log 'Can you pick your own subject?', JSON.stringify details, null, 2
result.resolve 'scientist' in projectRoles or 'admin' in projectRoles or talkUser.name in ['brian-c', 'eatyourgreens']
else
result.resolve false
result.promise()
onActivate: (e) ->
@targetSubjectID = e.originalEvent.detail.subjectID
if @targetSubjectID
@getNextSubject() unless @targetSubjectID is @Subject.current?.zooniverse_id
if @tutorial.el.getAttribute('aria-hidden') is 'false'
setTimeout => @tutorial.active_button?.focus()
onUserChange: (user) ->
super
@classificationsSubmitted = 0
if @tutorial?
tutorialDone = user?.project?.tutorial_done
tutorialDone ?= user?.preferences?[currentConfig.id]?.tutorial_done
if tutorialDone
if @tutorial.el.hasAttribute 'data-open'
@tutorial.close()
else
@startTutorial()
getNextSubject: ->
if @targetSubjectID
@isUserScientist().then (theyAre) =>
if theyAre
request = Api.current.get "/projects/#{Api.current.project}/subjects/#{@targetSubjectID}"
request.then (data) =>
subject = new @Subject data
subject.select()
request.fail =>
alert "There's no subject with the ID #{@targetSubjectID}."
else
alert 'Sorry, only science team members can choose the subjects they classify.'
super
else
super
startTutorial: ->
@tutorial.goTo 0
@tutorial.open()
@trigger @START_TUTORIAL, this, @tutorial
onNoMoreSubjects: ->
@noMoreSubjectsMessage.show()
@subjectViewerContainer.hide()
@decisionTreeContainer.hide()
@summaryContainer.hide()
createClassification: (subject) ->
super
if subject.zooniverse_id is @targetSubjectID
@classification.set 'chosen_subject', true
loadSubject: (subject, callback) ->
args = arguments
@noMoreSubjectsMessage.hide()
@subjectViewerContainer.show()
@decisionTreeContainer.show()
@summaryContainer.hide()
@subjectViewer.loadSubject subject, =>
super args...
@summary.loadSubject subject
loadClassification: (classification, callback) ->
args = arguments
@decisionTree.reset() # TODO: Pass in a classification.
@subjectViewer.loadClassification classification, =>
super args...
showSummary: ->
@decisionTreeContainer.hide()
@summaryContainer.show()
@targetSubjectID = '' # Don't keep loading the same subject
sendClassification: ->
@classification.set 'workflow', @workflow
for annotation in @composeAnnotations()
@classification.annotate annotation
super
@classificationsSubmitted += 1
unless User.current?
if @classificationsSubmitted % @loginPromptEvery is 0
@promptToLogIn()
composeAnnotations: ->
annotations = []
decisionTreeValues = @decisionTree.getValues()
for keyAndValue in decisionTreeValues then for key, value of keyAndValue
annotations.push {key, value}
annotations
promptToLogIn: ->
prompt = new Dialog
warning: true
content: """
<header>You've submitted #{@classificationsSubmitted} classifications, but you're you're not logged in!</header>
<p>Please sign in so we can make better use of your work and give you credit when the data is published.</p>
<p class="action">
<button name="close-dialog">No thanks</button>
<button name="register">Register</button>
<button name="sign-in">Sign in</button>
</p>
"""
events: $.extend {},
Dialog::events
'click button[name="register"]': ->
@hide()
signupDialog.show()
'click button[name="sign-in"]': ->
@hide()
loginDialog.show()
hide: ->
Dialog::hide.call this
setTimeout (=> @destroy()), 600
prompt.show()
setTimeout ->
prompt.el[0].querySelector('button[name=sign-in]').focus()
updateDrawingTask: ->
marks = []
for {mark} in @subjectViewer.markingSurface.tools
marks.push mark if mark._taskIndex is @subjectViewer.taskIndex
@decisionTree.currentTask?.reset marks
events:
'change input[name="favorite"]': (e) ->
@classification.favorite = e.target.checked
'click button[name="restart-tutorial"]': ->
@startTutorial()
module.exports = ClassifyPage