📄 wddx.js
字号:
///////////////////////////////////////////////////////////////////////////// isColumn(name) returns true/false based on whether this is a column namefunction wddxRecordset_isColumn(name){ // Columns must be objects // WddxRecordset extensions might use properties prefixed with // _private_ and these will not be treated as columns return (typeof(this[name]) == "object" && name.indexOf("_private_") == -1);}///////////////////////////////////////////////////////////////////////////// getRowCount() returns the number of rows in the recordsetfunction wddxRecordset_getRowCount(){ var nRowCount = 0; for (var col in this) { if (this.isColumn(col)) { nRowCount = this[col].length; break; } } return nRowCount;}///////////////////////////////////////////////////////////////////////////// addColumn(name) adds a column with that name and length == getRowCount()function wddxRecordset_addColumn(name){ var nLen = this.getRowCount(); var colValue = new Array(nLen); for (var i = 0; i < nLen; ++i) { colValue[i] = null; } this[this.preserveFieldCase ? name : name.toLowerCase()] = colValue;}///////////////////////////////////////////////////////////////////////////// addRows() adds n rows to all columns of the recordsetfunction wddxRecordset_addRows(n){ for (var col in this) { if (this.isColumn(col)) { var nLen = this[col].length; for (var i = nLen; i < nLen + n; ++i) { this[col][i] = null; } } }}///////////////////////////////////////////////////////////////////////////// getField() returns the element in a given (row, col) positionfunction wddxRecordset_getField(row, col){ return this[this.preserveFieldCase ? col : col.toLowerCase()][row];}///////////////////////////////////////////////////////////////////////////// setField() sets the element in a given (row, col) position to valuefunction wddxRecordset_setField(row, col, value){ this[this.preserveFieldCase ? col : col.toLowerCase()][row] = value;}///////////////////////////////////////////////////////////////////////////// wddxSerialize() serializes a recordset// returns true/falsefunction wddxRecordset_wddxSerialize(serializer){ // Create an array and a list of column names var colNamesList = ""; var colNames = new Array(); var i = 0; for (var col in this) { if (this.isColumn(col)) { colNames[i++] = col; if (colNamesList.length > 0) { colNamesList += ","; } colNamesList += col; } } var nRows = this.getRowCount(); serializer.write("<recordset rowCount='" + nRows + "' fieldNames='" + colNamesList + "'>"); var bSuccess = true; for (i = 0; bSuccess && i < colNames.length; i++) { var name = colNames[i]; serializer.write("<field name='" + name + "'>"); for (var row = 0; bSuccess && row < nRows; row++) { bSuccess = serializer.serializeValue(this[name][row]); } serializer.write("</field>"); } serializer.write("</recordset>"); return bSuccess;}///////////////////////////////////////////////////////////////////////////// dump(escapeStrings) returns an HTML table with the recordset data// It is a convenient routine for debugging and testing recordsets// The boolean parameter escapeStrings determines whether the <>& // characters in string values are escaped as <>&function wddxRecordset_dump(escapeStrings){ // Get row count var nRows = this.getRowCount(); // Determine column names var colNames = new Array(); var i = 0; for (var col in this) { if (typeof(this[col]) == "object") { colNames[i++] = col; } } // Build table headers var o = "<table border=1><tr><td><b>RowNumber</b></td>"; for (i = 0; i < colNames.length; ++i) { o += "<td><b>" + colNames[i] + "</b></td>"; } o += "</tr>"; // Build data cells for (var row = 0; row < nRows; ++row) { o += "<tr><td>" + row + "</td>"; for (i = 0; i < colNames.length; ++i) { var elem = this.getField(row, colNames[i]); if (escapeStrings && typeof(elem) == "string") { var str = ""; for (var j = 0; j < elem.length; ++j) { var ch = elem.charAt(j); if (ch == '<') { str += "<"; } else if (ch == '>') { str += ">"; } else if (ch == '&') { str += "&"; } else { str += ch; } } o += ("<td>" + str + "</td>"); } else { o += ("<td>" + elem + "</td>"); } } o += "</tr>"; } // Close table o += "</table>"; // Return HTML recordset dump return o; }///////////////////////////////////////////////////////////////////////////// WddxRecordset([flagPreserveFieldCase]) creates an empty recordset.// WddxRecordset(columns [, flagPreserveFieldCase]) creates a recordset // with a given set of columns provided as an array of strings.// WddxRecordset(columns, rows [, flagPreserveFieldCase]) creates a // recordset with these columns and some number of rows.// In all cases, flagPreserveFieldCase determines whether the exact case// of field names is preserved. If omitted, the default value is false// which means that all field names will be lowercased.function WddxRecordset(){ // Add default properties this.preserveFieldCase = false; // Add extensions if (typeof(wddxRecordsetExtensions) == "object") { for (var prop in wddxRecordsetExtensions) { // Hook-up method to WddxRecordset object this[prop] = wddxRecordsetExtensions[prop] } } // Add built-in methods this.getRowCount = wddxRecordset_getRowCount; this.addColumn = wddxRecordset_addColumn; this.addRows = wddxRecordset_addRows; this.isColumn = wddxRecordset_isColumn; this.getField = wddxRecordset_getField; this.setField = wddxRecordset_setField; this.wddxSerialize = wddxRecordset_wddxSerialize; this.dump = wddxRecordset_dump; // Perfom any needed initialization if (WddxRecordset.arguments.length > 0) { if (typeof(val = WddxRecordset.arguments[0].valueOf()) == "boolean") { // Case preservation flag is provided as 1st argument this.preserveFieldCase = WddxRecordset.arguments[0]; } else { // First argument is the array of column names var cols = WddxRecordset.arguments[0]; // Second argument could be the length or the preserve case flag var nLen = 0; if (WddxRecordset.arguments.length > 1) { if (typeof(val = WddxRecordset.arguments[1].valueOf()) == "boolean") { // Case preservation flag is provided as 2nd argument this.preserveFieldCase = WddxRecordset.arguments[1]; } else { // Explicitly specified recordset length nLen = WddxRecordset.arguments[1]; if (WddxRecordset.arguments.length > 2) { // Case preservation flag is provided as 3rd argument this.preserveFieldCase = WddxRecordset.arguments[2]; } } } for (var i = 0; i < cols.length; ++i) { var colValue = new Array(nLen); for (var j = 0; j < nLen; ++j) { colValue[j] = null; } this[this.preserveFieldCase ? cols[i] : cols[i].toLowerCase()] = colValue; } } }}/////////////////////////////////////////////////////////////////////////////// WddxRecordset extensions//// The WddxRecordset class has been designed with extensibility in mind.//// Developers can add new properties to the object and as long as their// names are prefixed with _private_ the WDDX serialization function of// WddxRecordset will not treat them as recordset columns.//// Developers can create new methods for the class outside this file as// long as they make a call to registerWddxRecordsetExtension() with the// name of the method and the function object that implements the method.// The WddxRecordset constructor will automatically register all these// methods with instances of the class.//// Example://// If I want to add a new WddxRecordset method called addOneRow() I can// do the following://// - create the method implementation//// function wddxRecordset_addOneRow()// {// this.addRows(1);// }//// - call registerWddxRecordsetExtension() //// registerWddxRecordsetExtension("addOneRow", wddxRecordset_addOneRow);//// - use the new function//// rs = new WddxRecordset();// rs.addOneRow();////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// registerWddxRecordsetExtension(name, func) can be used to extend // functionality by registering functions that should be added as methods // to WddxRecordset instances.function registerWddxRecordsetExtension(name, func){ // Perform simple validation of arguments if (typeof(name) == "string" && typeof(func) == "function") { // Guarantee existence of wddxRecordsetExtensions object if (typeof(wddxRecordsetExtensions) != "object") { // Create wddxRecordsetExtensions instance wddxRecordsetExtensions = new Object(); } // Register extension; override an existing one wddxRecordsetExtensions[name] = func; }}/////////////////////////////////////////////////////////////////////////////// WddxBinary////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// wddxSerialize() serializes a binary value// returns true/falsefunction wddxBinary_wddxSerialize(serializer) { serializer.write( "<binary encoding='" + this.encoding + "'>" + this.data + "</binary>"); return true;}///////////////////////////////////////////////////////////////////////////// WddxBinary() constructs an empty binary value// WddxBinary(base64Data) constructs a binary value from base64 encoded data// WddxBinary(data, encoding) constructs a binary value from encoded datafunction WddxBinary(data, encoding){ this.data = data != null ? data : ""; this.encoding = encoding != null ? encoding : "base64"; // Custom serialization mechanism this.wddxSerialize = wddxBinary_wddxSerialize;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -