You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xap-commits@incubator.apache.org by bb...@apache.org on 2007/09/12 01:38:49 UTC
svn commit: r574755 [3/3] - in /incubator/xap/site/javascript: prototype/
prototype/prototype.js xmodify/xmodify-mootools-compressed.js
xmodify/xmodify-mootools.js xmodify/xmodify-prototype-compressed.js
xmodify/xmodify-prototype.js
Added: incubator/xap/site/javascript/xmodify/xmodify-mootools-compressed.js
URL: http://svn.apache.org/viewvc/incubator/xap/site/javascript/xmodify/xmodify-mootools-compressed.js?rev=574755&view=auto
==============================================================================
--- incubator/xap/site/javascript/xmodify/xmodify-mootools-compressed.js (added)
+++ incubator/xap/site/javascript/xmodify/xmodify-mootools-compressed.js Tue Sep 11 18:38:48 2007
@@ -0,0 +1 @@
+XHR.xmodifyVersion="0.3.0";XHR.prototype.xmodify=function(_1,_2){var _3=this.onSuccess;var _4=this;this.onSuccess=function(){if(_4.transport.responseXML==null){_4.onFailure.call(this);}else{_4.xProc(_4.transport.responseXML);if(_3){_3.call(this);}}};return this.send(_1,_2);};XHR.prototype.xProc=function(_5){var _6={};$each(_5.documentElement.childNodes,function(_7){if(_7.nodeType==1&&XHR.xNodeName(_7.nodeName)=="modifications"){$each(_7.childNodes,function(_8){if(_8.nodeType==1){XHR.xProcElem(_8,_6);}});}});};XHR.xProcElem=function(_9,_a){var _b=XHR.xNodeName(_9.nodeName);var _c=_9.getAttribute("select");if(_b=="append"){XHR.xProcIn(_9.childNodes,function(_d){$A(_d).each(function(_e){$$(_c).each(function(_f){XHR.appendTableElement(_f,_e);});});},_a);}else{if(_b=="set-attribute"){$each(_9.childNodes,function(_10){if(_10.nodeType==1){$$(_c).setProperty(_10.getAttribute("name"),_10.getAttribute("value"));}});}else{if(_b=="set-style"){$each(_9.childNodes,function(_11){if(_11.nod
eType==1){$$(_c).setStyle(_11.getAttribute("name"),_11.getAttribute("value"));}});}else{if(_b=="insert-before"){XHR.xProcIn(_9.childNodes,function(_12){$A(_12).each(function(_13){$$(_c).each(function(_14){_14.parentNode.insertBefore(_13,_14);});});},_a);}else{if(_b=="insert-after"){XHR.xProcIn(_9.childNodes,function(_15){$A(_15).each(function(_16){$$(_c).each(function(_17){if(_17.nextSibling){_17.parentNode.insertBefore(_16,_17.nextSibling);}else{XHR.appendTableElement(_17.parentNode,_16);}});});},_a);}else{if(_b=="replace-children"||_b=="set-innerhtml"){XHR.xProcIn(_9.childNodes,function(_18){$$(_c).each(function(_19){_19.empty();$A(_18).each(function(_1a){XHR.appendTableElement(_19,_1a);});});},_a);}else{if(_b=="remove-element"){$each($$(_c),function(_1b){_1b.remove();});}else{if(_b=="remove-attribute"){$each(_9.childNodes,function(_1c){if(_1c.nodeType==1){$$(_c).removeProperty(_1c.getAttribute("name"));}});}else{if(_b=="replace"){XHR.xProcIn(_9.childNodes,function(_1d){$A
(_1d).each(function(_1e){$$(_c).each(function(_1f){_1f.replaceWith(_1e);});});});}else{if(_b=="insert-at"){var _20=_9.getAttribute("index");XHR.xProcIn(_9.childNodes,function(_21){$A(_21).each(function(_22){$$(_c).each(function(_23){var _24=null;var _25=_23;if(_23.childNodes.length>_20){_24=_23.childNodes[_20];}if(_24){_23.insertBefore(_22,_24);}else{XHR.appendTableElement(_23,_22);}});});},_a);}else{if(_b=="variable"){var _26=_9.getAttribute("name");var _27=_9.getAttribute("deep");var arr=new Array();$$(_c).each(function(_29){arr.push(_29);});_a[_26]=arr;}}}}}}}}}}}};XHR.xProcIn=function(_2a,_2b,_2c){$each(_2a,function(_2d){if((_2d.nodeType==3||_2d.nodeType==4)&&_2d.nodeValue.trim()!=""){_2b.call(_2d,XHR.convertStringToHTML(_2d.nodeValue));}else{if(_2d.nodeType==1&&XHR.xNodeName(_2d.nodeName)=="value-of"){_2b.call(_2d,_2c[_2d.getAttribute("name")]);}else{if(_2d.nodeType==1&&XHR.xNodeName(_2d.nodeName)=="clone"){var _2e=_2d.getAttribute("select");var _2f=_2d.getAttribute("de
ep");$$(_2e).each(function(_30){_2b.call(_2d,[_30.cloneNode(_2f)]);});}}}});};XHR.xNodeName=function(_31){var i=_31.indexOf(":");if(i!=-1){_31=_31.substring(i+1);}return _31;};XHR.appendTableElement=function(_33,_34){if(XHR.xNodeName(_33.nodeName)=="TABLE"&&XHR.xNodeName(_34.nodeName)=="TR"){obj=_33.getElementsByTagName("tbody")[0]||_33.appendChild(document.createElement("tbody"));obj.appendChild(_34);}else{_33.appendChild(_34);}};XHR.convertStringToHTML=function(_35){var s=_35.replace(/^\s+|\s+$/g,"");var div=document.createElement("div");var tb=[];var _39=!s.indexOf("<opt")&&[1,"<select>","</select>"]||(!s.indexOf("<thead")||!s.indexOf("<tbody")||!s.indexOf("<tfoot"))&&[1,"<table>","</table>"]||!s.indexOf("<tr")&&[2,"<table><tbody>","</tbody></table>"]||(!s.indexOf("<td")||!s.indexOf("<th"))&&[3,"<table><tbody><tr>","</tr></tbody></table>"]||[0,"",""];div.innerHTML=_39[1]+s+_39[2];while(_39[0]--){div=div.firstChild;}if(window.ie){if(!s.indexOf("<table")&&s.indexOf("<tbody"
)<0){tb=div.firstChild&&div.firstChild.childNodes;}else{if(_39[1]=="<table>"&&s.indexOf("<tbody")<0){tb=div.childNodes;}}for(var n=tb.length-1;n>=0;--n){if(tb[n].nodeName.toUpperCase()=="TBODY"&&!tb[n].childNodes.length){tb[n].parentNode.removeChild(tb[n]);}}}arg=new Array();for(var i=0,l=div.childNodes.length;i<l;i++){arg.push(div.childNodes[i]);}return arg;};
\ No newline at end of file
Added: incubator/xap/site/javascript/xmodify/xmodify-mootools.js
URL: http://svn.apache.org/viewvc/incubator/xap/site/javascript/xmodify/xmodify-mootools.js?rev=574755&view=auto
==============================================================================
--- incubator/xap/site/javascript/xmodify/xmodify-mootools.js (added)
+++ incubator/xap/site/javascript/xmodify/xmodify-mootools.js Tue Sep 11 18:38:48 2007
@@ -0,0 +1,330 @@
+/**
+ *
+ * xModify is an extension of the MooTools library that processes requests
+ * and update the HTML DOM using the xModify syntax. Included in xModify is
+ * a series of instructions that can be used to update html.
+ *
+ * insert-after, insert-before, append, set-attribute, set-style
+ * set-innerhtml, replace-children, remove-element, remove-attribute
+ * replace
+ *
+ *
+ */
+
+ /*
+ * Library version of xModify.
+ */
+ XHR.xmodifyVersion = "0.3.0";
+
+ /**
+ * Main function to kick off an xModify request.
+ * The results of the request will be automatically
+ * processed.
+ *
+ * Look at the documenation for the XHR.send function
+ * for information on the parameters.
+ *
+ * http://docs.mootools.net/Remote/XHR.js
+ *
+ */
+ XHR.prototype.xmodify = function(url, queryString){
+ var onSuccess = this.onSuccess;
+ var thisVar = this;
+ this.onSuccess = function(){
+ if (thisVar.transport.responseXML == null){
+ thisVar.onFailure.call(this);
+ }else{
+ thisVar.xProc(thisVar.transport.responseXML);
+ if (onSuccess){
+ onSuccess.call(this);
+ }
+ }
+ }
+ return this.send(url, queryString);
+ }
+
+ /**
+ * Will return the modification element.
+ *
+ * Assumes the modifications elements are under XAL
+ * <xal>
+ * <modifications>
+ * </modifications>
+ * </xal>
+ *
+ */
+ XHR.prototype.xProc = function(modificationDocument){
+ var variables = {};
+ $each(
+ modificationDocument.documentElement.childNodes, function(item){
+ //look up all the modification elements
+ if ( item.nodeType == 1 && XHR.xNodeName(item.nodeName) == "modifications"){
+ //process all the modification elements
+ $each(item.childNodes, function(item){
+ if (item.nodeType == 1){
+ XHR.xProcElem(item, variables);
+ }
+ }
+ );
+ }
+ }
+ );
+ }
+
+ /**
+ * Will return the modification element.
+ *
+ * Assumes the modifications elements are under XAL
+ *
+ * MAIN ELEMENT NAMES
+ * insert-after //This one is roughed out.
+ * insert-before //This one is roughed out.
+ * append //This one is roughed out.
+ * set-attribute //This one is roughed out.
+ * set-style //This one is roughed out.
+ * set-innerhtml //This one is roughed out.
+ * replace-children //This one is roughed out.
+ * remove-element //This one is roughed out.
+ * remove-attribute //This one is roughed out.
+ * replace //This one is roughed out.
+ *
+ * insert-at //This one is roughed out.
+ *
+ * clone //This one is roughed out.
+ * create-document //NOT IMPLEMENTED
+ *
+ * variable //This one is roughed out.
+ * attribute //This one is roughed out.
+ * value-of //This one is roughed out.
+ *
+ * TODOs
+ *
+ * Look into events and styles. Events and styles work
+ * as long as the programmer types them in correctly
+ *
+ * Need to add the must exist attribute
+ * Need to add events.
+ */
+ XHR.xProcElem = function(modElement, variables){
+ var nodeName = XHR.xNodeName(modElement.nodeName);
+ var select = modElement.getAttribute("select");
+
+ if (nodeName == "append"){
+ XHR.xProcIn(modElement.childNodes, function(nodes){
+ $A(nodes).each(function(node){
+ $$(select).each(function(item){
+ XHR.appendTableElement(item, node);
+ });
+ });
+ }, variables);
+ }else if (nodeName == "set-attribute"){
+ $each(modElement.childNodes, function(item){
+ if (item.nodeType == 1){
+ $$(select).setProperty(item.getAttribute("name"), item.getAttribute("value"));
+ }
+ });
+ }else if (nodeName == "set-style"){
+ $each(modElement.childNodes, function(item){
+ if (item.nodeType == 1){
+ $$(select).setStyle(item.getAttribute("name"), item.getAttribute("value"));
+ }
+ });
+ }else if (nodeName == "insert-before"){
+ XHR.xProcIn(modElement.childNodes, function(nodes){
+ $A(nodes).each(function(newNode){
+ $$(select).each(function(item){
+ item.parentNode.insertBefore(newNode, item);
+ });
+ });
+ }, variables);
+ }else if (nodeName == "insert-after"){
+ XHR.xProcIn(modElement.childNodes, function(nodes){
+ $A(nodes).each(function(newNode){
+ $$(select).each(function(item){
+ if (item.nextSibling){
+ item.parentNode.insertBefore(newNode, item.nextSibling);
+ }else{
+ XHR.appendTableElement(item.parentNode, newNode);
+ }
+ });
+ });
+ }, variables);
+ }else if (nodeName == "replace-children" ||
+ nodeName == "set-innerhtml"){
+ XHR.xProcIn(modElement.childNodes, function(nodes){
+ $$(select).each(function(item){
+ item.empty();
+ $A(nodes).each(function(newNode){
+ XHR.appendTableElement(item, newNode);
+ });
+ });
+ }, variables);
+ }else if (nodeName == "remove-element"){
+ $each($$(select), function(item){
+ item.remove();
+ });
+ }else if (nodeName == "remove-attribute"){
+ $each(modElement.childNodes, function(item){
+ if (item.nodeType == 1){
+ $$(select).removeProperty(item.getAttribute("name"));
+ }
+ });
+ }else if (nodeName == "replace"){
+ XHR.xProcIn(modElement.childNodes, function(nodes){
+ $A(nodes).each(function(newNode){
+ $$(select).each(function(item){
+ item.replaceWith(newNode);
+ });
+ });
+ });
+ }else if (nodeName == "insert-at"){
+ var index = modElement.getAttribute("index");
+
+ XHR.xProcIn(modElement.childNodes, function(nodes){
+ $A(nodes).each(function(newNode){
+ $$(select).each(function(item){
+ var beforeNode = null;
+ var node = item;
+
+ //if the index is more than the number
+ //of children nodes append it.
+ if (item.childNodes.length > index){
+ beforeNode = item.childNodes[index];
+ }
+
+ if (beforeNode){
+ item.insertBefore(newNode, beforeNode);
+ }else{
+ XHR.appendTableElement(item, newNode);
+ }
+ });
+ });
+ }, variables);
+ }else if (nodeName == "variable"){
+ var name = modElement.getAttribute("name");
+ var deep = modElement.getAttribute("deep");
+ var arr = new Array();
+ $$(select).each(function(node){
+ arr.push(node);
+ });
+ variables[name] = arr;
+ }
+ }
+
+ /**
+ * Called to process the an instruction will loop over each of the
+ * instruction's children and fire a callback
+ */
+ XHR.xProcIn = function(nodes, callback, variables){
+ $each(nodes, function(item){
+ //make sure there is a nodeValue and this isn't an empty string
+ if ((item.nodeType == 3 || item.nodeType == 4) && item.nodeValue.trim() != ""){
+ callback.call(item, XHR.convertStringToHTML(item.nodeValue));
+ }else if (item.nodeType == 1 && XHR.xNodeName(item.nodeName) == "value-of"){
+ callback.call(item, variables[item.getAttribute("name")]);
+ }else if (item.nodeType == 1 && XHR.xNodeName(item.nodeName) == "clone"){
+ var select = item.getAttribute("select");
+ var deep = item.getAttribute("deep");
+ $$(select).each(function(node){
+ callback.call(item, [node.cloneNode(deep)]);
+ });
+ }
+ });
+ }
+
+ XHR.xNodeName = function(name){
+ var i = name.indexOf(":");
+ if (i != -1){
+ name = name.substring(i + 1);
+ }
+ return name;
+ }
+
+ /**
+ * This function is a utility to append element correctly to a table. tables contain a tbody element
+ * which is the elemet we want to append to.
+ * @param {Object} item
+ * @param {Object} newNode
+ */
+ XHR.appendTableElement = function(item, newNode){
+ //need to check for a table and tr. we want the tbodyitem.nodeName
+ if (XHR.xNodeName(item.nodeName) == "TABLE" && XHR.xNodeName(newNode.nodeName) == "TR"){
+ obj = item.getElementsByTagName("tbody")[0] || item.appendChild(document.createElement("tbody"));
+ obj.appendChild(newNode);
+ }else{
+ item.appendChild(newNode);
+ }
+ }
+
+
+ /*
+ * This code is brought in from the jQuery library, their excellent work
+ * allowed me to not need to invent this portion.
+ *
+ * jQuery 1.1.2 - New Wave Javascript
+ *
+ * Copyright (c) 2007 John Resig (jquery.com)
+ * Dual licensed under the MIT (MIT-LICENSE.txt)
+ * and GPL (GPL-LICENSE.txt) licenses.
+ *
+ * $Date: 2007-02-28 12:03:00 -0500 (Wed, 28 Feb 2007) $
+ * $Rev: 1465 $
+ */
+ XHR.convertStringToHTML = function(sourceNode) {
+ // Convert html string into DOM nodes
+ // Trim whitespace, otherwise indexOf won't work as expected
+ var s = sourceNode.replace(/^\s+|\s+$/g, "");
+ var div = document.createElement("div")
+ var tb = [];
+
+ var wrap =
+ // option or optgroup
+ !s.indexOf("<opt") &&
+ [1, "<select>", "</select>"] ||
+
+ (!s.indexOf("<thead") || !s.indexOf("<tbody") || !s.indexOf("<tfoot")) &&
+ [1, "<table>", "</table>"] ||
+
+ !s.indexOf("<tr") &&
+ [2, "<table><tbody>", "</tbody></table>"] ||
+
+ // <thead> matched above
+ (!s.indexOf("<td") || !s.indexOf("<th")) &&
+ [3, "<table><tbody><tr>", "</tr></tbody></table>"] ||
+
+ [0,"",""];
+
+ // Go to html and back, then peel off extra wrappers
+ div.innerHTML = wrap[1] + s + wrap[2];
+
+ // Move to the right depth
+ while ( wrap[0]-- ){
+ div = div.firstChild;
+ }
+
+ // Remove IE's autoinserted <tbody> from table fragments
+ if ( window.ie ) {
+
+ // String was a <table>, *may* have spurious <tbody>
+ if ( !s.indexOf("<table") && s.indexOf("<tbody") < 0 ){
+ tb = div.firstChild && div.firstChild.childNodes;
+
+ // String was a bare <thead> or <tfoot>
+ }else if ( wrap[1] == "<table>" && s.indexOf("<tbody") < 0 ){
+ tb = div.childNodes;
+ }
+
+ for ( var n = tb.length-1; n >= 0 ; --n ){
+ if ( tb[n].nodeName.toUpperCase() == "TBODY" && !tb[n].childNodes.length ){
+ tb[n].parentNode.removeChild(tb[n]);
+ }
+ }
+ }
+
+ arg = new Array();
+ for (var i=0, l=div.childNodes.length; i<l; i++){
+ arg.push(div.childNodes[i]);
+ }
+ return arg;
+ }
+
Added: incubator/xap/site/javascript/xmodify/xmodify-prototype-compressed.js
URL: http://svn.apache.org/viewvc/incubator/xap/site/javascript/xmodify/xmodify-prototype-compressed.js?rev=574755&view=auto
==============================================================================
--- incubator/xap/site/javascript/xmodify/xmodify-prototype-compressed.js (added)
+++ incubator/xap/site/javascript/xmodify/xmodify-prototype-compressed.js Tue Sep 11 18:38:48 2007
@@ -0,0 +1 @@
+Prototype.xmodifyVersion="0.3.0";Ajax.xmodify=function(_1,_2){var _3=this.onSuccess;if(_2==null){_2={method:"get"};}else{if(_2.method==null){_2.method="get";}}var _4=this;_2.onSuccess=function(_5){if(_5.responseXML==null){_2.onFailure.call(this);}else{Ajax.xProc(_5.responseXML);if(_3){_3.call(this);}}};return new Ajax.Request(_1,_2);};Ajax.xProc=function(_6){var _7={};$A(_6.documentElement.childNodes).each(function(_8){if(_8.nodeType==1&&Ajax.xNodeName(_8.nodeName)=="modifications"){$A(_8.childNodes).each(function(_9){if(_9.nodeType==1){Ajax.xProcElem(_9,_7);}});}});};Ajax.xProcElem=function(_a,_b){var _c=Ajax.xNodeName(_a.nodeName);var _d=_a.getAttribute("select");if(_c=="append"){Ajax.xProcIn(_a.childNodes,function(_e){$$(_d).each(function(_f){$A(_e).each(function(_10){Ajax.appendTableElement(_f,_10);});});},_b);}else{if(_c=="set-attribute"){$A(_a.childNodes).each(function(_11){if(_11.nodeType==1){$$(_d).each(function(_12){_12.setAttribute(_11.getAttribute("name"),_11.getA
ttribute("value"));});}var _13;});}else{if(_c=="set-style"){$A(_a.childNodes).each(function(_14){if(_14.nodeType==1){$$(_d).each(function(_15){var _16=new Array();_16[_14.getAttribute("name")]=_14.getAttribute("value");$(_15).setStyle(_16);});}var _17;});}else{if(_c=="insert-before"){Ajax.xProcIn(_a.childNodes,function(_18){$$(_d).each(function(_19){$A(_18).each(function(_1a){_19.parentNode.insertBefore(_1a,_19);});});},_b);}else{if(_c=="insert-after"){Ajax.xProcIn(_a.childNodes,function(_1b){$$(_d).each(function(_1c){$A(_1b).each(function(_1d){if(_1c.nextSibling){_1c.parentNode.insertBefore(_1d,_1c.nextSibling);}else{Ajax.appendTableElement(_1c.parentNode,_1d);}});});},_b);}else{if(_c=="set-innerhtml"||_c=="replace-children"){Ajax.xProcIn(_a.childNodes,function(_1e){$$(_d).each(function(_1f){_1f.innerHTML="";$A(_1e).each(function(_20){Ajax.appendTableElement(_1f,_20);});});},_b);}else{if(_c=="remove-element"){$$(_d).each(function(_21){_21.remove();});}else{if(_c=="remove-at
tribute"){$A(_a.childNodes).each(function(_22){if(_22.nodeType==1){$$(_d).each(function(_23){_23.removeAttribute(_22.getAttribute("name"));});}});}else{if(_c=="replace"){Ajax.xProcIn(_a.childNodes,function(_24){$$(_d).each(function(_25){$A(_24).each(function(_26){_25.replace(_26,_25);});});});}else{if(_c=="insert-at"){var _27=_a.getAttribute("index");Ajax.xProcIn(_a.childNodes,function(_28){$$(_d).each(function(_29){$A(_28).each(function(_2a){var _2b=null;var _2c=_29;if(_29.childNodes.length>_27){_2b=_29.childNodes[_27];}if(_2b){_29.insertBefore(_2a,_2b);}else{Ajax.appendTableElement(_29,_2a);}});});},_b);}else{if(_c=="variable"){var _2d=_a.getAttribute("name");var _2e=_a.getAttribute("deep");var arr=new Array();$$(_d).each(function(_30){arr.push(_30);});_b[_2d]=arr;}}}}}}}}}}}};Ajax.xProcIn=function(_31,_32,_33){$A(_31).each(function(_34){if((_34.nodeType==3||_34.nodeType==4)&&_34.nodeValue.replace(/^\s+|\s+$/g,"")!=""){_32.call(_34,Ajax.convertStringToHTML(_34.nodeValue));
}else{if(_34.nodeType==1&&Ajax.xNodeName(_34.nodeName)=="value-of"){_32.call(_34,_33[_34.getAttribute("name")]);}else{if(_34.nodeType==1&&Ajax.xNodeName(_34.nodeName)=="clone"){var _35=_34.getAttribute("select");var _36=_34.getAttribute("deep");$$(_35).each(function(_37){_32.call(_34,[_37.cloneNode(_36)]);});}}}});};Ajax.xNodeName=function(_38){var i=_38.indexOf(":");if(i!=-1){_38=_38.substring(i+1);}return _38;};Ajax.appendTableElement=function(_3a,_3b){if(Ajax.xNodeName(_3a.nodeName)=="TABLE"&&Ajax.xNodeName(_3b.nodeName)=="TR"){obj=_3a.getElementsByTagName("tbody")[0]||_3a.appendChild(document.createElement("tbody"));obj.appendChild(_3b);}else{_3a.appendChild(_3b);}};Ajax.convertStringToHTML=function(_3c){var s=_3c.replace(/^\s+|\s+$/g,"");var div=document.createElement("div");var tb=[];var _40=!s.indexOf("<opt")&&[1,"<select>","</select>"]||(!s.indexOf("<thead")||!s.indexOf("<tbody")||!s.indexOf("<tfoot"))&&[1,"<table>","</table>"]||!s.indexOf("<tr")&&[2,"<table><tbody>"
,"</tbody></table>"]||(!s.indexOf("<td")||!s.indexOf("<th"))&&[3,"<table><tbody><tr>","</tr></tbody></table>"]||[0,"",""];div.innerHTML=_40[1]+s+_40[2];while(_40[0]--){div=div.firstChild;}if(window.ie){if(!s.indexOf("<table")&&s.indexOf("<tbody")<0){tb=div.firstChild&&div.firstChild.childNodes;}else{if(_40[1]=="<table>"&&s.indexOf("<tbody")<0){tb=div.childNodes;}}for(var n=tb.length-1;n>=0;--n){if(tb[n].nodeName.toUpperCase()=="TBODY"&&!tb[n].childNodes.length){tb[n].parentNode.removeChild(tb[n]);}}}arg=new Array();for(var i=0,l=div.childNodes.length;i<l;i++){arg.push(div.childNodes[i]);}return arg;};
\ No newline at end of file
Added: incubator/xap/site/javascript/xmodify/xmodify-prototype.js
URL: http://svn.apache.org/viewvc/incubator/xap/site/javascript/xmodify/xmodify-prototype.js?rev=574755&view=auto
==============================================================================
--- incubator/xap/site/javascript/xmodify/xmodify-prototype.js (added)
+++ incubator/xap/site/javascript/xmodify/xmodify-prototype.js Tue Sep 11 18:38:48 2007
@@ -0,0 +1,341 @@
+/**
+ *
+ * xModify is an extension of the MooTools library that processes requests
+ * and update the HTML DOM using the xModify syntax. Included in xModify is
+ * a series of instructions that can be used to update html.
+ *
+ * insert-after, insert-before, append, set-attribute, set-style
+ * set-innerhtml, replace-children, remove-element, remove-attribute
+ * replace
+ *
+ *
+ */
+
+ /*
+ * Library version of xModify.
+ */
+ Prototype.xmodifyVersion = "0.3.0";
+
+ /**
+ * Main function to kick off an xModify request.
+ * The results of the request will be automatically
+ * processed.
+ *
+ * Look at the documenation for the Ajax.send function
+ * for information on the parameters.
+ *
+ * http://prototypejs.org/api/ajax
+ *
+ */
+ Ajax.xmodify = function(url, options){
+ var onSuccess = this.onSuccess;
+ if (options == null){
+ options = {method:"get"};
+ }else if (options.method == null){
+ options.method = "get";
+ }
+ var thisVar = this;
+ options.onSuccess = function(transport){
+ if (transport.responseXML == null){
+ options.onFailure.call(this);
+ }else{
+ Ajax.xProc(transport.responseXML);
+ if (onSuccess){
+ onSuccess.call(this);
+ }
+ }
+ }
+ return new Ajax.Request(url, options);
+ }
+
+
+ /**
+ * Will return the modification element.
+ *
+ * Assumes the modifications elements are under XAL
+ * <xal>
+ * <modifications>
+ * </modifications>
+ * </xal>
+ *
+ */
+ Ajax.xProc = function(modificationDocument){
+ var variables = {};
+ $A(modificationDocument.documentElement.childNodes).each(function(item){
+ if (item.nodeType == 1 && Ajax.xNodeName(item.nodeName) == "modifications"){
+ $A(item.childNodes).each(function(item){
+ if (item.nodeType == 1){
+ Ajax.xProcElem(item, variables);
+ }
+ });
+ }
+ });
+ }
+
+ /**
+ * Will return the modification element.
+ *
+ * Assumes the modifications elements are under XAL
+ *
+ * MAIN ELEMENT NAMES
+ * insert-after //This one is roughed out.
+ * insert-before //This one is roughed out.
+ * append //This one is roughed out.
+ * set-attribute //This one is roughed out.
+ * set-style //This one is roughed out.
+ * set-innerhtml //This one is roughed out.
+ * replace-children //This one is roughed out.
+ * remove-element //This one is roughed out.
+ * remove-attribute //This one is roughed out.
+ * replace //This one is roughed out.
+ *
+ * insert-at //This one is roughed out.
+ *
+ * clone //This one is roughed out.
+ * create-document //NOT IMPLEMENTED
+ *
+ * variable //This one is roughed out.
+ * attribute //This one is roughed out.
+ * value-of //This one is roughed out.
+ *
+ * TODOs
+ *
+ * Look into events and styles. Events and styles work
+ * as long as the programmer types them in correctly
+ *
+ * Need to add the must exist attribute
+ * Need to add events.
+ */
+ Ajax.xProcElem = function(modElement, variables){
+ var nodeName = Ajax.xNodeName(modElement.nodeName);
+ var select = modElement.getAttribute("select");
+
+ if (nodeName == "append"){
+ Ajax.xProcIn(modElement.childNodes, function(nodes){
+ $$(select).each(function(item){
+ $A(nodes).each(function(newNode){
+ Ajax.appendTableElement(item, newNode);
+ });
+ });
+ }, variables);
+ }else if (nodeName == "set-attribute"){
+ $A(modElement.childNodes).each(function(item){
+ if (item.nodeType == 1){
+ $$(select).each(function(node){
+ node.setAttribute(item.getAttribute("name"), item.getAttribute("value"));
+ });
+ }
+ var item;
+ });
+ }else if (nodeName == "set-style"){
+ $A(modElement.childNodes).each(function(item){
+ if (item.nodeType == 1){
+ $$(select).each(function(node){
+ var styles = new Array();
+ styles[item.getAttribute("name")] = item.getAttribute("value");
+ $(node).setStyle(styles);
+ });
+ }
+ var item;
+ });
+ }else if (nodeName == "insert-before"){
+ Ajax.xProcIn(modElement.childNodes, function(nodes){
+ $$(select).each(function(item){
+ $A(nodes).each(function(newNode){
+ item.parentNode.insertBefore( newNode, item );
+ });
+ });
+ }, variables);
+ }else if (nodeName == "insert-after"){
+ Ajax.xProcIn(modElement.childNodes, function(nodes){
+ $$(select).each(function(item){
+ $A(nodes).each(function(newNode){
+ if (item.nextSibling){
+ item.parentNode.insertBefore(newNode, item.nextSibling);
+ }else{
+ Ajax.appendTableElement(item.parentNode, newNode);
+ }
+ });
+ });
+ }, variables);
+ }else if (nodeName == "set-innerhtml" ||
+ nodeName == "replace-children"){
+ Ajax.xProcIn(modElement.childNodes, function(nodes){
+ $$(select).each(function(item){
+ item.innerHTML = '';
+ $A(nodes).each(function(newNode){
+ Ajax.appendTableElement(item, newNode);
+ });
+ });
+ }, variables);
+ }else if (nodeName == "remove-element"){
+ $$(select).each(function(item){
+ item.remove();
+ });
+ }else if (nodeName == "remove-attribute"){
+ $A(modElement.childNodes).each(function(item){
+ if (item.nodeType == 1){
+ $$(select).each(function(node){
+ node.removeAttribute(item.getAttribute("name"));
+ });
+ }
+ });
+ }else if (nodeName == "replace"){
+ Ajax.xProcIn(modElement.childNodes, function(nodes){
+ $$(select).each(function(item){
+ $A(nodes).each(function(newNode){
+ item.replace(newNode, item);
+ });
+ });
+ });
+ }else if (nodeName == "insert-at"){
+ var index = modElement.getAttribute("index");
+
+ Ajax.xProcIn(modElement.childNodes, function(nodes){
+ $$(select).each(function(item){
+ $A(nodes).each(function(newNode){
+ var beforeNode = null;
+ var node = item;
+
+ //if the index is more than the number
+ //of children nodes append it.
+ if (item.childNodes.length > index){
+ beforeNode = item.childNodes[index];
+ }
+
+ if (beforeNode){
+ item.insertBefore(newNode,beforeNode);
+ }else{
+ Ajax.appendTableElement(item, newNode);
+ }
+ });
+ });
+ }, variables);
+
+ }else if (nodeName == "variable"){
+ var name = modElement.getAttribute("name");
+ var deep = modElement.getAttribute("deep");
+ var arr = new Array();
+ $$(select).each(function(node){
+ arr.push(node);
+ });
+ variables[name] = arr;
+ }
+ }
+
+ /**
+ * Called to process the an instruction will loop over each of the
+ * instruction's children and fire a callback
+ */
+ Ajax.xProcIn = function(nodes, callback, variables){
+ $A(nodes).each(function(item){
+ //make sure there is a nodeValue and this isn't an empty string
+ if ((item.nodeType == 3 || item.nodeType == 4) && item.nodeValue.replace(/^\s+|\s+$/g, '') != ""){
+ callback.call(item, Ajax.convertStringToHTML(item.nodeValue));
+ }else if (item.nodeType == 1 && Ajax.xNodeName(item.nodeName) == "value-of"){
+ callback.call(item, variables[item.getAttribute("name")]);
+ }else if (item.nodeType == 1 && Ajax.xNodeName(item.nodeName) == "clone"){
+ var select = item.getAttribute("select");
+ var deep = item.getAttribute("deep");
+ $$(select).each(function(node){
+ callback.call(item, [node.cloneNode(deep)]);
+ });
+ }
+ });
+ }
+
+ Ajax.xNodeName = function(name){
+ var i = name.indexOf(":");
+ if (i != -1){
+ name = name.substring(i + 1);
+ }
+ return name;
+ }
+
+ /**
+ * This function is a utility to append element correctly to a table. tables contain a tbody element
+ * which is the elemet we want to append to.
+ * @param {Object} item
+ * @param {Object} newNode
+ */
+ Ajax.appendTableElement = function(item, newNode){
+ //need to check for a table and tr. we want the tbodyitem.nodeName
+ if (Ajax.xNodeName(item.nodeName) == "TABLE" && Ajax.xNodeName(newNode.nodeName) == "TR"){
+ obj = item.getElementsByTagName("tbody")[0] || item.appendChild(document.createElement("tbody"));
+ obj.appendChild(newNode);
+ }else{
+ item.appendChild(newNode);
+ }
+ }
+
+
+ /*
+ * This code is brought in from the jQuery library, their excellent work
+ * allowed me to not need to invent this portion.
+ *
+ * jQuery 1.1.2 - New Wave Javascript
+ *
+ * Copyright (c) 2007 John Resig (jquery.com)
+ * Dual licensed under the MIT (MIT-LICENSE.txt)
+ * and GPL (GPL-LICENSE.txt) licenses.
+ *
+ * $Date: 2007-02-28 12:03:00 -0500 (Wed, 28 Feb 2007) $
+ * $Rev: 1465 $
+ */
+ Ajax.convertStringToHTML = function(sourceNode) {
+ // Convert html string into DOM nodes
+ // Trim whitespace, otherwise indexOf won't work as expected
+ var s = sourceNode.replace(/^\s+|\s+$/g, "");
+ var div = document.createElement("div")
+ var tb = [];
+
+ var wrap =
+ // option or optgroup
+ !s.indexOf("<opt") &&
+ [1, "<select>", "</select>"] ||
+
+ (!s.indexOf("<thead") || !s.indexOf("<tbody") || !s.indexOf("<tfoot")) &&
+ [1, "<table>", "</table>"] ||
+
+ !s.indexOf("<tr") &&
+ [2, "<table><tbody>", "</tbody></table>"] ||
+
+ // <thead> matched above
+ (!s.indexOf("<td") || !s.indexOf("<th")) &&
+ [3, "<table><tbody><tr>", "</tr></tbody></table>"] ||
+
+ [0,"",""];
+
+ // Go to html and back, then peel off extra wrappers
+ div.innerHTML = wrap[1] + s + wrap[2];
+
+ // Move to the right depth
+ while ( wrap[0]-- ){
+ div = div.firstChild;
+ }
+
+ // Remove IE's autoinserted <tbody> from table fragments
+ if ( window.ie ) {
+
+ // String was a <table>, *may* have spurious <tbody>
+ if ( !s.indexOf("<table") && s.indexOf("<tbody") < 0 ){
+ tb = div.firstChild && div.firstChild.childNodes;
+
+ // String was a bare <thead> or <tfoot>
+ }else if ( wrap[1] == "<table>" && s.indexOf("<tbody") < 0 ){
+ tb = div.childNodes;
+ }
+
+ for ( var n = tb.length-1; n >= 0 ; --n ){
+ if ( tb[n].nodeName.toUpperCase() == "TBODY" && !tb[n].childNodes.length ){
+ tb[n].parentNode.removeChild(tb[n]);
+ }
+ }
+ }
+
+ arg = new Array();
+ for (var i=0, l=div.childNodes.length; i<l; i++){
+ arg.push(div.childNodes[i]);
+ }
+ return arg;
+ }