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 			url += (type === "GET") ? "?"+this._serialize(data) : '';
 33 
 34 			request.open(type, url);
 35 
 36 			request.onreadystatechange = function ()
 37 			{
 38 				if (request.readyState === 4)
 39 				{
 40 					if (request.status === 200)
 41 					{
 42 						success_callback.call(request.responseText, request.responseText);
 43 					}
 44 					else
 45 					{
 46 						if (typeof error_callback !== 'undefined')
 47 						{
 48 							error_callback.call(request.status, request.status);
 49 						}
 50 					}
 51 					
 52 				}
 53 			};
 54 
 55 			if (type === "POST")
 56 			{
 57 				request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 58 				request.send(this._serialize(data));
 59 			}
 60 			else
 61 			{
 62 				request.send(null);
 63 			}
 64 		},
 65 		_serialize: function (data)
 66 		{
 67 			var name,
 68 				value,
 69 				pairs = [];
 70 
 71 			for (name in data)
 72 			{
 73 				if (!data.hasOwnProperty(name))
 74 				{
 75 					continue;
 76 				}
 77 				if (typeof data[name] === "function")
 78 				{
 79 					continue;
 80 				}
 81 
 82 				value = data[name].toString();
 83 
 84 				name = encodeURIComponent(name);
 85 				value = encodeURIComponent(value);
 86 
 87 				pairs.push(name + "=" + value);
 88 			}
 89 
 90 			return pairs.join("&");
 91 		}
 92 	};
 93 
 94 	/**
 95 	 * Sends a GET type ajax request
 96 	 *
 97 	 * @name get
 98 	 * @function
 99 	 * @memberOf $_
100 	 * @param string url
101 	 * @param object data
102 	 * @param function success_callback
103 	 * @param function error_callback
104 	 */
105 	$_.ext('get', function (url, data, success_callback, error_callback){
106 		ajax._do(url, data, success_callback, error_callback, false);
107 	});
108 
109 	/**
110 	 * Sends a POST type ajax request
111 	 *
112 	 * @name post
113 	 * @function
114 	 * @memberOf $_
115 	 * @param string url
116 	 * @param object data
117 	 * @param function success_callback
118 	 * @param function error_callback
119 	 */
120 	$_.ext('post', function (url, data, success_callback, error_callback){
121 		ajax._do(url, data, success_callback, error_callback, true);
122 	});
123 	
124 	/**
125 	 * Watches for server-sent events and applies a callback on message
126 	 *
127 	 * @name sse
128 	 * @function
129 	 * @memberOf $_
130 	 * @param string url
131 	 * @param function callback
132 	 */
133 	$_.ext('sse', function(url, callback, poll_rate){
134 	
135 		var source;
136 		
137 		// Check for server-sent event support
138 		if (typeof EventSource !== 'undefined')
139 		{
140 			source = new EventSource(url);
141 			
142 			// Apply the callback
143 			source.onmessage = function(event){
144 				callback.call(event.data, event.data);	
145 			};
146 		}
147 	});
148 	
149 }());