reservation plugin - unbound request (unclean
[unfold.git] / portal / static / unbound_reservation_static / js / createpost.js
1 $(function() {
2
3
4     // Submit post on submit
5    // $('#b1').bind('click', function(event){
6     //    event.preventDefault();
7    //     console.log("form submitted!")  // sanity check
8    //     create_post();
9   //  });
10  
11 $('#b1').bind('click', function(event){
12         event.preventDefault();
13         console.log("form submitted!")  // sanity check
14                 jsondata=document.getElementById("jsondata").value;
15                 user_data=document.getElementById("user_slices").value;
16         create_post(jsondata, user_data,1);
17     });
18         $('#b2').bind('click', function(event){
19         event.preventDefault();
20         console.log("form submitted!")  // sanity check
21                 //jsondata=document.getElementById("jsondata").value;
22                 //user_data=document.getElementById("user_slices").value;
23         create_post(jsondata, user_data,2);
24     });
25     // AJAX for posting
26     function create_post(jsondata, user_data,b) {
27         
28         $("button").hide(300);
29           $("#but").show(6000);
30         $('#but').html('<img src="http://preloaders.net/preloaders/287/Filling%20broken%20ring.gif"> loading...');
31         console.log("create post is working!")
32 console.log(JSON.stringify(jsondata));  
33 var obj = jQuery.parseJSON(jsondata);
34 console.log(JSON.stringify(obj));
35         // sanity check
36                 var myEvent = {"resources":[{"type": "Node"},{"type": "Node"}]};
37                 console.log(JSON.stringify(myEvent));
38                 //jsondata=JSON.parse(JSON.parse(json).jsondata);
39                 jsondata=jsondata.replace("\\", "");
40                 console.log(obj);
41         $.ajax({
42             url : "create_post/", // the endpoint
43             type : "POST", // http method
44             data : {"name" : user_data, "mitsos" : JSON.stringify(obj), "b": b}, // data sent with the post request
45             // handle a successful response
46                         complete:function(){
47                         //alert("Request completed");
48             $("#but").hide();
49                         $("button").show();
50                         $("#b2").show();
51         },
52             success : function(data,status) {
53                 
54                 console.log(data,status); // log the returned json to the console
55                
56                            var obj = JSON.parse(data);
57                           // alert(data);
58                         for (var i = 0; i < obj.resource_response.resources.length; i++) {
59                         var k=i+1;
60                         var m='#'+k;
61                            input_2 = "<span> Node-"+k+"</span>"+
62                                         //"<div class='msglabel'>"<div class='msglabel'>+obj.resource_response.resources[0].domain+"<br>"+obj.resource_response.resources[0].name+"</div>"+
63                                         "<div class='connect'><div class='msglabel'>domain:"+obj.resource_response.resources[i].domain+"<br>name:"+obj.resource_response.resources[i].name+"<br>"+obj.resource_response.resources[i].urn+"</div></div>";
64                            $(m).html(input_2);
65                            }
66                            
67                 console.log("success"); // another sanity check
68             },
69             // handle a non-successful response
70             error : function(xhr,errmsg,err) {
71                 $('#results').html("<div class='alert-box alert radius' data-alert>Oops! We have encountered an error: "+errmsg+
72                     " <a href='#' class='close'>&times;</a></div>"); // add the error to the dom
73                 console.log("papapa" + xhr.status + ": " + xhr.responseText); // provide a bit more info about the error to the console
74             }
75         });
76     };
77
78
79     // This function gets cookie with a given name
80     function getCookie(name) {
81         var cookieValue = null;
82         if (document.cookie && document.cookie != '') {
83             var cookies = document.cookie.split(';');
84             for (var i = 0; i < cookies.length; i++) {
85                 var cookie = jQuery.trim(cookies[i]);
86                 // Does this cookie string begin with the name we want?
87                 if (cookie.substring(0, name.length + 1) == (name + '=')) {
88                     cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
89                     break;
90                 }
91             }
92         }
93         return cookieValue;
94     }
95     var csrftoken = getCookie('csrftoken');
96
97     /*
98     The functions below will create a header with csrftoken
99     */
100
101     function csrfSafeMethod(method) {
102         // these HTTP methods do not require CSRF protection
103         return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
104     }
105     function sameOrigin(url) {
106         // test that a given url is a same-origin URL
107         // url could be relative or scheme relative or absolute
108         var host = document.location.host; // host + port
109         var protocol = document.location.protocol;
110         var sr_origin = '//' + host;
111         var origin = protocol + sr_origin;
112         // Allow absolute or scheme relative URLs to same origin
113         return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
114             (url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
115             // or any other URL that isn't scheme relative or absolute i.e relative.
116             !(/^(\/\/|http:|https:).*/.test(url));
117     }
118
119     $.ajaxSetup({
120         beforeSend: function(xhr, settings) {
121             if (!csrfSafeMethod(settings.type) && sameOrigin(settings.url)) {
122                 // Send the token to same-origin, relative URLs only.
123                 // Send the token only if the method warrants CSRF protection
124                 // Using the CSRFToken value acquired earlier
125                 xhr.setRequestHeader("X-CSRFToken", csrftoken);
126             }
127         }
128     });
129
130 });