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