igi_orion_cignacmb
Version:
Censors words out of text
202 lines (200 loc) • 8.15 kB
JavaScript
/*
* @Author: weicong.ling
* @Date: 2020-04-23 11:53:55
* @Last Modified by: weicong.ling
* @Last Modified time: 2020-05-11 12:11:42
*/
/**
*
* @param {*} window
* @param {*} document
* @param {*} $
*/
var fn = function (window, document, $) {
'use strict';
var defaults = {
pluginName: 'input',
name: 'input',
linkage: false,//是否和其他数据联动
};
/**
*
* @param {元素节点} element
* @param {初始入参} options
*/
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;
that.$element['0'].value = that.settings.field.value;
that.setLinkValue(that.settings);
that.$fns.getValue(that.settings.field.value, false);
that.validator(that.settings);
},
/**
*
* @param {id元素} id
*/
fix: function (id) {
var that = this;
$("#" + id).focus(function () {
}).blur(function () {
that.settings.field.value = this.value;
try {
that.setLinkValue(that.settings);
that.$fns.getValue(that.settings.field.value, that.settings.linkage);
that.$element['0'].value = that.settings.field.value;
} catch (error) {
console.error('***' + that.settings.pluginName + '未定义 $fns ! 或未给定回调数据***', error)
}
console.log(that.settings)
that.validator(that.settings);
});
that.$element.bind("input blur", function () {//失去焦点触发
// console.log('blur-失去焦点触发', that.$element['0'].value);
});
that.$element.bind("input change", function () {//数据改变及数据改变之后失去焦点触发
// console.log('change-数据改变及数据改变之后失去焦点触发', that.$element['0'].value);
});
that.$element.bind("input propertychange", function () {//数据改变触发
// console.log('propertychange-数据改变触发', that.$element['0'].value);
});
},
/**
* 判断是否存在联动数据
* @param {*} settings
*/
setLinkValue: function (settings) {
try {
var reArr = ['idNum','email','idNum','bankCardNumber']
var centralModel;
if (reArr.indexOf(settings.fieldName) != -1) {
centralModel = require('./centralModel');
}
if(!centralModel){
return;
}
if (settings.fieldName == 'idNum') {
if (settings.item.idType.value == 'I' || settings.item.idType.value == 'G') {
if (centralModel.getBirthdatByIdNo(settings.field.value, settings.item.idType.value)) {
settings.item.birthday.value = centralModel.getBirthdatByIdNo(settings.field.value, settings.item.idType.value);
}
settings.item.sex.value = centralModel.getSexByIdNum(settings.field.value, settings.item.sex.value);
}
}
if(settings.fieldName == 'email'){
settings.field.value = settings.field.value.toLocaleLowerCase();
}
if(settings.fieldName == 'idNum'){
var reg = /[\u4e00-\u9fa5]/g;
settings.field.value = settings.field.value.replace(reg, "");
// settings.field.value = settings.field.value.replace(/[^\u4e00-\u9fa5\w]/g, "");//去掉非法字符
settings.field.value = settings.field.value.toUpperCase()
}
if(settings.fieldName == 'bankCardNumber'){
var numReg = /\D/g;
settings.field.value = settings.field.value.replace(numReg, "")
}
} catch (error) {
console.error(error);
}
},
/**
*
* @param {验证数据} result
*/
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)
}
},
/**
*
* @param {验证数据} result
*/
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;
},
/**
*
* @param {空值数据} result
*/
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;