n20-common-lib 1.3.36 → 1.3.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,194 @@
1
+ const VD = '_v-rule-key_validate_'
2
+
3
+ function getRules(rules, evType) {
4
+ if (rules) {
5
+ let _rules = []
6
+ if (Array.isArray(rules)) {
7
+ _rules = rules
8
+ } else if (typeof rules === 'object') {
9
+ _rules.push(rules)
10
+ }
11
+ if (evType === 'submit') {
12
+ return _rules
13
+ } else {
14
+ return _rules.filter((r) => r.trigger.includes(evType))
15
+ }
16
+ } else {
17
+ return []
18
+ }
19
+ }
20
+
21
+ function validateRoule(rule, value) {
22
+ return new Promise(function (resolve, reject) {
23
+ if (rule.required) {
24
+ if (value === undefined || value === null || value === '' || (Array.isArray(value) && value.length === 0)) {
25
+ reject(rule.message)
26
+ return
27
+ }
28
+ }
29
+ if (rule.type === 'string') {
30
+ if (typeof value !== 'string') {
31
+ reject(rule.message)
32
+ return
33
+ }
34
+ }
35
+ if (rule.type === 'number') {
36
+ if (typeof value !== 'number') {
37
+ reject(rule.message)
38
+ return
39
+ }
40
+ }
41
+ if (rule.type === 'boolean') {
42
+ if (typeof value !== 'boolean') {
43
+ reject(rule.message)
44
+ return
45
+ }
46
+ }
47
+ if (rule.type === 'array') {
48
+ if (!(Array.isArray(value) && value.length > 0)) {
49
+ reject(rule.message)
50
+ return
51
+ }
52
+ }
53
+ if (rule.type === 'date') {
54
+ if (new Date(value).toString() === 'Invalid Date') {
55
+ reject(rule.message)
56
+ return
57
+ }
58
+ }
59
+ if (rule.min !== undefined || rule.max !== undefined) {
60
+ if (value === undefined || value === null) {
61
+ reject(rule.message)
62
+ return
63
+ } else {
64
+ let _v = value.toString()
65
+ if (_v.length < rule.min || _v.length > rule.max) {
66
+ reject(rule.message)
67
+ return
68
+ }
69
+ }
70
+ }
71
+ if (rule.regexp) {
72
+ if (new RegExp(rule.regexp).test(value)) {
73
+ reject(rule.message)
74
+ return
75
+ }
76
+ }
77
+ if (!rule.validator) {
78
+ resolve()
79
+ return
80
+ }
81
+ if (rule.validator) {
82
+ rule.validator(rule, value, (err) => {
83
+ if (err === undefined || err === false) {
84
+ resolve()
85
+ return
86
+ } else {
87
+ reject(err === true ? rule.message : err)
88
+ }
89
+ })
90
+ }
91
+ })
92
+ }
93
+
94
+ function showErr(el, err = '', rErr) {
95
+ el.classList.add('rule-key__is-error')
96
+
97
+ if (rErr) {
98
+ let msgEl = el.querySelector('.rule-key__error_msg')
99
+ if (!msgEl) {
100
+ let msgEl = document.createElement('div')
101
+ msgEl.classList.add('rule-key__error_msg')
102
+ msgEl.innerText = err.toString()
103
+ el.appendChild(msgEl)
104
+ } else if (msgEl) {
105
+ msgEl.innerText = err.toString()
106
+ }
107
+ }
108
+ }
109
+ function hideErr(el, rErr) {
110
+ el.classList.remove('rule-key__is-error')
111
+
112
+ if (rErr) {
113
+ let msgEl = el.querySelector('.rule-key__error_msg')
114
+ if (msgEl) {
115
+ el.removeChild(msgEl)
116
+ }
117
+ }
118
+ }
119
+
120
+ const directive = {}
121
+ directive.install = (Vue) => {
122
+ Vue.directive('rule-key', {
123
+ bind(el, binding, node) {
124
+ let _this = node.child
125
+
126
+ let ruleForm = _this.$attrs['rule-form'] || _this.$attrs['ruleForm']
127
+ ruleForm && el.classList.add(ruleForm)
128
+
129
+ let rErr = _this.$attrs['rule-error-hide'] || _this.$attrs['ruleErrorHide']
130
+ rErr = rErr !== 'true' && rErr !== true
131
+
132
+ el[VD] = (val, type) => {
133
+ let _val = _this[binding.value || 'value']
134
+ let _rules = getRules(_this.$attrs['rules'], type)
135
+ if (_rules.length > 0) {
136
+ return Promise.all(_rules.map((rule) => validateRoule(rule, _val)))
137
+ .then(() => {
138
+ hideErr(el, rErr)
139
+ })
140
+ .catch((err) => {
141
+ showErr(el, err, rErr)
142
+ })
143
+ } else {
144
+ return Promise.resolve()
145
+ }
146
+ }
147
+
148
+ _this.$on('change', () => {
149
+ setTimeout(() => el[VD](undefined, 'change'))
150
+ })
151
+ // el.addEventListener('change', () => {
152
+ // el[VD](undefined, 'change')
153
+ // })
154
+
155
+ _this.$on('blur', () => {
156
+ setTimeout(() => el[VD](undefined, 'blur'))
157
+ })
158
+ // el.addEventListener('blur', () => {
159
+ // el[VD](undefined, 'blur')
160
+ // })
161
+ }
162
+ })
163
+ }
164
+
165
+ function rulesValidateForm(query, fn) {
166
+ let nodeList = document.querySelectorAll('.' + query)
167
+ if (nodeList.length) {
168
+ let validateList = []
169
+ nodeList.forEach((el) => {
170
+ typeof el[VD] === 'function' && validateList.push(el[VD](undefined, 'submit'))
171
+ })
172
+ return Promise.all(validateList)
173
+ .then(() => {
174
+ fn && fn(true)
175
+ })
176
+ .catch(() => {
177
+ fn && fn(false)
178
+ })
179
+ } else {
180
+ fn && fn(true)
181
+ return Promise.resolve()
182
+ }
183
+ }
184
+ function rulesValidateFormClear(query) {
185
+ let nodeList = document.querySelectorAll('.' + query)
186
+ nodeList.forEach((el) => {
187
+ hideErr(el, true)
188
+ })
189
+ }
190
+
191
+ window.rulesValidateForm = rulesValidateForm
192
+ window.rulesValidateFormClear = rulesValidateFormClear
193
+
194
+ export default directive
package/src/index.js CHANGED
@@ -67,6 +67,7 @@ import DialogO from './components/Dialog/indexO.vue'
67
67
  import VTitle from './directives/VTitle/index.js'
68
68
  import VDrag from './directives/VDrag/index.js'
69
69
  import VMove from './directives/VMove/index.js'
70
+ import VRuleKey from './directives/VRuleKey/index.js'
70
71
  import VClickOutside from './directives/VClickOutside/index.js'
71
72
  import VHas from './directives/VHas/index.js'
72
73
 
@@ -165,6 +166,7 @@ const install = function (Vue, opts = { prefix: 'Cl', i18nConfig: {} }) {
165
166
  Vue.use(VTitle)
166
167
  Vue.use(VDrag)
167
168
  Vue.use(VMove)
169
+ Vue.use(VRuleKey)
168
170
  Vue.use(VClickOutside)
169
171
  Vue.use(VHas)
170
172