Refactoring Spring.js API.

This commit is contained in:
Jeremy Grelle
2008-04-18 20:16:20 +00:00
parent 9ad2a872a8
commit e71ed3d0a0
5 changed files with 216 additions and 127 deletions

View File

@@ -44,10 +44,10 @@
<form:input path="checkinDate"/>
<script type="text/javascript">
dojo.require('dijit.form.DateTextBox');
Spring.advisors.push(new Spring.ValidatingFieldAdvisor({
targetElId : 'checkinDate',
decoratorType : dijit.form.DateTextBox,
decoratorAttrs : { value : dojo.date.locale.parse(dojo.byId('checkinDate').value, {selector : 'date', datePattern : 'yyyy-MM-dd'}), required : true }}));
Spring.decorations.push(new Spring.ElementDecoration({
elementId : 'checkinDate',
widgetType : dijit.form.DateTextBox,
widgetAttrs : { value : dojo.date.locale.parse(dojo.byId('checkinDate').value, {selector : 'date', datePattern : 'yyyy-MM-dd'}), required : true }}));
</script>
</div>
</div>
@@ -59,10 +59,10 @@
<form:input path="checkoutDate"/>
<script type="text/javascript">
dojo.require('dijit.form.DateTextBox');
Spring.advisors.push(new Spring.ValidatingFieldAdvisor({
targetElId : 'checkoutDate',
decoratorType : dijit.form.DateTextBox,
decoratorAttrs : { value : dojo.date.locale.parse(dojo.byId('checkoutDate').value, {selector : 'date', datePattern : 'yyyy-MM-dd'}), required : true }}));
Spring.decorations.push(new Spring.ElementDecoration({
elementId : 'checkoutDate',
widgetType : dijit.form.DateTextBox,
widgetAttrs : { value : dojo.date.locale.parse(dojo.byId('checkoutDate').value, {selector : 'date', datePattern : 'yyyy-MM-dd'}), required : true }}));
</script>
</div>
</div>
@@ -87,14 +87,14 @@
<form:radiobutton id="non-smoking" path="smoking" label="Non Smoking" value="false"/>
<script type="text/javascript">
dojo.require('dijit.form.CheckBox');
Spring.advisors.push(new Spring.ValidatingFieldAdvisor({
targetElId : 'smoking',
decoratorType : dijit.form.RadioButton,
decoratorAttrs : { promptMessage : 'Smoking room' }}));
Spring.advisors.push(new Spring.ValidatingFieldAdvisor({
targetElId : 'non-smoking',
decoratorType : dijit.form.RadioButton,
decoratorAttrs : { promptMessage : 'Non-smoking room' }}));
Spring.decorations.push(new Spring.ElementDecoration({
elementId : 'smoking',
widgetType : dijit.form.RadioButton,
widgetAttrs : { promptMessage : 'Smoking room' }}));
Spring.decorations.push(new Spring.ElementDecoration({
elementId : 'non-smoking',
widgetType : dijit.form.RadioButton,
widgetAttrs : { promptMessage : 'Non-smoking room' }}));
</script>
</div>
@@ -107,10 +107,10 @@
<form:input path="creditCard"/>
<script type="text/javascript">
dojo.require('dijit.form.ValidationTextBox');
Spring.advisors.push(new Spring.ValidatingFieldAdvisor({
targetElId : 'creditCard',
decoratorType : dijit.form.ValidationTextBox,
decoratorAttrs : { required : true, invalidMessage : 'A 16-digit credit card number is required.', regExp : '[0-9]{16}' }}));
Spring.decorations.push(new Spring.ElementDecoration({
elementId : 'creditCard',
widgetType : dijit.form.ValidationTextBox,
widgetAttrs : { required : true, invalidMessage : 'A 16-digit credit card number is required.', regExp : '[0-9]{16}' }}));
</script>
</div>
</div>
@@ -119,13 +119,13 @@
<label for="creditCardName">Credit Card Name:</label>
</div>
<div class="input">
<form:input path="creditCardName" maxlength="4"/>
<form:input path="creditCardName" maxlength="40"/>
<script type="text/javascript">
dojo.require('dijit.form.ValidationTextBox');
Spring.advisors.push(new Spring.ValidatingFieldAdvisor({
targetElId : 'creditCardName',
decoratorType : dijit.form.ValidationTextBox,
decoratorAttrs : { required : true }}));
Spring.decorations.push(new Spring.ElementDecoration({
elementId : 'creditCardName',
widgetType : dijit.form.ValidationTextBox,
widgetAttrs : { required : true }}));
</script>
</div>
</div>
@@ -161,7 +161,7 @@
<input type="submit" id="proceed" name="_eventId_proceed" value="Proceed"/>&#160;
<input type="submit" name="_eventId_cancel" value="Cancel"/>&#160;
<script type="text/javascript">
Spring.advisors.push(new Spring.ValidateAllAdvisor({event : 'onclick', targetId : 'proceed'}));
Spring.decorations.push(new Spring.ValidateAllDecoration({event : 'onclick', elementId : 'proceed'}));
</script>
</div>
</fieldset>

