1 /*!
  2  * Wef net module
  3  * Copyright (c) 2011 Pablo Escalada
  4  * MIT Licensed
  5  *
  6  * Original ajax code from https://github.com/alexyoung/turing.js/blob/master/turing.net.js
  7  * Copyright (C) 2010-2011 Alex R. Young
  8  * MIT Licensed
  9  */
 10 (function (wef) {
 11     var net;
 12 
 13     /**
 14      * @namespace
 15      */
 16     net = function () {
 17         return new net.prototype.init();
 18     };
 19 
 20     net.prototype = {
 21         constructor:net,
 22         /**
 23          * Version number
 24          */
 25         version:"0.1.0",
 26         /**
 27          * @ignore
 28          */
 29         init:function () {
 30             return this;
 31         },
 32         /**
 33          * Launch a XMLHttpRequest, waiting the result
 34          * @param url request url
 35          * @param [options] additional arguments
 36          * @param {string}options.method request method, supports[get|post]
 37          * @param {boolean}options.asynchronous request type, synchronous or asynchronous
 38          * @param {string}options.postBody message, in post request
 39          * @param {Function}options.success success callback
 40          * @param {Function}options.failure
 41          */
 42         ajax:function (url, options) {
 43             var request;
 44 
 45             function isSuccessfulRequest(request) {
 46                 return (request.status >= 200 && request.status < 300)
 47                            || request.status == 304
 48                     || (request.status == 0 && request.responseText);
 49             }
 50 
 51             function respondToReadyState() {
 52                 if (request.readyState == 4) {
 53                     if (isSuccessfulRequest(request)) {
 54                         if (options.success) {
 55                             options.success(request);
 56                         }
 57                     } else {
 58                         if (options.failure) {
 59                             options.failure(request);
 60                         }
 61                     }
 62                 }
 63             }
 64 
 65             function setHeaders() {
 66                 var name, headers = {
 67                     "Accept":"text/javascript, text/html, application/xml, text/xml, */*"
 68                 };
 69                 for (name in headers) {
 70                     request.setRequestHeader(name, headers[name]);
 71                 }
 72             }
 73 
 74             request = xhr();
 75             if (typeof options === "undefined") {
 76                 options = {};
 77             }
 78 
 79             //TODO: refactor using wef.fn.extend
 80             options.method = options.method ? options.method.toLowerCase() : "get";
 81             options.asynchronous = options.asynchronous!=="undefined" ? options.asynchronous : true;
 82             options.postBody = options.postBody || "";
 83 
 84             request.onreadystatechange = respondToReadyState;
 85             request.open(options.method, url, options.asynchronous);
 86             setHeaders();
 87             request.send(options.postBody);
 88         }
 89     };
 90 
 91     function xhr() {
 92         if (typeof XMLHttpRequest !== "undefined" && (window.location.protocol !== "file:" || !window.ActiveXObject)) {
 93             return new XMLHttpRequest();
 94         } else {
 95             try {
 96                 return new ActiveXObject("Msxml2.XMLHTTP.6.0");
 97             } catch (e) {
 98             }
 99             try {
100                 return new ActiveXObject("Msxml2.XMLHTTP.3.0");
101             } catch (e) {
102             }
103             try {
104                 return new ActiveXObject("Msxml2.XMLHTTP");
105             } catch (e) {
106             }
107         }
108         return false;
109     }
110 
111     /**
112      * Extension point
113      */
114     net.fn = net.prototype;
115 
116     net.prototype.init.prototype = net.prototype;
117 
118     wef.net = net();
119 
120 })(window.wef);
121