@@ -6,59 +6,79 @@ angular.module('sm').directive('professorLookup', function ($http) {
6
6
} ,
7
7
template : '{{professorLookup}}' ,
8
8
link : {
9
- pre : function ( scope , elm , attrs ) {
10
-
11
- } ,
9
+ pre : function ( scope , elm , attrs ) { } ,
12
10
post : function ( scope , elm , attrs ) {
13
11
if ( scope . professorLookup !== '' && scope . professorLookup !== 'TBA' ) {
14
12
scope . stats = 'none'
15
13
elm . on ( 'click' , function ( ) {
16
- const nameParts = scope . professorLookup . split ( ' ' )
17
- const lastName = nameParts [ nameParts . length - 1 ]
18
14
if ( scope . stats === 'none' ) {
19
- $http ( {
20
- method : 'GET' ,
21
- url : '/rmp/' + lastName ,
22
- headers : {
23
- Accept : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
24
- }
25
- } ) . success ( function ( data , status , headers , config ) {
26
- const parser = new DOMParser ( )
27
- const doc = parser . parseFromString ( data , 'text/html' )
28
- const entry = doc . querySelectorAll ( '#ratingTable .entry' ) [ 0 ]
29
- const getStat = function ( selector ) {
30
- return entry . querySelectorAll ( selector ) [ 0 ] . innerHTML
31
- }
32
- const getUrl = function ( ) {
33
- return 'http://www.ratemyprofessors.com/ShowRatings.jsp?tid=' + ( entry . querySelectorAll ( '.profName a' ) [ 0 ] as HTMLLinkElement ) . href . split ( '?tid=' ) [ 1 ]
34
- }
35
- const ratingColor = function ( score ) {
36
- score = parseFloat ( score )
37
- if ( score >= 4 ) {
38
- return '#18BC9C'
39
- } else if ( score >= 3 ) {
40
- return '#F39C12'
41
- } else {
42
- return '#E74C3C'
15
+ $http
16
+ . post (
17
+ '/api/rmp.php' ,
18
+ { name : scope . professorLookup } ,
19
+ {
20
+ headers : {
21
+ 'Content-Type' : 'application/json'
22
+ }
43
23
}
44
- }
45
- scope . stats = {
46
- name : getStat ( '.profName a' ) ,
47
- url : getUrl ( ) ,
48
- dept : getStat ( '.profDept' ) ,
49
- numRatings : getStat ( '.profRatings' ) ,
50
- rating : getStat ( '.profAvg' ) ,
51
- easiness : getStat ( '.profEasy' )
52
- }
53
- elm . popover ( {
54
- html : true ,
55
- trigger : 'manual' ,
56
- placement : 'auto left' ,
57
- title : '<a target="_blank" href="' + scope . stats . url + '">' + scope . stats . name + ' - ' + scope . stats . dept + '</a>' ,
58
- content : '<div class="row"><div class="col-xs-6 rmp-rating"><h2 style="background-color:' + ratingColor ( scope . stats . rating ) + '">' + scope . stats . rating + '</h2>Average Rating</div><div class="col-xs-6 rmp-rating"><h2 style="background-color:' + ratingColor ( scope . stats . easiness ) + '">' + scope . stats . easiness + '</h2>Easiness</div></div><div style="text-align:center">Based on ' + scope . stats . numRatings + ' ratings<br><a target="_blank" href="http://www.ratemyprofessors.com/SelectTeacher.jsp?searchName=' + lastName + '&search_submit1=Search&sid=807">Not the right professor?</a><br><small>© 2013 <a target="_blank" href="http://www.ratemyprofessors.com">RateMyProfessors.com</a></small></div>'
24
+ )
25
+ . success ( function ( data , status , headers , config ) {
26
+ const results = data . data . search . teachers . edges
27
+ if ( ! results [ 0 ] ) {
28
+ elm . popover ( 'toggle' )
29
+ return
30
+ }
31
+ const teacher = results [ 0 ] . node
32
+ const ratingColor = function ( score ) {
33
+ score = parseFloat ( score )
34
+ if ( score >= 4 ) {
35
+ return '#18BC9C'
36
+ } else if ( score >= 3 ) {
37
+ return '#F39C12'
38
+ } else {
39
+ return '#E74C3C'
40
+ }
41
+ }
42
+ scope . stats = {
43
+ name : teacher . firstName + ' ' + teacher . lastName ,
44
+ url :
45
+ 'https://www.ratemyprofessors.com/professor/' +
46
+ teacher . legacyId ,
47
+ dept : teacher . department ,
48
+ numRatings : teacher . numRatings ,
49
+ rating : teacher . avgRating ,
50
+ difficulty : teacher . avgDifficulty
51
+ }
52
+ const yearNumber = new Date ( ) . getFullYear ( )
53
+ elm . popover ( {
54
+ html : true ,
55
+ trigger : 'manual' ,
56
+ placement : 'auto left' ,
57
+ title :
58
+ '<a target="_blank" href="' +
59
+ scope . stats . url +
60
+ '">' +
61
+ scope . stats . name +
62
+ ' - ' +
63
+ scope . stats . dept +
64
+ '</a>' ,
65
+ content :
66
+ '<div class="row"><div class="col-xs-6 rmp-rating"><h2 style="background-color:' +
67
+ ratingColor ( scope . stats . rating ) +
68
+ '">' +
69
+ scope . stats . rating +
70
+ '</h2>Average Rating</div><div class="col-xs-6 rmp-rating"><h2 style="background-color:' +
71
+ ratingColor ( scope . stats . easiness ) +
72
+ '">' +
73
+ scope . stats . difficulty +
74
+ '</h2>Level of Difficulty</div></div><div style="text-align:center">Based on ' +
75
+ scope . stats . numRatings +
76
+ ' ratings<br><a target="_blank" href="https://www.ratemyprofessors.com/search/professors/807?q=' +
77
+ scope . professorLookup +
78
+ `">Not the right professor?</a><br><small>© ${ yearNumber } <a target="_blank" href="http://www.ratemyprofessors.com">RateMyProfessors.com</a></small></div>`
79
+ } )
80
+ elm . popover ( 'show' )
59
81
} )
60
- elm . popover ( 'show' )
61
- } )
62
82
} else {
63
83
elm . popover ( 'toggle' )
64
84
}
0 commit comments