PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/wwwroot/vendor/jquery.mockjax.js

http://github.com/AF83/ucengine
JavaScript | 326 lines | 236 code | 23 blank | 67 comment | 105 complexity | 3f40de34b25945148015236ddb3579d0 MD5 | raw file
  1. /*!
  2. * MockJax - jQuery Plugin to Mock Ajax requests
  3. *
  4. * Version: 1.3.2
  5. * Released: 2010-10-07
  6. * Source: http://github.com/appendto/jquery-mockjax
  7. * Docs: http://enterprisejquery.com/2010/07/mock-your-ajax-requests-with-mockjax-for-rapid-development
  8. * Plugin: mockjax
  9. * Author: Jonathan Sharp (http://jdsharp.com)
  10. * License: MIT,GPL
  11. *
  12. * Copyright (c) 2010 appendTo LLC.
  13. * Dual licensed under the MIT or GPL licenses.
  14. * http://appendto.com/open-source-licenses
  15. */
  16. (function($) {
  17. var _ajax = $.ajax,
  18. mockHandlers = [];
  19. $.extend({
  20. ajax: function(origSettings) {
  21. var s = jQuery.extend(true, {}, jQuery.ajaxSettings, origSettings),
  22. mock = false;
  23. // Iterate over our mock handlers (in registration order) until we find
  24. // one that is willing to intercept the request
  25. $.each(mockHandlers, function(k, v) {
  26. if ( !mockHandlers[k] ) {
  27. return;
  28. }
  29. var m = null;
  30. // If the mock was registered with a function, let the function decide if we
  31. // want to mock this request
  32. if ( $.isFunction(mockHandlers[k]) ) {
  33. m = mockHandlers[k](s);
  34. } else {
  35. m = mockHandlers[k];
  36. // Inspect the URL of the request and check if the mock handler's url
  37. // matches the url for this ajax request
  38. if ( $.isFunction(m.url.test) ) {
  39. // The user provided a regex for the url, test it
  40. if ( !m.url.test( s.url ) ) {
  41. m = null;
  42. }
  43. } else {
  44. // Look for a simple wildcard '*' or a direct URL match
  45. var star = m.url.indexOf('*');
  46. if ( ( m.url != '*' && m.url != s.url && star == -1 ) ||
  47. ( star > -1 && m.url.substr(0, star) != s.url.substr(0, star) ) ) {
  48. // The url we tested did not match the wildcard *
  49. m = null;
  50. }
  51. }
  52. if ( m ) {
  53. // Inspect the data submitted in the request (either POST body or GET query string)
  54. if ( m.data && s.data ) {
  55. var identical = false;
  56. // Deep inspect the identity of the objects
  57. (function ident(mock, live) {
  58. // Test for situations where the data is a querystring (not an object)
  59. if (typeof live === 'string') {
  60. // Querystring may be a regex
  61. identical = $.isFunction( mock.test ) ? mock.test(live) : mock == live;
  62. return identical;
  63. }
  64. $.each(mock, function(k, v) {
  65. if ( live[k] === undefined ) {
  66. identical = false;
  67. return false;
  68. } else {
  69. identical = true;
  70. if ( typeof live[k] == 'object' ) {
  71. return ident(mock[k], live[k]);
  72. } else {
  73. if ( $.isFunction( mock[k].test ) ) {
  74. identical = mock[k].test(live[k]);
  75. } else {
  76. identical = ( mock[k] == live[k] );
  77. }
  78. return identical;
  79. }
  80. }
  81. });
  82. })(m.data, s.data);
  83. // They're not identical, do not mock this request
  84. if ( identical == false ) {
  85. m = null;
  86. }
  87. }
  88. // Inspect the request type
  89. if ( m && m.type && m.type != s.type ) {
  90. // The request type doesn't match (GET vs. POST)
  91. m = null;
  92. }
  93. }
  94. }
  95. if ( m ) {
  96. if ( typeof console !== 'undefined' && console.log ) {
  97. console.log('MOCK GET: ' + s.url);
  98. }
  99. mock = true;
  100. // Handle JSONP Parameter Callbacks, we need to replicate some of the jQuery core here
  101. // because there isn't an easy hook for the cross domain script tag of jsonp
  102. if ( s.dataType === "jsonp" ) {
  103. if ( s.type.toUpperCase() === "GET" ) {
  104. if ( !jsre.test( s.url ) ) {
  105. s.url += (rquery.test( s.url ) ? "&" : "?") + (s.jsonp || "callback") + "=?";
  106. }
  107. } else if ( !s.data || !jsre.test(s.data) ) {
  108. s.data = (s.data ? s.data + "&" : "") + (s.jsonp || "callback") + "=?";
  109. }
  110. s.dataType = "json";
  111. }
  112. // Build temporary JSONP function
  113. var jsre = /=\?(&|$)/;
  114. if ( s.dataType === "json" && (s.data && jsre.test(s.data) || jsre.test(s.url)) ) {
  115. jsonp = s.jsonpCallback || ("jsonp" + jsc++);
  116. // Replace the =? sequence both in the query string and the data
  117. if ( s.data ) {
  118. s.data = (s.data + "").replace(jsre, "=" + jsonp + "$1");
  119. }
  120. s.url = s.url.replace(jsre, "=" + jsonp + "$1");
  121. // We need to make sure
  122. // that a JSONP style response is executed properly
  123. s.dataType = "script";
  124. // Handle JSONP-style loading
  125. window[ jsonp ] = window[ jsonp ] || function( tmp ) {
  126. data = tmp;
  127. success();
  128. complete();
  129. // Garbage collect
  130. window[ jsonp ] = undefined;
  131. try {
  132. delete window[ jsonp ];
  133. } catch(e) {}
  134. if ( head ) {
  135. head.removeChild( script );
  136. }
  137. };
  138. }
  139. var rurl = /^(\w+:)?\/\/([^\/?#]+)/,
  140. parts = rurl.exec( s.url ),
  141. remote = parts && (parts[1] && parts[1] !== location.protocol || parts[2] !== location.host);
  142. // Test if we are going to create a script tag (if so, intercept & mock)
  143. if ( s.dataType === "script" && s.type.toUpperCase() === "GET" && remote ) {
  144. // Synthesize the mock request for adding a script tag
  145. var callbackContext = origSettings && origSettings.context || s;
  146. function success() {
  147. // If a local callback was specified, fire it and pass it the data
  148. if ( s.success ) {
  149. s.success.call( callbackContext, ( m.response ? m.response.toString() : m.responseText || ''), status, {} );
  150. }
  151. // Fire the global callback
  152. if ( s.global ) {
  153. trigger( "ajaxSuccess", [{}, s] );
  154. }
  155. }
  156. function complete() {
  157. // Process result
  158. if ( s.complete ) {
  159. s.complete.call( callbackContext, {} , status );
  160. }
  161. // The request was completed
  162. if ( s.global ) {
  163. trigger( "ajaxComplete", [{}, s] );
  164. }
  165. // Handle the global AJAX counter
  166. if ( s.global && ! --jQuery.active ) {
  167. jQuery.event.trigger( "ajaxStop" );
  168. }
  169. }
  170. function trigger(type, args) {
  171. (s.context ? jQuery(s.context) : jQuery.event).trigger(type, args);
  172. }
  173. //if ( m.response && $.isFunction(m.response) ) {
  174. // m.response();
  175. //} else {
  176. $.globalEval(m.responseText);
  177. //}
  178. success();
  179. complete();
  180. return false;
  181. }
  182. mock = _ajax.call($, $.extend(true, {}, origSettings, {
  183. // Mock the XHR object
  184. xhr: function() {
  185. // Extend with our default mockjax settings
  186. m = $.extend({}, $.mockjaxSettings, m);
  187. // Return our mock xhr object
  188. return {
  189. status: m.status,
  190. readyState: 1,
  191. open: function() { },
  192. send: function() {
  193. var process = $.proxy(function() {
  194. // The request has returned
  195. this.status = m.status;
  196. this.readyState = 4;
  197. // We have an executable function, call it to give
  198. // the mock a chance to update it's data
  199. if ( $.isFunction(m.response) ) {
  200. m.response();
  201. }
  202. // Copy over our mock to our xhr object before passing control back to
  203. // jQuery's onreadystatechange callback
  204. if ( s.dataType == 'json' && ( typeof m.responseText == 'object' ) ) {
  205. this.responseText = JSON.stringify(m.responseText);
  206. } else if ( s.dataType == 'xml' ) {
  207. if ( $.xmlDOM && typeof m.responseXML == 'string' ) {
  208. // Parse the XML from a string into a DOM
  209. this.responseXML = $.xmlDOM( m.responseXML )[0];
  210. } else {
  211. this.responseXML = m.responseXML;
  212. }
  213. } else {
  214. this.responseText = m.responseText;
  215. }
  216. this.onreadystatechange( m.isTimeout ? 'timeout' : undefined );
  217. }, this);
  218. if ( m.proxy ) {
  219. // We're proxying this request and loading in an external file instead
  220. _ajax({
  221. global: false,
  222. url: m.proxy,
  223. type: m.type,
  224. data: m.data,
  225. dataType: s.dataType,
  226. complete: function(xhr, txt) {
  227. m.responseXML = xhr.responseXML;
  228. m.responseText = xhr.responseText;
  229. this.responseTimer = setTimeout(process, m.responseTime || 0);
  230. }
  231. });
  232. } else {
  233. // type == 'POST' || 'GET' || 'DELETE'
  234. if ( s.async === false ) {
  235. // TODO: Blocking delay
  236. process();
  237. } else {
  238. this.responseTimer = setTimeout(process, m.responseTime || 50);
  239. }
  240. }
  241. },
  242. abort: function() {
  243. clearTimeout(this.responseTimer);
  244. },
  245. setRequestHeader: function() { },
  246. getResponseHeader: function(header) {
  247. // 'Last-modified', 'Etag', 'content-type' are all checked by jQuery
  248. if ( m.headers && m.headers[header] ) {
  249. // Return arbitrary headers
  250. return m.headers[header];
  251. } else if ( header == 'Last-modified' ) {
  252. return m.lastModified || (new Date()).toString();
  253. } else if ( header == 'Etag' ) {
  254. return m.etag || '';
  255. } else if ( header == 'content-type' ) {
  256. return m.contentType || 'text/plain';
  257. }
  258. }
  259. };
  260. }
  261. }));
  262. return false;
  263. }
  264. });
  265. // We don't have a mock request, trigger a normal request
  266. if ( !mock ) {
  267. return _ajax.apply($, arguments);
  268. } else {
  269. return mock;
  270. }
  271. }
  272. });
  273. $.mockjaxSettings = {
  274. //url: null,
  275. //type: 'GET',
  276. status: 200,
  277. responseTime: 500,
  278. isTimeout: false,
  279. contentType: 'text/plain',
  280. response: '',
  281. responseText: '',
  282. responseXML: '',
  283. proxy: '',
  284. lastModified: null,
  285. etag: '',
  286. headers: {
  287. etag: 'IJF@H#@923uf8023hFO@I#H#',
  288. 'content-type' : 'text/plain'
  289. }
  290. };
  291. $.mockjax = function(settings) {
  292. var i = mockHandlers.length;
  293. mockHandlers[i] = settings;
  294. return i;
  295. };
  296. $.mockjaxClear = function(i) {
  297. if ( arguments.length == 1 ) {
  298. mockHandlers[i] = null;
  299. } else {
  300. mockHandlers = [];
  301. }
  302. };
  303. })(jQuery);