igi_orion_cignacmb
Version:
Censors words out of text
176 lines (175 loc) • 7.25 kB
JavaScript
/*
* @Author: weicong.ling
* @Date: 2020-04-23 11:53:55
* @Last Modified by: weicong.ling
* @Last Modified time: 2020-05-11 20:22:40
*/
var centralModel = require('./centralModel');
var fn = function (window, document, $) {
'use strict';
var defaults = {
pluginName: 'multipleChoice',
value: '',
name: '',
validators: [],
optionals: [{ key: "-1", value: '请选择配置对应所选项' }],
itemKey: 'key',
itemValue: 'value',
linkage: false,//是否和其他数据联动
};
function Field(element, options) {
var that = this;
that.element = element;
that.$element = $(element);
that.$fns = options;
that._data = {
id: options.field.id ? options.field.id : (that.$element.data().id ? that.$element.data().id : defaults.pluginName + parseInt(Math.random() * 1000000)),
name: options.field.name ? options.field.name : (that.$element.data().name ? that.$element.data().name : defaults.name + parseInt(Math.random() * 1000000)),
linkage: options.field.linkage ? options.field.linkage : defaults.linkage,
};
that.settings = $.extend({}, defaults, that.$element.data(), options, that._data);
that.init();
that.fix(that.settings.id);
};
Field.prototype = {
init: function () {
var that = this;
var settings = that.setptionals(that.settings);
var html = '<span class="" value="1" id="' + that.settings.id + '_long">长期</span><span class="" value="0" id="' + that.settings.id + '_to">有效期至</span>'
$('#' + that.settings.id).html(html);
that.$fns.getValue(settings.field.value, false);
that.$element['0'].value = settings.field.value;
that.judgeIdExpDateShow(settings);
that.validator(settings);
},
judgeIdExpDateShow: function (settings) {
try {
if (settings.field.value == '1') {
settings.item.idExpDate.value = '';
settings.item.idExpDate.error = null;
settings.item.idExpDate.visible = false;
$('#' + settings.item.idExpDate.id).hide();
} else {
settings.item.idExpDate.visible = true;
$('#' + settings.item.idExpDate.id).show();
}
} catch (error) {
console.error('judgeIdExpDateShow-->', error)
}
},
setptionals: function (settings) {
if (settings.field.optional) {
var settings = settings;
var newArr = [];
var bool = false;
for (var i = 0; i < settings.field.optional.length; i++) {
var optional_item = settings.field.optional[i];
if (settings.field.value == optional_item) {
bool = true
}
for (var j = 0; j < settings.field.optionals.length; j++) {
var optionals_item = settings.field.optionals[j];
if (optional_item == optionals_item[settings.field.itemKey]) {
newArr.push(optionals_item);
}
}
}
settings.field.optionals = newArr;
if (!bool) {
settings.field.value = settings.field.optionals[0].key;
}
}
if (!settings.field.value) {
settings.field.value = settings.field.optionals[0].key;
}
return settings;
},
fix: function (id) {
var that = this;
$('#' + that.settings.id + '_long').click(function () {
that.settings.field.value = '1';
that.$fns.getValue(that.settings.field.value, that.settings.linkage);
that.judgeIdExpDateShow(that.settings);
that.validator(that.settings);
})
$('#' + that.settings.id + '_to').click(function () {
that.settings.field.value = '0';
that.$fns.getValue(that.settings.field.value, that.settings.linkage);
that.judgeIdExpDateShow(that.settings);
that.validator(that.settings);
})
},
validator: function (result) {
var that = this;
var errors = that.validate(result);
that.$element['0'].errors = errors;
try {
that.$fns.errors(errors);
} catch (error) {
console.error('***' + that.settings.pluginName + '未定义 $fns ! 或未给定回调数据***', error)
}
},
validate: function (result) {
var that = this;
var errors = [];
if(!result.field.visible){
return null;
}
var validateName = ['validateNull', 'otherValidate'];
for (var i = 0; i < validateName.length; i++) {
var key = validateName[i];
if (typeof (that[key]) == "function" && key != "validate") {
var runningFunc = that[key](result);
if (!runningFunc.isCorrect) {
errors.push(runningFunc);
break;
}
}
}
return errors.length > 0 ? errors : null;
},
validateNull: function (result) {
var error = {
isCorrect: true,
type: "tooltip",
msg: "请输入" + result.name,
code: "",
id: result.id
};
var value = result.field.value;
if ((value == '-1' || value == '' || value == null || value == undefined) && (value != '0')) {//不为空
error.isCorrect = false;
}
return error;
},
otherValidate: function (result) {
var error = {
isCorrect: true,
type: "tooltip",
msg: "请输入" + result.name,
code: "",
id: result.id
};
if(!result.field.type){
return error;
}
for (var i = 0; i < result.field.type.length; i++) {
var other = result.field.type[i] ? require('./fieldValidator/' + result.field.type[i]) : false;
if (other) {
return other(result);
}
}
return error;
}
};
$.fn[defaults.pluginName] = function (options) {
var args = arguments;
var that = this;
return that.each(function () {
if (!$.data(that, defaults.pluginName)) {
$.data(that, defaults.pluginName, new Field(that, options));
}
});
};
};
module.exports = fn;