UNPKG

igi_orion_cignacmb

Version:

Censors words out of text

212 lines (209 loc) 9.39 kB
/* * @Author: weicong.ling * @Date: 2020-04-23 11:53:55 * @Last Modified by: weicong.ling * @Last Modified time: 2020-05-11 11:45:33 */ var centralModel = require('./centralModel'); var fn = function (window, document, $) { 'use strict'; var defaults = { pluginName: 'select', 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.setptionals(that.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.setLinkValue(that.settings); 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; } } if (!settings.field.value) { 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 that.setLinkValue(that.settings); } 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); }); }, setLinkValue: function (settings) { try { var reArr = ['idType']; var centralModel; if (reArr.indexOf(settings.fieldName) != -1) { centralModel = require('./centralModel'); } if(!centralModel){ return; } if (settings.fieldName == 'idType') { if (settings.field.value == 'I' || settings.field.value == 'G') { if (centralModel.getBirthdatByIdNo(settings.item.idNum.value, settings.field.value)) { settings.item.birthday.value = centralModel.getBirthdatByIdNo(settings.item.idNum.value, settings.field.value); } settings.item.sex.value = centralModel.getSexByIdNum(settings.item.idNum.value, settings.field.value); } if (settings.field.value == 'I') { settings.item.nationality.value = 'CHN';//证件类型为身份证,国籍默认为中国 settings.item.nationality.visible = false; } else { settings.item.nationality.visible = true; } if(settings.field.value == 'H'){ settings.item.idExpDate.visible = false; settings.item.idExpDate.value = '2999-01-01'; settings.item.idIsLongValid.visible = false; settings.item.idIsLongValid.value = 1; }else{ settings.item.idExpDate.visible = true; settings.item.idIsLongValid.visible = true; } } } catch (error) { console.error(error); } }, 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 && !other(result).isCorrect) { 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;