webgui/www/extras/yui-ext/docs/overview-summary-XMLDataModel.js.html
JT Smith 4f68a0933c added YUI and YUI-ext
fixed the resizable text area with IE problem
fixed the ad space with IE problem
merged the 7.2.0 and 7.1.4 change logs
2006-11-07 23:15:57 +00:00

297 lines
14 KiB
HTML

<!doctype html public "-//W3C//DTD HTML 4.0 Frameset//EN""http://www.w3.org/TR/REC-html40/frameset.dtd">
<html>
<head>
<title>
Overview
</title>
<link rel ="stylesheet" type="text/css" href="stylesheet.css" title="Style">
<script>
function asd() {
parent.document.title="XMLDataModel.js Overview";
}
</script>
</head>
<body bgcolor="white" onload="asd();" style="margin:15px;">
<center>
<h2>XMLDataModel.js</h2>
</center>
<h4>Summary</h4>
<p>
</p>
<table border="1" cellpadding="3" cellspacing="0" width="100%">
<tr bgcolor="#CCCCFF" class="TableHeadingColor">
<td colspan="2" class="title-cell">
<b>Class Summary</b>
</td>
</tr>
<tr bgcolor="white" class="TableRowColor">
<td width="15%"><b><a href="YAHOO.ext.grid.XMLDataModel.html">YAHOO.ext.grid.XMLDataModel</a></b></td>
<td>This is an implementation of a DataModel used by the Grid.</td>
</tr>
</table>
<hr/>
<!-- ========== METHOD SUMMARY =========== -->
<!-- ========== END METHOD SUMMARY =========== -->
<pre class="sourceview">
<span class="comment">/**
* <span class="attrib">@class</span>
* This is an implementation of a DataModel used by the Grid. It works
* with XML data.
* &lt;br&gt;Example schema from Amazon search:
* &lt;pre&gt;&lt;code&gt;
* var schema = {
* tagName: 'Item',
* id: 'ASIN',
* fields: ['Author', 'Title', 'Manufacturer', 'ProductGroup']
* };
* &lt;/code&gt;&lt;/pre&gt;
* <span class="attrib">@extends</span> YAHOO.ext.grid.LoadableDataModel
* <span class="attrib">@constructor</span>
* <span class="attrib">@param</span> {Object} schema The schema to use
* <span class="attrib">@param</span> {XMLDocument} xml An XML document to load immediately
*/</span>
YAHOO.ext.grid.XMLDataModel = <span class="reserved">function</span>(schema, xml){
YAHOO.ext.grid.XMLDataModel.superclass.constructor.call(<span class="reserved">this</span>, YAHOO.ext.grid.LoadableDataModel.XML);
<span class="comment">/**<span class="attrib">@private</span>*/</span>
<span class="reserved">this</span>.schema = schema;
<span class="reserved">this</span>.xml = xml;
<span class="reserved">if</span>(xml){
<span class="reserved">this</span>.loadData(xml);
}
};
YAHOO.extendX(YAHOO.ext.grid.XMLDataModel, YAHOO.ext.grid.LoadableDataModel);
YAHOO.ext.grid.XMLDataModel.<span class="reserved">prototype</span>.getDocument = <span class="reserved">function</span>(){
<span class="reserved">return</span> <span class="reserved">this</span>.xml;
};
<span class="comment">/**
* Overrides loadData in LoadableDataModel to process XML
* <span class="attrib">@param</span> {XMLDocument} doc The document to load
* <span class="attrib">@param</span> {&lt;i&gt;Function&lt;/i&gt;} callback (optional) callback to call when loading is complete
* <span class="attrib">@param</span> {&lt;i&gt;Boolean&lt;/i&gt;} keepExisting (optional) true to keep existing data
* <span class="attrib">@param</span> {&lt;i&gt;Number&lt;/i&gt;} insertIndex (optional) if present, loaded data is inserted at the specified index instead of overwriting existing data
*/</span>
YAHOO.ext.grid.XMLDataModel.<span class="reserved">prototype</span>.loadData = <span class="reserved">function</span>(doc, callback, keepExisting, insertIndex){
<span class="reserved">this</span>.xml = doc;
var idField = <span class="reserved">this</span>.schema.id;
var fields = <span class="reserved">this</span>.schema.fields;
<span class="reserved">if</span>(<span class="reserved">this</span>.schema.totalTag){
<span class="reserved">this</span>.totalCount = null;
var totalNode = doc.getElementsByTagName(<span class="reserved">this</span>.schema.totalTag);
<span class="reserved">if</span>(totalNode &amp;&amp; totalNode.item(0) &amp;&amp; totalNode.item(0).firstChild) {
var v = parseInt(totalNode.item(0).firstChild.nodeValue, 10);
<span class="reserved">if</span>(!isNaN(v)){
<span class="reserved">this</span>.totalCount = v;
}
}
}
var rowData = [];
var nodes = doc.getElementsByTagName(<span class="reserved">this</span>.schema.tagName);
<span class="reserved">if</span>(nodes &amp;&amp; nodes.length &gt; 0) {
<span class="reserved">for</span>(var i = 0; i &lt; nodes.length; i++) {
var node = nodes.item(i);
var colData = [];
colData.node = node;
colData.id = <span class="reserved">this</span>.getNamedValue(node, idField, String(i));
<span class="reserved">for</span>(var j = 0; j &lt; fields.length; j++) {
var val = <span class="reserved">this</span>.getNamedValue(node, fields[j], <span class="literal">""</span>);
<span class="reserved">if</span>(<span class="reserved">this</span>.preprocessors[j]){
val = <span class="reserved">this</span>.preprocessors[j](val);
}
colData.push(val);
}
rowData.push(colData);
}
}
<span class="reserved">if</span>(keepExisting !== true){
YAHOO.ext.grid.XMLDataModel.superclass.removeAll.call(<span class="reserved">this</span>);
}
<span class="reserved">if</span>(typeof insertIndex != <span class="literal">'number'</span>){
insertIndex = <span class="reserved">this</span>.getRowCount();
}
YAHOO.ext.grid.XMLDataModel.superclass.insertRows.call(<span class="reserved">this</span>, insertIndex, rowData);
<span class="reserved">if</span>(typeof callback == <span class="literal">'function'</span>){
callback(<span class="reserved">this</span>, true);
}
<span class="reserved">this</span>.fireLoadEvent();
};
<span class="comment">/**
* Adds a row to this DataModel and syncs the XML document
* <span class="attrib">@param</span> {String} id The id of the row, if null the next row index is used
* <span class="attrib">@param</span> {Array} cellValues The cell values for this row
* <span class="attrib">@return</span> {Number} The index of the new row
*/</span>
YAHOO.ext.grid.XMLDataModel.<span class="reserved">prototype</span>.addRow = <span class="reserved">function</span>(id, cellValues){
var newIndex = <span class="reserved">this</span>.getRowCount();
var node = <span class="reserved">this</span>.createNode(<span class="reserved">this</span>.xml, id, cellValues);
cellValues.id = id || newIndex;
cellValues.node = node;
YAHOO.ext.grid.XMLDataModel.superclass.addRow.call(<span class="reserved">this</span>, cellValues);
<span class="reserved">return</span> newIndex;
};
<span class="comment">/**
* Inserts a row into this DataModel and syncs the XML document
* <span class="attrib">@param</span> {Number} index The index to insert the row
* <span class="attrib">@param</span> {String} id The id of the row, if null the next row index is used
* <span class="attrib">@param</span> {Array} cellValues The cell values for this row
* <span class="attrib">@return</span> {Number} The index of the new row
*/</span>
YAHOO.ext.grid.XMLDataModel.<span class="reserved">prototype</span>.insertRow = <span class="reserved">function</span>(index, id, cellValues){
var node = <span class="reserved">this</span>.createNode(<span class="reserved">this</span>.xml, id, cellValues);
cellValues.id = id || <span class="reserved">this</span>.getRowCount();
cellValues.node = node;
YAHOO.ext.grid.XMLDataModel.superclass.insertRow.call(<span class="reserved">this</span>, index, cellValues);
<span class="reserved">return</span> index;
};
<span class="comment">/**
* Removes the row from DataModel and syncs the XML document
* <span class="attrib">@param</span> {Number} index The index of the row to remove
*/</span>
YAHOO.ext.grid.XMLDataModel.<span class="reserved">prototype</span>.removeRow = <span class="reserved">function</span>(index){
var node = <span class="reserved">this</span>.data[index].node;
node.parentNode.removeChild(node);
YAHOO.ext.grid.XMLDataModel.superclass.removeRow.call(<span class="reserved">this</span>, index, index);
};
YAHOO.ext.grid.XMLDataModel.<span class="reserved">prototype</span>.getNode = <span class="reserved">function</span>(rowIndex){
<span class="reserved">return</span> <span class="reserved">this</span>.data[rowIndex].node;
};
<span class="comment">/**
* Override this method to define your own node creation routine for when new rows are added.
* By default this method clones the first node and sets the column values in the newly cloned node.
* <span class="attrib">@param</span> {XMLDocument} xmlDoc The xml document being used by this model
* <span class="attrib">@param</span> {Array} colData The column data for the new node
* <span class="attrib">@return</span> {XMLNode} The created node
*/</span>
YAHOO.ext.grid.XMLDataModel.<span class="reserved">prototype</span>.createNode = <span class="reserved">function</span>(xmlDoc, id, colData){
var template = <span class="reserved">this</span>.data[0].node;
var newNode = template.cloneNode(true);
var fields = <span class="reserved">this</span>.schema.fields;
<span class="reserved">for</span>(var i = 0; i &lt; fields.length; i++){
var nodeValue = colData[i];
<span class="reserved">if</span>(<span class="reserved">this</span>.postprocessors[i]){
nodeValue = <span class="reserved">this</span>.postprocessors[i](nodeValue);
}
<span class="reserved">this</span>.setNamedValue(newNode, fields[i], nodeValue);
}
<span class="reserved">if</span>(id){
<span class="reserved">this</span>.setNamedValue(newNode, <span class="reserved">this</span>.schema.idField, id);
}
template.parentNode.appendChild(newNode);
<span class="reserved">return</span> newNode;
};
<span class="comment">/**
* Convenience function looks for value in attributes, then in children tags - also
* normalizes namespace matches (ie matches ns:tag, FireFox matches tag and not ns:tag).
*/</span>
YAHOO.ext.grid.XMLDataModel.<span class="reserved">prototype</span>.getNamedValue = <span class="reserved">function</span>(node, name, defaultValue){
<span class="reserved">if</span>(!node || !name){
<span class="reserved">return</span> defaultValue;
}
var nodeValue = defaultValue;
var attrNode = node.attributes.getNamedItem(name);
<span class="reserved">if</span>(attrNode) {
nodeValue = attrNode.value;
} <span class="reserved">else</span> {
var childNode = node.getElementsByTagName(name);
<span class="reserved">if</span>(childNode &amp;&amp; childNode.item(0) &amp;&amp; childNode.item(0).firstChild) {
nodeValue = childNode.item(0).firstChild.nodeValue;
}<span class="reserved">else</span>{
<span class="comment">// try to strip namespace for FireFox</span>
var index = name.indexOf(<span class="literal">':'</span>);
<span class="reserved">if</span>(index &gt; 0){
<span class="reserved">return</span> <span class="reserved">this</span>.getNamedValue(node, name.substr(index+1), defaultValue);
}
}
}
<span class="reserved">return</span> nodeValue;
};
<span class="comment">/**
* Convenience function set a value in the underlying xml node.
*/</span>
YAHOO.ext.grid.XMLDataModel.<span class="reserved">prototype</span>.setNamedValue = <span class="reserved">function</span>(node, name, value){
<span class="reserved">if</span>(!node || !name){
<span class="reserved">return</span>;
}
var attrNode = node.attributes.getNamedItem(name);
<span class="reserved">if</span>(attrNode) {
attrNode.value = value;
<span class="reserved">return</span>;
}
var childNode = node.getElementsByTagName(name);
<span class="reserved">if</span>(childNode &amp;&amp; childNode.item(0) &amp;&amp; childNode.item(0).firstChild) {
childNode.item(0).firstChild.nodeValue = value;
}<span class="reserved">else</span>{
<span class="comment">// try to strip namespace for FireFox</span>
var index = name.indexOf(<span class="literal">':'</span>);
<span class="reserved">if</span>(index &gt; 0){
<span class="reserved">this</span>.setNamedValue(node, name.substr(index+1), value);
}
}
};
<span class="comment">/**
* Overrides DefaultDataModel.setValueAt to update the underlying XML Document
* <span class="attrib">@param</span> {Object} value The new value
* <span class="attrib">@param</span> {Number} rowIndex
* <span class="attrib">@param</span> {Number} colIndex
*/</span>
YAHOO.ext.grid.XMLDataModel.<span class="reserved">prototype</span>.setValueAt = <span class="reserved">function</span>(value, rowIndex, colIndex){
var node = <span class="reserved">this</span>.data[rowIndex].node;
<span class="reserved">if</span>(node){
var nodeValue = value;
<span class="reserved">if</span>(<span class="reserved">this</span>.postprocessors[colIndex]){
nodeValue = <span class="reserved">this</span>.postprocessors[colIndex](value);
}
<span class="reserved">this</span>.setNamedValue(node, <span class="reserved">this</span>.schema.fields[colIndex], nodeValue);
}
YAHOO.ext.grid.XMLDataModel.superclass.setValueAt.call(<span class="reserved">this</span>, value, rowIndex, colIndex);
};
<span class="comment">/**
* Overrides getRowId in DefaultDataModel to return the ID value of the specified node.
* <span class="attrib">@param</span> {Number} rowIndex
* <span class="attrib">@return</span> {Number}
*/</span>
YAHOO.ext.grid.XMLDataModel.<span class="reserved">prototype</span>.getRowId = <span class="reserved">function</span>(rowIndex){
<span class="reserved">return</span> <span class="reserved">this</span>.data[rowIndex].id;
};</pre>
<hr>
<hr>
<font size="-1">
</font>
<div class="jsdoc_ctime">Documentation generated by <a href="http://jsdoc.sourceforge.net/" target="_parent">JSDoc</a> on Sat Oct 14 06:07:10 2006</div>
</body>
</html>