summaryrefslogtreecommitdiff
path: root/framework/Web/Javascripts/prado/ajax3.js
blob: ded631049e6f9647c95d7cb80e4ff4101a630595 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
 * Prado Callback client-side request handler.
 */
Prado.Callback = Class.create();

/**
 * Static definitions.
 */
Object.extend(Prado.Callback,
{
	/**
	 * Callback request target POST field name.
	 */
	FIELD_CALLBACK_TARGET : 'PRADO_CALLBACK_TARGET',
	
	/**
	 * Callback request parameter POST field name.
	 */
	FIELD_CALLBACK_PARAMETER : 'PRADO_CALLBACK_PARAMETER',
	
	/**
	 * List of form fields that will be collected during callback.
	 */
	PostDataLoaders : ['PRADO_PAGESTATE'],
	
	/**
	 * Respond to Prado Callback request exceptions.
	 */
	Exception :
	{
		/**
		 * Server returns 505 exception. Just log it.
		 */
		"on505" : function(request, transport, data)
		{		
			var msg = 'HTTP '+transport.status+" with response";
			Logger.error(msg, transport.responseText);
			this.logException(data);
		},
		
		/**
		 * Callback OnComplete event,logs reponse and data to console.
		 */
		onComplete : function(request, transport, data)
		{
			if(transport.status != 505)
			{
				var msg = 'HTTP '+transport.status+" with response : \n";
				msg += transport.responseText + "\n";
				msg += "Data : \n"+inspect(data);
				Logger.warn(msg);
			}
		},
	
		/**
		 * Formats the exception message for display in console.
		 */
		formatException : function(e)
		{
			var msg = e.type + " with message \""+e.message+"\"";
			msg += " in "+e.file+"("+e.line+")\n";
			msg += "Stack trace:\n";
			var trace = e.trace;
			for(var i = 0; i<trace.length; i++)
			{
				msg += "  #"+i+" "+trace[i].file;
				msg += "("+trace[i].line+"): ";
				msg += trace[i]["class"]+"->"+trace[i]["function"]+"()"+"\n";
			}
			return msg;
		},
	
		/**
		 * Log Callback response exceptions to console.
		 */
		logException : function(e)
		{
			Logger.error("Callback Request Error "+e.code, this.formatException(e));
		}
	},
	
	/**
	 * @return string JSON encoded data.
	 */
	encode : function(data)
	{
		Prado.JSON.stringify(data);
	},
	
	/**
	 * @return mixed javascript data decoded from string using JSON decoding.
	 */
	decode : function(data)
	{
		return Prado.JSON.parse(data);
	}
})

//Add HTTP exception respones when logger is enabled.
Event.OnLoad(function()
{ 
	if(typeof Logger != "undefined") 
		Ajax.Responders.register(Prado.Callback.Exception);
});

/**
 * Create and prepare a new callback request.
 */
Prado.Callback.prototype = 
{
	/**
	 * Callback URL, same url as the current page.
	 */
	url : window.location.href,
	
	/**
	 * Callback options, including onXXX events.
	 */
	options : {},
	
	/**
	 * Callback target ID. E.g. $control->getUniqueID();
	 */
	id : null,
	
	/**
	 * Callback parameters.
	 */
	parameters : null,
	
	/**
	 * Prepare and inititate a callback request.
	 */
	initialize : function(id, parameters, onSuccess, options)
	{
		this.options = options || {};	
		this.id = id;
		this.parameters = parameters;
		
		var request = 
		{
			postBody : this._getPostData(),
			onSuccess : this._onSuccess.bind(this)
		}
		Object.extend(this.options || {},request);
		
		new Ajax.Request(this.url, this.options);
	},

	/**
	 * Collects the form inputs, encode the parameters, and sets the callback 
	 * target id. The resulting string is the request content body.
	 * @return string request body content containing post data.
	 */
	_getPostData : function()
	{
		var data = {};
		
		Prado.Callback.PostDataLoaders.each(function(name)
		{
			$A(document.getElementsByName(name)).each(function(element)
			{
				var value = $F(element);
				if(typeof(value) != "undefined")
					data[name] = value;
			})
		})
		if(typeof(this.parameters) != "undefined")
			data[Prado.Callback.FIELD_CALLBACK_PARAMETER] = Prado.Callback.encode(this.parameters);
		data[Prado.Callback.FIELD_CALLBACK_TARGET] = this.id;
		return $H(data).toQueryString();
	},
	
	/**
	 * Dispatch a successfull response to the appropriate responders.
	 */
	_onSuccess : function(response, transport, json)
	{
		//Logger.info("asd");	
	}
}