1var HttpRequest = (function() { 2 'use strict'; 3 4 var HttpRequest = function(url) { 5 // Because we overwrite this.url in the get-function every GET-parameter is appended after each request. 6 // This results in broken urls after multiple calls. 7 // this.urlWithoutParameters is used to reset the url before we append the parameters. 8 this.url = url; 9 this.urlWithoutParameters = url; 10 11 this.requestHeaders = { 12 'x-requested-with': 'xmlhttprequest' 13 }; 14 15 // JavaScript Events only work with DOM elements (such as a "div" or text nodes) 16 // so we create a new empty text node (which will never be added to the DOM) 17 // to bind the actual event to this node 18 var target = document.createTextNode(null); 19 20 // Pass EventTarget interface calls to DOM EventTarget object 21 this.addEventListener = target.addEventListener.bind(target); 22 this.removeEventListener = target.removeEventListener.bind(target); 23 this.dispatchEvent = target.dispatchEvent.bind(target); 24 }; 25 26 /** 27 * Make a HTTP GET request with optional query parameters 28 * 29 * @author Jonas Hantelmann <[email protected]> 30 */ 31 HttpRequest.prototype.get = function(payload, requestSilently) { 32 // Add the payload as query parameters to the URL 33 if (payload) { 34 this.url = this.urlWithoutParameters; // Reset url 35 this.url += '?' + Object.keys(payload).map(function(key) { 36 return encodeURIComponent(key) + '=' + encodeURIComponent(payload[key]); 37 }).join('&'); 38 } 39 40 this.send('GET', null, requestSilently); 41 }; 42 43 /** 44 * Make a HTTP POST request with an optional payload 45 * 46 * @author Jonas Hantelmann <[email protected]> 47 */ 48 HttpRequest.prototype.post = function(payload, requestSilently) { 49 // Set the correct content-type header if we want to pass parameters 50 if (typeof payload == 'object' && !(payload instanceof FormData)) { 51 this.addRequestHeader('content-type', 'application/x-www-form-urlencoded'); 52 53 // Format the payload as urlencoded query string 54 payload = Object.keys(payload).map(function(key) { 55 return encodeURIComponent(key) + '=' + encodeURIComponent(payload[key]); 56 }).join('&'); 57 } 58 59 this.send('POST', payload, requestSilently); 60 }; 61 62 /** 63 * Add a request header (key, value) 64 * 65 * @author Jonas Hantelmann <[email protected]> 66 */ 67 HttpRequest.prototype.addRequestHeader = function(key, value) { 68 this.requestHeaders[key.toLowerCase()] = value; 69 }; 70 71 /** 72 * Generic function to make a HTTP request with an optional payload 73 * 74 * @author Jonas Hantelmann <[email protected]> 75 */ 76 HttpRequest.prototype.send = function(type, payload, requestSilently) { 77 var request = new XMLHttpRequest(); 78 var _this = this; 79 80 request.open(type, this.url, true); 81 82 // Set previously defined request headers 83 for (var requestHeaderName in this.requestHeaders) { 84 request.setRequestHeader(requestHeaderName, this.requestHeaders[requestHeaderName]); 85 } 86 87 request.addEventListener('readystatechange', function() { 88 if (this.readyState == XMLHttpRequest.HEADERS_RECEIVED) { 89 // If we got a JSON response, tell JavaScript to format it accordingly 90 if (/^application\/json/.test(this.getResponseHeader('Content-Type'))) { 91 request.responseType = 'json'; 92 } 93 } 94 }); 95 96 request.addEventListener('load', function() { 97 // Update URL in browser history/address bar after Ajax call 98 if (!requestSilently && typeof this.responseURL !== 'undefined') { 99 history.pushState(null, '', this.responseURL); 100 } 101 102 // If we got any response from the Ajax request, 103 // fire the "load" event and pass the XMLHttpRequest object 104 _this.dispatchEvent(new CustomEvent('load', { 105 detail: this 106 })); 107 }); 108 109 request.addEventListener('error', function() { 110 // If the request failed (maybe the server is offline) 111 // fire the "error" event 112 _this.dispatchEvent(new CustomEvent('error')); 113 }); 114 115 request.send(payload || null); 116 }; 117 118 return HttpRequest; 119}());
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.