igi_orion_cignacmb
Version:
Censors words out of text
182 lines (179 loc) • 7.84 kB
JavaScript
/*
* @Author: weicong.ling
* @Date: 2020-04-23 11:53:55
* @Last Modified by: weicong.ling
* @Last Modified time: 2020-05-11 11:26:16
*/
var centralModel = require('./centralModel');
var fn = function (window, document, $) {
'use strict';
var defaults = {
pluginName: 'bankCodeSelect',
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,
};
options.field.itemKey = options.field.itemKey ? options.field.itemKey : defaults.itemKey;
options.field.itemValue = options.field.itemValue ? options.field.itemValue : defaults.itemValue;
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.settings;
settings.field.optionals = settings.item.bankCardType.value == 3 ? settings.field.optionals_c : settings.field.optionals_d;
settings = that.setptionals(settings);
centralModel.setSelectHtmlForId($, true, settings.id, settings.field.optionals, null, null, settings.field.value, {
key: settings.field.itemKey,
value: settings.field.itemValue
});
that.$fns.getValue(settings.field.value, false);// 初始化的时候必须设置 false
that.$element['0'].value = settings.field.value
that.validator(settings);
},
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;
}
}
var noIn = true;
for (var j = 0; j < settings.field.optionals.length; j++) {
var optionals_item = settings.field.optionals[j];
if (settings.field.value == optionals_item[settings.field.itemKey]) {
noIn = false
}
}
if (!settings.field.value || noIn) {
settings.field.value = settings.field.optionals[0].key;
}
return settings;
},
fix: function (id) {
var that = this;
$("#" + id).focus(function () {
}).blur(function () {
});
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.settings.field.value = this.value;
try {
that.$fns.getValue(that.settings.field.value, that.settings.linkage);// 数据改变的时候可设置 true Or false
} catch (error) {
console.error('***' + that.settings.pluginName + '未定义 $fns ! 或未给定回调数据***', error)
}
that.validator(that.settings);
});
that.$element.bind("input propertychange", function () {//数据改变触发
// console.log('propertychange-数据改变触发', that.$element['0'].value);
});
},
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;