-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.courselistcontroller.ts
139 lines (130 loc) · 5.24 KB
/
app.courselistcontroller.ts
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
module App {
/**
* AngularJS contorller for the simple course list view. Holds the list of courses and can
* update the table via some computed parameters e.g. whether a class should be shown in the table based on the current settings.
* @class
*/
export class CourseListController {
protected _courses: Course[] = [];
protected _specializations: Specialization[] = [];
protected _$scope: ng.IScope;
protected _ngTableClass: any;
/** @property {NgTableParams} tableParams The ng-table settings */
public tableParams: any;
/** @property {Semester} currentSelection The currently selected semester (via HTML) as an enum */
public currentSelection: Semester;
// these will be the options in the semester selector dropdown that we will be populating with angular from here
// id will be a number on the JS side
public semesterOptions = [{ id: Semester.Future, text: "All courses" },
{ id: Semester.Spring2017, text: "Current only" },
{ id: Semester.Fall2017, text: "Current + Fall2017" }]
/**
* @constructor
* @param {ng.IScope} $scope - AngularJS scope
* @param {NgTableParams} NgTableParams - ng-table module
*/
constructor($scope: ng.IScope, NgTableParams, courses?: Course[]) {
this._$scope = $scope;
this._ngTableClass = NgTableParams;
if (courses) {
this._courses = courses;
}
}
/** @property {Course[]} Courses The course data as an array */
get courses(): Course[] {
return this._courses;
}
set courses(c: Course[]) {
this._courses = c;
this.currentSelection = Semester.Spring2017;
this.tableParams = new this._ngTableClass(
{
count: 80, // initial page size
sorting: { available: "asc" }
},
{
counts: [],
dataset: this._courses
});
}
/**
* @function
* @param {number} id - Course id (without the subject)
* @returns {Course} The found course, null if not found
*/
public getById(id: number): Course {
var found: Course;
this._courses.forEach((item) => {
if (item.id == id) {
found = item;
}
});
return found;
}
/**
* @function
* @param {number} id - Course id (without the subject)
* @returns {boolean} Whether the queried class should be shown in the table based on the current settings
*/
public canShow(id: number) {
var course = this.getById(id);
if (course == null) {
console.log("Can't find course id# " + id + ", it won't show up in the table");
return false;
}
var avail = course.available;
if (typeof avail == "string") {
// can happen that it's in string format convert back to enum/number and compare
// (toString needed for typescript because it thinks it is an enum but this case it isn't)
if (this.currentSelection == Semester.Future) return true;
return 0 < Semester[avail.toString()] && Semester[avail.toString()] <= this.currentSelection;
}
else if (typeof avail == "number") {
// happy path
if (this.currentSelection == Semester.Future) return true;
return 0 < avail && avail <= this.currentSelection;
}
else {
console.log("Cannot read availability property of id# " + id + ", it won't show up in the table");
return false;
}
}
/**
* Convert the semester enum into user-friendly text
* @function
* @param {number} num - Semester enum, which is esentially a number on the JS side
* @returns {string} Enum converted to display-friendly text
*/
public availabilityText(num: Semester): string {
switch (num)
{
case Semester.Before:
return "Before Fall 2015";
break;
case Semester.Fall2015:
return "Fall 2015";
break;
case Semester.Spring2016:
return "Spring 2016"
break;
case Semester.Fall2016:
return "Fall 2016"
break;
case Semester.Spring2017:
return "Spring 2017"
break;
case Semester.Fall2017:
return "Fall 2017"
break;
case Semester.Future:
return "Future"
break;
case Semester.Discontinued:
return "Discountinued"
break;
default:
return "Uknown availability property"
}
}
}
}