forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeverDropCollection.coffee
More file actions
63 lines (55 loc) · 1.96 KB
/
Copy pathNeverDropCollection.coffee
File metadata and controls
63 lines (55 loc) · 1.96 KB
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
define [
'underscore'
'Backbone'
'compiled/util/UniqueDropdownCollection'
], (_, Backbone, UniqueDropdownCollection) ->
class NeverDropCollection extends UniqueDropdownCollection
initialize: (records, options={}) ->
#need to pass in the assignments list
#and the assignment group id
{@assignments, @ag_id} = (options or {})
# set up options for UniqueDropdownCollection
options.possibleValues = (@assignments?.map (a) -> a.id) or []
options.propertyName = 'chosen_id'
options.model = Backbone.Model
super
updateAssignments: (assignments)->
@assignments = assignments
updateAssignmentGroupId: (id) ->
@ag_id = id
#pass the chosen_id to include in the output
#used to return a list of models to build the select
#this will retain the assignment order when rendering
#the <options>
toAssignments: (include_id) ->
# map assignments to retain order
models = @assignments.map (m) =>
available = @availableValues.find (am) ->
m.id == am.id
if available or (m.id == include_id)
return m.toView()
# compact results because we're mapping assignments :(
_.compact(models)
parse: (resp, opts) ->
coll = []
for drop, idx in resp
if assignment = @findAssignment(drop)
model_obj =
id: resp.id or idx
chosen: assignment.name()
chosen_id: assignment.get('id')
label_id: @ag_id or 'new'
coll.push model_obj
coll
findAssignment: (id) ->
@assignments.find (a) ->
a.id == id
#override default UniqueCollection logic for finding the next item
#returns a model from @availableValues
findNextAvailable: ->
# find the first assignment, in order, that has
# an `id` in @availableValues
next = @assignments.find (a) =>
@availableValues.find (av) ->
a.id == av.id
@availableValues.get next.id