mwf.xdesktop.requireapp("process.xform", "$module", null, false);
/** @class widget 门户的部件组件。
* @o2cn 部件
* @example
* //可以在脚本中获取该组件
* //方法1:
* var widget = this.form.get("fieldid"); //获取组件
* //方法2
* var widget = this.target; //在组件本身的脚本中获取
* @extends mwf.xapplication.process.xform.$module
* @o2category formcomponents
* @o2range {portal}
* @hideconstructor
*/
mwf.xapplication.process.xform.widget = mwf.appwidget = new class(
/** @lends mwf.xapplication.process.xform.widget# */
{
extends: mwf.app$module,
options: {
/**
* 部件的设计已经获取到,但还没有插入html及生成内部组件。
* @event mwf.xapplication.process.xform.widget#beforemodulesload
* @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zts|组件事件说明}
*/
/**
* 部件的设计已经获取到,已经插入html,组件json已经获取到,但未生成内部组件。
* * @example
* //获取部件所有组件id
* var moduleidlist = object.keys(this.target.widgetdata.json.modulelist);
* @event mwf.xapplication.process.xform.widget#modulesload
* @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zts|组件事件说明}
*/
/**
* 部件内部组件加载完成。
* @example
* //获取部件所有组件id
* var moduleidlist = object.keys(this.target.widgetdata.json.modulelist);
* //获取部件所有组件
* var modulelist = moduleidlist.map(function(id){
* return this.form.get(id, widgetid); //widgetid为当前部件id,布局组件有可能id冲突,通过widgetid来确定当前部件的组件
* }.bind(this))
* @event mwf.xapplication.process.xform.widget#aftermodulesload
* @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zts|组件事件说明}
*/
"moduleevents": ["load", "queryload", "postload", "beforemodulesload", "modulesload", "aftermodulesload"]
},
_loaduserinterface: function(){
this.node.empty();
this.modules = [];
this.modulelist = {};
this.getwidget(function(){
this.loadwidget();
}.bind(this));
},
/**
* @summary 重新加载部件
* @example
* this.form.get("fieldid").reload()
*/
reload: function(){
this.clean();
this.getwidget(function(){
this.loadwidget();
}.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.widgetdata && this.widgetdata.json.id ){
var id = this.widgetdata.json.id;
// if( this.form.subformloaded && this.form.subformloaded.length ){
// this.form.subformloaded.erase(id);
// }
if( this.parentpageidlist && this.parentpageidlist.length){
this.parentpageidlist.erase(id);
}
}
if( this.json && this.json.id && this.form.widgetmodules && this.form.widgetmodules[ this.json.id ] ){
this.form.widgetmodules[ this.json.id ] = {};
}
this.modules = [];
this.modulelist = {};
this.node.empty();
},
loadcss: function(){
if (this.widgetdata.json.css && this.widgetdata.json.css.code){
var csstext = this.widgetdata.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);
}
}
},
checkwidgetnested : function( id ){
if( this.parentpageidlist ){
return !this.parentpageidlist.contains( id );
}else{
return ![ this.form.json.id ].contains( id );
}
},
getparentpageidlist : function(){
var parentpageidlist;
if( this.parentpageidlist ){
parentpageidlist = array.clone( this.parentpageidlist );
parentpageidlist.push( this.widgetdata.json.id )
}else{
parentpageidlist = [ this.form.json.id, this.widgetdata.json.id ];
}
return parentpageidlist;
},
loadwidget: function(){
if (this.widgetdata ){
if( this.checkwidgetnested( this.widgetdata.json.id ) ){
//this.form.addevent("postload", function(){
this.fireevent("beforemodulesload");
this.loadcss();
this.modules = [];
this.modulelist = {};
this.form.widgetmodules = this.form.widgetmodules || {};
var widgetmodules = this.form.widgetmodules[ this.json.id ] = {};
var params = this.getpageparamenters();
if( typeof(params) === "object" && this.form.macro && this.form.macro.environment ){
var environment = this.form.macro.environment;
environment.widgetparameters = environment.widgetparameters || {};
environment.widgetparameters[ this.json.id ] = params;
}
this.node.set("html", this.widgetdata.html);
object.each(this.widgetdata.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.orgiginalid = key;
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);
var module = this.form._loadmodule(json, node, function(){
this.widget = _self;
this.parentpageidlist = _self.getparentpageidlist();
});
this.form.modules.push(module);
this.modules.push(module);
widgetmodules[ json.orgiginalid || json.id ] = module;
}
}.bind(this));
this.fireevent("aftermodulesload");
//}.bind(this));
}else{
this.form.notice(mwf.xapplication.process.xform.lp.widgetnestederror, "error");
}
}
if( this.form.widgetloadedcount ){
this.form.widgetloadedcount ;
}else{
this.form.widgetloadedcount = 1
}
this.form.checksubformloaded();
},
getwidget: function(callback){
var method = (this.form.options.mode !== "mobile" && !layout.mobile) ? "getwidgetbyname" : "getwidgetbynamemobile";
if (this.json.widgettype==="script"){
if (this.json.widgetscript && this.json.widgetscript.code){
var data = this.form.macro.exec(this.json.widgetscript.code, this);
if (data){
var widgetname, app;
if (typeof(data) === "string") {
widgetname = data;
} else {
if (data.application) app = data.application;
if (data.widget) widgetname = data.widget;
}
if (widgetname) {
if (!app) app = this.form.businessdata.pageinfor.portal;
o2.actions.get("x_portal_assemble_surface")[method](widgetname, app, function(json){
this.getwidgetdata(json.data);
if (callback) callback();
}.bind(this));
}else{
if (callback) callback();
}
}else{
if (callback) callback();
}
}
}else{
if (this.json.widgetselected && this.json.widgetselected!=="none"){
var widgetdata = (this.form.app.relatedformmap) ? this.form.app.relatedformmap[this.json.widgetselected] : null;
if (widgetdata){
this.getwidgetdata({"data": widgetdata.data});
if (callback) callback();
}else{
var app;
if (this.json.widgetappselected) {
app = this.json.widgetappselected;
} else {
app = this.form.businessdata.pageinfor.portal;
}
o2.actions.get("x_portal_assemble_surface")[method](this.json.widgetselected, app, function(json){
this.getwidgetdata(json.data);
if (callback) callback();
}.bind(this));
}
}else{
if (callback) callback();
}
}
},
getwidgetdata: function(data){
var widgetdatastr = null;
//if (this.form.options.mode !== "mobile" && !layout.mobile){
// widgetdatastr = data.data;
//}else{
// widgetdatastr = data.mobiledata;
//}
widgetdatastr = data.data;
this.widgetdata = null;
if (widgetdatastr){
if( this.form.isparselanguage ){
var jsonstr = o2.bindjson(mwf.decodejsonstring(widgetdatastr), {"lp": mwf.xapplication.process.xform.lp.form});
this.widgetdata = json.decode(jsonstr);
}else{
this.widgetdata = json.decode(mwf.decodejsonstring(widgetdatastr));
}
this.widgetdata.updatetime = data.updatetime;
}
},
/**
* @summary 获取设计部件时设置的参数
* @return 设置的参数
* @example
* var param = this.form.get("fieldid").getpageparamenters()
*/
getpageparamenters : function(){
var params = null;
if( this.json.parametertype === "map" ){
params = this.json.parametersmaplist;
}else if( this.json.parametertype === "script" ){
var code = (this.json.parametersscript) ? this.json.parametersscript.code : "";
if (code){
params = this.form.macro.exec(code, this);
}
}
return params;
}
});
source