fixing one typo and making string formatting consistent.
[oweals/karmaworld.git] / karmaworld / assets / js / add-course.js
1 // Setup all the javascript stuff we need for the various
2 // incarnations of the Add Course form
3
4 $(function() {
5
6   var schoolSelected = false;
7   var courseNameSelected = false;
8   var instructorSelected = false;
9
10   function fieldEdited() {
11     if (schoolSelected && courseNameSelected && instructorSelected) {
12       $('#save-btn').hide();
13       $('#save-btn').addClass('disabled');
14       $('#existing-course-msg').show();
15     } else {
16       $('#save-btn').show();
17       $('#save-btn').removeClass('disabled');
18       $('#existing-course-msg').hide();
19     }
20   }
21
22   addCourse = function() {
23     // Show the add a course form
24     $('#add-course-form').show();
25     // Hide the add a course button
26     $('#add-course-btn').hide();
27     // Scroll the user's page to here
28     $('#add-course-divider').ScrollTo();
29     // Put focus in first input field
30     $('#str_school').focus();
31   };
32
33   // Set up the "Add Course" button at bottom
34   // of page
35   $('#add-course-btn').click(addCourse);
36
37   // Set up the "Add Course" button in the
38   // page header
39   $('#add_course_header_button').click(addCourse);
40
41   // Dismiss on exit x click FIXME
42   $(".lightbox_close").click(function() {
43     $(".modal_content").hide();
44   });
45
46   $("#str_school").autocomplete({
47     source: function(request, response){
48       $.ajax({
49         url: json_school_list,
50         data: {q: request.term},
51         success: function(data) {
52           if (data['status'] === 'success') {
53             response($.map(data['schools'], function(item) {
54               return {
55                   value: item.name,
56                   real_value: item.id,
57                   label: item.name,
58               };
59             }));
60           } else {
61             // FIXME: do something if school not found
62             $('#create_school_link').show();
63           }
64         },
65         dataType: "json",
66         type: 'POST'
67       });
68     },
69     select: function(event, ui) {
70       // set the school id as the value of the hidden field
71       $('#id_school').val(ui.item.real_value);
72       schoolSelected = true;
73       $('#str_school').removeClass('error');
74       $('#save-btn').removeClass('disabled');
75       fieldEdited();
76     },
77     change: function(event, ui) {
78       if (ui.item == null) {
79         $('#id_school').val('');
80         schoolSelected = false;
81         $('#str_school').addClass('error');
82         $('#save-btn').addClass('disabled');
83         fieldEdited();
84       }
85     },
86     minLength: 3
87   });
88
89   $("#id_name").autocomplete({
90     source: function(request, response){
91       var school_id = $('#id_school').val();
92       $.ajax({
93         url: json_school_course_list,
94         data: {q: request.term, school_id: school_id},
95         success: function(data) {
96           if (data['status'] === 'success') {
97             response($.map(data['courses'], function(item) {
98               return {
99                   value: item.name,
100                   label: item.name,
101               };
102             }));
103           }
104         },
105         dataType: "json",
106         type: 'POST'
107       });
108     },
109     select: function(event, ui) {
110       courseNameSelected = true;
111       fieldEdited();
112     },
113     change: function(event, ui) {
114       if (ui.item == null) {
115         courseNameSelected = false;
116         fieldEdited();
117       }
118     },
119     minLength: 3
120   });
121
122   $("#id_instructor_name").autocomplete({
123     source: function(request, response) {
124       var school_id = $('#id_school').val();
125       var course_name = $('#id_name').val();
126       $.ajax({
127         url: json_school_course_instructor_list,
128         data: {q: request.term, school_id: school_id, course_name: course_name},
129         success: function(data) {
130           if (data['status'] === 'success') {
131             // Fill in the autocomplete entries
132             response($.map(data['instructors'], function(item) {
133               return {
134                   value: item.name,
135                   label: item.name,
136                   url:   item.url
137               };
138             }));
139           }
140         },
141         dataType: "json",
142         type: 'POST'
143       });
144     },
145     select: function(event, ui) {
146       instructorSelected = true;
147       $('#existing-course-btn').attr('href', ui.item.url);
148       fieldEdited();
149     },
150     change: function(event, ui) {
151       if (ui.item == null) {
152         instructorSelected = false;
153         $('#existing-course-btn').attr('href', '');
154         fieldEdited();
155       }
156     },
157     minLength: 3
158   });
159
160
161 });