You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2010/03/16 23:19:03 UTC
svn commit: r924025 - in
/tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5:
corelib/components/datefield.js tapestry.js
Author: hlship
Date: Tue Mar 16 22:19:03 2010
New Revision: 924025
URL: http://svn.apache.org/viewvc?rev=924025&view=rev
Log:
TAP5-863: Tapestry.ajaxRequest is insufficient when doing extra, such as adding query parameters
Modified:
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/components/datefield.js
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/tapestry.js
Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/components/datefield.js
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/components/datefield.js?rev=924025&r1=924024&r2=924025&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/components/datefield.js (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/components/datefield.js Tue Mar 16 22:19:03 2010
@@ -12,207 +12,185 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-Tapestry.DateField = Class.create({
+Tapestry.DateField = Class.create( {
- // Initializes a DateField from a JSON specification.
+ // Initializes a DateField from a JSON specification.
- initialize : function(spec)
- {
- this.field = $(spec.field);
- this.trigger = $(spec.field + "-trigger");
- this.parseURL = spec.parseURL;
- this.formatURL = spec.formatURL;
+ initialize : function(spec) {
+ this.field = $(spec.field);
+ this.trigger = $(spec.field + "-trigger");
+ this.parseURL = spec.parseURL;
+ this.formatURL = spec.formatURL;
- this.trigger.observe("click", this.triggerClicked.bind(this));
+ this.trigger.observe("click", this.triggerClicked.bind(this));
- this.popup = null;
+ this.popup = null;
- if (spec.localization)
- {
- DatePicker.months = spec.localization.months;
- DatePicker.days = spec.localization.days.toArray();
+ if (spec.localization) {
+ DatePicker.months = spec.localization.months;
+ DatePicker.days = spec.localization.days.toArray();
- Tapestry.DateField.prototype.firstDay = spec.localization.firstDay;
- }
- },
+ Tapestry.DateField.prototype.firstDay = spec.localization.firstDay;
+ }
+ },
- triggerClicked : function()
- {
- if (this.field.disabled) return;
+ triggerClicked : function() {
+ if (this.field.disabled)
+ return;
- if (this.popup == null)
- {
- this.createPopup();
+ if (this.popup == null) {
+ this.createPopup();
- }
- else
- {
- if (this.popup.visible())
- {
- this.hidePopup();
- return;
- }
- }
+ } else {
+ if (this.popup.visible()) {
+ this.hidePopup();
+ return;
+ }
+ }
+ var value = $F(this.field);
- var value = $F(this.field);
+ if (value == "") {
+ this.datePicker.setDate(null);
- if (value == "")
- {
- this.datePicker.setDate(null);
+ this.positionPopup();
- this.positionPopup();
+ this.revealPopup();
- this.revealPopup();
+ return;
+ }
- return;
- }
+ var resultHandler = function(result) {
+ var date = new Date();
- var resultHandler = function(result)
- {
- var date = new Date();
+ date.setTime(result);
- date.setTime(result);
+ this.datePicker.setDate(date);
- this.datePicker.setDate(date);
+ this.positionPopup();
- this.positionPopup();
+ this.revealPopup();
+ };
- this.revealPopup();
- };
+ var errorHandler = function(message) {
+ this.field.showValidationMessage(message);
+ this.field.activate();
+ };
- var errorHandler = function(message)
- {
- this.field.showValidationMessage(message);
- this.field.activate();
- };
+ this.sendServerRequest(this.parseURL, value, resultHandler,
+ errorHandler);
+ },
- this.sendServerRequest(this.parseURL, value, resultHandler, errorHandler);
- },
+ sendServerRequest : function(url, input, resultHandler, errorHandler) {
+ var successHandler = function(response) {
+ var json = response.responseJSON;
- sendServerRequest : function (url, input, resultHandler, errorHandler)
- {
- var successHandler = function(response)
- {
- var json = response.responseJSON;
+ var result = json.result;
- var result = json.result;
+ if (result) {
+ resultHandler.call(this, result);
+ return;
+ }
- if (result)
- {
- resultHandler.call(this, result);
- return;
- }
+ errorHandler.call(this, json.error);
+ }.bind(this);
- errorHandler.call(this, json.error);
- }.bind(this);
+ Tapestry.ajaxRequest(url, {
+ method : 'get',
+ parameters : {
+ input : input
+ },
+ onSuccess : successHandler
+ });
+ },
- new Ajax.Request(url,
- {
- method: 'get',
- parameters: {
- input: input
- },
- onSuccess: successHandler,
- onFailure: Tapestry.ajaxFailureHandler
- });
- },
+ createPopup : function() {
+ this.datePicker = new DatePicker();
- createPopup : function()
- {
- this.datePicker = new DatePicker();
+ this.datePicker.setFirstWeekDay(this.firstDay);
- this.datePicker.setFirstWeekDay(this.firstDay);
+ this.popup = $(this.datePicker.create());
- this.popup = $(this.datePicker.create());
+ this.field.insert( {
+ after : this.popup
+ });
- this.field.insert({
- after : this.popup
- });
+ this.popup.absolutize().hide();
- this.popup.absolutize().hide();
+ this.datePicker.onselect = function() {
+ var date = this.datePicker.getDate();
- this.datePicker.onselect = function()
- {
- var date = this.datePicker.getDate();
+ var resultHandler = function(result) {
+ this.field.value = result;
- var resultHandler = function(result)
- {
- this.field.value = result;
+ this.hidePopup();
- this.hidePopup();
+ new Effect.Highlight(this.field);
+ };
- new Effect.Highlight(this.field);
- };
+ var errorHandler = function(message) {
+ this.field.showValidationMessage(message);
+ this.field.activate();
- var errorHandler = function(message)
- {
- this.field.showValidationMessage(message);
- this.field.activate();
+ this.hidePopup();
+ };
- this.hidePopup();
- };
+ // If the field is blank, don't bother going to the server to parse!
- // If the field is blank, don't bother going to the server to parse!
+ if (date == null) {
+ resultHandler.call(this, "");
+ return;
+ }
- if (date == null)
- {
- resultHandler.call(this, "");
- return;
- }
+ this.sendServerRequest(this.formatURL, date.getTime(),
+ resultHandler, errorHandler);
+ }.bind(this);
+ },
- this.sendServerRequest(this.formatURL, date.getTime(), resultHandler, errorHandler);
- }.bind(this);
- },
+ positionPopup : function() {
+ // The field may be a hidden field, in which csae, position the popup
+ // based on the trigger, not
+ // the hidden.
- positionPopup : function()
- {
- // The field may be a hidden field, in which csae, position the popup based on the trigger, not
- // the hidden.
+ var reference = this.field.type == "text" ? this.field : this.trigger;
- var reference = this.field.type == "text" ? this.field : this.trigger;
+ this.popup.clonePosition(reference, {
+ offsetTop : reference.getHeight() + 2
+ }).setStyle( {
+ width : "",
+ height : ""
+ });
+ },
- this.popup.clonePosition(reference, {
- offsetTop: reference.getHeight() + 2
- }).setStyle({
- width: "",
- height: ""
- });
- },
+ /** Duration, in seconds, used when fading the popup in or out. */
- /** Duration, in seconds, used when fading the popup in or out. */
+ FADE_DURATION : .20,
- FADE_DURATION : .20,
+ hidePopup : function() {
+ new Effect.Fade(this.popup, {
+ duration : this.FADE_DURATION
+ });
+ },
- hidePopup : function()
- {
- new Effect.Fade(this.popup, {
- duration: this.FADE_DURATION
- });
- },
+ revealPopup : function() {
- revealPopup : function()
- {
+ // Only show one DateField popup at a time.
- // Only show one DateField popup at a time.
+ if (Tapestry.DateField.activeDateField != undefined
+ && Tapestry.DateField.activeDateField != this) {
+ Tapestry.DateField.activeDateField.hidePopup();
+ }
- if (Tapestry.DateField.activeDateField != undefined &&
- Tapestry.DateField.activeDateField != this)
- {
- Tapestry.DateField.activeDateField.hidePopup();
- }
+ new Effect.Appear(this.popup, {
+ duration : this.FADE_DURATION
+ });
- new Effect.Appear(this.popup, {
- duration: this.FADE_DURATION
- });
-
- Tapestry.DateField.activeDateField = this;
- }
+ Tapestry.DateField.activeDateField = this;
+}
});
Tapestry.DateField.localized = false;
-Tapestry.Initializer.dateField = function(spec)
-{
- new Tapestry.DateField(spec);
+Tapestry.Initializer.dateField = function(spec) {
+ new Tapestry.DateField(spec);
}
\ No newline at end of file
Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/tapestry.js
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/tapestry.js?rev=924025&r1=924024&r2=924025&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/tapestry.js (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/tapestry.js Tue Mar 16 22:19:03 2010
@@ -329,25 +329,41 @@ var Tapestry = {
},
/**
- * Processes a typical Ajax request for a URL invoking the provided handler
- * on success. On failure, error() is invoked to inform the user.
+ * Processes a typical Ajax request for a URL. In the simple case, a success
+ * handler is provided (as options). In a more complex case, an options
+ * object is provided, with keys as per Ajax.Request. The onSuccess key will
+ * be overwritten, and defaults for onException and onFailure will be
+ * provided. The handler shoudl take up-to two parameters: the
+ * XMLHttpRequest object itself, and the JSON Response (from the X-JSON
+ * response header, usually null).
*
* @param url
* of Ajax request
- * @param successHandler
- * to invoke on success
+ * @param options
+ * either a success handler
* @return the Ajax.Request object
*/
- ajaxRequest : function(url, successHandler) {
- return new Ajax.Request(url, {
- onSuccess : function(response, jsonResponse) {
+ ajaxRequest : function(url, options) {
+
+ if (Object.isFunction(options)) {
+ return Tapestry.ajaxRequest(url, {
+ onSuccess : options
+ });
+ }
+
+ var successHandler = options.onSuccess;
+ var finalOptions = $H( {
+ onException : Tapestry.ajaxFailureHandler,
+ onFailure : Tapestry.ajaxFailureHandler
+ }).update(options).update( {
+ onSuccess : function(response, jsonResponse) {
/*
* When the page is unloaded, pending Ajax requests appear to
- * terminate as succesful (but with no reply value). Since we're
- * trying to navigate to a new page anyway, we just ignore those
- * false success callbacks. We have a listener for the window's
- * "beforeunload" event that sets this flag.
+ * terminate as successful (but with no reply value). Since
+ * we're trying to navigate to a new page anyway, we just ignore
+ * those false success callbacks. We have a listener for the
+ * window's "beforeunload" event that sets this flag.
*/
if (Tapestry.windowUnloaded)
return;
@@ -363,10 +379,10 @@ var Tapestry = {
} catch (e) {
Tapestry.error(Tapestry.Messages.clientException + e);
}
- },
- onException : Tapestry.ajaxFailureHandler,
- onFailure : Tapestry.ajaxFailureHandler
+ }
});
+
+ return new Ajax.Request(url, finalOptions.toObject());
},
/**
@@ -645,10 +661,6 @@ Element.addMethods('FORM', {
*/
options = Object.clone(options || {});
- /* Set a default failure handler if none is provided. */
-
- options.onFailure |= Tapestry.ajaxFailureHandler;
-
/*
* Find the elements, skipping over any submit buttons. This works
* around bugs in Prototype 1.6.0.2.
@@ -669,7 +681,7 @@ Element.addMethods('FORM', {
/* Ajax.Request will convert the hash into a query string and post it. */
- return new Ajax.Request(url, options);
+ return Tapestry.ajaxRequest(url, options);
}
});