📄 wicket-ajax.js
字号:
if (submitButton != null) { s += Wicket.Form.encode(submitButton) + "=1"; } return s; } return this.request.post(body); }, // Submits a form using ajax submitFormById: function(formId, submitButton) { var form = Wicket.$(formId); if (form == null || typeof (form) == "undefined") Wicket.Log.error("Trying to submit form with id '"+formId+"' that is not in document."); return this.submitForm(form, submitButton); }, // Processes the response loadedCallback: function(envelope) { // To process the response, we go through the xml document and add a function for every action (step). // After this is done, a FunctionExecuter object asynchronously executes these functions. // The asynchronous execution is necessary, because some steps might involve loading external javascript, // which must be asynchronous, so that it doesn't block the browser, but we also have to maintain // the order in which scripts are loaded and we have to delay the next steps until the script is // loaded. try { var root = envelope.getElementsByTagName("ajax-response")[0]; // the root element must be <ajax-response if (root == null || root.tagName != "ajax-response") { this.failure("Could not find root <ajax-response> element"); return; } // iinitialize the array for steps (closures that execute each action) var steps = new Array(); // start it a bit later so that the browser does handle the next event // before the component is or can be replaced. We could do (if (!posponed)) // because if there is already something in the queue then we could execute that immedietly steps.push(function(notify) { window.setTimeout(notify,2); }.bind(this)); if (Wicket.Browser.isKHTML()) { // there's a nasty bug in KHTML that makes the browser crash // when the methods are delayed. Therefore we have to fire it // ASAP. The javascripts that would cause dependency problems are // loaded synchronously in konqueror. steps.push = function(method) { method(function() { }); } } // go through the ajax response and for every action (component, js evaluation, header contribution) // ad the proper closure to steps for (var i = 0; i < root.childNodes.length; ++i) { var node = root.childNodes[i]; if (node.tagName == "component") { this.processComponent(steps, node); } else if (node.tagName == "evaluate") { this.processEvaluation(steps, node); } else if (node.tagName == "header-contribution") { this.processHeaderContribution(steps, node); } } // add the last step, which should trigger the success call the done method on request this.success(steps); if (Wicket.Browser.isKHTML() == false) { Wicket.Log.info("Response parsed. Now invoking steps..."); var executer = new Wicket.FunctionsExecuter(steps); executer.start(); } } catch (e) { this.failure(e.message); } }, // Adds a closure to steps that should be invoked after all other steps have been successfully executed success: function(steps) { steps.push(function(notify) { Wicket.Log.info("Response processed successfully."); Wicket.Ajax.invokePostCallHandlers(); // retach the events to the new components (a bit blunt method...) // This should be changed for IE See comments in wicket-event.js add (attachEvent/detachEvent) // IE this will cause double events for everything.. (mostly because of the Function.prototype.bind(element)) Wicket.Focus.attachFocusEvent(); this.request.done(); this.successHandler(); // set the focus to the last component setTimeout("Wicket.Focus.requestFocus();", 0); // continue to next step (which should make the processing stop, as success should be the final step) notify(); }.bind(this)); }, // Adds a closure that replaces a component processComponent: function(steps, node) { steps.push(function(notify) { // get the component id var compId = node.getAttribute("id"); var text=""; // get the new component body if (node.hasChildNodes()) { text = node.firstChild.nodeValue; } // if the text was escaped, unascape it // (escaping is done when the component body contains a CDATA section) var encoding = node.getAttribute("encoding"); if (encoding != null && encoding!="") { text = Wicket.decode(encoding, text); } // get existing component var element = Wicket.$(compId); if (element == null || typeof(element) == "undefined") { Wicket.Log.error("Component with id [["+compId+"]] a was not found while trying to perform markup update. Make sure you called component.setOutputMarkupId(true) on the component whose markup you are trying to update."); } else { // replace the component Wicket.replaceOuterHtml(element, text); } // continue to next step notify(); }); }, // Adds a closure that evaluates javascript code processEvaluation: function(steps, node) { steps.push(function(notify) { // get the javascript body var text = node.firstChild.nodeValue; // unescape it if necessary var encoding = node.getAttribute("encoding"); if (encoding != null) { text = Wicket.decode(encoding, text); } // test if the javascript is in form of identifier|code // if it is, we allow for letting the javascript decide when the rest of processing will continue // by invoking identifier(); var res = text.match(new RegExp("^([a-z|A-Z_][a-z|A-Z|0-9_]*)\\|((.|\\n)*)$")); if (res != null) { text = "var f = function(" + res[1] + ") {" + res[2] +"};"; try { // do the evaluation eval(text); f(notify); } catch (exception) { Wicket.Log.error("Exception evaluating javascript: " + exception); } } else { // just evaluate the javascript try { // do the evaluation eval(text); } catch (exception) { Wicket.Log.error("Exception evaluating javascript: " + exception); } // continue to next step notify(); } }); }, // Adds a closure that processes a header contribution processHeaderContribution: function(steps, node) { var c = new Wicket.Head.Contributor(); c.processContribution(steps, node); }};/** * Header contribution allows component to include custom javascript and stylesheet. * * Header contributor takes the code component would render to page head and * interprets it just as browser would when loading a page. * That means loading external javascripts and stylesheets, executing inline * javascript and aplying inline styles. * * Header contributor also filters duplicate entries, so that it doesn't load/process * resources that have been loaded. * For inline styles and javascript, element id is used to filter out duplicate entries. * For stylesheet and javascript references, url is used for filtering. */Wicket.Head = { };Wicket.Head.Contributor = Wicket.Class.create();Wicket.Head.Contributor.prototype = { initialize: function() { }, // Parses the header contribution element (returns a DOM tree with the contribution) parse: function(headerNode) { // the header contribution is stored as CDATA section in the header-contribution element. // even though we need to parse it (and we have aleady parsed the response), header // contribution needs to be treated separately. The reason for this is that // Konqueror crashes when it there is a <script element in the parsed string. So we // need to replace that first // get the header contribution text and unescape it if necessary var text = headerNode.firstChild.nodeValue; var encoding = headerNode.getAttribute("encoding"); if (encoding != null && encoding != "") { text = Wicket.decode(encoding, text); } if (Wicket.Browser.isKHTML()) { // konqueror crashes if there is a <script element in the xml, but <SCRIPT is fine. text = text.replace(/<script/g,"<SCRIPT"); text = text.replace(/<\/script>/g,"</SCRIPT>"); } // build a DOM tree of the contribution var xmldoc; if (window.ActiveXObject) { xmldoc = new ActiveXObject("Microsoft.XMLDOM"); xmldoc.loadXML(text); } else { var parser = new DOMParser(); xmldoc = parser.parseFromString(text, "text/xml"); } return xmldoc; }, // Processes the parsed header contribution processContribution: function(steps, headerNode) { var xmldoc = this.parse(headerNode); var rootNode = xmldoc.documentElement; // go through the individual elements and process them according to their type for (var i = 0; i < rootNode.childNodes.length; i++) { var node = rootNode.childNodes[i]; if (node.tagName != null) { var name = node.tagName.toLowerCase(); // it is possible that a reference is surrounded by a <wicket:link // in that case, we need to find the inner element if (name == "wicket:link") { for (var j = 0; j < node.childNodes.length; ++j) { var childNode = node.childNodes[j]; // try to find a regular node inside wicket:link if (childNode.nodeType == 1) { node = childNode; name = node.tagName.toLowerCase(); break; } } } // process the element if (name == "link") { this.processLink(steps, node); } else if (name == "script") { this.processScript(steps, node); } else if (name == "style") { this.processStyle(steps, node); } } } }, // Process an external stylesheet element processLink: function(steps, node) { steps.push(function(notify) { // if the element is already in head, skip it if (Wicket.Head.containsElement(node, "href")) { notify(); return; } // create link element var css = Wicket.Head.createElement("link"); // copy required attributes css.id = node.getAttribute("id"); css.rel = node.getAttribute("rel"); css.href = node.getAttribute("href"); css.type = node.getAttribute("type"); // add element to head Wicket.Head.addElement(css); // continue to next step notify(); }); }, // Process an inline style element processStyle: function(steps, node) { steps.push(function(notify) { // if element with same id is already in document, skip it if (Wicket.DOM.containsElement(node)) { notify(); return; } // serialize the style to string var content = Wicket.DOM.serializeNodeChildren(node); // create style element var style = Wicket.Head.createElement("style"); // copy id attribute style.id = node.getAttribute("id"); // create stylesheet if (Wicket.Browser.isIE()) { try { document.createStyleSheet().cssText = content; } catch(ignore) { var run = function() { try { document.createStyleSheet().cssText = content; } catch(e) { Wicket.Log.error(e); } } window.setTimeout(run, 1); } } else { var textNode = document.createTextNode(content); style.appendChild(textNode); } Wicket.Head.addElement(style); // continue to next step notify(); }); }, // Process a script element (both inline and external) processScript: function(steps, node) { steps.push(function(notify) { // if element in same id is already in document, // or element with same src attribute is in document, skip it if (Wicket.DOM.containsElement(node) || Wicket.Head.containsElement(node, "src")) { notify(); return; } // determine whether it is external javascript (has src attribute set) var src = node.getAttribute("src"); if (src != null && src != "") { // load the external javascript using Wicket.Ajax.Request // callback when script is loaded var onLoad = function(content) { Wicket.Head.addJavascript(content, null, src); Wicket.Ajax.invokePostCallHandlers(); // continue to next step notify(); } // we need to schedule the request as timeout // calling xml http request from another request call stack doesn't work window.setTimeout(function() { var req = new Wicket.Ajax.Request(src, onLoad, false, false); req.debugContent = false; if (Wicket.Browser.isKHTML()) // konqueror can't process the ajax response asynchronously, threfore the // javascript loading must be also synchronous req.async = false; // get the javascript req.get(); },1); } else { // serialize the element content to string var text = Wicket.DOM.serializeNodeChildren(node); // add javascript to document head Wicket.Head.addJavascript(text, node.getAttribute("id")); // continue to next step notify(); } }); } };/** * Head manipulation */// Creates an element in documentWicket.Head.createElement = function(name) { return document.createElement(name);}// Adds the element to page headWicket.Head.addElement = function(element) { var head = document.getElementsByTagName("head"); if (head[0]) { head[0].appendChild(element); }}// Returns true, if the page head contains element that has attribute with// name mandatoryAttribute same as the given element and their names match.//// e.g. Wicket.Head.containsElement(myElement, "src") return true, if there// is an element in head that is of same type as myElement, and whose src// attribute is same as myElement.src.Wicket.Head.containsElement = function(element, mandatoryAttribute) { var attr = element.getAttribute(mandatoryAttribute); if (attr == null || attr == "" || typeof(attr) == "undefined") return false; var head = document.getElementsByTagName("head")[0]; var nodes = head.getElementsByTagName(element.tagName); for (var i = 0; i < nodes.length; ++i) { var node = nodes[i]; // check node names and mandatory attribute values // we also have to check for attribute name that is suffixed by "_". // this is necessary for filtering script references if (node.tagName.toLowerCase() == element.tagName.toLowerCase() && (node.getAttribute(mandatoryAttribute) == attr || node.getAttribute(mandatoryAttribute+"_") == attr)) { return true; } } return false;}// Adds a javascript element to page header. // The fakeSrc attribute is used to filter out duplicate javascript references.// External javascripts are loaded using xmlhttprequest. Then a javascript element is created and the// javascript body is used as text for the element. For javascript references, wicket uses the src // attribute to filter out duplicates. However, since we set the body of the element, we can't assign// also a src value. Therefore we put the url to the src_ (notice the underscore) attribute.// Wicket.Head.containsElement is aware of that and takes also the underscored attributes into account.Wicket.Head.addJavascript = function(content, id, fakeSrc) { var script = Wicket.Head.createElement("script"); script.id = id; script.setAttribute("src_", fakeSrc); // set the javascript as element content if (null == script.canHaveChildren || script.canHaveChildren) { var textNode = document.createTextNode(content); script.appendChild(textNode); } else { script.text = content; } Wicket.Head.addElement(script); }// Goes through all script elements contained by the element and add them to headWicket.Head.addJavascripts = function(element, contentFilter) { function add(element) { var src = element.getAttribute("src"); // if it is a reference, just add it to head if (src != null && src.length > 0) { var e = document.createElement("script"); e.setAttribute("type","text/javascript"); e.setAttribute("src", src); Wicket.Head.addElement(e); } else { var content = Wicket.DOM.serializeNodeChildren(element); if (content == null || content == "") content = element.text; if (typeof(contentFilter) == "function") { content = contentFilter(content); } Wicket.Head.addJavascript(content); } } if (typeof(element) != "undefined" && typeof(element.tagName) != "undefined" && element.tagName.toLowerCase() == "script") { add(element); } else { // we need to check if there are any children, because Safari // aborts when the element is a text node if (element.childNodes.length > 0) { var scripts = element.getElementsByTagName("script"); for (var i = 0; i < scripts.length; ++i) { add(scripts[i]); } } }}/** * Throttler's purpose is to make sure that ajax requests wont be fired too often. */Wicket.ThrottlerEntry = Wicket.Class.create();Wicket.ThrottlerEntry.prototype = { initialize: function(func) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -