1 /**
  2  * Ajax
  3  *
  4  * Module for making ajax requests
  5  */
  6 (function (){
  7 
  8 	"use strict";
  9 
 10 	// Don't bother even defining the object if the XMLHttpRequest isn't available
 11 	if(typeof window.XMLHttpRequest === "undefined")
 12 	{
 13 		return;
 14 	}
 15 
 16 	var ajax = {
 17 		_do: function (url, data, success_callback, error_callback, isPost)
 18 		{
 19 			var type,
 20 				request = new XMLHttpRequest();
 21 
 22 			if (typeof success_callback === "undefined")
 23 			{
 24 				/**
 25 				 * @private
 26 				 */
 27 				success_callback = function (){};
 28 			}
 29 
 30 			type = (isPost) ? "POST" : "GET";
 31 
 32 			if (type === "GET")
 33 			{
 34 				url += (url.match(/\?/))
 35 					? this._serialize(data)
 36 					: "?" + this._serialize(data);
 37 			}
 38 
 39 			request.open(type, url);
 40 
 41 			request.onreadystatechange = function ()
 42 			{
 43 				if (request.readyState === 4)
 44 				{
 45 					if (request.status === 200)
 46 					{
 47 						success_callback.call(request.responseText, request.responseText);
 48 					}
 49 					else
 50 					{
 51 						if (typeof error_callback !== 'undefined')
 52 						{
 53 							error_callback.call(request.status, request.status);
 54 						}
 55 					}
 56 
 57 				}
 58 			};
 59 
 60 			if (type === "POST")
 61 			{
 62 				request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 63 				request.send(this._serialize(data));
 64 			}
 65 			else
 66 			{
 67 				request.send(null);
 68 			}
 69 		},
 70 		_serialize: function (data)
 71 		{
 72 			var name,
 73 				value,
 74 				pairs = [];
 75 
 76 			for (name in data)
 77 			{
 78 				if (!data.hasOwnProperty(name))
 79 				{
 80 					continue;
 81 				}
 82 				if (typeof data[name] === "function")
 83 				{
 84 					continue;
 85 				}
 86 
 87 				value = data[name].toString();
 88 
 89 				name = encodeURIComponent(name);
 90 				value = encodeURIComponent(value);
 91 
 92 				pairs.push(name + "=" + value);
 93 			}
 94 
 95 			return pairs.join("&");
 96 		}
 97 	};
 98 
 99 	/**
100 	 * Sends a GET type ajax request
101 	 *
102 	 * @name get
103 	 * @function
104 	 * @memberOf $_
105 	 * @param string url
106 	 * @param object data
107 	 * @param function success_callback
108 	 * @param function error_callback
109 	 */
110 	$_.ext('get', function (url, data, success_callback, error_callback){
111 		ajax._do(url, data, success_callback, error_callback, false);
112 	});
113 
114 	/**
115 	 * Sends a POST type ajax request
116 	 *
117 	 * @name post
118 	 * @function
119 	 * @memberOf $_
120 	 * @param string url
121 	 * @param object data
122 	 * @param function success_callback
123 	 * @param function error_callback
124 	 */
125 	$_.ext('post', function (url, data, success_callback, error_callback){
126 		ajax._do(url, data, success_callback, error_callback, true);
127 	});
128 
129 	/**
130 	 * Watches for server-sent events and applies a callback on message
131 	 *
132 	 * @name sse
133 	 * @function
134 	 * @memberOf $_
135 	 * @param string url
136 	 * @param function callback
137 	 */
138 	$_.ext('sse', function(url, callback){
139 
140 		var source;
141 
142 		// Check for server-sent event support
143 		if (typeof EventSource !== 'undefined')
144 		{
145 			source = new EventSource(url);
146 
147 			// Apply the callback
148 			source.onmessage = function(event){
149 				callback.call(event.data, event.data);
150 			};
151 		}
152 	});
153 
154 }());