From d255f4d0e332740b3984e21ce3f7a4a4f1968ba3 Mon Sep 17 00:00:00 2001 From: wei <> Date: Tue, 2 May 2006 08:28:17 +0000 Subject: Adding more work on ActiveControls. --- framework/Web/Javascripts/TJavaScript.php | 15 +- framework/Web/Javascripts/js/ajax.js | 53 ++++-- framework/Web/Javascripts/js/prado.js | 4 +- framework/Web/Javascripts/js/validator.js | 3 +- framework/Web/Javascripts/prado/ajax3.js | 213 ++++++++++++++++++++----- framework/Web/Javascripts/prado/validation3.js | 9 ++ 6 files changed, 234 insertions(+), 63 deletions(-) (limited to 'framework/Web/Javascripts') diff --git a/framework/Web/Javascripts/TJavaScript.php b/framework/Web/Javascripts/TJavaScript.php index 75fc2438..0f6414ec 100644 --- a/framework/Web/Javascripts/TJavaScript.php +++ b/framework/Web/Javascripts/TJavaScript.php @@ -23,6 +23,11 @@ */ class TJavaScript { + /** + * @var TJSON JSON decoder and encoder instance + */ + private static $_json; + /** * Renders a list of javascript files * @param array URLs to the javascript files @@ -191,8 +196,9 @@ class TJavaScript */ public static function jsonEncode($value) { - Prado::using('System.Web.Javascripts.TJSON'); - return TJSON::encode($value); + if(is_null(self::$_json)) + self::$_json = Prado::createComponent('System.Web.Javascripts.TJSON'); + return self::$_json->encode($value); } /** @@ -203,8 +209,9 @@ class TJavaScript */ public static function jsonDecode($value) { - Prado::using('System.Web.Javascripts.TJSON'); - return TJSON::decode($value); + if(is_null(self::$_json)) + self::$_json = Prado::createComponent('System.Web.Javascripts.TJSON'); + return self::$_json->decode($value); } } diff --git a/framework/Web/Javascripts/js/ajax.js b/framework/Web/Javascripts/js/ajax.js index 2b509d61..6ae737bf 100644 --- a/framework/Web/Javascripts/js/ajax.js +++ b/framework/Web/Javascripts/js/ajax.js @@ -20,28 +20,53 @@ this.transport=Ajax.getTransport();this.setOptions(options);var onComplete=this. response=response.stripScripts();if(receiver){if(this.options.insertion){new this.options.insertion(receiver,response);}else{Element.update(receiver,response);}} if(this.responseIsSuccess()){if(this.onComplete) setTimeout(this.onComplete.bind(this),10);}}});Ajax.PeriodicalUpdater=Class.create();Ajax.PeriodicalUpdater.prototype=Object.extend(new Ajax.Base(),{initialize:function(container,url,options){this.setOptions(options);this.onComplete=this.options.onComplete;this.frequency=(this.options.frequency||2);this.decay=(this.options.decay||1);this.updater={};this.container=container;this.url=url;this.start();},start:function(){this.options.onComplete=this.updateComplete.bind(this);this.onTimerEvent();},stop:function(){this.updater.onComplete=undefined;clearTimeout(this.timer);(this.onComplete||Prototype.emptyFunction).apply(this,arguments);},updateComplete:function(request){if(this.options.decay){this.decay=(request.responseText==this.lastText?this.decay*this.options.decay:1);this.lastText=request.responseText;} -this.timer=setTimeout(this.onTimerEvent.bind(this),this.decay*this.frequency*1000);},onTimerEvent:function(){this.updater=new Ajax.Updater(this.container,this.url,this.options);}});Prado.Callback=Class.create();Object.extend(Prado.Callback,{FIELD_CALLBACK_TARGET:'PRADO_CALLBACK_TARGET',FIELD_CALLBACK_PARAMETER:'PRADO_CALLBACK_PARAMETER',PostDataLoaders:['PRADO_PAGESTATE'],Exception:{"on505":function(request,transport,data) -{var msg='HTTP '+transport.status+" with response";Logger.error(msg,transport.responseText);this.logException(data);},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);}},formatException:function(e) +this.timer=setTimeout(this.onTimerEvent.bind(this),this.decay*this.frequency*1000);},onTimerEvent:function(){this.updater=new Ajax.Updater(this.container,this.url,this.options);}});Object.extend(Ajax.Request.prototype,{respondToReadyState:function(readyState) +{var event=Ajax.Request.Events[readyState];var transport=this.transport,json=this.getHeaderData(Prado.CallbackRequest.DATA_HEADER);if(event=='Complete') +{Ajax.Responders.dispatch('on'+transport.status,this,transport,json);Prado.CallbackRequest.dispatchActions(this.getHeaderData(Prado.CallbackRequest.ACTION_HEADER));try{(this.options['on'+this.transport.status]||this.options['on'+(this.responseIsSuccess()?'Success':'Failure')]||Prototype.emptyFunction)(transport,json);}catch(e){this.dispatchException(e);} +if((this.header('Content-type')||'').match(/^text\/javascript/i)) +this.evalResponse();} +try{(this.options['on'+event]||Prototype.emptyFunction)(transport,json);Ajax.Responders.dispatch('on'+event,this,transport,json);}catch(e){this.dispatchException(e);} +if(event=='Complete') +this.transport.onreadystatechange=Prototype.emptyFunction;},getHeaderData:function(name) +{try +{var json=this.header(name);return eval('('+json+')');} +catch(e) +{if(typeof(json)=="string") +{Logger.info("using json") +return Prado.CallbackRequest.decode(json);}}}});Prado.CallbackRequest=Class.create();Object.extend(Prado.CallbackRequest,{FIELD_CALLBACK_TARGET:'PRADO_CALLBACK_TARGET',FIELD_CALLBACK_PARAMETER:'PRADO_CALLBACK_PARAMETER',PostDataLoaders:['PRADO_PAGESTATE'],DATA_HEADER:'X-PRADO-DATA',ACTION_HEADER:'X-PRADO-ACTIONS',ERROR_HEADER:'X-PRADO-ERROR',dispatchActions:function(actions) +{actions.each(this.__run);},__run:function(command) +{for(var method in command) +{if(command[method][0]) +{var id=command[method][0];if($(id)||id.indexOf("[]")>-1) +method.toFunction().apply(this,command[method]);else if(typeof(Logger)!="undefined") +{Logger.error("Error in executing callback response:","Unable to find HTML element with ID '"+id+"' before executing "+method+"().");}}}},Exception:{"on505":function(request,transport,data) +{var e=request.getHeaderData(Prado.CallbackRequest.ERROR_HEADER);Logger.error("Callback Server Error "+e.code,this.formatException(e));},'on200':function(request,transport,data) +{if(transport.status<500) +{var msg='HTTP '+transport.status+" with response : \n";msg+=transport.responseText+"\n";msg+="Data : \n"+inspect(data)+"\n";msg+="Actions : \n";request.getHeaderData(Prado.CallbackRequest.ACTION_HEADER).each(function(action) +{msg+=inspect(action)+"\n";}) +Logger.warn(msg);}},onException:function(e) +{Logger.error('Uncaught Callback Client Exception:',e);},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[i]["function"]+"()"+"\n";} -return msg;},logException:function(e) -{Logger.error("Callback Request Error "+e.code,this.formatException(e));}},encode:function(data) -{Prado.JSON.stringify(data);},decode:function(data) +msg+=e.version+" "+e.time+"\n";return msg;}},encode:function(data) +{return Prado.JSON.stringify(data);},decode:function(data) {return Prado.JSON.parse(data);}}) Event.OnLoad(function() {if(typeof Logger!="undefined") -Ajax.Responders.register(Prado.Callback.Exception);});Prado.Callback.prototype={url:window.location.href,options:{},id:null,parameters:null,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);},_getPostData:function() -{var data={};Prado.Callback.PostDataLoaders.each(function(name) +Ajax.Responders.register(Prado.CallbackRequest.Exception);});Prado.CallbackRequest.prototype={url:window.location.href,options:{},id:null,request:null,initialize:function(id,options) +{this.options=options||{};this.id=id;var request={postBody:this._getPostData(),parameters:''} +Object.extend(this.options||{},request);if(this.options.CausesValidation!=false&&typeof(Prado.Validation)!="undefined") +{var form=this.options.Form||Prado.Validation.getForm();if(Prado.Validation.validate(form,this.options.ValidationGroup,this)==false) +return;} +this.request=new Ajax.Request(this.url,this.options);},_getPostData:function() +{var data={};Prado.CallbackRequest.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();},_onSuccess:function(response,transport,json) -{}} +if(typeof(this.options.params)!="undefined") +data[Prado.CallbackRequest.FIELD_CALLBACK_PARAMETER]=Prado.CallbackRequest.encode(this.options.params);data[Prado.CallbackRequest.FIELD_CALLBACK_TARGET]=this.id;return $H(data).toQueryString();}} +Prado.Callback=function(UniqueID,parameter,onSuccess,options) +{var callback={'params':parameter||'','onSuccess':onSuccess||Prototype.emptyFunction,'CausesValidation':true};Object.extend(callback,options||{});new Prado.CallbackRequest(UniqueID,callback);return false;} Array.prototype.______array='______array';Prado.JSON={org:'http://www.JSON.org',copyright:'(c)2005 JSON.org',license:'http://www.crockford.com/JSON/license.html',stringify:function(arg){var c,i,l,s='',v;switch(typeof arg){case'object':if(arg){if(arg.______array=='______array'){for(i=0;i0) el.remove(0);for(var i=0;i -1) + method.toFunction().apply(this,command[method]); + else if(typeof(Logger) != "undefined") + { + Logger.error("Error in executing callback response:", + "Unable to find HTML element with ID '"+id+"' before executing "+method+"()."); + } + } + } + }, /** * Respond to Prado Callback request exceptions. @@ -32,26 +136,39 @@ Object.extend(Prado.Callback, * 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); + { + var e = request.getHeaderData(Prado.CallbackRequest.ERROR_HEADER); + Logger.error("Callback Server Error "+e.code, this.formatException(e)); }, /** * Callback OnComplete event,logs reponse and data to console. */ - onComplete : function(request, transport, data) + 'on200' : function(request, transport, data) { - if(transport.status != 505) + if(transport.status < 500) { var msg = 'HTTP '+transport.status+" with response : \n"; msg += transport.responseText + "\n"; - msg += "Data : \n"+inspect(data); + msg += "Data : \n"+inspect(data)+"\n"; + msg += "Actions : \n"; + request.getHeaderData(Prado.CallbackRequest.ACTION_HEADER).each(function(action) + { + msg += inspect(action)+"\n"; + }) + Logger.warn(msg); } }, + /** + * Uncaught exceptions during callback response. + */ + onException : function(e) + { + Logger.error('Uncaught Callback Client Exception:', e); + }, + /** * Formats the exception message for display in console. */ @@ -67,15 +184,8 @@ Object.extend(Prado.Callback, msg += "("+trace[i].line+"): "; msg += trace[i]["class"]+"->"+trace[i]["function"]+"()"+"\n"; } + msg += e.version+" "+e.time+"\n"; return msg; - }, - - /** - * Log Callback response exceptions to console. - */ - logException : function(e) - { - Logger.error("Callback Request Error "+e.code, this.formatException(e)); } }, @@ -84,7 +194,7 @@ Object.extend(Prado.Callback, */ encode : function(data) { - Prado.JSON.stringify(data); + return Prado.JSON.stringify(data); }, /** @@ -100,13 +210,13 @@ Object.extend(Prado.Callback, Event.OnLoad(function() { if(typeof Logger != "undefined") - Ajax.Responders.register(Prado.Callback.Exception); + Ajax.Responders.register(Prado.CallbackRequest.Exception); }); /** * Create and prepare a new callback request. */ -Prado.Callback.prototype = +Prado.CallbackRequest.prototype = { /** * Callback URL, same url as the current page. @@ -124,27 +234,31 @@ Prado.Callback.prototype = id : null, /** - * Callback parameters. + * Current callback request. */ - parameters : null, + request : null, /** * Prepare and inititate a callback request. */ - initialize : function(id, parameters, onSuccess, options) + initialize : function(id, options) { this.options = options || {}; this.id = id; - this.parameters = parameters; var request = { postBody : this._getPostData(), - onSuccess : this._onSuccess.bind(this) + parameters : '' } Object.extend(this.options || {},request); - - new Ajax.Request(this.url, this.options); + if(this.options.CausesValidation != false && typeof(Prado.Validation) != "undefined") + { + var form = this.options.Form || Prado.Validation.getForm(); + if(Prado.Validation.validate(form,this.options.ValidationGroup,this) == false) + return; + } + this.request = new Ajax.Request(this.url, this.options); }, /** @@ -156,7 +270,7 @@ Prado.Callback.prototype = { var data = {}; - Prado.Callback.PostDataLoaders.each(function(name) + Prado.CallbackRequest.PostDataLoaders.each(function(name) { $A(document.getElementsByName(name)).each(function(element) { @@ -165,17 +279,32 @@ Prado.Callback.prototype = 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; + if(typeof(this.options.params) != "undefined") + data[Prado.CallbackRequest.FIELD_CALLBACK_PARAMETER] = Prado.CallbackRequest.encode(this.options.params); + data[Prado.CallbackRequest.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"); } -} \ No newline at end of file +} + +/** + * Create a new callback request using default settings. + * @param string callback handler unique ID. + * @param mixed parameter to pass to callback handler on the server side. + * @param function client side onSuccess event handler. + * @param object additional request options. + * @return boolean always false. + */ +Prado.Callback = function(UniqueID, parameter, onSuccess, options) +{ + var callback = + { + 'params' : parameter || '', + 'onSuccess' : onSuccess || Prototype.emptyFunction, + 'CausesValidation' : true + }; + + Object.extend(callback, options || {}); + + new Prado.CallbackRequest(UniqueID, callback); + return false; +} diff --git a/framework/Web/Javascripts/prado/validation3.js b/framework/Web/Javascripts/prado/validation3.js index 1f154fec..8becac70 100644 --- a/framework/Web/Javascripts/prado/validation3.js +++ b/framework/Web/Javascripts/prado/validation3.js @@ -89,6 +89,15 @@ Object.extend(Prado.Validation, } }, + /** + * @return string first form ID. + */ + getForm : function() + { + var keys = $H(this.managers).keys(); + return keys[0]; + }, + /** * Check if the validators are valid for a particular form (and group). * The validators states will not be changed. -- cgit v1.2.3