View File

@@ -12,10 +12,10 @@
<form:input path="searchString"/>
<script type="text/javascript">
dojo.require('dijit.form.ValidationTextBox');
Spring.advisors.push(new Spring.ValidatingFieldAdvisor({
targetElId : 'searchString',
decoratorType : dijit.form.ValidationTextBox,
decoratorAttrs : { promptMessage : 'Search hotels by name, address, city, or zip.' }}));
Spring.decorations.push(new Spring.ElementDecoration({
elementId : 'searchString',
widgetType : dijit.form.ValidationTextBox,
widgetAttrs : { promptMessage : 'Search hotels by name, address, city, or zip.' }}));
</script>
<label for="pageSize">Maximum results:</label>
<form:select path="pageSize">

View File

@@ -32,10 +32,10 @@
<input type="text" name="j_username" id="j_username" <c:if test="${not empty param.login_error}">value="<%= session.getAttribute(AuthenticationProcessingFilter.SPRING_SECURITY_LAST_USERNAME_KEY) %>"</c:if> />
<script type="text/javascript">
dojo.require('dijit.form.ValidationTextBox');
Spring.advisors.push(new Spring.ValidatingFieldAdvisor({
targetElId : 'j_username',
decoratorType : dijit.form.ValidationTextBox,
decoratorAttrs : { promptMessage : 'Your username', required : true }}));
Spring.decorations.push(new Spring.ElementDecoration({
elementId : 'j_username',
widgetType : dijit.form.ValidationTextBox,
widgetAttrs : { promptMessage : 'Your username', required : true }}));
</script>
</div>
</div>
@@ -45,10 +45,10 @@
<input type="password" name="j_password" id="j_password" />
<script type="text/javascript">
dojo.require('dijit.form.ValidationTextBox');
Spring.advisors.push(new Spring.ValidatingFieldAdvisor({
targetElId : 'j_password',
decoratorType : dijit.form.ValidationTextBox,
decoratorAttrs : { promptMessage : 'Your password', required : true, type : 'password' }}));
Spring.decorations.push(new Spring.ElementDecoration({
elementId : 'j_password',
widgetType : dijit.form.ValidationTextBox,
widgetAttrs : { promptMessage : 'Your password', required : true}}));
</script>
</div>
</div>
@@ -58,10 +58,9 @@
<input type="checkbox" name="_spring_security_remember_me" id="remember_me" />
<script type="text/javascript">
dojo.require('dijit.form.CheckBox');
Spring.advisors.push(new Spring.ValidatingFieldAdvisor({
targetElId : 'remember_me',
decoratorType : dijit.form.CheckBox,
decoratorAttrs : { promptMessage : 'Remember me for 2 weeks' }}));
Spring.decorations.push(new Spring.ElementDecoration({
elementId : 'remember_me',
widgetType : dijit.form.CheckBox}));
</script>
</div>
</div>
@@ -69,7 +68,7 @@
<div class="buttonGroup">
<input name="submit" id="submit" type="submit" value="Login" />
<script type="text/javascript">
Spring.advisors.push(new Spring.ValidateAllAdvisor({event : 'onclick', targetId : 'submit'}));
Spring.decorations.push(new Spring.ValidateAllDecoration({event : 'onclick', elementId : 'submit'}));
</script>
</div>
</form>

View File

