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