styling upload button on course_list - turns orange on :hover instead of drawing...
[oweals/karmaworld.git] / karmaworld / assets / js / lightbox-add-course.js
1 // setup ajax based autocomplete for the school field in the add course lightbox
2 $(function() {
3
4   // Dismiss on exit x click
5   $(".lightbox_close").click(function() {
6     $(".modal_content").hide();
7   });
8
9   function setupAjax(){
10     // Assumes variable csrf_token is made available
11     // by embedding document
12     $.ajaxSetup({
13       beforeSend: function(xhr, settings) {
14         if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
15           // Only send the token to relative URLs i.e. locally.
16           xhr.setRequestHeader("X-CSRFToken", '{{ csrf_token }}');
17         }
18       }
19     });
20   }
21
22   setupAjax();
23
24   $("#str_school").autocomplete({
25     source: function(request, response){
26       $.ajax({
27         url: "{% url 'json_school_list' %}",
28         data: {q: request.term},
29         success: function(data) {
30           console.log(data);
31           if (data['status'] === 'success') {
32             response($.map(data['schools'], function(item) {
33               return {
34                   value: item.name,
35                   real_value: item.id,
36                   label: item.name,
37               };
38             }));
39           } else {
40             // FIXME: do something if school not found
41             $('#create_school_link').show();
42           }
43         },
44         dataType: "json",
45         type: 'POST'
46       });
47     },
48     select: function(event, ui) { 
49       console.log("select func");
50       console.log("id");
51       console.log(ui.item.value);
52       console.log("name");
53       console.log(ui.item.label);
54       // set the school id as the value of the hidden field
55       $('#id_school').val(ui.item.real_value);
56       // set the School name as the textbox field
57       //$('#str_school').val(ui.item.label);
58     },
59     minLength: 3
60   });
61 });