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