Simlet.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * @author Don Griffin
  3. *
  4. * This is a base class for more advanced "simlets" (simulated servers). A simlet is asked
  5. * to provide a response given a {@link Ext.ux.ajax.SimXhr} instance.
  6. */
  7. Ext.define('Ext.ux.ajax.Simlet', function () {
  8. var urlRegex = /([^?#]*)(#.*)?$/,
  9. dateRegex = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/,
  10. intRegex = /^[+-]?\d+$/,
  11. floatRegex = /^[+-]?\d+\.\d+$/;
  12. function parseParamValue (value) {
  13. var m;
  14. if (Ext.isDefined(value)) {
  15. value = decodeURIComponent(value);
  16. if (intRegex.test(value)) {
  17. value = parseInt(value, 10);
  18. } else if (floatRegex.test(value)) {
  19. value = parseFloat(value);
  20. } else if (!!(m = dateRegex.test(value))) {
  21. value = new Date(Date.UTC(+m[1], +m[2]-1, +m[3], +m[4], +m[5], +m[6]));
  22. }
  23. }
  24. return value;
  25. }
  26. return {
  27. alias: 'simlet.basic',
  28. isSimlet: true,
  29. responseProps: ['responseText', 'responseXML', 'status', 'statusText'],
  30. /**
  31. * @cfg {Number} responseText
  32. */
  33. /**
  34. * @cfg {Number} responseXML
  35. */
  36. /**
  37. * @cfg {Object} responseHeaders
  38. */
  39. /**
  40. * @cfg {Number} status
  41. */
  42. status: 200,
  43. /**
  44. * @cfg {String} statusText
  45. */
  46. statusText: 'OK',
  47. constructor: function (config) {
  48. Ext.apply(this, config);
  49. },
  50. doGet: function (ctx) {
  51. var me = this,
  52. ret = {};
  53. Ext.each(me.responseProps, function (prop) {
  54. if (prop in me) {
  55. ret[prop] = me[prop];
  56. }
  57. });
  58. return ret;
  59. },
  60. doRedirect: function (ctx) {
  61. return false;
  62. },
  63. /**
  64. * Performs the action requested by the given XHR and returns an object to be applied
  65. * on to the XHR (containing `status`, `responseText`, etc.). For the most part,
  66. * this is delegated to `doMethod` methods on this class, such as `doGet`.
  67. *
  68. * @param {Ext.ux.ajax.SimXhr} xhr The simulated XMLHttpRequest instance.
  69. * @returns {Object} The response properties to add to the XMLHttpRequest.
  70. */
  71. exec: function (xhr) {
  72. var me = this,
  73. ret = {},
  74. method = 'do' + Ext.String.capitalize(xhr.method.toLowerCase()), // doGet
  75. fn = me[method];
  76. if (fn) {
  77. ret = fn.call(me, me.getCtx(xhr.method, xhr.url, xhr));
  78. } else {
  79. ret = { status: 405, statusText: 'Method Not Allowed' };
  80. }
  81. return ret;
  82. },
  83. getCtx: function (method, url, xhr) {
  84. return {
  85. method: method,
  86. params: this.parseQueryString(url),
  87. url: url,
  88. xhr: xhr
  89. };
  90. },
  91. openRequest: function (method, url, options, async) {
  92. var ctx = this.getCtx(method, url),
  93. redirect = this.doRedirect(ctx),
  94. xhr;
  95. if (redirect) {
  96. xhr = redirect;
  97. } else {
  98. xhr = new Ext.ux.ajax.SimXhr({
  99. mgr: this.manager,
  100. simlet: this,
  101. options: options
  102. });
  103. xhr.open(method, url, async);
  104. }
  105. return xhr;
  106. },
  107. parseQueryString : function (str) {
  108. var m = urlRegex.exec(str),
  109. ret = {},
  110. key,
  111. value,
  112. i, n;
  113. if (m && m[1]) {
  114. var pair, parts = m[1].split('&');
  115. for (i = 0, n = parts.length; i < n; ++i) {
  116. if ((pair = parts[i].split('='))[0]) {
  117. key = decodeURIComponent(pair.shift());
  118. value = parseParamValue((pair.length > 1) ? pair.join('=') : pair[0]);
  119. if (!(key in ret)) {
  120. ret[key] = value;
  121. } else if (Ext.isArray(ret[key])) {
  122. ret[key].push(value);
  123. } else {
  124. ret[key] = [ret[key], value];
  125. }
  126. }
  127. }
  128. }
  129. return ret;
  130. },
  131. redirect: function (method, url, params) {
  132. switch (arguments.length) {
  133. case 2:
  134. if (typeof url == 'string') {
  135. break;
  136. }
  137. params = url;
  138. // fall...
  139. case 1:
  140. url = method;
  141. method = 'GET';
  142. break;
  143. }
  144. if (params) {
  145. url = Ext.urlAppend(url, Ext.Object.toQueryString(params));
  146. }
  147. return this.manager.openRequest(method, url);
  148. }
  149. };
  150. }());