1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | import Verify from './verify.js' import errorRender from './error.js' import vTips from './vtips/index.js' import { filterRegParams, splitRegs } from './utils.js' /** * registered directives in VUE for all verifies * @param {Object} Vue * @param {Object} config */ export default class Directive extends Verify { constructor (Vue, config) { Vue.validator = Vue.prototype.$validator = super(config.validators) this.config = config this.disError = errorRender(Vue, config) this.tipError = vTips(Vue, config) } verifyRegs (value, options) { const {el, regs, name} = options const _regs = splitRegs(regs) const _result = [] if (!_regs) return for (let i = 0; i < _regs.length; i++) { const reg = _regs[i].trim() if (this.verify(reg, value)) { _result.push(true) continue } _result.push(false) break } return this.dealVerifyResult(_result, _regs, options) } dealVerifyResult (_result, _regs, options) { const _bool = !_result[_result.length - 1] const {bind, el, name} = options const _mode = options.mode || config.mode || 'insert' let _text = _bool ? this.getMessage(_regs[_result.length - 1].trim(), name) : '' if (_mode === 'insert') { this.insertError(bind, _text, !_bool) } else if (_mode === 'tip') { this.tipsError(el, _text, !_bool) } this.dealFormClass(_bool, options) return !_bool } insertError (el, _text, _bool) { if (el.instance && el.instance.message === _text) return if (el.instance && _text !== '') { el.instance.message = _text return } el.instance = this.disError({ el: el, target: el.instance || null, message: _text }) } tipsError (el, _text, _bool) { if (el.instance && el.instance.message === _text) return if (el.instance && _text !== '') { el.instance.message = _text return } el.instance = this.tipError({ el: el, remove: _bool, target: el.instance || null, message: _text }) } dealFormClass (type, options) { const { el, style } = options if (!style || (type && el.className.indexOf(style) !== -1)) return if (!type) { el.className = el.className.replace(style, '').replace(/\s+/gi, ' ') return } el.className += ` ${style}` } getMessage (reg, value) { const _reg = filterRegParams(reg) const _msg = this.config.messages[_reg[0]] return _msg ? _msg(value, _reg[1]) : '' } bindSubmit (options) { const { el, submit } = options if (!submit) return const self = this this.addEvent(submit, () => { return self.verifyEvent(options) }) } verifyEvent (options) { const { el } = options const _setValue = el.dataset.verifyVal let _value = el.value if (_setValue !== 'null' && _setValue !== 'undefined' && _setValue) { _value = _setValue } return this.verifyRegs(_value, options) } bindEvent (options) { const { el, events } = options events.forEach(item => { if (item === 'initial') { return this.verifyEvent(options) } if (!this.isForm(el)) return el.addEventListener(item, (e) => { return this.verifyEvent(options) }) }) } isForm (el) { if (!el) return let isForm = false const Form = ['input', 'textarea'] if (Form.indexOf(el.tagName.toLowerCase()) > -1) { isForm = true } for (let i = 0; i < Form.length; i++) { if (el.querySelector(Form[i])) { isForm = true break } } return isForm } setVerifyVal (el, val) { el.setAttribute('data-verify-val', val) } createOptions (el, binding) { const _type = typeof binding.value === 'string' const _events = Object.keys(binding.modifiers) function generateParam (param) { const data = _type ? el.getAttribute(`data-verify-${param}`) : binding.value[param] return data ? data : null } return { bind: el, el: el.querySelector('input') || el.querySelector('textarea') || el, regs: _type ? binding.value : binding.value.regs, name: generateParam('name') || '', style: generateParam('style') || this.config.errorForm || '', mode: generateParam('mode') || this.config.mode || null, submit: generateParam('submit'), events: _events.length ? _events : ['change'] } } install (Vue) { const self = this Vue.directive('verify', { inserted: function (el, binding, vnode) { const options = self.createOptions(el, binding) if (!self.isForm(el)) { self.setVerifyVal(el, vnode.data.props.value) } self.bindSubmit(options) self.bindEvent(options) }, // only verify VUE components that with directives of v-model update: function (el, binding, vnode, oldVnode) { if (self.isForm(el) || vnode.data.props.value === oldVnode.data.props.value) return const options = self.createOptions(el, binding) self.setVerifyVal(el, vnode.data.props.value) self.verifyEvent(options) }, unbind: function (el, binding) { debugger const _type = typeof binding.value === 'string' const _submit = el.getAttribute(`data-verify-submit`) || binding.value.submit if (!_submit) return if (this.getListener(_submit)) { debugger this.removeEvent(_submit) } } }) } } |