📄 jsclass_base.js
字号:
/********************************************************************************* * SugarCRM is a customer relationship management program developed by * SugarCRM, Inc. Copyright (C) 2004 - 2007 SugarCRM Inc. * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License version 3 as published by the * Free Software Foundation with the addition of the following permission added * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * this program; if not, see http://www.gnu.org/licenses or write to the Free * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * 02110-1301 USA. * * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road, * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com. * * The interactive user interfaces in modified source and object code versions * of this program must display Appropriate Legal Notices, as required under * Section 5 of the GNU General Public License version 3. * * In accordance with Section 7(b) of the GNU General Public License version 3, * these Appropriate Legal Notices must retain the display of the "Powered by * SugarCRM" logo. If the display of the logo is not reasonably feasible for * technical reasons, the Appropriate Legal Notices must display the words * "Powered by SugarCRM". ********************************************************************************/// inline modules, don't do importmodule!// from file langlite.jslanglite = Module("langlite", "0.3.7", function(mod){ mod.JSONParser=Class("JSONParser", function(publ, supr){ publ.init=function(){ this.libs = {}; } publ.addLib = function(obj, name, exports){ if(exports == null){ this.libs[name] = obj; }else{ for(var i=0;i<exports.length;i++){ this.libs[name + "." + exports[i]] = obj[exports[i]]; } } } publ.objToJson=function(obj){ if(obj == null){ return "null"; }else{ return mod.objToJson(obj); } } }) mod.parser = new mod.JSONParser(); /** Turns JSON code into JavaScript objects. @param src The source as a String. */ mod.jsonToObj=function(src){ return mod.parser.jsonToObj(src); } var json_types = new Object(); json_types['object'] = function(obj){ var v=[]; for(attr in obj){ if(typeof obj[attr] != "function"){ v.push('"' + attr + '": ' + mod.objToJson(obj[attr])); } } return "{" + v.join(", ") + "}"; } json_types['string'] = function(obj){ var s = '"' + obj.replace(/(["\\])/g, '\\$1') + '"'; s = s.replace(/(\n)/g,"\\n"); return s; } json_types['number'] = function(obj){ return obj.toString(); } json_types['boolean'] = function(obj){ return obj.toString(); } json_types['date'] = function(obj){ var padd=function(s, p){ s=p+s return s.substring(s.length - p.length) } var y = padd(obj.getUTCFullYear(), "0000"); var m = padd(obj.getUTCMonth() + 1, "00"); var d = padd(obj.getUTCDate(), "00"); var h = padd(obj.getUTCHours(), "00"); var min = padd(obj.getUTCMinutes(), "00"); var s = padd(obj.getUTCSeconds(), "00"); var isodate = y + m + d + "T" + h + ":" + min + ":" + s return '{"jsonclass":["sys.ISODate", ["' + isodate + '"]]}'; } json_types['array'] = function(obj){ var v = []; for(var i=0;i<obj.length;i++){ v.push(mod.objToJson(obj[i])) ; } return "[" + v.join(", ") + "]"; } mod.objToJson=function(obj){ if ( typeof(obj) == 'undefined') { return ''; } if ( typeof(json_types[typeof(obj)]) == 'undefined') { alert('class not defined for toJSON():'+typeof(obj)); } return json_types[typeof(obj)](obj); } mod.test=function(){ try{ print(mod.objToJson(['sds', -12377,-1212.1212, 12, '-2312'])); }catch(e){ print(e.toTraceString()); } } })// from file jsonrpclite.jsjsonrpclite = Module("jsonrpclite", "0.3.2", function(mod){ // SUGARCRM: var lang = langlite // inline module // var lang = importModule("langlite"); // END SUGARCRM var tokens = lang.tokens; var ObjectBuffer=Class("ObjectBuffer", function(publ, supr){ publ.init=function(){ this.data=""; } publ.getObjects=function(data){ this.data += data; var t = new lang.Tokenizer(this.data); var brCnt= 0; var objects = []; var readCnt = 0 while(! t.finished()){ var n = t.next(); if(n.type != tokens.ERR){ if(n.value == "{"){ brCnt+=1; }else if(n.value == "}"){ brCnt-=1; if(brCnt==0){ var s = this.data.slice(readCnt, n.pos+1); readCnt += s.length; objects.push(s); } } }else{ break; } } this.data = this.data.slice(readCnt); return objects; } }) var nameAllowed=function(name){ return name.match(/^[a-zA-Z]\w*$/) != null; } var getMethodByName=function(obj, name){ try{//to get a method by asking the service obj = obj._getMethodByName(name) }catch(e){ var names = name.split("."); for(var i=0;i<names.length;i++){ name = names[i]; if(nameAllowed(name)){ obj = obj[name]; } } } return obj; } var Response=Class("Response", function(publ, supr){ publ.init=function(id, result, error){ this.id=id; this.result = result; this.error = error; } publ._toJSON=function(){ var p = [lang.objToJson(this.id), lang.objToJson(this.result),lang.objToJson(this.error)]; return '{"id":' + p[0] + ', "result":' + p[1] + ', "error":' + p[2] + "}"; } }) var Request=Class("Request", function(publ, supr){ publ.init=function(id, method, params){ this.id=id; this.method = method; this.params = params;/* var str = '';for(hello in this.params){ str += "var:"+hello+":"+this.params[hello]+":\n";}alert(str);*/ } publ._toJSON=function(){ var p = [lang.objToJson(this.id), lang.objToJson(this.method),lang.objToJson(this.params)]; return '{"id":' + p[0] + ', "method":' + p[1] + ', "params":' + p[2] + "}"; } }) var Notification=Class("Notification", function(publ, supr){ publ.init=function(method, params){ this.method = method; this.params = params; } publ._toJSON=function(){ var p = [lang.objToJson(this.method),lang.objToJson(this.params)]; return '{"method":' + p[0] + ', "params":' + p[1] + "}"; } }) var ResponseHandler=Class("ResponseHandler", function(publ, supr){ publ.init=function(callback){ this.callback=callback; } publ.handleResponse=function(resp){ this.callback(resp.result, resp.error); } }) var RPCLib = Class("RPCLib", function(publ, supr){ }) var BaseConnectionHandler = Class("BaseConnectionHandler", function(publ, supr){ publ.init=function(service){ this.service = service; this.jsonParser = new lang.JSONParser(); this.jsonParser.addLib(new RPCLib(), "rpc", []); this.respHandlers = []; this.objBuffer = new ObjectBuffer(); } publ.addResponseHandler=function(cb){ var id=1; while(this.respHandlers[""+id] ){ id+=1; } id="" + id; this.respHandlers[id] = new ResponseHandler(cb); return id; } publ.send = function(data){ } publ.sendNotify = function(name, args){ var n = new Notification(name, args); n = this.jsonParser.objToJson(n); this.send(n) } publ.sendRequest = function(name, args, callback){ var id = this.addResponseHandler(callback); var r = new Request(id, name, args); r = this.jsonParser.objToJson(r); this.send(r); } publ.sendResponse = function(id, result, error){ var r = new Response(id, result, error); r = this.jsonParser.objToJson(r); this.send(r); } publ.handleRequest = function(req){ var name = req.method; var params = req.params; var id=req.id; if(this.service[name]){ try{ var rslt = this.service[name].apply(this.service,params); this.sendResponse(id, rslt, null) }catch(e){ this.sendResponse(id, null, new ApplicationError("" + e)) } }else{ this.sendResponse(id, null, new MethodNotFound()); } } publ.handleNotification = function(notif){ if(this.service[notif.method]){ try{ this.service[notif.method].apply(this.service, notif.params); }catch(e){ } } } publ.handleResponse = function(resp){ var id=resp.id; var h = this.respHandlers[id]; h.handleResponse(resp) delete this.respHandlers[id] } publ.handleData = function(data){ var objs = this.objBuffer.getObjects(data); for(var i=0;i<objs.length;i++){ try{ var obj = this.jsonParser.jsonToObj(objs[i]); }catch(e){ throw "Not well formed"; } if(obj.method != null){ if(obj.id != null){ this.handleRequest(new Request(obj.id, obj.method, obj.params)); }else{ this.handleNotification(new Notification(obj.method, obj.params)); } }else if(obj.id != null){ this.handleResponse(new Response(obj.id, obj.result, obj.error)); }else{ throw "Unknown Data"; } } } }) var SocketConnectionHandler = Class("SocketConnectionHandler", BaseConnectionHandler, function(publ, supr){ publ.init=function(socket, localService){ this.socket = socket; socket.addEventListener("connectionData", this, false); supr(this).init( localService); } publ.handleEvent=function(evt){ this.handleData(evt.data); } publ.send=function(data){ this.socket.send(data); } publ.close=function(data){ this.socket.close(); } }) var HTTPConnectionHandler = Class("HTTPConnectionHandler", BaseConnectionHandler, function(publ, supr){ var urllib; publ.request_id = 1; publ.init=function(url, localService){ urllib=importModule("urllib"); this.url = url; supr(this).init( localService); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -