/* * 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. */ dojo.declare("Spring.DefaultEquals", null, { equals : function(/*Object*/other){ if (other.declaredClass && other.declaredClass == this.declaredClass) { return true; }else{ return false; } } }); dojo.declare("Spring.ElementDecoration", [Spring.AbstractElementDecoration, Spring.DefaultEquals], { constructor : function(config) { this.copyFields = new Array('name', 'value', 'type', 'checked', 'selected', 'readOnly', 'disabled', 'alt', 'maxLength', 'class', 'title'); dojo.mixin(this, config); if(this.widgetModule == "") { this.widgetModule = this.widgetType; } }, apply : function(){ if (dijit.byId(this.elementId)) { dijit.byId(this.elementId).destroyRecursive(false); } var element = dojo.byId(this.elementId); if (!element) { console.error("Could not apply " + this.widgetType + " decoration. Element with id '" + this.elementId + "' not found in the DOM."); } else { for (var copyField in this.copyFields) { copyField = this.copyFields[copyField]; if (!this.widgetAttrs[copyField] && element[copyField] && (typeof element[copyField] != 'number' || (typeof element[copyField] == 'number' && element[copyField] >= 0))) { this.widgetAttrs[copyField] = element[copyField]; } } if(element['style'] && element['style'].cssText){ this.widgetAttrs['style'] = element['style'].cssText; } dojo.require(this.widgetModule); var widgetConstructor = dojo.eval(this.widgetType); this.widget = new widgetConstructor(this.widgetAttrs, element); this.widget.startup(); } //return this to support method chaining return this; }, validate : function(){ if (!this.widget.isValid) { // some widgets cannot be validated return true; } var isValid = this.widget.isValid(false); if (!isValid) { this.widget.state = "Error"; this.widget._setStateClass(); } return isValid; } }); dojo.declare("Spring.ValidateAllDecoration", [Spring.AbstractValidateAllDecoration, Spring.DefaultEquals], { constructor : function(config) { this.originalHandler = null; this.connection = null; dojo.mixin(this, config); }, apply : function() { var element = dojo.byId(this.elementId); this.originalHandler = element[this.event]; var context = this; element[this.event] = function(event){ context.handleEvent(event, context); }; return this; }, cleanup : function(){ dojo.disconnect(this.connection); }, handleEvent : function(event, context){ if (!Spring.validateAll()) { dojo.stopEvent(event); } else if(dojo.isFunction(context.originalHandler)) { var result = context.originalHandler(event); if (result == false) { dojo.stopEvent(event); } } } }); dojo.declare("Spring.AjaxEventDecoration", [Spring.AbstractAjaxEventDecoration, Spring.DefaultEquals], { constructor : function(config){ this.connection = null; dojo.mixin(this, config); }, apply : function() { this.connection = dojo.connect(dojo.byId(this.elementId), this.event, this, "submit"); return this; }, cleanup : function(){ dojo.disconnect(this.connection); }, submit : function(event){ if (this.sourceId == ""){ this.sourceId = this.elementId; } if(this.formId == ""){ Spring.remoting.getLinkedResource(this.sourceId, this.params, this.popup); } else { Spring.remoting.submitForm(this.sourceId, this.formId, this.params); } dojo.stopEvent(event); } }); dojo.declare("Spring.RemotingHandler", Spring.AbstractRemotingHandler, { constructor : function(){}, submitForm : function(/*String */ sourceId, /*String*/formId, /*Object*/ params) { var content = new Object(); for (var key in params) { content[key] = params[key]; } var sourceComponent = dojo.byId(sourceId); if (sourceComponent != null){ if(sourceComponent.value != undefined && sourceComponent.type && ("button,submit,reset").indexOf(sourceComponent.type) < 0) { content[sourceId] = sourceComponent.value; } else if(sourceComponent.name != undefined) { content[sourceComponent.name] = sourceComponent.name; } else { content[sourceId] = sourceId; } } if (!content['ajaxSource']) { content['ajaxSource'] = sourceId; } dojo.xhrPost({ content: content, form: formId, handleAs: "text", headers: {"Accept" : "text/html;type=ajax"}, // The LOAD function will be called on a successful response. load: this.handleResponse, // The ERROR function will be called in an error case. error: this.handleError }); }, getLinkedResource: function(/*String */ linkId, /*Object*/params, /*boolean*/ modal) { this.getResource(dojo.byId(linkId).href, params, modal); }, getResource: function(/*String */ resourceUri, /*Object*/params, /*boolean*/ modal) { dojo.xhrGet({ url: resourceUri, content: params, handleAs: "text", headers: {"Accept" : "text/html;type=ajax"}, load: this.handleResponse, error: this.handleError, modal: modal }); }, handleResponse: function(response, ioArgs) { //First check if this response should redirect var redirectURL = ioArgs.xhr.getResponseHeader('Spring-Redirect-URL'); var modalViewHeader = ioArgs.xhr.getResponseHeader('Spring-Modal-View'); var modalView = ((dojo.isString(modalViewHeader) && modalViewHeader.length > 0) || ioArgs.args.modal); if (dojo.isString(redirectURL) && redirectURL.length > 0) { if (modalView) { //render a popup with the new URL Spring.remoting.renderURLToModalDialog(redirectURL, ioArgs); return response; } else { if (redirectURL.indexOf("/") >= 0) { window.location = window.location.protocol + "//" + window.location.host + redirectURL; } else { var location = window.location.protocol + "//" + window.location.host + window.location.pathname; var appendIndex = location.lastIndexOf("/"); location = location.substr(0,appendIndex+1) + redirectURL; if (location == window.location) { Spring.remoting.getResource(location, ioArgs.args.content, false); } else { window.location = location; } } return response; } } //Extract and store all