mwf.xdesktop.requireapp("process.xform", "$module", null, false);
/** @class tinymceeditor html编辑器。
* @o2cn tinymceeditor编辑器
* @example
* //可以在脚本中获取该组件
* //方法1:
* var tinymceeditor = this.form.get("name"); //获取组件
* //方法2
* var tinymceeditor = this.target; //在组件事件脚本中获取
* @extends mwf.xapplication.process.xform.$module
* @o2category formcomponents
* @o2range {process|cms}
* @hideconstructor
*/
mwf.xapplication.process.xform.tinymceeditor = mwf.apptinymceeditor = new class(
/** @lends mwf.xapplication.process.xform.tinymceeditor# */
{
extends: mwf.app$module,
options: {
/**
* 组件异步加载后触发.
* @event mwf.xapplication.process.xform.tinymceeditor#afterload
* @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zts|组件事件说明}
*/
"moduleevents": ["queryload", "load", "postload", "afterload"]
},
initialize: function (node, json, form, options) {
this.node = $(node);
this.node.store("module", this);
this.json = json;
this.form = form;
this.field = true;
this.fieldmoduleloaded = false;
},
load: function () {
this._loadmoduleevents();
if (this.fireevent("queryload")) {
this._queryloaded();
this._loaduserinterface();
this._loadstyles();
//this._loadevents();
this._afterloaded();
this.fireevent("postload");
this.fireevent("load");
}
},
_loaduserinterface: function () {
this.node.empty();
if (this.isreadonly()) {
// this.node.set("html", this._getbusinessdata());
this.node.setstyles({
"-webkit-user-select": "text",
"-moz-user-select": "text"
});
if (layout.mobile) {
this.loadlazyimage(function () { //图片懒加载
var images = this.node.getelements("img");
//移动端设置图片宽度为100%
images.each(function (img) {
if (img.hasclass("lozad")) {
img.setstyles({
"max-width": "100%"
});
} else {
img.setstyles({
"height": "auto",
"max-width": "100%"
});
}
}.bind(this));
this.fireevent("afterload");
this.fieldmoduleloaded = true;
}.bind(this))
} else {
this.loadlazyimage(function () { //图片懒加载
if (this.json.enablepreview !== "n") {
this.loadimageviewer(); //pc端点击显示大图
this.fireevent("afterload");
this.fieldmoduleloaded = true;
}
}.bind(this))
}
} else {
var config = object.clone(this.json.editorproperties);
if (this.json.config) {
if (this.json.config.code) {
var obj = this.form.macro.exec(this.json.config.code, this);
object.each(obj, function (v, k) {
config[k] = v;
});
}
}
this.loadtinymceeditor(config);
}
// this._loadvalue();
},
loadlazyimage: function (callback) {
o2.require("o2.widget.imagelazyloader", function () {
var loadder = new o2.widget.imagelazyloader(this.node, this._getbusinessdata());
loadder.load(function () {
if (callback) callback();
}.bind(this))
}.bind(this));
},
loadimageviewer: function () {
o2.require("o2.widget.imageviewer", function () {
var imageviewer = new o2.widget.imageviewer(this.node);
imageviewer.load();
}.bind(this));
},
loadresource: function ( callback ) {
o2.load([
"../o2_lib/tinymce/tinymce_5.9.2/tinymce.min.js",
"../o2_lib/tinymce/tinymce_5.9.2/o2config.js"
], function () {
var config = o2.tinymceconfig( this.form.json.mode === "mobile" );
callback( config );
}.bind(this))
},
getimageuploadoption: function(){
return {
localimagemaxwidth : 2000,
reference: this.form.businessdata.work.job,
referencetype: "processplatformjob"
};
},
geteditorid: function(){
return this.form.businessdata.work.id "_" this.json.id.split(".").join("_") "_" (layout.mobile ? "mobile" : "pc");
},
loadtinymceeditor: function (config) {
this.loadresource( function( defaultconfig ){
var editorconfig = object.merge(defaultconfig, config || {});
var id = this.geteditorid();
editorconfig.selector = '#' id;
var editordiv = new element("div", {"id": id}).inject(this.node);
var htmldata = this._getbusinessdata();
if (htmldata) {
editordiv.set("html", htmldata || "");
} else if (this.json.templatecode) {
editordiv.set("html", this.json.templatecode || "");
}
// var height = this.node.getsize().y;
editorconfig.base64encode = !layout.mobile && (this.json.base64encode === "y");
editorconfig.enablepreview = (this.json.enablepreview !== "n");
var options = this.getimageuploadoption();
for(var key in options){
editorconfig[key] = options[key];
}
var setup = editorconfig.setup;
var _self = this;
editorconfig.setup = function(editor) {
this.form.app.addevent("queryclose", function () {
try{
editor.destroy();
_self.editor = null;
}catch (e) {}
});
this.form.app.addevent("queryreload", function () {
try{
editor.destroy();
_self.editor = null;
}catch (e) {}
});
this.form.addevent("reloadreadform", function () {
try{
editor.destroy();
_self.editor = null;
}catch (e) {}
});
this._loadevents(editor);
if(setup)setup(editor);
}.bind(this);
var init_instance_callback = editorconfig.init_instance_callback;
editorconfig.init_instance_callback = function(editor) {
this.editor = editor;
this.editor.on("change", function () {
this._setbusinessdata(this.getdata());
}.bind(this));
if(init_instance_callback)init_instance_callback(editor);
if(editorconfig.init_instance_defaultcallback)editorconfig.init_instance_defaultcallback(editor);
this.fireevent("afterload");
}.bind(this);
tinymce.init(editorconfig);
this.fieldmoduleloaded = true;
}.bind(this));
},
_loadevents: function (editor) {
object.each(this.json.events, function (e, key) {
if (e.code) {
editor.on(key, function (event) {
return this.form.macro.fire(e.code, this, event);
}.bind(this), this);
}
}.bind(this));
},
addmoduleevent: function (key, fun) {
this.editor.on(key, function (event) {
return (fun) ? fun(this, event) : null;
}.bind(this), this);
},
_loadvalue: function () {
var data = this._getbusinessdata();
},
// /**
// * @summary 重置组件的值为默认值或置空。
// * @example
// * this.form.get('fieldid').resetdata();
// */
resetdata: function () {
this.setdata(this._getbusinessdata());
},
/**
* @summary 判断组件值是否为空.
* @example
* if( this.form.get('fieldid').isempty() ){
* this.form.notice('html编辑器不能为空', 'warn');
* }
* @return {boolean} 值是否为空.
*/
isempty: function () {
return !this.getdata().trim();
},
/**
* 当表单上没有对应组件的时候,可以使用this.data[fieldid]获取值,但是this.form.get('fieldid')无法获取到组件。
* @summary 获取组件值。
* @example
* var data = this.form.get('fieldid').getdata();
* @example
* //如果无法确定表单上是否有组件,需要判断
* var data;
* if( this.form.get('fieldid') ){ //判断表单是否有无对应组件
* data = this.form.get('fieldid').getdata();
* }else{
* data = this.data['fieldid']; //直接从数据中获取字段值
* }
* @return 组件的数据.
*/
getdata: function () {
return (this.editor && this.editor.getcontent) ? this.editor.getcontent() : this._getbusinessdata();
},
/**
* 当表单上没有对应组件的时候,可以使用this.data[fieldid] = data赋值。
* @summary 为组件赋值。
* @param data{string} .
* @example
* this.form.get("fieldid").setdata("test"); //赋文本值
* @example
* //如果无法确定表单上是否有组件,需要判断
* if( this.form.get('fieldid') ){ //判断表单是否有无对应组件
* this.form.get('fieldid').setdata( data );
* }else{
* this.data['fieldid'] = data;
* }
*/
setdata: function (data) {
this._setbusinessdata(data);
if (this.editor && this.editor.setcontent) this.editor.setcontent(data);
},
destroy: function(){
if( this.editor ){
this.editor.destroy();
this.editor = null;
}
},
createerrornode: function (text) {
var node = new element("div");
var iconnode = new element("div", {
"styles": {
"width": "20px",
"height": "20px",
"float": "left",
"background": " center center no-repeat"
}
}).inject(node);
var textnode = new element("div", {
"styles": {
"line-height": "20px",
"margin-left": "20px",
"color": "red",
"word-break": "keep-all"
},
"text": text
}).inject(node);
return node;
},
notvalidationmode: function (text) {
if (!this.isnotvalidationmode) {
this.isnotvalidationmode = true;
this.node.store("borderstyle", this.node.getstyles("border-left", "border-right", "border-top", "border-bottom"));
this.node.setstyle("border", "1px solid red");
this.errnode = this.createerrornode(text).inject(this.node, "after");
this.shownotvalidationmode(this.node);
if (!this.errnode.isintoview()) this.errnode.scrollintoview(false);
}
},
shownotvalidationmode: function (node) {
var p = node.getparent("div");
if (p) {
if (p.get("mwftype") == "tab$content") {
if (p.getparent("div").getstyle("display") == "none") {
var contentareanode = p.getparent("div").getparent("div");
var tabareanode = contentareanode.getprevious("div");
var idx = contentareanode.getchildren().indexof(p.getparent("div"));
var tabnode = tabareanode.getlast().getfirst().getchildren()[idx];
tabnode.click();
p = tabareanode.getparent("div");
}
}
this.shownotvalidationmode(p);
}
},
validationmode: function () {
if (this.isnotvalidationmode) {
this.isnotvalidationmode = false;
this.node.setstyles(this.node.retrieve("borderstyle"));
if (this.errnode) {
this.errnode.destroy();
this.errnode = null;
}
}
},
validationconfigitem: function (routename, data) {
var flag = (data.status == "all") ? true : (routename == data.decision);
if (flag) {
var n = this.getdata();
var v = (data.valuetype == "value") ? n : n.length;
switch (data.operateor) {
case "isnull":
if (!v) {
this.notvalidationmode(data.prompt);
return false;
}
break;
case "notnull":
if (v) {
this.notvalidationmode(data.prompt);
return false;
}
break;
case "gt":
if (v > data.value) {
this.notvalidationmode(data.prompt);
return false;
}
break;
case "lt":
if (v < data.value) {
this.notvalidationmode(data.prompt);
return false;
}
break;
case "equal":
if (v == data.value) {
this.notvalidationmode(data.prompt);
return false;
}
break;
case "neq":
if (v != data.value) {
this.notvalidationmode(data.prompt);
return false;
}
break;
case "contain":
if (v.indexof(data.value) != -1) {
this.notvalidationmode(data.prompt);
return false;
}
break;
case "notcontain":
if (v.indexof(data.value) == -1) {
this.notvalidationmode(data.prompt);
return false;
}
break;
}
}
return true;
},
validationconfig: function (routename, opinion) {
if (this.json.validationconfig) {
if (this.json.validationconfig.length) {
for (var i = 0; i < this.json.validationconfig.length; i ) {
var data = this.json.validationconfig[i];
if (!this.validationconfigitem(routename, data)) return false;
}
}
return true;
}
return true;
},
validation: function (routename, opinion) {
if (!this.validationconfig(routename, opinion)) return false;
if (!this.json.validation) return true;
if (!this.json.validation.code) return true;
this.currentroutename = routename;
var flag = this.form.macro.exec(this.json.validation.code, this);
this.currentroutename = "";
if (!flag) flag = mwf.xapplication.process.xform.lp.notvalidation;
if (flag.tostring() != "true") {
this.notvalidationmode(flag);
return false;
}
return true;
},
getexceldata: function(){
return this.getdata();
},
setexceldata: function(data){
if( typeof(data) === "string" )data = data.replace(/
/g,"
"); //excel字段换行是
this.exceldata = data;
this.setdata(data, true);
},
validationexcel: function () {
if (!this.isreadonly()){
var errorlist = this.validationconfigexcel();
if (errorlist.length) return errorlist;
if (!this.json.validation) return [];
if (!this.json.validation.code) return [];
var flag = this.form.macro.exec(this.json.validation.code, this);
if (!flag) flag = mwf.xapplication.process.xform.lp.notvalidation;
if (flag.tostring() !== "true") {
return [flag];
}
}
return [];
},
validationconfigexcel: function () {
var errorlist = [];
if (this.json.validationconfig){
if (this.json.validationconfig.length){
for (var i=0; idata.value)return data.prompt;
break;
case "lt":
if (v