directive.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import Vue from 'vue'
  2. /**
  3. * 限制输入数字指令
  4. * 自定义修饰符,
  5. * nozero:不可输入0,
  6. * integer:只可输入整数,
  7. * positive:只可输入正数,
  8. * negative:只可输入负数,
  9. * 指令值是个对象,{
  10. * keyStr: 绑定的值,必传
  11. * point: 限制输入小数位数,非必传,默认不限
  12. * }
  13. * e.g:<input v-model="ruleform.input" v-number.integer.positive="{keyStr: 'ruleform.input', point: 3}" />
  14. */
  15. Vue.directive('number', {
  16. bind (el, binding, vnode, oldVnode) {
  17. el.__numberHandler__ = function () {
  18. function getOrSetValue (keyStr, obj, value, type) {
  19. var arrKey = keyStr.split('.')
  20. var temp = obj
  21. for (let i = 0; i < arrKey.length; i++) {
  22. if (arrKey[i].indexOf('[') > -1 && arrKey[i].indexOf(']') > -1) {
  23. // 复杂结构,e.g,ruleform[1]
  24. var str = arrKey[i].replace(/\[|\]/gim, '-|-').split('-|-')
  25. if (i === arrKey.length - 1) {
  26. if (type === 'set') {
  27. temp[str[0]][str[1]] = value
  28. } else if (type === 'get') {
  29. return temp[str[0]][str[1]]
  30. }
  31. } else {
  32. temp = temp[str[0]][str[1]]
  33. }
  34. } else {
  35. if (i === arrKey.length - 1) {
  36. if (type === 'set') {
  37. temp[arrKey[i]] = value
  38. } else if (type === 'get') {
  39. return temp[arrKey[i]]
  40. }
  41. } else {
  42. temp = temp[arrKey[i]]
  43. }
  44. }
  45. }
  46. }
  47. // 输入值
  48. var strValue = getOrSetValue(binding.value.keyStr, vnode.context, null, 'get')
  49. // 防止值不是字符串,无法使用replace方法
  50. if (strValue === null || strValue === undefined) {
  51. strValue = ''
  52. } else {
  53. strValue = strValue + ''
  54. }
  55. // 清除数字,'.','-'以外的字符
  56. strValue = strValue.replace(/[^\d.-]/g, '')
  57. // 不可输入0
  58. if (binding.modifiers.nozero) strValue = strValue.replace(/^0/g, '')
  59. // 限制输入整数
  60. if (binding.modifiers.integer) strValue = strValue.replace(/\./g, '')
  61. // 限制输入正数
  62. if (binding.modifiers.positive && binding.modifiers.negative) {
  63. // 限制输入,正数,负数,则相当于未设置,都可输入
  64. } else if (binding.modifiers.positive) {
  65. // 限制输入正数
  66. strValue = strValue.replace(/-/g, '')
  67. } else if (binding.modifiers.negative) {
  68. // 限制输入负数
  69. strValue = strValue ? '-' + strValue.replace(/-/g, '') : ''
  70. }
  71. // 0开头,去除开头连续的0
  72. strValue = strValue.replace(/^00/g, '0')
  73. // 0开头,后面是数字,则去除开头0
  74. strValue = strValue.replace(/^0([1-9])/g, '$1')
  75. // 只保留第一个'.'
  76. strValue = strValue.replace(/(\..*)\./g, '$1')
  77. // 第一个字符如果是.号,则补充前缀0
  78. strValue = strValue.replace(/^\./g, '0.')
  79. // '-'只能是第一个,其他情况去除
  80. strValue = strValue.replace(/(.+)-/g, '$1')
  81. // 第一个字符如果是-.号,则补充前缀0
  82. strValue = strValue.replace(/^-\./g, '-0.')
  83. // 限制输入小数位数
  84. if (binding.value.point && binding.value.point > 0) {
  85. strValue = strValue.replace(new RegExp('^(.*\\..{0,' + binding.value.point + '}).*$', 'g'), '$1')
  86. }
  87. // 转为数字
  88. if (strValue !== '' && strValue !== '-' && strValue !== '-0' && strValue.slice(-1) !== '.') {
  89. strValue = strValue * 1
  90. }
  91. // 更新值
  92. getOrSetValue(binding.value.keyStr, vnode.context, strValue, 'set')
  93. }
  94. el.addEventListener('keyup', el.__numberHandler__)
  95. },
  96. unbind (el, binding, vnode, oldVnode) {
  97. el.removeEventListener('keyup', el.__numberHandler__)
  98. }
  99. })