Simlet.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. This file is part of Ext JS 4
  3. Copyright (c) 2011 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  7. If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
  8. */
  9. /**
  10. * @author Don Griffin
  11. * @class Ext.ux.ajax.Simlet
  12. *
  13. * This is a base class for more advanced "simlets" (simulated servers). A simlet is asked
  14. * to provide a response given a {@link SimXhr} instance.
  15. */
  16. Ext.define('Ext.ux.ajax.Simlet', function () {
  17. var urlRegex = /([^?#]*)(#.*)?$/,
  18. dateRegex = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/,
  19. intRegex = /^[+-]?\d+$/,
  20. floatRegex = /^[+-]?\d+\.\d+$/;
  21. function parseParamValue (value) {
  22. var m;
  23. if (Ext.isDefined(value)) {
  24. value = decodeURIComponent(value);
  25. if (intRegex.test(value)) {
  26. value = parseInt(value, 10);
  27. } else if (floatRegex.test(value)) {
  28. value = parseFloat(value);
  29. } else if (!!(m = dateRegex.test(value))) {
  30. value = new Date(Date.UTC(+m[1], +m[2]-1, +m[3], +m[4], +m[5], +m[6]));
  31. }
  32. }
  33. return value;
  34. }
  35. return {
  36. alias: 'simlet.basic',
  37. isSimlet: true,
  38. responseProps: ['responseText', 'responseXML', 'status', 'statusText'],
  39. /**
  40. * @cfg {Number} responseText
  41. */
  42. /**
  43. * @cfg {Number} responseXML
  44. */
  45. /**
  46. * @cfg {Object} responseHeaders
  47. */
  48. /**
  49. * @cfg {Number} status
  50. */
  51. status: 200,
  52. /**
  53. * @cfg {String} statusText
  54. */
  55. statusText: 'OK',
  56. constructor: function (config) {
  57. Ext.apply(this, config);
  58. },
  59. doGet: function (ctx) {
  60. var me = this,
  61. ret = {};
  62. Ext.each(me.responseProps, function (prop) {
  63. if (prop in me) {
  64. ret[prop] = me[prop];
  65. }
  66. });
  67. return ret;
  68. },
  69. /**
  70. * Performs the action requested by the given XHR and returns an object to be applied
  71. * on to the XHR (containing `status`, `responseText`, etc.). For the most part,
  72. * this is delegated to `doMethod` methods on this class, such as `doGet`.
  73. *
  74. * @param {SimXhr} xhr The simulated XMLHttpRequest instance.
  75. * @returns {Object} The response properties to add to the XMLHttpRequest.
  76. * @markdown
  77. */
  78. exec: function (xhr) {
  79. var me = this,
  80. ret = {},
  81. method = 'do' + Ext.String.capitalize(xhr.method.toLowerCase()), // doGet
  82. fn = me[method];
  83. if (fn) {
  84. ret = fn.call(me, {
  85. xhr: xhr,
  86. params: me.parseQueryString(xhr.url)
  87. });
  88. } else {
  89. ret = { status: 405, statusText: 'Method Not Allowed' };
  90. }
  91. return ret;
  92. },
  93. parseQueryString : function (str) {
  94. var m = urlRegex.exec(str),
  95. ret = {},
  96. key,
  97. value,
  98. i, n;
  99. if (m && m[1]) {
  100. var pair, parts = m[1].split('&');
  101. for (i = 0, n = parts.length; i < n; ++i) {
  102. if ((pair = parts[i].split('='))[0]) {
  103. key = decodeURIComponent(pair.shift());
  104. value = parseParamValue((pair.length > 1) ? pair.join('=') : pair[0]);
  105. if (!(key in ret)) {
  106. ret[key] = value;
  107. } else if (Ext.isArray(ret[key])) {
  108. ret[key].push(value);
  109. } else {
  110. ret[key] = [ret[key], value];
  111. }
  112. }
  113. }
  114. }
  115. return ret;
  116. }
  117. };
  118. }());