Starting on the course detail page
[oweals/karmaworld.git] / karmaworld / assets / js / course-detail.js
1 $(function() {
2   $('#flag-button').click(function(event) {
3     event.preventDefault();
4
5     if (confirm('Do you wish to flag this course for deletion?')) {
6       // disable thank button so it can't
7       // be pressed again
8       $(this).hide();
9       $('#flag-button-disabled').show();
10       $(this).unbind('click');
11
12       // tell server that somebody thanked
13       // this note
14       $.ajax({
15         url: course_flag_url,
16         dataType: 'json',
17         type: 'POST'
18       });
19
20     }
21   });
22
23   $('#edit-course-form').dialog({
24     autoOpen: false,
25     modal: true,
26     show: { effect: 'fade', duration: 500 },
27     width: dialogWidth
28   });
29
30   $('#edit-button').click(function(event) {
31     $('#edit-course-form').dialog("open");
32   });
33
34   $('#edit-save-btn').click(function(event) {
35     $.ajax({
36       url: course_edit_url,
37       dataType: 'json',
38       data: $('#edit-course-form').children().serialize(),
39       type: 'POST',
40       success: function(data) {
41         if (data.fields.new_url) {
42           window.location.href = data.fields.new_url;
43         }
44
45         // We might want to use a template here instead of rehashing logic
46         // on both the client and server side
47         $('#edit-course-form').slideUp();
48         $('.validation_error').remove()
49         $('#course_form_errors').empty();
50         $('#course_name').text(data.fields.name);
51         $('#course_instructor_name').text(data.fields.instructor_name);
52
53         var $externalLinkSquare = $('<i>', {'class': 'fa fa-external-link-square'});
54         $('#course_url').text(data.fields.url.slice(0, 50) + ' ');
55         $('#course_url').append($externalLinkSquare);
56         $('#course_url').attr('href', data.fields.url);
57         if (data.fields.url === '') {
58           $('#course_link').parent().hide();
59         } else {
60           $('#course_link').parent().show();
61         }
62       },
63       error: function(resp) {
64         var json;
65         try {
66           json = JSON.parse(resp.responseText);
67         } catch(e) {
68           json = { message: 'Unknown Error' };
69         }
70
71         var errors = json.errors;
72
73         // Delete all errors that currently exist
74         $('.validation_error').remove()
75         $('#course_form_errors').empty();
76
77         // Failed responses with no errors -> display message
78         if (!errors) {
79           $('#course_form_errors').text(json.message);
80         } else {
81           // Ugly, be works.  Could look into backbone or something similar to make it cleaner.
82           if (errors.__all__) {
83             $.each(errors.__all__, function(index, value) {
84               $('#course_form_errors').append($('<span>', { class: 'validation_error', text: value }));
85             });
86           }
87
88           if (errors.instructor_email) {
89             $.each(errors.instructor_email, function(index, value) {
90               $('#id_instructor_email').parent().children('legend').append($('<span>', { class: 'validation_error', text: value }));
91             });
92           }
93
94           if (errors.url) {
95             $.each(errors.url, function(index, value) {
96               $('#id_url').parent().children('legend').append($('<span>', { class: 'validation_error' , text: value }));
97             });
98           }
99         }
100       }
101     });
102   });
103
104   KARMAWORLD.Course.initCourseNameAutocomplete({});
105   KARMAWORLD.Course.initInstructorNameAutocomplete({});
106
107   $('#data_table_list').dataTable({
108     // remove the default filter label
109     'oLanguage': {
110       'sSearch': '',
111     },
112     // we will set column widths explicitly
113     'bAutoWidth': false,
114     // don't provide a option for the user to change the table page length
115     'bLengthChange': false,
116     // sepcify the number of rows in a page
117     'iDisplayLength': 20,
118     // Position the filter bar at the top
119     // DIFF: do not show search bar (f)
120     'sDom': '<"top">rt<"bottom"p><"clear">',
121     // Specify options for each column
122     "aoColumnDefs": [
123       {
124         // 3rd element: likes
125         "aTargets": [ 2 ],
126         "bSortable": true,
127         "bVisible": true,
128         "mData": function ( source, type, val ) {
129           //console.log(source);
130           if (type === 'set') {
131             source.count = val;
132             // Store the computed dislay and filter values for efficiency
133             // DIFF: label name change.
134             source.count_display = val=="" ? "" : "<span>"+val+" Thanks</span>";
135             return;
136           }
137           else if (type === 'display') {
138             return source.count_display;
139           }
140           // 'sort', 'type', 'filter' and undefined all just use the integer
141           return source.count;
142         }
143       },
144       {
145         // 2nd element: date
146         "aTargets": [ 1 ],
147         "bSortable": true,
148         "bVisible": true,
149         "mData": function ( source, type, val ) {
150           //console.log(source);
151           if (type === 'set') {
152             source.date = val;
153             // DIFF: label name change.
154             source.date_display = val=="" ? "" : "<span>Uploaded "+val+"</span>";
155             return;
156           }
157           else if (type === 'display') {
158             return source.date_display;
159           }
160           // for types 'sort', 'type', 'filter' and undefined use raw date
161           return source.date;
162         }
163       },
164       {
165         // 1st element: "sort by" label
166         "aTargets": [ 0 ],
167         "bSortable": false,
168         "bVisible": true
169       }
170     ],
171     // Initial sorting
172     'aaSorting': [[2,'desc']]
173   });
174
175 });