forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyllabusCollection.coffee
More file actions
119 lines (105 loc) · 3.76 KB
/
Copy pathSyllabusCollection.coffee
File metadata and controls
119 lines (105 loc) · 3.76 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
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
#
# Copyright (C) 2013 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
define [
'jquery'
'Backbone'
], ($, Backbone) ->
class SyllabusCollection extends Backbone.Collection
# Attach to externally provided collections
#
# This cannot be the initialize method as that would cause the
# collections to be passed to backbone as if they were model
# instances... ungoodness.
constructor: (collections) ->
super()
for collection in collections
collection.on 'add', (model, collection, options) => @add model, options
collection.on 'remove', (model, collection, options) => @remove model, options
collection.on 'reset', (collection, options) =>
find_collection_models = (memo, model) ->
if model.get('collection') == collection
memo.push model
else
memo
for model in @reduce find_collection_models, []
@remove model
for model in collection.models
@add model
# No-op fetch
#
# This fetch should never do anything; this collection is
# populated with models from other collections (see initialize).
fetch: ->
# Gives a "natural feeling" sort order to syllabus events
#
# Because this collection is an amalgamation of several types of
# models, not all models will have the same fields. Additionally,
# some events may not have a date at all. So we must account for
# these discrepancies here.
#
# Sort conditions:
# 1) start_at (oldest first)
# When start_at times match, sorted by end_at (youngest first)
#
# For example, an appointment being sorted above an
# appointment group while the other appointments are sorted
# below the appointment group.
#
# 2) title (alphabetically)
# Gives a consistent ordering when start_at and end_at times
# on both items match.
#
comparator: (model1, model2) ->
m1start_at = model1.get('start_at')
m2start_at = model2.get('start_at')
m1end_at = model1.get('end_at')
m2end_at = model2.get('end_at')
m1title = model1.get('title')
m2title = model2.get('title')
m1title = m1title.toLowerCase() if m1title
m2title = m2title.toLowerCase() if m2title
# if the start_at times are different
if m1start_at != m2start_at
# if one of the start_at times is missing
if not m1start_at
return 1
if not m2start_at
return -1
if m1start_at < m2start_at
return -1
else
return 1
# if the end_at times are different
else if m1end_at != m2end_at
# if one of the end_at times is missing (shouldn't be)
if not m1end_at
return 1
if not m2end_at
return -1
# This may seem backwards, but is intentional!
# we sort longer running events first to give a better "feel" to
# the sort order (for overlapping time spans)
if m1end_at < m2end_at
return 1
else
return -1
if m1title < m2title
return -1
else if m1title > m2title
return 1
return 0