vuejs 1.0.31 → 1.0.33

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * vue-validator v2.1.3
2
+ * vue-validator v3.0.0-alpha.1
3
3
  * (c) 2016 kazuya kawaguchi
4
4
  * Released under the MIT License.
5
5
  */
@@ -7,22 +7,1822 @@
7
7
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
8
8
  typeof define === 'function' && define.amd ? define(factory) :
9
9
  (global.VueValidator = factory());
10
- }(this, function () { 'use strict';
10
+ }(this, (function () { 'use strict';
11
11
 
12
- function plugin(Vue) {
13
- var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
12
+ /* */
14
13
 
15
- Vue.prototype.$add = function (a, b) {
16
- return a + b;
14
+ var inBrowser =
15
+ typeof window !== 'undefined' &&
16
+ Object.prototype.toString.call(window) !== '[object Object]';
17
+ var UA = inBrowser && window.navigator.userAgent.toLowerCase();
18
+ var isIE9 = UA && UA.indexOf('msie 9.0') > 0;
19
+
20
+ var MODEL_NOTIFY_EVENT = '__VUE_VALIDATOR_MODEL_NOTIFY_EVENT__';
21
+
22
+ function warn (msg, err) {
23
+ if (window.console) {
24
+ console.warn('[vue-validator] ' + msg);
25
+ if (err) {
26
+ console.warn(err.stack);
27
+ }
28
+ }
29
+ }
30
+
31
+ function looseEqual (a, b) {
32
+ return a === b || (
33
+ isObject(a) && isObject(b)
34
+ ? JSON.stringify(a) === JSON.stringify(b)
35
+ : false
36
+ )
37
+ }
38
+
39
+ function getClass (el) {
40
+ var classname = el.className;
41
+ if (typeof classname === 'object') {
42
+ classname = classname.baseVal || '';
43
+ }
44
+ return classname
45
+ }
46
+
47
+ function setClass (el, cls) {
48
+ if (isIE9 && !/svg$/.test(el.namespaceURI)) {
49
+ el.className = cls;
50
+ } else {
51
+ el.setAttribute('class', cls);
52
+ }
53
+ }
54
+
55
+ function addClass (el, cls) {
56
+ if (el.classList) {
57
+ el.classList.add(cls);
58
+ } else {
59
+ var cur = ' ' + getClass(el) + ' ';
60
+ if (cur.indexOf(' ' + cls + ' ') < 0) {
61
+ setClass(el, (cur + cls).trim());
62
+ }
63
+ }
64
+ }
65
+
66
+ function removeClass (el, cls) {
67
+ if (el.classList) {
68
+ el.classList.remove(cls);
69
+ } else {
70
+ var cur = ' ' + getClass(el) + ' ';
71
+ var tar = ' ' + cls + ' ';
72
+ while (cur.indexOf(tar) >= 0) {
73
+ cur = cur.replace(tar, ' ');
74
+ }
75
+ setClass(el, cur.trim());
76
+ }
77
+ if (!el.className) {
78
+ el.removeAttribute('class');
79
+ }
80
+ }
81
+
82
+ function toggleClasses (el, key, fn) {
83
+ if (!el) { return }
84
+
85
+ key = key.trim();
86
+ if (key.indexOf(' ') === -1) {
87
+ fn(el, key);
88
+ return
89
+ }
90
+
91
+ var keys = key.split(/\s+/);
92
+ for (var i = 0, l = keys.length; i < l; i++) {
93
+ fn(el, keys[i]);
94
+ }
95
+ }
96
+
97
+ function triggerEvent (el, event, fn) {
98
+ var e = document.createEvent('HTMLEvents');
99
+ e.initEvent(event, true, true);
100
+ fn && fn(e);
101
+ el.dispatchEvent(e);
102
+ }
103
+
104
+ // TODO: should be defined strict type
105
+ function mapValidation (results) {
106
+ var res = {};
107
+
108
+ normalizeMap(results).forEach(function (ref) {
109
+ var key = ref.key;
110
+ var val = ref.val;
111
+
112
+ res[key] = function mappedValidation () {
113
+ var validation = this.$validation;
114
+ if (!this._isMounted) {
115
+ return null
116
+ }
117
+ var paths = val.split('.');
118
+ var first = paths.shift();
119
+ if (first !== '$validation') {
120
+ warn(("unknown validation result path: " + val));
121
+ return null
122
+ }
123
+ var path;
124
+ var value = validation;
125
+ do {
126
+ path = paths.shift();
127
+ value = value[path];
128
+ } while (paths.length > 0)
129
+ return value
130
+ };
131
+ });
132
+
133
+ return res
134
+ }
135
+
136
+ function isObject (obj) {
137
+ return obj !== null && typeof obj === 'object'
138
+ }
139
+
140
+ // TODO: should be defined strict type
141
+ function normalizeMap (map) {
142
+ return Array.isArray(map)
143
+ ? map.map(function (key) { return ({ key: key, val: key }); })
144
+ : Object.keys(map).map(function (key) { return ({ key: key, val: map[key] }); })
145
+ }
146
+
147
+ /* */
148
+
149
+ // validator configrations
150
+ var validator = {
151
+ classes: {}
152
+ };
153
+
154
+ var Config = function (Vue) {
155
+ // define Vue.config.validator configration
156
+ // $FlowFixMe: https://github.com/facebook/flow/issues/285
157
+ Object.defineProperty(Vue.config, 'validator', {
158
+ enumerable: true,
159
+ configurable: true,
160
+ get: function () { return validator },
161
+ set: function (val) { validator = val; }
162
+ });
163
+ };
164
+
165
+ /* */
166
+ /**
167
+ * build-in validators
168
+ */
169
+
170
+ /**
171
+ * required
172
+ * This function validate whether the value has been filled out.
173
+ */
174
+ function required (val) {
175
+ if (Array.isArray(val)) {
176
+ if (val.length !== 0) {
177
+ var valid = true;
178
+ for (var i = 0, l = val.length; i < l; i++) {
179
+ valid = required(val[i]);
180
+ if (!valid) {
181
+ break
182
+ }
183
+ }
184
+ return valid
185
+ } else {
186
+ return false
187
+ }
188
+ } else if (typeof val === 'number' || typeof val === 'function') {
189
+ return true
190
+ } else if (typeof val === 'boolean') {
191
+ return val
192
+ } else if (typeof val === 'string') {
193
+ return val.length > 0
194
+ } else if (val !== null && typeof val === 'object') {
195
+ return Object.keys(val).length > 0
196
+ } else if (val === null || val === undefined) {
197
+ return false
198
+ } else {
199
+ return false
200
+ }
201
+ }
202
+
203
+ /**
204
+ * pattern
205
+ * This function validate whether the value matches the regex pattern
206
+ */
207
+ function pattern (val, pat) {
208
+ if (typeof pat !== 'string') { return false }
209
+
210
+ var match = pat.match(new RegExp('^/(.*?)/([gimy]*)$'));
211
+ if (!match) { return false }
212
+
213
+ return new RegExp(match[1], match[2]).test(val)
214
+ }
215
+
216
+ /**
217
+ * minlength
218
+ * This function validate whether the minimum length.
219
+ */
220
+ function minlength (val, min) {
221
+ if (typeof val === 'string') {
222
+ return isInteger(min, 10) && val.length >= parseInt(min, 10)
223
+ } else if (Array.isArray(val)) {
224
+ return val.length >= parseInt(min, 10)
225
+ } else {
226
+ return false
227
+ }
228
+ }
229
+
230
+ /**
231
+ * maxlength
232
+ * This function validate whether the maximum length.
233
+ */
234
+ function maxlength (val, max) {
235
+ if (typeof val === 'string') {
236
+ return isInteger(max, 10) && val.length <= parseInt(max, 10)
237
+ } else if (Array.isArray(val)) {
238
+ return val.length <= parseInt(max, 10)
239
+ } else {
240
+ return false
241
+ }
242
+ }
243
+
244
+ /**
245
+ * min
246
+ * This function validate whether the minimum value of the numberable value.
247
+ */
248
+ function min (val, arg) {
249
+ return !isNaN(+(val)) && !isNaN(+(arg)) && (+(val) >= +(arg))
250
+ }
251
+
252
+ /**
253
+ * max
254
+ * This function validate whether the maximum value of the numberable value.
255
+ */
256
+ function max (val, arg) {
257
+ return !isNaN(+(val)) && !isNaN(+(arg)) && (+(val) <= +(arg))
258
+ }
259
+
260
+ /**
261
+ * isInteger
262
+ * This function check whether the value of the string is integer.
263
+ */
264
+ function isInteger (val) {
265
+ return /^(-?[1-9]\d*|0)$/.test(val)
266
+ }
267
+
268
+
269
+ var validators = Object.freeze({
270
+ required: required,
271
+ pattern: pattern,
272
+ minlength: minlength,
273
+ maxlength: maxlength,
274
+ min: min,
275
+ max: max
276
+ });
277
+
278
+ /* */
279
+ var Asset = function (Vue) {
280
+ var ref = Vue.util;
281
+ var extend = ref.extend;
282
+
283
+ // set global validators asset
284
+ var assets = Object.create(null);
285
+ extend(assets, validators);
286
+ Vue.options.validators = assets;
287
+
288
+ // set option merge strategy
289
+ var strats = Vue.config.optionMergeStrategies;
290
+ if (strats) {
291
+ strats.validators = function (parent, child) {
292
+ if (!child) { return parent }
293
+ if (!parent) { return child }
294
+ var ret = Object.create(null);
295
+ extend(ret, parent);
296
+ var key;
297
+ for (key in child) {
298
+ ret[key] = child[key];
299
+ }
300
+ return ret
301
+ };
302
+ }
303
+
304
+ /**
305
+ * Register or retrieve a global validator definition.
306
+ */
307
+ function validator (
308
+ id,
309
+ def
310
+ ) {
311
+ if (def === undefined) {
312
+ return Vue.options['validators'][id]
313
+ } else {
314
+ Vue.options['validators'][id] = def;
315
+ if (def === null) {
316
+ delete Vue.options['validators']['id'];
317
+ }
318
+ }
319
+ }
320
+ Vue['validator'] = validator;
321
+ };
322
+
323
+ /* */
324
+
325
+ var Group = function (Vue) {
326
+ var ref = Vue.util;
327
+ var extend = ref.extend;
328
+
329
+ return {
330
+ data: function data () {
331
+ return {
332
+ valid: true,
333
+ dirty: false,
334
+ touched: false,
335
+ modified: false,
336
+ results: {}
337
+ }
338
+ },
339
+ computed: {
340
+ invalid: function invalid () { return !this.valid },
341
+ pristine: function pristine () { return !this.dirty },
342
+ untouched: function untouched () { return !this.touched },
343
+ result: function result () {
344
+ var ret = {
345
+ valid: this.valid,
346
+ invalid: this.invalid,
347
+ dirty: this.dirty,
348
+ pristine: this.pristine,
349
+ touched: this.touched,
350
+ untouched: this.untouched,
351
+ modified: this.modified
352
+ };
353
+ var results = this.results;
354
+ this._validityKeys.forEach(function (key) {
355
+ ret[key] = results[key];
356
+ });
357
+ return ret
358
+ }
359
+ },
360
+ watch: {
361
+ results: function results (val, old) {
362
+ var keys = this._validityKeys;
363
+ var results = this.results;
364
+ this.valid = this.checkResults(keys, results, 'valid', true);
365
+ this.dirty = this.checkResults(keys, results, 'dirty', false);
366
+ this.touched = this.checkResults(keys, results, 'touched', false);
367
+ this.modified = this.checkResults(keys, results, 'modified', false);
368
+ }
369
+ },
370
+ created: function created () {
371
+ this._validities = Object.create(null);
372
+ this._validityWatchers = Object.create(null);
373
+ this._validityKeys = [];
374
+ this._committing = false;
375
+ },
376
+ destroyed: function destroyed () {
377
+ var this$1 = this;
378
+
379
+ this._validityKeys.forEach(function (key) {
380
+ this$1._validityWatchers[key]();
381
+ delete this$1._validityWatchers[key];
382
+ delete this$1._validities[key];
383
+ });
384
+ delete this._validityWatchers;
385
+ delete this._validities;
386
+ delete this._validityKeys;
387
+ },
388
+ methods: {
389
+ register: function register (name, validity) {
390
+ var this$1 = this;
391
+
392
+ this._validities[name] = validity;
393
+ this._validityKeys = Object.keys(this._validities);
394
+ this.setResults(name, {});
395
+ this.withCommit(function () {
396
+ this$1._validityWatchers[name] = validity.$watch('result', function (val, old) {
397
+ this$1.setResults(name, val);
398
+ }, { deep: true, immediate: true });
399
+ });
400
+ },
401
+ unregister: function unregister (name) {
402
+ var this$1 = this;
403
+
404
+ this._validityWatchers[name]();
405
+ delete this._validityWatchers[name];
406
+ delete this._validities[name];
407
+ this._validityKeys = Object.keys(this._validities);
408
+ this.withCommit(function () {
409
+ this$1.resetResults(name);
410
+ });
411
+ },
412
+ isRegistered: function isRegistered (name) {
413
+ return name in this._validities
414
+ },
415
+ getValidityKeys: function getValidityKeys () {
416
+ return this._validityKeys
417
+ },
418
+ checkResults: function checkResults (
419
+ keys,
420
+ results,
421
+ prop,
422
+ checking
423
+ ) {
424
+ var ret = checking;
425
+ for (var i = 0; i < keys.length; i++) {
426
+ var result = results[keys[i]];
427
+ if (result[prop] !== checking) {
428
+ ret = !checking;
429
+ break
430
+ }
431
+ }
432
+ return ret
433
+ },
434
+ setResults: function setResults (name, val) {
435
+ var this$1 = this;
436
+
437
+ var newVal = {};
438
+ this._validityKeys.forEach(function (key) {
439
+ newVal[key] = extend({}, this$1.results[key]);
440
+ });
441
+ newVal[name] = extend({}, val);
442
+ this.results = newVal;
443
+ },
444
+ resetResults: function resetResults (ignore) {
445
+ var this$1 = this;
446
+
447
+ var newVal = {};
448
+ this._validityKeys.forEach(function (key) {
449
+ if (ignore && ignore !== key) {
450
+ newVal[key] = extend({}, this$1.results[key]);
451
+ }
452
+ });
453
+ this.results = newVal;
454
+ },
455
+ withCommit: function withCommit (fn) {
456
+ var committing = this._committing;
457
+ this._committing = true;
458
+ fn();
459
+ this._committing = committing;
460
+ }
461
+ }
462
+ }
463
+ };
464
+
465
+ /* */
466
+ var ValidationClass = function (Vue) {
467
+ var ValidityGroup = Group(Vue);
468
+
469
+ var Validation = function Validation (options) {
470
+ if ( options === void 0 ) options = {};
471
+
472
+ this._result = {};
473
+ this._host = options.host;
474
+ this._named = Object.create(null);
475
+ this._group = Object.create(null);
476
+ this._validities = Object.create(null);
477
+ this._beginDestroy = false;
478
+ Vue.util.defineReactive(this._host, '$validation', this._result);
479
+ };
480
+
481
+ Validation.prototype.register = function register (
482
+ field,
483
+ validity,
484
+ options
485
+ ) {
486
+ if ( options === void 0 ) options = {};
487
+
488
+ // NOTE: lazy setup (in constructor, occured callstack recursive errors ...)
489
+ if (!this._validityManager) {
490
+ this._validityManager = new Vue(ValidityGroup);
491
+ this._watchValidityResult();
492
+ }
493
+
494
+ if (this._validities[field]) {
495
+ // TODO: should be output console.error
496
+ return
497
+ }
498
+ this._validities[field] = validity;
499
+
500
+ var named = options.named;
501
+ var group = options.group;
502
+ var groupValidity = group
503
+ ? this._getValidityGroup('group', group) || this._registerValidityGroup('group', group)
504
+ : null;
505
+ var namedValidity = named
506
+ ? this._getValidityGroup('named', named) || this._registerValidityGroup('named', named)
507
+ : null;
508
+ if (named && group && namedValidity && groupValidity) {
509
+ groupValidity.register(field, validity);
510
+ !namedValidity.isRegistered(group) && namedValidity.register(group, groupValidity);
511
+ !this._validityManager.isRegistered(named) && this._validityManager.register(named, namedValidity);
512
+ } else if (namedValidity) {
513
+ namedValidity.register(field, validity);
514
+ !this._validityManager.isRegistered(named) && this._validityManager.register(named, namedValidity);
515
+ } else if (groupValidity) {
516
+ groupValidity.register(field, validity);
517
+ !this._validityManager.isRegistered(group) && this._validityManager.register(group, groupValidity);
518
+ } else {
519
+ this._validityManager.register(field, validity);
520
+ }
521
+ };
522
+
523
+ Validation.prototype.unregister = function unregister (
524
+ field,
525
+ options
526
+ ) {
527
+ if ( options === void 0 ) options = {};
528
+
529
+ if (!this._validityManager) {
530
+ // TODO: should be output error
531
+ return
532
+ }
533
+
534
+ if (!this._validities[field]) {
535
+ // TODO: should be output error
536
+ return
537
+ }
538
+ delete this._validities[field];
539
+
540
+ var named = options.named;
541
+ var group = options.group;
542
+ var groupValidity = group ? this._getValidityGroup('group', group) : null;
543
+ var namedValidity = named ? this._getValidityGroup('named', named) : null;
544
+ if (named && group && namedValidity && groupValidity) {
545
+ groupValidity.unregister(field);
546
+ namedValidity.isRegistered(group) && namedValidity.unregister(group);
547
+ this._validityManager.isRegistered(named) && this._validityManager.unregister(named);
548
+ } else if (namedValidity) {
549
+ namedValidity.unregister(field);
550
+ this._validityManager.isRegistered(named) && this._validityManager.unregister(named);
551
+ } else if (groupValidity) {
552
+ groupValidity.unregister(field);
553
+ this._validityManager.isRegistered(group) && this._validityManager.unregister(group);
554
+ } else {
555
+ this._validityManager.unregister(field);
556
+ }
557
+
558
+ group && this._unregisterValidityGroup('group', group);
559
+ named && this._unregisterValidityGroup('named', named);
560
+ };
561
+
562
+ Validation.prototype.destroy = function destroy () {
563
+ var this$1 = this;
564
+
565
+ var validityKeys = Object.keys(this._validities);
566
+ var namedKeys = Object.keys(this._named);
567
+ var groupKeys = Object.keys(this._group);
568
+
569
+ // unregister validity
570
+ validityKeys.forEach(function (validityKey) {
571
+ groupKeys.forEach(function (groupKey) {
572
+ var group = this$1._getValidityGroup('group', groupKey);
573
+ if (group && group.isRegistered(groupKey)) {
574
+ group.unregister(validityKey);
575
+ }
576
+ });
577
+ namedKeys.forEach(function (namedKey) {
578
+ var named = this$1._getValidityGroup('named', namedKey);
579
+ if (named && named.isRegistered(validityKey)) {
580
+ named.unregister(validityKey);
581
+ }
582
+ });
583
+ if (this$1._validityManager.isRegistered(validityKey)) {
584
+ this$1._validityManager.unregister(validityKey);
585
+ }
586
+ delete this$1._validities[validityKey];
587
+ });
588
+
589
+ // unregister grouped validity
590
+ groupKeys.forEach(function (groupKey) {
591
+ namedKeys.forEach(function (namedKey) {
592
+ var named = this$1._getValidityGroup('named', namedKey);
593
+ if (named && named.isRegistered(groupKey)) {
594
+ named.unregister(groupKey);
595
+ }
596
+ });
597
+ if (this$1._validityManager.isRegistered(groupKey)) {
598
+ this$1._validityManager.unregister(groupKey);
599
+ }
600
+ this$1._unregisterValidityGroup('group', groupKey);
601
+ });
602
+
603
+ // unregister named validity
604
+ namedKeys.forEach(function (namedKey) {
605
+ if (this$1._validityManager.isRegistered(namedKey)) {
606
+ this$1._validityManager.unregister(namedKey);
607
+ }
608
+ this$1._unregisterValidityGroup('named', namedKey);
609
+ });
610
+
611
+ this._beginDestroy = true;
612
+ };
613
+
614
+ Validation.prototype._getValidityGroup = function _getValidityGroup (type, name) {
615
+ return type === 'named' ? this._named[name] : this._group[name]
616
+ };
617
+
618
+ Validation.prototype._registerValidityGroup = function _registerValidityGroup (type, name) {
619
+ var groups = type === 'named' ? this._named : this._group;
620
+ groups[name] = new Vue(ValidityGroup);
621
+ return groups[name]
622
+ };
623
+
624
+ Validation.prototype._unregisterValidityGroup = function _unregisterValidityGroup (type, name) {
625
+ var groups = type === 'named' ? this._named : this._group;
626
+ if (!groups[name]) {
627
+ // TODO: should be warn
628
+ return
629
+ }
630
+
631
+ groups[name].$destroy();
632
+ delete groups[name];
633
+ };
634
+
635
+ Validation.prototype._watchValidityResult = function _watchValidityResult () {
636
+ var this$1 = this;
637
+
638
+ this._watcher = this._validityManager.$watch('results', function (val, old) {
639
+ Vue.set(this$1._host, '$validation', val);
640
+ if (this$1._beginDestroy) {
641
+ this$1._destroyValidityMananger();
642
+ }
643
+ }, { deep: true });
644
+ };
645
+
646
+ Validation.prototype._unwatchValidityResult = function _unwatchValidityResult () {
647
+ this._watcher();
648
+ delete this._watcher;
649
+ };
650
+
651
+ Validation.prototype._destroyValidityMananger = function _destroyValidityMananger () {
652
+ this._unwatchValidityResult();
653
+ this._validityManager.$destroy();
654
+ this._validityManager = null;
655
+ };
656
+
657
+ return Validation
658
+ };
659
+
660
+ /* */
661
+
662
+ var Mixin = function (Vue) {
663
+ var Validation = ValidationClass(Vue);
664
+
665
+ return {
666
+ beforeCreate: function beforeCreate () {
667
+ this._validation = new Validation({ host: this });
668
+ }
669
+ }
670
+ };
671
+
672
+ var baseProps = {
673
+ field: {
674
+ type: String,
675
+ required: true
676
+ },
677
+ validators: {
678
+ type: [String, Array, Object],
679
+ required: true
680
+ },
681
+ group: {
682
+ type: String
683
+ },
684
+ multiple: {
685
+ type: Boolean
686
+ },
687
+ classes: {
688
+ type: Object,
689
+ default: function () {
690
+ return {
691
+ valid: 'valid',
692
+ invalid: 'invalid',
693
+ touched: 'touched',
694
+ untouched: 'untouched',
695
+ pristine: 'pristine',
696
+ dirty: 'dirty',
697
+ modified: 'modified'
698
+ }
699
+ }
700
+ }
701
+ };
702
+
703
+ /* */
704
+ var States = function (Vue) {
705
+ var ref = Vue.util;
706
+ var extend = ref.extend;
707
+
708
+ var props = extend({
709
+ child: {
710
+ type: Object,
711
+ required: true
712
+ }
713
+ }, baseProps);
714
+
715
+ function data () {
716
+ var validators = nomalizeValidators(this.validators);
717
+ return {
718
+ results: getInitialResults(validators),
719
+ valid: true,
720
+ dirty: false,
721
+ touched: false,
722
+ modified: false,
723
+ progresses: getInitialProgresses(validators)
724
+ }
725
+ }
726
+
727
+ return {
728
+ props: props,
729
+ data: data
730
+ }
731
+ };
732
+
733
+ function nomalizeValidators (target) {
734
+ var validators;
735
+ if (typeof target === 'string') {
736
+ validators = [target];
737
+ } else if (Array.isArray(target)) {
738
+ validators = target;
739
+ } else {
740
+ validators = Object.keys(target);
741
+ }
742
+ return validators
743
+ }
744
+
745
+ function getInitialResults (validators) {
746
+ var results = {};
747
+ validators.forEach(function (validator) {
748
+ results[validator] = undefined;
749
+ });
750
+ return results
751
+ }
752
+
753
+ function getInitialProgresses (validators) {
754
+ var progresses = {};
755
+ validators.forEach(function (validator) {
756
+ progresses[validator] = '';
757
+ });
758
+ return progresses
759
+ }
760
+
761
+ /* */
762
+
763
+ var Computed = function (Vue) {
764
+ function invalid () {
765
+ return !this.valid
766
+ }
767
+
768
+ function pristine () {
769
+ return !this.dirty
770
+ }
771
+
772
+ function untouched () {
773
+ return !this.touched
774
+ }
775
+
776
+ function result () {
777
+ var this$1 = this;
778
+
779
+ var ret = {
780
+ valid: this.valid,
781
+ invalid: this.invalid,
782
+ dirty: this.dirty,
783
+ pristine: this.pristine,
784
+ touched: this.touched,
785
+ untouched: this.untouched,
786
+ modified: this.modified
787
+ };
788
+
789
+ var keys = this._keysCached(this._uid.toString(), this.results);
790
+ keys.forEach(function (validator) {
791
+ var result = getValidatorResult(validator, this$1.results[validator]);
792
+ if (result === false) { // success
793
+ ret[validator] = false;
794
+ } else { // failed
795
+ var error = { field: this$1.field, validator: validator };
796
+ if (typeof result === 'string') {
797
+ error.message = result;
798
+ }
799
+ if (!ret.errors) {
800
+ ret.errors = [];
801
+ }
802
+ if (Array.isArray(ret.errors)) {
803
+ ret.errors.push(error);
804
+ }
805
+ ret[validator] = result;
806
+ }
807
+ });
808
+
809
+ return ret
810
+ }
811
+
812
+ return {
813
+ invalid: invalid,
814
+ pristine: pristine,
815
+ untouched: untouched,
816
+ result: result
817
+ }
818
+ };
819
+
820
+ function getValidatorResult (
821
+ validator,
822
+ result
823
+ ) {
824
+ if (typeof result === 'boolean' && !result) {
825
+ return true
826
+ }
827
+
828
+ if (typeof result === 'string' && result) {
829
+ return result
830
+ }
831
+
832
+ return false
833
+ }
834
+
835
+ /* */
836
+
837
+ var Render = function (Vue) {
838
+ return {
839
+ render: function render (h) {
840
+ this._interceptEvents(this.child, this.multiple);
841
+ return this.child
842
+ }
843
+ }
844
+ };
845
+
846
+ /* */
847
+ function addEventInfo (e) {
848
+ e[MODEL_NOTIFY_EVENT] = 'DOM';
849
+ }
850
+
851
+ function modelValueEqual (vnode) {
852
+ var directives = (vnode.data && vnode.data.directives) || [];
853
+ var directive = directives.find(function (dir) {
854
+ return dir.name === 'model'
855
+ });
856
+ return !directive
857
+ ? null
858
+ : looseEqual(directive.value, directive.oldValue)
859
+ }
860
+
861
+ /* */
862
+ var SingleElement = function SingleElement (vm, vnode) {
863
+ this._vm = vm;
864
+ this._vnode = vnode;
865
+ this.initValue = this.getValue();
866
+ this.attachValidity();
867
+ };
868
+
869
+ var prototypeAccessors = { _isBuiltIn: {},_isComponent: {} };
870
+
871
+ prototypeAccessors._isBuiltIn.get = function () {
872
+ var vnode = this._vnode;
873
+ return !vnode.child &&
874
+ !vnode.componentOptions &&
875
+ vnode.tag
876
+ };
877
+
878
+ prototypeAccessors._isComponent.get = function () {
879
+ var vnode = this._vnode;
880
+ return vnode.child &&
881
+ vnode.componentOptions &&
882
+ vnode.tag.match(/vue-component/)
883
+ };
884
+
885
+ SingleElement.prototype.attachValidity = function attachValidity () {
886
+ this._vm.$el.$validity = this._vm;
887
+ };
888
+
889
+ SingleElement.prototype.getValue = function getValue () {
890
+ if (this._isBuiltIn) {
891
+ var el = this._vm.$el;
892
+ if (el.tagName === 'SELECT') {
893
+ return getSelectValue(el)
894
+ } else {
895
+ if (el.type === 'checkbox') {
896
+ return el.checked
897
+ } else {
898
+ return el.value
899
+ }
900
+ }
901
+ } else if (this._isComponent) {
902
+ return this._vnode.child.value
903
+ } else {
904
+ // TODO: should be warn !!
905
+ return ''
906
+ }
907
+ };
908
+
909
+ SingleElement.prototype.checkModified = function checkModified () {
910
+ if (this._isBuiltIn) {
911
+ var el = this._vm.$el;
912
+ if (el.tagName === 'SELECT') {
913
+ return !looseEqual(this.initValue, getSelectValue(el))
914
+ } else {
915
+ if (el.type === 'checkbox') {
916
+ return !looseEqual(this.initValue, el.checked)
917
+ } else {
918
+ return !looseEqual(this.initValue, el.value)
919
+ }
920
+ }
921
+ } else if (this._isComponent) {
922
+ return !looseEqual(this.initValue, this._vnode.child.value)
923
+ } else {
924
+ // TODO: should be warn !!
925
+ return false
926
+ }
927
+ };
928
+
929
+ SingleElement.prototype.listenToucheableEvent = function listenToucheableEvent () {
930
+ this._vm.$el.addEventListener('focusout', this._vm.willUpdateTouched);
931
+ };
932
+
933
+ SingleElement.prototype.unlistenToucheableEvent = function unlistenToucheableEvent () {
934
+ this._vm.$el.removeEventListener('focusout', this._vm.willUpdateTouched);
935
+ };
936
+
937
+ SingleElement.prototype.listenInputableEvent = function listenInputableEvent () {
938
+ var vm = this._vm;
939
+ if (this._isBuiltIn) {
940
+ var el = vm.$el;
941
+ if (el.tagName === 'SELECT') {
942
+ el.addEventListener('change', vm.handleInputable);
943
+ } else {
944
+ if (el.type === 'checkbox') {
945
+ el.addEventListener('change', vm.handleInputable);
946
+ } else {
947
+ el.addEventListener('input', vm.handleInputable);
948
+ }
949
+ }
950
+ } else if (this._isComponent) {
951
+ this._unwatchInputable = this._vnode.child.$watch('value', vm.watchInputable);
952
+ } else {
953
+ // TODO: should be warn !!
954
+ }
955
+ };
956
+
957
+ SingleElement.prototype.unlistenInputableEvent = function unlistenInputableEvent () {
958
+ var vm = this._vm;
959
+ if (this._isBuiltIn) {
960
+ var el = vm.$el;
961
+ if (el.tagName === 'SELECT') {
962
+ el.removeEventListener('change', vm.handleInputable);
963
+ } else {
964
+ if (el.type === 'checkbox') {
965
+ el.removeEventListener('change', vm.handleInputable);
966
+ } else {
967
+ el.removeEventListener('input', vm.handleInputable);
968
+ }
969
+ }
970
+ } else if (this._isComponent) {
971
+ if (this._unwatchInputable) {
972
+ this._unwatchInputable();
973
+ this._unwatchInputable = undefined;
974
+ delete this._unwatchInputable;
975
+ }
976
+ } else {
977
+ // TODO: should be warn !!
978
+ }
979
+ };
980
+
981
+ SingleElement.prototype.fireInputableEvent = function fireInputableEvent () {
982
+ if (this._isBuiltIn) {
983
+ var el = this._vm.$el;
984
+ if (el.tagName === 'SELECT') {
985
+ triggerEvent(el, 'change', addEventInfo);
986
+ } else {
987
+ if (el.type === 'checkbox') {
988
+ triggerEvent(el, 'change', addEventInfo);
989
+ } else {
990
+ triggerEvent(el, 'input', addEventInfo);
991
+ }
992
+ }
993
+ } else if (this._isComponent) {
994
+ var args = { value: this.getValue() };
995
+ args[MODEL_NOTIFY_EVENT] = 'COMPONENT';
996
+ this._vnode.child.$emit('input', args);
997
+ } else {
998
+ // TODO: should be warn !!
999
+ }
1000
+ };
1001
+
1002
+ SingleElement.prototype.modelValueEqual = function modelValueEqual$1 () {
1003
+ return modelValueEqual(this._vnode)
1004
+ };
1005
+
1006
+ Object.defineProperties( SingleElement.prototype, prototypeAccessors );
1007
+
1008
+ function getSelectValue (el) {
1009
+ var value = [];
1010
+ for (var i = 0, l = el.options.length; i < l; i++) {
1011
+ var option = el.options[i];
1012
+ if (!option.disabled && option.selected) {
1013
+ value.push(option.value);
1014
+ }
1015
+ }
1016
+ return value
1017
+ }
1018
+
1019
+ /* */
1020
+ var MultiElement = function MultiElement (vm) {
1021
+ // TODO: should be checked whether included radio or checkbox
1022
+ this._vm = vm;
1023
+ this.initValue = this.getValue();
1024
+ this.attachValidity();
1025
+ };
1026
+
1027
+ MultiElement.prototype.attachValidity = function attachValidity () {
1028
+ var this$1 = this;
1029
+
1030
+ this._vm.$el.$validity = this._vm;
1031
+ this._eachItems(function (item) {
1032
+ item.$validity = this$1._vm;
1033
+ });
1034
+ };
1035
+
1036
+ MultiElement.prototype.getValue = function getValue () {
1037
+ return this._getCheckedValue()
1038
+ };
1039
+
1040
+ MultiElement.prototype.checkModified = function checkModified () {
1041
+ return !looseEqual(this.initValue, this._getCheckedValue())
1042
+ };
1043
+
1044
+ MultiElement.prototype.listenToucheableEvent = function listenToucheableEvent () {
1045
+ var this$1 = this;
1046
+
1047
+ this._eachItems(function (item) {
1048
+ item.addEventListener('focusout', this$1._vm.willUpdateTouched);
1049
+ });
1050
+ };
1051
+
1052
+ MultiElement.prototype.unlistenToucheableEvent = function unlistenToucheableEvent () {
1053
+ var this$1 = this;
1054
+
1055
+ this._eachItems(function (item) {
1056
+ item.removeEventListener('focusout', this$1._vm.willUpdateTouched);
1057
+ });
1058
+ };
1059
+
1060
+ MultiElement.prototype.listenInputableEvent = function listenInputableEvent () {
1061
+ var this$1 = this;
1062
+
1063
+ this._eachItems(function (item) {
1064
+ item.addEventListener('change', this$1._vm.handleInputable);
1065
+ });
1066
+ };
1067
+
1068
+ MultiElement.prototype.unlistenInputableEvent = function unlistenInputableEvent () {
1069
+ var this$1 = this;
1070
+
1071
+ this._eachItems(function (item) {
1072
+ item.removeEventListener('change', this$1._vm.handleInputable);
1073
+ });
1074
+ };
1075
+
1076
+ MultiElement.prototype.fireInputableEvent = function fireInputableEvent () {
1077
+ this._eachItems(function (item) {
1078
+ triggerEvent(item, 'change', addEventInfo);
1079
+ });
1080
+ };
1081
+
1082
+ MultiElement.prototype.modelValueEqual = function modelValueEqual$1 () {
1083
+ var ret = null;
1084
+ var children = (this._vm.child && this._vm.child.children) || [];
1085
+ for (var i = 0; i < children.length; i++) {
1086
+ if (!modelValueEqual(children[i])) {
1087
+ ret = false;
1088
+ break
1089
+ }
1090
+ }
1091
+ return ret
1092
+ };
1093
+
1094
+ MultiElement.prototype._getCheckedValue = function _getCheckedValue () {
1095
+ var value = [];
1096
+ this._eachItems(function (item) {
1097
+ if (!item.disabled && item.checked) {
1098
+ value.push(item.value);
1099
+ }
1100
+ });
1101
+ return value
1102
+ };
1103
+
1104
+ MultiElement.prototype._getItems = function _getItems () {
1105
+ return this._vm.$el.querySelectorAll('input[type="checkbox"], input[type="radio"]')
1106
+ };
1107
+
1108
+ MultiElement.prototype._eachItems = function _eachItems (cb) {
1109
+ var items = this._getItems();
1110
+ for (var i = 0; i < items.length; i++) {
1111
+ cb(items[i]);
1112
+ }
1113
+ };
1114
+
1115
+ /* */
1116
+
1117
+ /* */
1118
+ var Lifecycles = function (Vue) {
1119
+ function created () {
1120
+ this._elementable = null;
1121
+
1122
+ this._keysCached = memoize(function (results) {
1123
+ return Object.keys(results)
1124
+ });
1125
+
1126
+ // for event control flags
1127
+ this._modified = false;
1128
+
1129
+ // watch validation raw results
1130
+ this._watchValidationRawResults();
1131
+
1132
+ var validation = this.$options.propsData ? this.$options.propsData.validation : null;
1133
+ if (validation) {
1134
+ var instance = validation.instance;
1135
+ var name = validation.name;
1136
+ var group = this.group;
1137
+ instance.register(this.field, this, { named: name, group: group });
1138
+ }
1139
+ }
1140
+
1141
+ function destroyed () {
1142
+ var validation = this.$options.propsData ? this.$options.propsData.validation : null;
1143
+ if (validation) {
1144
+ var instance = validation.instance;
1145
+ var name = validation.name;
1146
+ var group = this.group;
1147
+ instance.unregister(this.field, this, { named: name, group: group });
1148
+ }
1149
+
1150
+ this._unwatchValidationRawResults();
1151
+
1152
+ this._elementable.unlistenInputableEvent();
1153
+ this._elementable.unlistenToucheableEvent();
1154
+ this._elementable = null;
1155
+ }
1156
+
1157
+ function mounted () {
1158
+ this._elementable = createValidityElement(this);
1159
+ this._elementable.listenToucheableEvent();
1160
+ this._elementable.listenInputableEvent();
1161
+
1162
+ toggleClasses(this.$el, this.classes.untouched, addClass);
1163
+ toggleClasses(this.$el, this.classes.pristine, addClass);
1164
+ }
1165
+
1166
+ function updated () {
1167
+ var maybeChangeModel = this._elementable.modelValueEqual();
1168
+ if (!this._applyWithUserHandler && maybeChangeModel !== null && !maybeChangeModel) {
1169
+ this._elementable.fireInputableEvent();
1170
+ }
1171
+ delete this._applyWithUserHandler;
1172
+ }
1173
+
1174
+ return {
1175
+ created: created,
1176
+ destroyed: destroyed,
1177
+ mounted: mounted,
1178
+ updated: updated
1179
+ }
1180
+ };
1181
+
1182
+ function memoize (fn) {
1183
+ var cache = Object.create(null);
1184
+ return function memoizeFn (id) {
1185
+ var args = [], len = arguments.length - 1;
1186
+ while ( len-- > 0 ) args[ len ] = arguments[ len + 1 ];
1187
+
1188
+ var hit = cache[id];
1189
+ return hit || (cache[id] = fn.apply(void 0, args))
1190
+ }
1191
+ }
1192
+
1193
+ function createValidityElement (vm) {
1194
+ var vnode = vm.child;
1195
+ return !vm.multiple
1196
+ ? new SingleElement(vm, vnode)
1197
+ : new MultiElement(vm)
1198
+ }
1199
+
1200
+ /* */
1201
+ var Event = function (Vue) {
1202
+ var ref = Vue.util;
1203
+ var toArray = ref.toArray;
1204
+
1205
+ function _fireEvent (type) {
1206
+ var args = [], len = arguments.length - 1;
1207
+ while ( len-- > 0 ) args[ len ] = arguments[ len + 1 ];
1208
+
1209
+ (ref = this).$emit.apply(ref, [ type ].concat( args ));
1210
+ var ref;
1211
+ }
1212
+
1213
+ function _interceptEvents (child, multiple) {
1214
+ var this$1 = this;
1215
+
1216
+ (multiple ? (child.children || []) : [child]).forEach(function (child) { this$1._wrapEvent(child); });
1217
+ }
1218
+
1219
+ function _wrapEvent (child) {
1220
+ var this$1 = this;
1221
+
1222
+ var ret = {};
1223
+ if (!child.tag || !child.data) { return ret }
1224
+
1225
+ var dir = getModelDirective(child);
1226
+ if (!dir) { return ret }
1227
+
1228
+ var ref = getEventSources(child);
1229
+ var type = ref.type;
1230
+ var orgListeners = ref.orgListeners;
1231
+ var listeners = ref.listeners;
1232
+ if (!Array.isArray(orgListeners)) { return ret }
1233
+
1234
+ var modelHandler = orgListeners[0];
1235
+ var userHandler = orgListeners[1];
1236
+ var modelApplyer = function (args) {
1237
+ return function () {
1238
+ this$1._applyWithUserHandler = true;
1239
+ modelHandler.apply(child.context, args);
1240
+ }
17
1241
  };
1242
+ var modifier = (dir.modifiers || {}).validity;
1243
+ listeners[type] = function () {
1244
+ var args = toArray(arguments, 0);
1245
+ var event = args[0];
1246
+ if (event[MODEL_NOTIFY_EVENT] === 'DOM') {
1247
+ delete event[MODEL_NOTIFY_EVENT];
1248
+ userHandler.apply(child.context, args);
1249
+ return
1250
+ } else if (event[MODEL_NOTIFY_EVENT] === 'COMPONENT') {
1251
+ var value = event.value;
1252
+ args[0] = value;
1253
+ userHandler.apply(child.context, args);
1254
+ return
1255
+ }
1256
+
1257
+ if (modifier) {
1258
+ args.push(modelApplyer(args));
1259
+ userHandler.apply(child.context, args);
1260
+ } else {
1261
+ userHandler.apply(child.context, args);
1262
+ modelHandler.apply(child.context, args);
1263
+ }
1264
+ };
1265
+
1266
+ ret.dir = dir;
1267
+ return ret
1268
+ }
1269
+
1270
+ return {
1271
+ _fireEvent: _fireEvent,
1272
+ _interceptEvents: _interceptEvents,
1273
+ _wrapEvent: _wrapEvent
1274
+ }
1275
+ };
1276
+
1277
+ function getModelDirective (child) {
1278
+ return ((child.data && child.data.directives) || []).find(function (dir) { return dir.name === 'model' })
1279
+ }
1280
+
1281
+ function getEventSources (child) {
1282
+ var sources = {};
1283
+ var listeners = sources.listeners = child.componentOptions
1284
+ ? child.componentOptions.listeners
1285
+ : (child.data && child.data.on);
1286
+ sources.type =
1287
+ (child.tag === 'input' && (child.data && child.data.attrs && child.data.attrs.type) === 'text') ||
1288
+ (child.tag && child.tag.match(/vue-component/))
1289
+ ? 'input'
1290
+ : 'change';
1291
+ if (listeners) {
1292
+ sources.orgListeners = listeners[sources.type];
1293
+ }
1294
+ return sources
1295
+ }
1296
+
1297
+ /* */
1298
+ var State = function (Vue) {
1299
+ function getValue (options) {
1300
+ return this._elementable.getValue()
18
1301
  }
19
1302
 
20
- plugin.version = '2.1.3';
1303
+ function checkModified () {
1304
+ return this._elementable.checkModified()
1305
+ }
1306
+
1307
+ function willUpdateTouched (options) {
1308
+ if (!this.touched) {
1309
+ this.touched = true;
1310
+ toggleClasses(this.$el, this.classes.touched, addClass);
1311
+ toggleClasses(this.$el, this.classes.untouched, removeClass);
1312
+ this._fireEvent('touched');
1313
+ }
1314
+ }
1315
+
1316
+ function willUpdateDirty () {
1317
+ if (!this.dirty && this.checkModified()) {
1318
+ this.dirty = true;
1319
+ toggleClasses(this.$el, this.classes.dirty, addClass);
1320
+ toggleClasses(this.$el, this.classes.pristine, removeClass);
1321
+ this._fireEvent('dirty');
1322
+ }
1323
+ }
1324
+
1325
+ function willUpdateModified () {
1326
+ var modified = this.modified = this.checkModified();
1327
+ if (this._modified !== modified) {
1328
+ this._modified = modified;
1329
+ toggleClasses(this.$el, this.classes.modified, modified ? addClass : removeClass);
1330
+ this._fireEvent('modified', modified);
1331
+ }
1332
+ }
1333
+
1334
+ function handleInputable (e) {
1335
+ this.willUpdateDirty();
1336
+ this.willUpdateModified();
1337
+ }
21
1338
 
22
- if (typeof window !== 'undefined' && window.Vue) {
23
- window.Vue.use(plugin);
1339
+ function watchInputable (val) {
1340
+ this.willUpdateDirty();
1341
+ this.willUpdateModified();
24
1342
  }
25
1343
 
26
- return plugin;
1344
+ function reset () {
1345
+ var this$1 = this;
1346
+
1347
+ this._unwatchValidationRawResults();
1348
+ var keys = this._keysCached(this._uid.toString(), this.results);
1349
+ for (var i = 0; i < keys.length; i++) {
1350
+ this$1.results[keys[i]] = undefined;
1351
+ this$1.progresses[keys[i]] = '';
1352
+ }
1353
+ toggleClasses(this.$el, this.classes.valid, removeClass);
1354
+ toggleClasses(this.$el, this.classes.invalid, removeClass);
1355
+ toggleClasses(this.$el, this.classes.touched, removeClass);
1356
+ toggleClasses(this.$el, this.classes.untouched, addClass);
1357
+ toggleClasses(this.$el, this.classes.dirty, removeClass);
1358
+ toggleClasses(this.$el, this.classes.pristine, addClass);
1359
+ toggleClasses(this.$el, this.classes.modified, removeClass);
1360
+ this.valid = true;
1361
+ this.dirty = false;
1362
+ this.touched = false;
1363
+ this.modified = false;
1364
+ this._modified = false;
1365
+ this._watchValidationRawResults();
1366
+ }
1367
+
1368
+ function _watchValidationRawResults () {
1369
+ var this$1 = this;
1370
+
1371
+ this._unwatch = this.$watch('results', function (val) {
1372
+ var valid = true;
1373
+ var keys = this$1._keysCached(this$1._uid.toString(), this$1.results);
1374
+ for (var i = 0; i < keys.length; i++) {
1375
+ var result = this$1.results[keys[i]];
1376
+ if (typeof result === 'boolean' && !result) {
1377
+ valid = false;
1378
+ break
1379
+ }
1380
+ if (typeof result === 'string' && result) {
1381
+ valid = false;
1382
+ break
1383
+ }
1384
+ }
1385
+ this$1.valid = valid;
1386
+
1387
+ if (valid) {
1388
+ toggleClasses(this$1.$el, this$1.classes.valid, addClass);
1389
+ toggleClasses(this$1.$el, this$1.classes.invalid, removeClass);
1390
+ } else {
1391
+ toggleClasses(this$1.$el, this$1.classes.valid, removeClass);
1392
+ toggleClasses(this$1.$el, this$1.classes.invalid, addClass);
1393
+ }
1394
+
1395
+ this$1._fireEvent(valid ? 'valid' : 'invalid');
1396
+ }, { deep: true });
1397
+ }
1398
+
1399
+ function _unwatchValidationRawResults () {
1400
+ this._unwatch();
1401
+ this._unwatch = undefined;
1402
+ delete this._unwatch;
1403
+ }
1404
+
1405
+ return {
1406
+ getValue: getValue,
1407
+ checkModified: checkModified,
1408
+ willUpdateTouched: willUpdateTouched,
1409
+ willUpdateDirty: willUpdateDirty,
1410
+ willUpdateModified: willUpdateModified,
1411
+ handleInputable: handleInputable,
1412
+ watchInputable: watchInputable,
1413
+ reset: reset,
1414
+ _watchValidationRawResults: _watchValidationRawResults,
1415
+ _unwatchValidationRawResults: _unwatchValidationRawResults
1416
+ }
1417
+ };
1418
+
1419
+ /* */
1420
+
1421
+ /**
1422
+ * Forgiving check for a promise
1423
+ */
1424
+ function isPromise (p) {
1425
+ return p && typeof p.then === 'function'
1426
+ }
1427
+
1428
+ var Validate = function (Vue) {
1429
+ function _resolveValidator (name) {
1430
+ var ref = Vue.util;
1431
+ var resolveAsset = ref.resolveAsset;
1432
+ var options = (this.child && this.child.context)
1433
+ ? this.child.context.$options
1434
+ : this.$options;
1435
+ return resolveAsset(options, 'validators', name)
1436
+ }
1437
+
1438
+ function _getValidateDescriptor (
1439
+ validator,
1440
+ field,
1441
+ value
1442
+ ) {
1443
+ var ref = Vue.util;
1444
+ var isPlainObject = ref.isPlainObject;
1445
+
1446
+ var asset = this._resolveValidator(validator);
1447
+ if (!asset) {
1448
+ // TODO: should be warned
1449
+ return null
1450
+ }
1451
+
1452
+ var fn = null;
1453
+ var rule = null;
1454
+ var msg = null;
1455
+ if (isPlainObject(asset)) {
1456
+ if (asset.check && typeof asset.check === 'function') {
1457
+ fn = asset.check;
1458
+ }
1459
+ if (asset.message) {
1460
+ msg = asset.message;
1461
+ }
1462
+ } else if (typeof asset === 'function') {
1463
+ fn = asset;
1464
+ } else {
1465
+ // TODO: should be warned
1466
+ return null
1467
+ }
1468
+
1469
+ if (!fn) {
1470
+ // TODO: should be warned
1471
+ return null
1472
+ }
1473
+
1474
+ if (isPlainObject(this.validators)) {
1475
+ if (isPlainObject(this.validators[validator])) {
1476
+ if (this.validators[validator].rule) {
1477
+ rule = this.validators[validator].rule;
1478
+ }
1479
+ if (this.validators[validator].message) {
1480
+ msg = this.validators[validator].message;
1481
+ }
1482
+ } else {
1483
+ rule = this.validators[validator];
1484
+ }
1485
+ }
1486
+
1487
+ var descriptor = { fn: fn, value: value, field: field };
1488
+ if (rule) {
1489
+ descriptor.rule = rule;
1490
+ }
1491
+ if (msg) {
1492
+ descriptor.msg = msg;
1493
+ }
1494
+
1495
+ return descriptor
1496
+ }
1497
+
1498
+ function _resolveMessage (
1499
+ field,
1500
+ msg,
1501
+ override
1502
+ ) {
1503
+ if (override) { return override }
1504
+ return msg
1505
+ ? typeof msg === 'function'
1506
+ ? msg(field)
1507
+ : msg
1508
+ : undefined
1509
+ }
1510
+
1511
+ function _invokeValidator (
1512
+ ref,
1513
+ cb
1514
+ ) {
1515
+ var this$1 = this;
1516
+ var fn = ref.fn;
1517
+ var value = ref.value;
1518
+ var field = ref.field;
1519
+ var rule = ref.rule;
1520
+ var msg = ref.msg;
1521
+
1522
+ var future = fn.call(this, value, rule);
1523
+ if (typeof future === 'function') { // function
1524
+ future(function () { // resolve
1525
+ cb(true);
1526
+ }, function (err) { // reject
1527
+ cb(false, this$1._resolveMessage(field, msg, err));
1528
+ });
1529
+ } else if (isPromise(future)) { // promise
1530
+ future.then(function () { // resolve
1531
+ cb(true);
1532
+ }, function (err) { // reject
1533
+ cb(false, this$1._resolveMessage(field, msg, err));
1534
+ }).catch(function (err) {
1535
+ cb(false, this$1._resolveMessage(field, msg, err.message));
1536
+ });
1537
+ } else { // sync
1538
+ cb(future, future === false ? this._resolveMessage(field, msg) : undefined);
1539
+ }
1540
+ }
1541
+
1542
+ function _validate (validator, value, cb) {
1543
+ var this$1 = this;
1544
+
1545
+ var descriptor = this._getValidateDescriptor(validator, this.field, value);
1546
+ if (descriptor) {
1547
+ if (this.progresses[validator]) { return false }
1548
+ this.progresses[validator] = 'running';
1549
+ this.$nextTick(function () {
1550
+ this$1._invokeValidator(descriptor, function (ret, msg) {
1551
+ this$1.progresses[validator] = '';
1552
+ this$1.results[validator] = msg || ret;
1553
+ if (cb) {
1554
+ this$1.$nextTick(function () {
1555
+ cb.call(this$1, null, ret, msg);
1556
+ });
1557
+ } else {
1558
+ var e = { result: ret };
1559
+ if (msg) {
1560
+ e['msg'] = msg;
1561
+ }
1562
+ this$1._fireEvent('validate', validator, e);
1563
+ }
1564
+ });
1565
+ });
1566
+ } else {
1567
+ // TODO:
1568
+ var err = new Error();
1569
+ cb ? cb.call(this, err) : this._fireEvent('validate', validator, err);
1570
+ }
1571
+ return true
1572
+ }
1573
+
1574
+ function validate () {
1575
+ var this$1 = this;
1576
+ var args = [], len = arguments.length;
1577
+ while ( len-- ) args[ len ] = arguments[ len ];
1578
+
1579
+ var validators;
1580
+ var value;
1581
+ var cb;
1582
+ var ret = true;
1583
+
1584
+ if (args.length === 3) {
1585
+ validators = [args[0]];
1586
+ value = args[1];
1587
+ cb = args[2];
1588
+ } else if (args.length === 2) {
1589
+ validators = this._keysCached(this._uid.toString(), this.results);
1590
+ value = args[0];
1591
+ cb = args[1];
1592
+ } else if (args.length === 1) {
1593
+ validators = this._keysCached(this._uid.toString(), this.results);
1594
+ value = this.getValue();
1595
+ cb = args[0];
1596
+ } else {
1597
+ validators = this._keysCached(this._uid.toString(), this.results);
1598
+ value = this.getValue();
1599
+ cb = null;
1600
+ }
1601
+
1602
+ if (args.length === 3) {
1603
+ ret = this._validate(validators[0], value, cb);
1604
+ } else {
1605
+ validators.forEach(function (validator) {
1606
+ ret = this$1._validate(validator, value, cb);
1607
+ });
1608
+ }
1609
+
1610
+ return ret
1611
+ }
1612
+
1613
+ return {
1614
+ _resolveValidator: _resolveValidator,
1615
+ _getValidateDescriptor: _getValidateDescriptor,
1616
+ _resolveMessage: _resolveMessage,
1617
+ _invokeValidator: _invokeValidator,
1618
+ _validate: _validate,
1619
+ validate: validate
1620
+ }
1621
+ };
1622
+
1623
+ /* */
1624
+
1625
+ var Methods = function (Vue) {
1626
+ var ref = Vue.util;
1627
+ var extend = ref.extend;
1628
+
1629
+ var methods = {};
1630
+ extend(methods, Event(Vue));
1631
+ extend(methods, State(Vue));
1632
+ extend(methods, Validate(Vue));
1633
+
1634
+ return methods
1635
+ };
1636
+
1637
+ /* */
1638
+
1639
+ var ValidityControl = function (Vue) {
1640
+ var ref = Vue.util;
1641
+ var extend = ref.extend;
1642
+
1643
+ var ref$1 = States(Vue);
1644
+ var props = ref$1.props;
1645
+ var data = ref$1.data;
1646
+ var computed = Computed(Vue);
1647
+ var lifecycles = Lifecycles(Vue);
1648
+ var ref$2 = Render(Vue);
1649
+ var render = ref$2.render;
1650
+ var methods = Methods(Vue);
1651
+
1652
+ var validity = {
1653
+ props: props,
1654
+ data: data,
1655
+ render: render,
1656
+ computed: computed,
1657
+ methods: methods
1658
+ };
1659
+ extend(validity, lifecycles);
1660
+
1661
+ return validity
1662
+ };
1663
+
1664
+ /* */
1665
+ var Validity = function (Vue) {
1666
+ var ref = Vue.util;
1667
+ var extend = ref.extend;
1668
+
1669
+ return {
1670
+ functional: true,
1671
+ props: baseProps,
1672
+ render: function render (
1673
+ h,
1674
+ ref
1675
+ ) {
1676
+ var props = ref.props;
1677
+ var data = ref.data;
1678
+ var children = ref.children;
1679
+
1680
+ return children.map(function (child) {
1681
+ if (!child.tag) { return child }
1682
+ var newData = extend({}, data);
1683
+ newData.props = extend({}, props);
1684
+ extend(newData.props.classes, Vue.config.validator.classes);
1685
+ newData.props.child = child;
1686
+ return h('validity-control', newData)
1687
+ })
1688
+ }
1689
+ }
1690
+ };
1691
+
1692
+ /* */
1693
+ var ValidityGroup = function (Vue) {
1694
+ var ref = Vue.util;
1695
+ var extend = ref.extend;
1696
+
1697
+ var props = extend({
1698
+ tag: {
1699
+ type: String,
1700
+ default: 'fieldset'
1701
+ }
1702
+ }, baseProps);
1703
+
1704
+ return {
1705
+ functional: true,
1706
+ props: props,
1707
+ render: function render (
1708
+ h,
1709
+ ref
1710
+ ) {
1711
+ var props = ref.props;
1712
+ var data = ref.data;
1713
+ var children = ref.children;
1714
+
1715
+ var child = h(props.tag, children);
1716
+ var newData = extend({}, data);
1717
+ newData.props = extend({}, props);
1718
+ extend(newData.props.classes, Vue.config.validator.classes);
1719
+ newData.props.child = child;
1720
+ newData.props.multiple = true;
1721
+ return h('validity-control', newData)
1722
+ }
1723
+ }
1724
+ };
1725
+
1726
+ /* */
1727
+
1728
+ var Validation = function (Vue) {
1729
+ return {
1730
+ functional: true,
1731
+ props: {
1732
+ name: {
1733
+ type: String
1734
+ },
1735
+ tag: {
1736
+ type: String,
1737
+ default: 'form'
1738
+ }
1739
+ },
1740
+ render: function render (
1741
+ h,
1742
+ ref
1743
+ ) {
1744
+ var props = ref.props;
1745
+ var data = ref.data;
1746
+ var parent = ref.parent;
1747
+ var children = ref.children;
1748
+ var slots = ref.slots;
1749
+
1750
+ if (!parent._validation) {
1751
+ // TODO: should be warned
1752
+ return children
1753
+ }
1754
+ var tag = props.tag || 'form';
1755
+ walkChildren(parent._validation, props.name, children);
1756
+ return h(tag, tag === 'form' ? { attrs: { novalidate: true }} : {}, children)
1757
+ }
1758
+ }
1759
+ };
1760
+
1761
+ function walkChildren (validation, name, children) {
1762
+ children.forEach(function (child) {
1763
+ if (child &&
1764
+ child.componentOptions &&
1765
+ child.componentOptions.propsData && child.componentOptions.tag === 'validity-control') {
1766
+ child.componentOptions.propsData.validation = {
1767
+ instance: validation,
1768
+ name: name
1769
+ };
1770
+ }
1771
+ child.children && walkChildren(validation, name, child.children);
1772
+ });
1773
+ }
1774
+
1775
+ /* */
1776
+ var Component = function (Vue) {
1777
+ return {
1778
+ 'validity-control': ValidityControl(Vue),
1779
+ 'validity': Validity(Vue),
1780
+ 'validity-group': ValidityGroup(Vue),
1781
+ 'validation': Validation(Vue)
1782
+ }
1783
+ };
1784
+
1785
+ /* */
1786
+ var installed = false;
1787
+
1788
+ function plugin (Vue, options) {
1789
+ if ( options === void 0 ) options = {};
1790
+
1791
+ if (installed) {
1792
+ warn('already installed.');
1793
+ return
1794
+ }
1795
+
1796
+ Config(Vue);
1797
+ Asset(Vue);
1798
+ installMixin(Vue);
1799
+ installComponent(Vue);
1800
+ installed = true;
1801
+ }
1802
+
1803
+ function installMixin (Vue) {
1804
+ Vue.mixin(Mixin(Vue));
1805
+ }
1806
+
1807
+ function installComponent (Vue) {
1808
+ var components = Component(Vue);
1809
+ Object.keys(components).forEach(function (id) {
1810
+ Vue.component(id, components[id]);
1811
+ });
1812
+ }
1813
+
1814
+ plugin.mapValidation = mapValidation; // for standalone
1815
+ plugin.version = '3.0.0-alpha.1';
1816
+
1817
+ if (typeof window !== 'undefined' && window.Vue) {
1818
+ window.Vue.use(plugin);
1819
+ }
1820
+
1821
+ var index = {
1822
+ install: plugin,
1823
+ mapValidation: mapValidation
1824
+ };
1825
+
1826
+ return index;
27
1827
 
28
- }));
1828
+ })));