mwf.xdesktop.requireapp("process.xform", "$module", null, false);
/** @class subform 子表单组件。
* @o2cn 子表单
* @example
* //可以在脚本中获取该组件
* //方法1:
* var subform = this.form.get("fieldid"); //获取组件
* //方法2
* var subform = this.target; //在组件本身的脚本中获取
* @extends mwf.xapplication.process.xform.$module
* @o2category formcomponents
* @o2range {process|cms}
* @hideconstructor
*/
mwf.xapplication.process.xform.subform = mwf.appsubform = new class(
/** @lends mwf.xapplication.process.xform.subform# */
{
extends: mwf.app$module,
options: {
/**
* 子表单的设计已经获取到,但还没有插入html及生成内部组件。
* @event mwf.xapplication.process.xform.subform#beforemodulesload
* @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zts|组件事件说明}
*/
/**
* 子表单的设计已经获取到,已经插入html,组件json已经获取到,但未生成内部组件。
* @example
* //获取子表单所有组件id
* var moduleidlist = object.keys(this.target.subformdata.json.modulelist);
* @event mwf.xapplication.process.xform.subform#modulesload
* @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zts|组件事件说明}
*/
/**
* 子表单内部组件加载完成。
* @example
* //获取子表单所有组件id
* var moduleidlist = object.keys(this.target.subformdata.json.modulelist);
* //获取子表单所有组件
* var modulelist = moduleidlist.map(function(id){
* return this.form.get(id, subformid); //subformid为当前子表单id,布局组件有可能id冲突,通过subformid来确定当前子表单的组件
* }.bind(this))
* @event mwf.xapplication.process.xform.subform#aftermodulesload
* @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zts|组件事件说明}
*/
"moduleevents": ["load", "queryload", "postload", "beforemodulesload", "modulesload", "aftermodulesload"]
},
_loaduserinterface: function () {
/**
* @ignore
* @member parentline
* @memberof mwf.xapplication.process.xform.subform#
*/
this.node.empty();
this.modules = [];
this.modulelist = {};
if (this.json.isdelay) {
if (this.form.subformloadedcount) {
this.form.subformloadedcount ;
} else {
this.form.subformloadedcount = 1
}
this.form.checksubformloaded();
this.checked = true;
} else {
this.getsubform(function () {
this.loadsubform();
}.bind(this));
}
},
/**
* @summary 当子表单被设置为延迟加载,通过active方法激活
* @param {function} callback 激活后的回调方法,另外已经激活过该方法还会被执行。
* @example
* var subform = this.form.get("fieldid");
* subform.active(function(){
* //do someting
* })
*/
active: function (callback) {
if (!this.loaded) {
this.reload(callback)
} else {
if (callback) callback();
}
},
/**
* @summary 重新加载子表单
* @param {function} callback
* @example
* this.form.get("fieldid").reload(function(){
* //do someting
* })
*/
reload: function (callback) {
this.clean();
this.getsubform(function () {
this.loadsubform();
if (callback) callback();
}.bind(this));
},
clean: function(){
(this.modules || []).each(function(module){
if (this.form.all[module.json.id]) delete this.form.all[module.json.id];
if (this.form.forms[module.json.id])delete this.form.forms[module.json.id];
this.form.modules.erase(module);
}.bind(this));
object.each(this.modulelist || {}, function (module, formkey) {
delete this.form.json.modulelist[formkey];
}.bind(this));
if( this.subformdata && this.subformdata.json.id ){
var id = this.subformdata.json.id;
if( this.form.subformloaded && this.form.subformloaded.length ){
this.form.subformloaded.erase(id);
}
if( this.parentformidlist && this.parentformidlist.length){
this.parentformidlist.erase(id);
}
}
this.modules = [];
this.modulelist = {};
this.node.empty();
},
loadcss: function () {
if (this.subformdata.json.css && this.subformdata.json.css.code) {
var csstext = this.subformdata.json.css.code;
//删除注释
csstext = csstext.replace(/\/\*[\s\s]*?\*\/\n|([^:]|^)\/\/.*\n$/g, '').replace(/\\n/, '');
csstext = this.form.parsecss(csstext);
var rex = new regexp("(. )(?=\\{)", "g");
var match;
var id = this.form.json.id.replace(/\-/g, "");
var prefix = ".css" id " ";
while ((match = rex.exec(csstext)) !== null) {
var rulesstr = match[0];
var startwith = rulesstr.substring(0, 1);
if (startwith === "@" || startwith === ":" || rulesstr.indexof("%") !== -1) {
}else if (rulesstr.trim()==='from' || rulesstr.trim()==='to'){
} else {
if (rulesstr.indexof(",") != -1) {
//var rules = rulesstr.split(/\s*,\s*/g);
var rules = rulesstr.split(/,/g);
rules = rules.map(function (r) {
return prefix r;
});
var rule = rules.join(",");
csstext = csstext.substring(0, match.index) rule csstext.substring(rex.lastindex, csstext.length);
rex.lastindex = rex.lastindex (prefix.length * rules.length);
} else {
var rule = prefix match[0];
csstext = csstext.substring(0, match.index) rule csstext.substring(rex.lastindex, csstext.length);
rex.lastindex = rex.lastindex prefix.length;
}
}
}
var stylenode = $("style" this.form.json.id);
if (!stylenode) {
var stylenode = document.createelement("style");
stylenode.setattribute("type", "text/css");
stylenode.id = "style" this.form.json.id;
stylenode.inject(this.form.container, "before");
}
if (stylenode.stylesheet) {
var setfunc = function () {
stylenode.stylesheet.csstext = csstext;
};
if (stylenode.stylesheet.disabled) {
settimeout(setfunc, 10);
} else {
setfunc();
}
} else {
var csstextnode = document.createtextnode(csstext);
stylenode.appendchild(csstextnode);
}
}
},
checksubformnested: function (id) {
if (!id) return true;
if (this.parentformidlist) {
return !this.parentformidlist.contains(id);
} else {
return ![this.form.json.id].contains(id);
}
},
checksubformunique: function (id) {
if (!id) return true;
if (!this.form.subformloaded) return true;
return !this.form.subformloaded.contains(id);
},
getparentformidlist: function () {
var parentformidlist;
if (this.parentformidlist) {
parentformidlist = array.clone(this.parentformidlist);
parentformidlist.push(this.subformdata.json.id)
} else {
parentformidlist = [this.form.json.id, this.subformdata.json.id];
}
return parentformidlist;
},
loadsubform: function () {
if (this.subformdata) {
if (!this.checksubformnested(this.subformdata.json.id)) {
this.form.notice(mwf.xapplication.process.xform.lp.subformnestederror, "error");
} else if (!this.checksubformunique(this.subformdata.json.id)) {
this.form.notice(mwf.xapplication.process.xform.lp.subformuniqueerror, "error");
} else {
//this.form.addevent("postload", function(){
this.fireevent("beforemodulesload");
this.loadcss();
this.modules = [];
this.modulelist = {};
this.node.set("html", this.subformdata.html);
object.each(this.subformdata.json.modulelist, function (module, key) {
var formkey = key;
if (this.form.json.modulelist[key]) {
formkey = this.json.id "_" key;
var modulenode = this.node.getelement("#" key);
if (modulenode) modulenode.set("id", formkey);
module.id = formkey;
}
this.form.json.modulelist[formkey] = module;
this.modulelist[formkey] = module;
}.bind(this));
this.fireevent("modulesload");
var modulenodes = this.form._getmodulenodes(this.node);
modulenodes.each(function (node) {
if (node.get("mwftype") !== "form") {
var _self = this;
var json = this.form._getdomjson(node);
//if( json.type === "subform" || json.modulename === "subform" )this.form.subformcount ;
var module = this.form._loadmodule(json, node, function () {
this.parentformidlist = _self.getparentformidlist();
});
this.form.modules.push(module);
this.modules.push(module);
}
}.bind(this));
this.form.subformloaded.push(this.subformdata.json.id);
this.fireevent("aftermodulesload");
//}.bind(this));
}
}
if (!this.checked) {
if (this.form.subformloadedcount) {
this.form.subformloadedcount ;
} else {
this.form.subformloadedcount = 1
}
this.form.checksubformloaded();
}
//console.log( "add subformloadedcount , this.form.subformloadedcount = " this.form.subformloadedcount)
/**
* @summary 表单是否加载(激活)过。
* @member {boolean}
* @example
* if( !this.form.get("fieldid").loaded ){ //判断子表单是否加载过
* this.form.get("fieldid").active(); //没有加载过则激活
* }
*/
this.loaded = true;
this.checked = true;
},
getsubform: function (callback) {
var method = (this.form.json.mode !== "mobile" && !layout.mobile) ? "getform" : "getformmobile";
if (this.json.subformtype === "script") {
if (this.json.subformscript && this.json.subformscript.code) {
var data = this.form.macro.exec(this.json.subformscript.code, this);
if (data) {
var formname, app;
if (typeof(data) === "string") {
formname = data;
} else {
if (data.application) app = data.application;
if (data.subform) formname = data.subform;
}
if (formname) {
if (!app) app = (this.form.businessdata.work || this.form.businessdata.workcompleted).application;
mwf.actions.get("x_processplatform_assemble_surface")[method](formname, app, function (json) {
this.getsubformdata(json.data);
if (callback) callback();
}.bind(this));
} else {
if (callback) callback();
}
} else {
if (callback) callback();
}
}
} else {
if (this.json.subformselected && this.json.subformselected !== "none") {
var subformdata = (this.form.app.relatedformmap) ? this.form.app.relatedformmap[this.json.subformselected] : null;
if (subformdata) {
this.getsubformdata({"data": subformdata.data});
if (callback) callback();
} else {
var app;
if (this.json.subformappselected) {
app = this.json.subformappselected;
} else {
app = (this.form.businessdata.work || this.form.businessdata.workcompleted).application;
}
mwf.actions.get("x_processplatform_assemble_surface")[method](this.json.subformselected, app, function (json) {
this.getsubformdata(json.data);
if (callback) callback();
}.bind(this));
}
} else {
if (callback) callback();
}
}
},
getsubformdata: function (data) {
if (!data || typeof(data) !== "object") return;
var subformdatastr = null;
// if ( this.form.json.mode !== "mobile" && !layout.mobile){
// subformdatastr = data.data;
// }else{
// subformdatastr = data.mobiledata;
// }
subformdatastr = data.data;
this.subformdata = null;
if (subformdatastr) {
if( this.form.isparselanguage ) {
var jsonstr = o2.bindjson(mwf.decodejsonstring(subformdatastr), {"lp": mwf.xapplication.process.xform.lp.form});
this.subformdata = json.decode(jsonstr);
}else{
this.subformdata = json.decode(mwf.decodejsonstring(subformdatastr));
}
this.subformdata.updatetime = data.updatetime;
}
}
});
mwf.xapplication.process.xform.submitform = mwf.appsubmitform = new class({
extends: mwf.appsubform,
_loaduserinterface: function () {
// this.node.empty();
this.getsubform(function () {
this.loadsubform();
}.bind(this));
},
reload: function () {
// this.node.empty();
this.getsubform(function () {
this.loadsubform();
}.bind(this));
},
show: function ( defaultroute ) {
if (this.json.submitscript && this.json.submitscript.code) {
this.form.macro.environment.defaultroute = defaultroute;
this.form.macro.exec(this.json.submitscript.code, this);
}
// this.firesubformevent("load");
},
// firesubformevent : function( name ){
// var events = this.subformdata.json.events;
// if( events && events[name] && events[name]["code"] ){
// this.form.macro.exec(events[name]["code"], this);
// }
// },
loadsubform: function () {
if (this.subformdata) {
if (!this.checksubformunique(this.subformdata.json.id)) { //如果提交表单已经嵌入到表单中,那么把这个表单弹出来
// this.form.notice(mwf.xapplication.process.xform.lp.subformuniqueerror, "error");
this.isembedded = true;
this.fireevent("aftermodulesload");
} else if (!this.checksubformnested(this.subformdata.json.id)) {
this.form.notice(mwf.xapplication.process.xform.lp.subformnestederror, "error");
} else {
//this.form.addevent("postload", function(){
// this.firesubformevent("queryload");
this.fireevent("beforemodulesload");
this.loadcss();
this.node.set("html", this.subformdata.html);
object.each(this.subformdata.json.modulelist, function (module, key) {
var formkey = key;
if (this.form.json.modulelist[key]) {
formkey = this.json.id "_" key;
var modulenode = this.node.getelement("#" key);
if (modulenode) modulenode.set("id", formkey);
module.id = formkey;
}
this.form.json.modulelist[formkey] = module;
}.bind(this));
var modulenodes = this.form._getmodulenodes(this.node);
modulenodes.each(function (node) {
if (node.get("mwftype") !== "form") {
var _self = this;
var json = this.form._getdomjson(node);
//if( json.type === "subform" || json.modulename === "subform" )this.form.subformcount ;
var module = this.form._loadmodule(json, node, function () {
this.parentformidlist = _self.getparentformidlist();
});
this.form.modules.push(module);
}
}.bind(this));
this.form.subformloaded.push(this.subformdata.json.id);
this.fireevent("aftermodulesload");
// this.firesubformevent("postload");
// this.firesubformevent("load");
// this.firesubformevent("afterload");
}
}
// if( this.form.subformloadedcount ){
// this.form.subformloadedcount ;
// }else{
// this.form.subformloadedcount = 1
// }
// this.form.checksubformloaded();
},
getsubform: function (callback) {
var method = (this.form.json.mode !== "mobile" && !layout.mobile) ? "getform" : "getformmobile";
if (this.json.submitformtype === "script") {
if (this.json.submitformscript && this.json.submitformscript.code) {
var data = this.form.macro.exec(this.json.submitformscript.code, this);
if (data) {
var formname, app;
if (typeof(data) === "string") {
formname = data;
} else {
if (data.application) app = data.application;
if (data.form) formname = data.form;
}
if (formname) {
if (!app) app = (this.form.businessdata.work || this.form.businessdata.workcompleted).application;
mwf.actions.get("x_processplatform_assemble_surface")[method](formname, app, function (json) {
this.getsubformdata(json.data);
if (callback) callback();
}.bind(this));
} else {
if (callback) callback();
}
} else {
if (callback) callback();
}
}
} else {
if (this.json.submitformselected && this.json.submitformselected !== "none") {
var app;
if (this.json.submitformappselected) {
app = this.json.submitformappselected;
} else {
app = (this.form.businessdata.work || this.form.businessdata.workcompleted).application;
}
mwf.actions.get("x_processplatform_assemble_surface")[method](this.json.submitformselected, app, function (json) {
this.getsubformdata(json.data);
if (callback) callback();
}.bind(this));
} else {
if (callback) callback();
}
}
}
});
source