]>
git.proxmox.com Git - extjs.git/blob - extjs/packages/core/test/resources/ux/ajax/Simlet.js
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.
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+$/;
11 function parseParamValue(value
) {
14 if (Ext
.isDefined(value
)) {
15 value
= decodeURIComponent(value
);
17 if (intRegex
.test(value
)) {
18 value
= parseInt(value
, 10);
20 else if (floatRegex
.test(value
)) {
21 value
= parseFloat(value
);
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]));
32 alias
: 'simlet.basic',
36 responseProps
: ['responseText', 'responseXML', 'status', 'statusText'],
39 * @cfg {Number} responseText
43 * @cfg {Number} responseXML
47 * @cfg {Object} responseHeaders
51 * @cfg {Number} status
56 * @cfg {String} statusText
60 constructor: function(config
) {
61 Ext
.apply(this, config
);
64 doGet: function(ctx
) {
68 Ext
.each(me
.responseProps
, function(prop
) {
77 doRedirect: function(ctx
) {
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`.
86 * @param {Ext.ux.ajax.SimXhr} xhr The simulated XMLHttpRequest instance.
87 * @return {Object} The response properties to add to the XMLHttpRequest.
92 method
= 'do' + Ext
.String
.capitalize(xhr
.method
.toLowerCase()), // doGet
96 ret
= fn
.call(me
, me
.getCtx(xhr
.method
, xhr
.url
, xhr
));
99 ret
= { status
: 405, statusText
: 'Method Not Allowed' };
105 getCtx: function(method
, url
, xhr
) {
108 params
: this.parseQueryString(url
),
114 openRequest: function(method
, url
, options
, async
) {
115 var ctx
= this.getCtx(method
, url
),
116 redirect
= this.doRedirect(ctx
),
123 xhr
= new Ext
.ux
.ajax
.SimXhr({
128 xhr
.open(method
, url
, async
);
134 parseQueryString: function(str
) {
135 var m
= urlRegex
.exec(str
),
143 parts
= m
[1].split('&');
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]);
153 else if (Ext
.isArray(ret
[key
])) {
154 ret
[key
].push(value
);
157 ret
[key
] = [ret
[key
], value
];
166 redirect: function(method
, url
, params
) {
167 switch (arguments
.length
) {
169 if (typeof url
=== 'string') {
176 // eslint-disable-next-line no-fallthrough
184 url
= Ext
.urlAppend(url
, Ext
.Object
.toQueryString(params
));
187 return this.manager
.openRequest(method
, url
);