📄 ajaxtags_tags.js
字号:
/** * Copyright 2005 Darren L. Spurgeon * Copyright 2007-2008 Jens Kapitza * * 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. *//**************************************************************** * TODO rewrite all JS files to use new * prototype syntax look at * http://www.prototypejs.org/learn/class-inheritance * for more information ****************************************************************//** * AjaxTags */AjaxJspTag.Base = Class.create( { resolveParameters : function() { if (this.url === null || Object.isUndefined(this.url) || this.url.strip().length == 0) { return; } // Strip URL of querystring and append it to parameters var indexOf = this.url.indexOf('?'); var qs = ''; if (indexOf >= 0) { if (indexOf < this.url.length - 1) { qs = this.url.substr(indexOf + 1); } this.url = this.url.substr(0, indexOf); } // append the url params to params of ajax if (qs.length > 0) { qs = qs.split('&').join(','); } if (this.options.parameters) { this.options.parameters += ',' + qs; } else { this.options.parameters = qs; } if ((this.options.parameters.length > 0) && (this.options.parameters .charAt(this.options.parameters.length - 1) === ',')) { this.options.parameters = this.options.parameters.substr(0, this.options.parameters.length - 1); } }, setOptions : function(options) { this.options = Object.extend( { parser :options.parser || new DefaultResponseParser("xml") // baseUrl , target // preFunction , postFunction , errorFunction }, options || {}); return this.options; }, getMethod:function(){ return "post"; }}); /** * UPDATEFIELD TAG */AjaxJspTag.UpdateField = Class.create(AjaxJspTag.Base, { initialize : function(options) { this.url = options.baseUrl; this.setOptions(options); this.setListeners(); addAjaxListener(this); }, setOptions : function(options) { this.options = Object.extend( { parameters :options.parameters || '', valueUpdateByName :options.valueUpdateByName || false, eventType :options.eventType ? options.eventType : "click", parser :options.parser ? options.parser : (options.valueUpdateByName ? new DefaultResponseParser( "xml") : new DefaultResponseParser("text")), handler :options.handler ? options.handler : this.handler }, options || {}); }, setListeners : function() { if ($(this.options.action)) { $(this.options.action)["on" + this.options.eventType] = this.execute.bindAsEventListener(this); } else { removeAjaxListener(this); } }, execute : function(e) { if (Object.isFunction(this.options.preFunction)) { this.options.preFunction(); } if (this.options.cancelExecution) { this.cancelExecution = false; return; } // parse parameters and do replacements var params = buildParameterString(this.options.parameters); var obj = this; // required because 'this' conflict with Ajax.Request var setFunc = this.setField; var aj = new Ajax.Request(this.url, { asynchronous :true, method : this.getMethod(), evalScripts :true, parameters :params, onSuccess : function(request) { obj.options.parser.load(request); obj.options.handler( ); }, onFailure : obj.options.errorFunction, onComplete :obj.options.postFunction });},handler : function() { // this points to options // parse targets var targets = this.target.split(','); var items = this.parser.content; var i,j; for (i = 0; i < targets.length && i < items.length; i++) { namedIndex = i; if (this.valueUpdateByName) { for (j = 0; j < items.length; j++) { if (targets[i] === items[j][0]) { namedIndex = j; } } } $(targets[i]).value = items[namedIndex][1]; }}});/** * CALLBACK TAG */AjaxJspTag.Callback = Class.create(AjaxJspTag.Base, { initialize : function(options) { this.url = options.baseUrl; this.setOptions(options); this.errorCount = 0; // onload!? }, setOptions : function(options) { this.options = Object.extend( { parameters :options.parameters || '', parser :options.parser ? options.parser : new ResponseCallBackXmlParser(), plainText :options.plainText ? true : false, handler :options.handler ? options.handler : this.handler }, options || {}); }, onload : function() { this.run(); }, run : function() { // wenn fehler kommen den client veranlassen eben nicht mehr versuchen // sich anzumelden if (!this.isRunning && this.errorCount < 100) { this.execute(); }},execute : function(e) { if (Object.isFunction(this.options.preFunction)) { this.options.preFunction(); } if (this.options.cancelExecution) { this.cancelExecution = false; return; } // parse targets this.isRunning = true; var obj = this; // required because 'this' conflict with Ajax.Request var aj = new Ajax.Request(this.url, { asynchronous :true, method : this.getMethod(), evalScripts :true, onSuccess : function(request) { obj.options.parser.load(request); obj.options.list = obj.options.parser.items; obj.errorCount = 0; }, onFailure : function(request) { if (Object.isFunction(obj.options.errorFunction)) { obj.options.errorFunction(); } obj.isRunning = false; obj.errorCount++; }, onComplete : function(request) { // nun this.list kann mit der antwor alles gemacht werden was man // will if (Object.isFunction(obj.options.postFunction)) { obj.options.postFunction(); } obj.isRunning = false; obj.run(); } });}});/** * SELECT TAG */AjaxJspTag.Select = Class.create(AjaxJspTag.Base, { initialize : function(options) { this.url = options.baseUrl; this.setOptions(options); this.setListeners(); if (parseBoolean(this.options.executeOnLoad)) { this.execute(); } addAjaxListener(this); }, setOptions : function(options) { this.options = Object.extend( { parameters :options.parameters || '', emptyOptionValue :options.emptyOptionValue || '', emptyOptionName :options.emptyOptionName || '', eventType :options.eventType ? options.eventType : "change", parser :options.parser ? options.parser : new DefaultResponseParser("xml"), handler :options.handler ? options.handler : this.handler }, options || {}); }, setListeners : function() { $(this.options.source).ajaxSelect = this; Event.observe($(this.options.source), this.options.eventType, this.execute.bindAsEventListener(this), false); $(this.options.source)["on" + this.options.eventType] = function(){return false;}; }, execute : function(e) { if (Object.isFunction(this.options.preFunction)) { this.options.preFunction(); } if (this.options.cancelExecution) { this.cancelExecution = false; return; } // parse parameters and do replacements var params = buildParameterString(this.options.parameters); var obj = this; // required because 'this' conflict with Ajax.Request var aj = new Ajax.Request(this.url, { asynchronous :true, method : this.getMethod(), evalScripts :true, parameters :params, onSuccess : function(request) { obj.options.parser.load(request); obj.options.handler(request, { target :obj.options.target, items :obj.options.parser.content }); }, onFailure : function(request) { if (Object.isFunction(obj.options.errorFunction)) { obj.options.errorFunction(); } }, onComplete : function(request) { if (Object.isFunction(obj.options.postFunction)) { obj.options.postFunction(); } } });},handler : function(request, options) { // build an array of option values to be set as selected $(options.target).options.length = 0; $(options.target).disabled = false; for ( var i = 0; i < options.items.length; i++) { var newOption = new Option(options.items[i][0], options.items[i][1]); // $(options.target).options[i] = new Option(options.items[i][0], // options.items[i][1]); // set the option as selected if it is in the default list if (newOption.selected == false && options.items[i].length == 3 && parseBoolean(options.items[i][2])) { newOption.selected = true; } $(options.target).options[i] = newOption; } if (options.items.length == 0) { $(options.target).options[i] = new Option(this.emptyOptionName, this.emptyOptionValue); $(options.target).disabled = true; } // auch ein SELECT TAG ? if ($(options.target).ajaxSelect && $(options.target).ajaxSelect.execute) { $(options.target).ajaxSelect.execute(); }}});/** * HTMLCONTENT TAG */AjaxJspTag.HtmlContent = Class .create( AjaxJspTag.Base, { initialize : function(options) { this.url = options.baseUrl; this.setOptions(options); this.setListeners(); addAjaxListener(this); }, setOptions : function(options) { this.options = Object .extend( { parameterName :options.parameterName ? options.parameterName : AJAX_DEFAULT_PARAMETER, parameters :options.parameters || '', preFunctionParameter :options.preFunctionParameter || null, errorFunctionParameter :options.errorFunctionParameter || null, postFunctionParameter :options.postFunctionParameter || null, eventType :options.eventType ? options.eventType : "click", parser :options.parser ? options.parser : new DefaultResponseParser( "html"), handler :options.handler ? options.handler : this.handler }, options || {}); }, setListeners : function() { if (this.options.source) { $(this.options.source)["on" + this.options.eventType] = this.execute.bindAsEventListener(this); } else if (this.options.sourceClass) { var elementArray = document .getElementsByClassName(this.options.sourceClass); for ( var i = 0; i < elementArray.length; i++) { elementArray[i]["on"+ this.options.eventType] = this.execute.bindAsEventListener(this); } } }, execute : function(e) { this.options.preFunctionParameters = evalJScriptParameters(this.options.preFunctionParameter); if (Object.isFunction(this.options.preFunction)) { this.options.preFunction(); } if (this.options.cancelExecution) { this.cancelExecution = false; return; } // replace default parameter with value/content of // source element selected var ajaxParameters = this.options.parameters; if (this.options.sourceClass) { var re = new RegExp("(\\{" + this.options.parameterName + "\\})", 'g'); var elem = Event.element(e); if (elem.type) { ajaxParameters = ajaxParameters.replace(re, $F(elem)); } else { ajaxParameters = ajaxParameters.replace(re, elem.innerHTML); } } // parse parameters and do replacements var params = buildParameterString(ajaxParameters); var obj = this; // required because 'this' conflict with // Ajax.Request var aj = new Ajax.Updater( this.options.target, this.url, { asynchronous :true, method : this.getMethod(), evalScripts :true, parameters :params, onFailure : function(request) { obj.options.errorFunctionParameters = evalJScriptParameters(obj.options.errorFunctionParameter); if (Object .isFunction(obj.options.errorFunction)) { obj.options.errorFunction(); } }, onComplete : function(request) { obj.options.postFunctionParameters = evalJScriptParameters(obj.options.postFunctionParameter); if (Object .isFunction(obj.options.postFunction)) { obj.options.postFunction(); } } }); } });/** * TREE TAG */AjaxJspTag.Tree = Class.create( AjaxJspTag.Base, { initialize : function( options) { this.url = options.baseUrl; this.setOptions(options); this.execute(); }, setOptions : function(options) { this.options = Object.extend( { parameters :options.parameters || '', eventType :options.eventType ? options.eventType : "click", parser :options.parser || new ResponseXmlToHtmlLinkListParser(), collapsedClass :options.collapsedClass || "collapsedNode", expandedClass :options.expandedClass || "expandedNode", treeClass :options.treeClass || "tree", nodeClass :options.nodeClass || '' }, options || {}); },
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -