📄 buffalo.js
字号:
if (typeof(obj[i]) != typeof(obj[0])) {
canBeArray = false;
break;
} else {
if (typeof(obj[i]) == 'object') {
if (obj[0][Buffalo.BOCLASS] != obj[i][Buffalo.BOCLASS]) {
canBeArray = false;
break;
}
}
}
}
if (canBeArray) {
type+="[";
obj = obj[0];
} else {
break;
}
}
if (type.indexOf("[") == -1) return "";
var componentType = obj[Buffalo.BOCLASS] || typeof(obj);
if (componentType == 'object') return "";
return type+componentType;
},
isArray: function(obj) {
return typeof(obj) == 'object' && obj.constructor == Array;
},
doStructXML : function(data){
var boType = data[Buffalo.BOCLASS] || "java.util.HashMap";
var xml = "<map>\n";
xml += "<type>" +boType+ "</type>\n";
for (var i in data){
if (data[i] != boType) {
if (typeof(data[i]) == "function") continue; /* the function shouldn't transfered. */
xml += this.getParamXML(this.dataTypeOf(i),i)+"\n";
xml += this.getParamXML(this.dataTypeOf(data[i]),data[i]) + "\n";
}
}
xml += "</map>\n";
return xml;
},
doNullXML : function() {
return "<null></null>";
},
getParamXML: function(type,data){
var xml;
switch (type){
case "date": xml = this.doDateXML(data); break;
case "list": xml = this.doArrayXML(data); break;
case "map": xml = this.doStructXML(data); break;
case "boolean": xml = this.doBooleanXML(data); break;
case "null": xml = this.doNullXML(); break;
default: xml = this.doValueXML(type,data); break;
}
return xml;
}
}
function dateToISO8609(date){
var year = date.getYear();
/* Fix for Y2K */
if (year < 2000) {
year += 1900;
}
var month = leadingZero(new String(date.getMonth()+1));
var day = leadingZero(new String(date.getDate()));
var time = leadingZero(new String(date.getHours())) + leadingZero(new String(date.getMinutes())) + leadingZero(new String(date.getSeconds()));
var converted = year+month+day+"T"+time+"Z";
return converted;
}
function leadingZero(n){
if (n.length==1) n = "0" + n;
return n;
}
Buffalo.Reply = Class.create();
Buffalo.Reply.prototype = {
initialize: function(xhr) {
this._isFault = false;
this._type = "null";
this._objects = [];
this._objectNodes = [];
this._source = xhr.responseText;
var root = xhr.responseXML ? xhr.responseXML.documentElement :
this.constructNodeFromXmlStringInIEOrFF(this._source);
this.dataNode = root.firstChild;
this._type = this._getType(this.dataNode);
},
constructNodeFromXmlStringInIEOrFF: function(xmlString) {
var xmldoc = XmlDocument.create();
xmldoc.async=false;
xmldoc.loadXML(xmlString);
return xmldoc.documentElement;
},
getType: function() { return this._type; },
getResult : function() { return this.deserialize(this.dataNode); },
isFault : function() { return (this._type == "fault"); },
isNull: function() { return (this._type == "null"); },
getSource : function() { return this._source; },
deserialize: function(dataNode) {
var ret;
var type = this._getType(dataNode);
switch (type) {
case "boolean": ret = this.doBoolean(dataNode); break;
case "date": ret = this.doDate(dataNode); break;
case "double": ret = this.doDouble(dataNode); break;
case "int":
case "long":
ret = this.doInt(dataNode);
break;
case "list": ret = this.doList(dataNode); break;
case "map": ret = this.doMap(dataNode); break;
case "null": ret = this.doNull(dataNode); break;
case "ref": ret = this.doRef(dataNode); break;
case "string": ret = this.doString(dataNode);break;
case "xml": ret = this.doXML(dataNode); break;
case "fault": ret = this.doFault(dataNode); break;
default: ;
}
return ret;
},
_getType : function(dataNode) {
return dataNode.tagName.toLowerCase();
},
getNodeText :function(dataNode) {
if (dataNode.childNodes.length == 0) {
return null;
} else
return dataNode.firstChild.nodeValue;
},
doBoolean : function (dataNode) {
var value = this.getNodeText(dataNode);
return (value == "1");
},
doDate : function (dataNode) {
var dateStr = this.getNodeText(dataNode);
var year = parseInt(dateStr.substring(0,4),"10");
var month = parseInt(dateStr.substring(4,6),"10") - 1;
var day = parseInt(dateStr.substring(6,8),"10");
var hour = parseInt(dateStr.substring(9,11),"10");
var minute = parseInt(dateStr.substring(11,13),"10");
var second = parseInt(dateStr.substring(13,15),"10");
var d = new Date(year, month, day, hour, minute, second);
return d;
},
doDouble : function (dataNode) {
var value = this.getNodeText(dataNode);
return parseFloat(value);
},
doInt: function (dataNode) {
var value = this.getNodeText(dataNode);
return parseInt(value);
},
doList: function (dataNode) {
var arr = new Array();
this._objects[this._objects.length] = arr;
var children = dataNode.childNodes;
arr[Buffalo.BOCLASS] = this.getNodeText(children[0]);
for (var i=2; i < children.length; i++) {
arr[arr.length] = this.deserialize(children[i]);
}
return arr;
},
doMap: function (dataNode) {
var obj = new Object();
this._objects[this._objects.length] = obj;
var attrs = dataNode.childNodes;
obj[Buffalo.BOCLASS] = this.getNodeText(attrs[0]);
for (var i = 1; i < attrs.length; i+=2) {
if (attrs[i+1].hasChildNodes() ) {
obj[this.getNodeText(attrs[i])] = this.deserialize(attrs[i+1]);
} else {
obj[this.getNodeText(attrs[i])] = attrs[i+1].text;
}
}
return obj;
},
doNull: function (dataNode) { return null; },
doRef: function (dataNode) {
var value = this.getNodeText(dataNode);
var idx = parseInt(value);
return this._objects[idx];
},
doString: function (dataNode) {
var value = this.getNodeText(dataNode);
if (value == null) {
return "";
}
return (value);
},
doXML : function (dataNode) {
var value = this.getNodeText(dataNode);
return unescape(value);
},
doFault : function (dataNode) {
var code = this.getNodeText(dataNode.childNodes[1]);
var msg = this.getNodeText(dataNode.childNodes[3]);
var detail = this.deserialize(dataNode.childNodes[5]);
return new Buffalo.Fault(code, msg, detail);
}
}
Buffalo.Fault = Class.create();
Buffalo.Fault.prototype = {
initialize: function(code, message, detail) {
this.code = code;
this.message = message;
this.detail = detail;
},
toString: function() {
return "Buffalo.Fault:[code=" + this.code + ", message=" + this.message + ", detail=" + this.detail+"]";
}
}
Object.extend(Buffalo.prototype, {
bindReply : function(service, params, bindElemId, options) {
this.remoteCall(service, params, function(reply) {
Buffalo.Bind.bind(bindElemId, reply.getResult(), options);
})
}
});
Buffalo.Bind = {
bind : function(elementId, bindValue, options) {
var elem = $(elementId);
switch(elem.tagName) {
case "INPUT":
switch (elem.type.toLowerCase()) {
case "text": ;
case "hidden": ;
case "password": Buffalo.BindFactory.bindText(elem, bindValue); break;
case "checkbox": ;
case "radio": Buffalo.BindFactory.bindRadioOrCheckbox(elem, bindValue); break;
}
break;
case "TEXTAREA":
Buffalo.BindFactory.bindText(elem, bindValue);
break;
case "TABLE":
Buffalo.BindFactory.bindTable(elem, bindValue);
break;
case "SELECT":
Buffalo.BindFactory.bindSelect(elem, bindValue, options);
break;
case "DIV":
case "SPAN":
elem.innerHTML = bindValue;
break;
case "FORM":
Buffalo.Form.bindForm(elem, bindValue);
}
}
}
Buffalo.BindFactory = {
reportError: function(elem, value, msg) {
throw "Data bind failed: "+msg;
},
bindText: function(elem, value) {
elem.value = value;
},
bindRadioOrCheckbox: function(elem, value) {
elem.checked = Buffalo.BindFactory.checkTrue(value);
},
bindSelect : function(elem, value, options) {
//TODO: Check the data type
if (typeof(value) != "object" || value.constructor != Array) {
this.reportError(elem,value,"Array Type Needed for binding select!");
}
while (elem.childNodes.length > 0) {
elem.removeChild(elem.childNodes[0]);
}
// bind data
for (var i = 0; i < value.length; i++) {
var option = document.createElement("OPTION");
if (options && options.binder) {
options.binder(value, option, i);
} else {
var data = value[i];
if (typeof(data) != 'object') {
option.value = data;
option.text = data;
} else {
option.value = data[elem.getAttribute("jvalue")];
option.text = data[elem.getAttribute("jtext")];
if (Buffalo.BindFactory.checkTrue(data.selected)) {
option.selected = true;
}
}
}
elem.options.add(option);
}
},
bindTable: function(elem, value) {
var jHeight = parseInt(elem.getAttribute("jheight"));
var dataHeader = [];
var tBody = elem.getElementsByTagName("TBODY")[0];
// clear the generated rows
if (elem.getElementsByTagName("TBODY").length > 0) {
while (tBody.rows.length > jHeight) {
tBody.deleteRow(jHeight);
}
}
if (jHeight == 0) { // if table is null, push the data to the tables.
for (var x in value[0] ) {
dataHeader[dataHeader.length] = x;
}
var hTr = elem.insertRow(elem.rows.length);
for (var i = 0; i < dataHeader.length; i++) {
var td = hTr.insertCell(hTr.cells.length);
td.innerHTML = dataHeader[i];
}
for (var i = 0; i < value.length; i++) {
var tr = elem.insertRow(elem.rows.length);
var data = value[i];
for (x in data ) {
var td = tr.insertCell(tr.cells.length);
td.innerHTML = data[x];
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -