123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- MWF.xAction = MWF.xAction || {};
- //MWF.require("MWF.xDesktop.Actions.RestActions", null, false);
- MWF.xAction.RestActions = MWF.Actions = {
- "actions": {},
- "loadedActions": {},
- "get": function(root){
- if (this.actions[root]) return this.actions[root];
- var actions = null;
- var url = o2.session.path+"/xAction/services/"+root+".json";
- MWF.getJSON(url, function(json){actions = json;}.bind(this), false, false, false);
- if (!MWF.xAction.RestActions.Action[root] && actions.clazz) MWF.require("MWF.xAction.services."+actions.clazz, null, false);
- if (!MWF.xAction.RestActions.Action[root]) MWF.xAction.RestActions.Action[root] = new Class({Extends: MWF.xAction.RestActions.Action});
- this.actions[root] = new MWF.xAction.RestActions.Action[root](root, actions);
- return this.actions[root];
- },
- "load": function(root){
- if (this.loadedActions[root]) return this.loadedActions[root];
- var jaxrs = null;
- //var url = this.getHost(root)+"/"+root+"/describe/describe.json";
- var url = this.getHost(root)+"/"+root+"/describe/api.json";
- //var url = "../o2_core/o2/xAction/temp.json";
- try{
- MWF.getJSON(url, function(json){jaxrs = json.jaxrs;}.bind(this), false, false, false);
- if (jaxrs){
- var actionObj = {};
- jaxrs.each(function(o){
- if (o.methods && o.methods.length){
- var actions = {};
- o.methods.each(function(m){
- var o = {"uri": "/"+m.uri};
- if (m.method) o.method = m.method;
- if (m.enctype) o.enctype = m.enctype;
- actions[m.name] = o;
- }.bind(this));
- actionObj[o.name] = new MWF.xAction.RestActions.Action(root, actions);
- //actionObj[o.name] = new MWF.xAction.RestActions.Action(root, o.methods);
- }
- }.bind(this));
- this.loadedActions[root] = actionObj;
- return actionObj;
- }
- }catch(e){}
- return null;
- },
- //actions: [{"action": "", "subAction": "TaskAction", "name": "list", "par": [], "body": "", "urlEncode": false, "cache": false}]
- async: function(actions, callback){
- var cbs = (o2.typeOf(callback)==="function") ? callback : callback.success;
- var cbf = (o2.typeOf(callback)==="function") ? null : callback.failure;
- var res = [];
- var len = actions.length;
- var jsons = new Array(len-1);
- var cb = function(){
- if (res.length===len) cbs.apply(this, jsons);
- };
- var _doError = function(xhr, text, error){
- if (xhr.status!=0){
- var errorText = error;
- if (xhr){
- var json = JSON.decode(xhr.responseText);
- if (json){
- errorText = json.message.trim() || "request json error";
- }else{
- errorText = "request json error: "+xhr.responseText;
- }
- }
- MWF.xDesktop.notice("error", {x: "right", y:"top"}, errorText);
- }
- };
- actions.each(function(action, i){
- var actionArgs = action.par || [];
- actionArgs.push(function(json){
- jsons[i] = json;
- res.push(true);
- cb();
- });
- actionArgs.push(function(xhr, text, error){
- res.push(false);
- if (!cbf){
- _doError(xhr, text, error);
- }else{
- cbf();
- }
- cb();
- });
- actionArgs.push(true);
- actionArgs.push(action.urlEncode);
- actionArgs.push(action.cache);
- action.action[action.subAction][action.name].apply(action.action[action.subAction], actionArgs);
- });
- },
- //actions: [{"action": "", "name": "list", "par": [], "body": "", "urlEncode": false, "cache": false}]
- invokeAsync2: function(actions, callback){
- var cbs = (o2.typeOf(callback)==="function") ? callback : callback.success;
- var cbf = (o2.typeOf(callback)==="function") ? null : callback.failure;
- var res = [];
- var len = actions.length;
- var jsons = new Array(len-1);
- var cb = function(){
- if (res.length===len) cbs.apply(this, jsons);
- };
- var _doError = function(xhr, text, error){
- if (xhr.status!=0){
- var errorText = error;
- if (xhr){
- var json = JSON.decode(xhr.responseText);
- if (json){
- errorText = json.message.trim() || "request json error";
- }else{
- errorText = "request json error: "+xhr.responseText;
- }
- }
- MWF.xDesktop.notice("error", {x: "right", y:"top"}, errorText);
- }
- };
- actions.each(function(action, i){
- var actionArgs = action.par || [];
- actionArgs.push(function(json){
- jsons[i] = json;
- res.push(true);
- cb();
- });
- actionArgs.push(function(xhr, text, error){
- res.push(false);
- if (!cbf){
- _doError(xhr, text, error);
- }else{
- cbf();
- }
- cb();
- });
- actionArgs.push(true);
- actionArgs.push(action.urlEncode);
- actionArgs.push(action.cache);
- action.action[action.name].apply(action.action, actionArgs);
- });
- },
- "getHost": function(root){
- var addressObj = layout.serviceAddressList[root];
- var address = "";
- var defaultPort = layout.config.app_protocol==='https' ? "443" : "80";
- if (addressObj){
- var appPort = addressObj.port || window.location.port;
- address = layout.config.app_protocol+"//"+(addressObj.host || window.location.hostname)+ ((!appPort || appPort.toString()===defaultPort) ? "" : ":"+appPort);
- }else{
- var host = layout.desktop.centerServer.host || window.location.hostname;
- var port = layout.desktop.centerServer.port || window.location.port;
- //var mapping = layout.getCenterUrlMapping();
- address = layout.config.app_protocol+"//"+host+( (!port || port.toString()===defaultPort) ? "" : ":"+port);
- }
- return address;
- },
- "invokeAsync": function(actions, callback){
- var len = actions.length;
- var parlen = arguments.length-2;
- var res = [];
- var jsons = new Array(len-1);
- var args = arguments;
- var cbs = (o2.typeOf(callback)==="function") ? callback : callback.success;
- var cbf = (o2.typeOf(callback)==="function") ? null : callback.failure;
- var cb = function(){
- if (res.length===len) cbs.apply(this, jsons);
- };
- var _doError = function(xhr, text, error){
- if (xhr.status!=0){
- var errorText = error;
- if (xhr){
- var json = JSON.decode(xhr.responseText);
- if (json){
- errorText = json.message.trim() || "request json error";
- }else{
- errorText = "request json error: "+xhr.responseText;
- }
- }
- MWF.xDesktop.notice("error", {x: "right", y:"top"}, errorText);
- }
- };
- actions.each(function(action, i){
- var par = (i<parlen) ? args[i+2] : args[parlen+1];
- if (par){
- var actionArgs = (o2.typeOf(par)==="array") ? par : [par];
- actionArgs.unshift(function(xhr, text, error){
- res.push(false);
- if (!cbf){
- _doError(xhr, text, error);
- }else{
- cbf();
- }
- cb();
- return true;
- });
- //actionArgs.unshift(null);
- actionArgs.unshift(function(json){
- jsons[i] = json;
- res.push(true);
- cb();
- });
- var p = action.action[action.name].apply(action.action, actionArgs);
- // p.catch(function(xhr, text, error){
- // res.push(false);
- // if (!cbf){
- // _doError(xhr, text, error);
- // }else{
- // cbf();
- // }
- // cb();
- // })
- }else{
- action.action[action.name](function(){
- jsons[i] = json;
- res.push(true);
- cb();
- }, function(xhr, text, error){
- res.push(false);
- if (!cbf){
- _doError(xhr, text, error);
- }else{
- cbf();
- }
- cb();
- });
- }
- });
- }
- };
- MWF.xAction.RestActions.Action = new Class({
- initialize: function(root, actions){
- this.action = new MWF.xDesktop.Actions.RestActions("/xAction/services/"+root+".json", root, "");
- this.action.actions = actions;
- Object.each(this.action.actions, function(service, key){
- if (service.uri) if (!this[key]) this.createMethod(service, key);
- }.bind(this));
- },
- createMethod: function(service, key){
- var jaxrsUri = service.uri;
- var re = new RegExp("\{.+?\}", "g");
- var replaceWords = jaxrsUri.match(re);
- var parameters = [];
- if (replaceWords) parameters = replaceWords.map(function(s){
- return s.substring(1,s.length-1);
- });
- this[key] = this.invokeFunction(service, parameters, key);
- },
- invokeFunction: function(service, parameters, key){
- //uri的参数, data(post, put), file(formData), success, failure, async
- return function(){
- var i = parameters.length-1;
- var n = arguments.length;
- var functionArguments = arguments;
- var parameter = {};
- var success, failure, async, data, file;
- if (typeOf(functionArguments[0])==="function"){
- i=-1;
- success = (n>++i) ? functionArguments[i] : null;
- failure = (n>++i) ? functionArguments[i] : null;
- parameters.each(function(p, x){
- parameter[p] = (n>++i) ? functionArguments[i] : null;
- });
- if (service.method && (service.method.toLowerCase()==="post" || service.method.toLowerCase()==="put")){
- if ((!service.enctype) || service.enctype.toLowerCase()!=="formdata"){
- data = (n>++i) ? functionArguments[i] : null;
- }else{
- data = (n>++i) ? functionArguments[i] : null;
- file = (n>++i) ? functionArguments[i] : null;
- }
- }
- async = (n>++i) ? functionArguments[i] : null;
- urlEncode = (n>++i) ? functionArguments[i] : true;
- cache = (n>++i) ? functionArguments[i] : (Browser.name != "ie");
- }else{
- parameters.each(function(p, x){
- parameter[p] = (n>x) ? functionArguments[x] : null;
- });
- if (service.method && (service.method.toLowerCase()==="post" || service.method.toLowerCase()==="put")){
- if ((!service.enctype) || service.enctype.toLowerCase()!=="formdata"){
- data = (n>++i) ? functionArguments[i] : null;
- }else{
- data = (n>++i) ? functionArguments[i] : null;
- file = (n>++i) ? functionArguments[i] : null;
- }
- }
- success = (n>++i) ? functionArguments[i] : null;
- failure = (n>++i) ? functionArguments[i] : null;
- async = (n>++i) ? functionArguments[i] : null;
- urlEncode = (n>++i) ? functionArguments[i] : true;
- cache = (n>++i) ? functionArguments[i] : (Browser.name != "ie");
- }
- return this.invoke(service,{"name": key, "async": async, "data": data, "file": file, "parameter": parameter, "success": success, "failure": failure, "urlEncode": urlEncode, "cache": cache});
- //if (!cache) debugger;
- //return this.action.invoke({"name": key, "async": async, "data": data, "file": file, "parameter": parameter, "success": success, "failure": failure, "urlEncode": urlEncode, "cache": cache});
- }.bind(this);
- },
- invoke: function(service, options){
- return this.action.invoke(options);
- }
- });
|