@@ -13,69 +13,53 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Spring.DojoValidatingFieldAdvisor = function(config){
dojo.mixin(this, config);
};
Spring.DojoValidatingFieldAdvisor.prototype = {
targetElId : "",
decoratorType : null,
decorator : null,
decoratorAttrs : null,
copyFields : new Array('name', 'value', 'type', 'checked', 'selected', 'readOnly', 'disabled', 'alt', 'maxLength'),
dojo.declare("Spring.ElementDecoration", Spring.AbstractElementDecoration, {
constructor : function(config) {
this.copyFields = new Array('name', 'value', 'type', 'checked', 'selected', 'readOnly', 'disabled', 'alt', 'maxLength');
dojo.mixin(this, config);
},
apply : function(){
if (dijit.byId(this.targetElId)) {
dijit.byId(this.targetElId).destroyRecursive(false);
if (dijit.byId(this.elementId)) {
dijit.byId(this.elementId).destroyRecursive(false);
}
var targetEl = dojo.byId(this.targetElId);
var element = dojo.byId(this.elementId);
for (var copyField in this.copyFields) {
copyField = this.copyFields[copyField];
if (!this.decoratorAttrs[copyField] && targetEl[copyField] && (typeof targetEl[copyField] != 'number' || (typeof targetEl[copyField] == 'number' && targetEl[copyField] >= 0))) {
this.decoratorAttrs[copyField] = targetEl[copyField];
if (!this.widgetAttrs[copyField] && element[copyField] && (typeof element[copyField] != 'number' ||
(typeof element[copyField] == 'number' && element[copyField] >= 0))) {
this.widgetAttrs[copyField] = element[copyField];
}
}
this.decorator = new this.decoratorType(this.decoratorAttrs, targetEl);
this.decorator.startup();
this.widget = new this.widgetType(this.widgetAttrs, element);
this.widget.startup();
//return this to support method chaining
return this;
},
validate : function(){
if (!this.decorator.isValid) {
// some decorators cannot be validated
if (!this.widget.isValid) {
// some widgets cannot be validated
return true;
}
var isValid = this.decorator.isValid(false);
var isValid = this.widget.isValid(false);
if (!isValid) {
this.decorator.state = "Error";
this.decorator._setStateClass();
this.widget.state = "Error";
this.widget._setStateClass();
}
return isValid;
}
};
});
Spring.ValidatingFieldAdvisor = Spring.DojoValidatingFieldAdvisor;
Spring.DojoRemoteEventAdvisor = function(config){
dojo.mixin(this, config);
};
Spring.DojoRemoteEventAdvisor.prototype = {
event : "",
targetId : "",
sourceId : "",
formId : "",
processIds : "",
renderIds : "",
params : [],
connection : null,
dojo.declare("Spring.RemoteEventDecoration", Spring.AbstractRemoteEventDecoration, {
constructor : function(config){
this.connection = null;
dojo.mixin(this, config);
},
apply : function() {
this.connection = dojo.connect(dojo.byId(this.targetId), this.event, this, "submit");
this.connection = dojo.connect(dojo.byId(this.elementId), this.event, this, "submit");
return this;
},
@@ -86,26 +70,20 @@ Spring.DojoRemoteEventAdvisor.prototype = {
submit : function(){
Spring.RemotingHandler.submitForm(this.sourceId, this.formId, this.processIds, this.renderIds, this.params);
}
};
});
Spring.RemoteEventAdvisor = Spring.DojoRemoteEventAdvisor;
Spring.DojoValidateAllAdvisor = function(config){
dojo.mixin(this, config);
};
Spring.DojoValidateAllAdvisor.prototype = {
event : "",
targetId : "",
originalHandler : null,
connection : null,
dojo.declare("Spring.ValidateAllDecoration", Spring.AbstractValidateAllDecoration, {
constructor : function(config) {
this.originalHandler = null;
this.connection = null;
dojo.mixin(this, config);
},
apply : function() {
var targetEl = dojo.byId(this.targetId);
this.originalHandler = targetEl[this.event];
var element = dojo.byId(this.elementId);
this.originalHandler = element[this.event];
var context = this;
targetEl[this.event] = function(event){
element[this.event] = function(event){
context.handleEvent(event, context);
};
return this;
@@ -118,20 +96,17 @@ Spring.DojoValidateAllAdvisor.prototype = {
handleEvent : function(event, context){
if (!Spring.validateAll()) {
dojo.stopEvent(event);
} else {
} else if(dojo.isFunction(context.originalHandler)) {
var result = context.originalHandler(event);
if (result == false) {
dojo.stopEvent(event);
}
}
}
};
});
Spring.ValidateAllAdvisor = Spring.DojoValidateAllAdvisor;
Spring.DojoRemotingHandler = function(){};
Spring.DojoRemotingHandler.prototype = {
dojo.declare("Spring.RemotingHandler", Spring.AbstractRemotingHandler, {
constructor : function(){},
submitForm : function(/*String */ sourceId, /*String*/formId, /*String*/ processIds, /*String*/renderIds, /*Array*/ params) {
var content = new Object();
@@ -282,28 +257,21 @@ Spring.DojoRemotingHandler.prototype = {
});
dialog.show();
}
};
});
Spring.RemotingHandler = new Spring.DojoRemotingHandler();
Spring.DojoCommandLinkAdvisor = function(config){
dojo.mixin(this, config);
};
Spring.DojoCommandLinkAdvisor.prototype = {
targetElId : "",
linkHtml : "",
dojo.declare("Spring.CommandLinkDecoration", Spring.AbstractCommandLinkDecoration, {
constructor : function(config){
dojo.mixin(this, config);
},
apply : function(){
var advisedNode = dojo.byId(this.targetElId);
var advisedNode = dojo.byId(this.elementId);
if (!dojo.hasClass(advisedNode, "progressiveLink")) {
//Node must be replaced
var nodeToReplace = new dojo.NodeList(advisedNode);
nodeToReplace.addContent(this.linkHtml, "after").orphan("*");
//Get the new node
advisedNode = dojo.byId(this.targetElId);
advisedNode = dojo.byId(this.elementId);
}
advisedNode.submitFormFromLink = this.submitFormFromLink;
//return this to support method chaining
@@ -336,8 +304,6 @@ Spring.DojoCommandLinkAdvisor.prototype = {
});
}
}
};
});
Spring.CommandLinkAdvisor = Spring.DojoCommandLinkAdvisor;
dojo.addOnLoad(Spring.applyAdvisors);
dojo.addOnLoad(Spring.applyDecorations);

View File

@@ -0,0 +1,124 @@
/*
* Copyright 2004-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Spring = {};
Spring.decorations = [];
Spring.decorations.applied = false;
Spring.applyDecorations = function(){
if (!Spring.decorations.applied) {
for (var x=0; x<Spring.decorations.length; x++) {
Spring.decorations[x].apply();
}
Spring.decorations.applied = true;
}
};
Spring.validateAll = function(){
var valid = true;
for(x in Spring.decorations) {
if (Spring.decorations[x].widget &&
!Spring.decorations[x].validate()) {
valid = false;
}
}
return valid;
};
Spring.validateRequired = function(){
var valid = true;
for(x in Spring.decorations) {
if(Spring.decorations[x].decorator &&
Spring.decorations[x].isRequired() &&
!Spring.decorations[x].validate())
valid = false;
}
return valid;
};
Spring.AbstractElementDecoration = function(){};
Spring.AbstractElementDecoration.prototype = {
elementId : "",
widgetType : null,
widget : null,
widgetAttrs : {},
apply : function(){},
validate : function(){},
isRequired : function(){}
};
Spring.AbstractValidateAllDecoration = function(){};
Spring.AbstractValidateAllDecoration.prototype = {
event : "",
elementId : "",
apply : function() {},
cleanup : function(){},
handleEvent : function(event){}
};
Spring.AbstractCommandLinkDecoration = function(){};
Spring.AbstractCommandLinkDecoration.prototype = {
elementId : "",
linkHtml : "",
apply : function(){},
submitFormFromLink : function(/*String*/ formId, /*String*/ sourceId, /*Array of name,value params*/ params){}
};
Spring.AbstractRemoteEventDecoration = function(){};
Spring.AbstractRemoteEventDecoration.prototype = {
event : "",
elementId : "",
sourceId : "",
formId : "",
processIds : "",
params : [],
apply : function(){},
cleanup : function(){},
submit : function(event){}
};
Spring.AbstractRemotingHandler = function(){};
Spring.AbstractRemotingHandler.prototype = {
submitForm : function(/*String */ sourceId, /*String*/formId, /*String*/ processIds, /*Array*/ params){},
getResource : function(/*String */ sourceId, /*String*/ processIds) {},
handleResponse : function() {},
handleError : function() {}
};