simple_form_password_with_hints 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7419ad3088ab2082e7add86e147a77074323b818e826be24448aa343d56df4ba
4
- data.tar.gz: 577677c492ec09edcbfb4344045db326c69321a124aff6be5833e2a5799581fe
3
+ metadata.gz: 6eded650894c5c64046bec10c0770af6613ff101df0422c18d4109ebebdb3b2f
4
+ data.tar.gz: 65fe956b8f35a532ad47daf6a75d0b5de1bf23790af7e9ade84915553dad9d32
5
5
  SHA512:
6
- metadata.gz: 6c6ac35523fe06d5ea783dff307e56f29b544cf29af108f093df802fa8825d8aa4d12d180023305c35c1d6bbad770401d975093c3707fd2bea9d6fcc35d48e75
7
- data.tar.gz: 4240a2b8e992ff02f0ac3bc7c063e346127eb11a62e6bc54e8b9de580827495205fff5c216cbac65e00359448e00111b160904efb09738e872f390b88d6544da
6
+ metadata.gz: 2e72ce9f0973ce9005f836abb08b50a521ea61c78398deac7866763731dfde78aea1a34ceae1c20245b3f6c64cadf6b00497b4fe2d5f7117aad084ca8dfccfaa
7
+ data.tar.gz: 24e0346b43f8bbd43ab4aa88833f5c9649ebf16a384e3588c34e29d1a13c25015ea8903341903ac9a30f18c83b487d16ecf39c17a360cbf3d40f4bde57acd211
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple_form_password_with_hints (0.0.3)
4
+ simple_form_password_with_hints (0.0.4)
5
5
  rails
6
6
  simple_form
7
7
 
@@ -1,10 +1,120 @@
1
- /*global $, RegExp */
2
- $(function () {
1
+ Element.prototype.parents = function (selector) {
3
2
  'use strict';
4
- var getCheckRegex = function (key, $check) {
3
+
4
+ var elements = [],
5
+ // eslint-disable-next-line consistent-this
6
+ elem = this,
7
+ ishaveselector = typeof selector !== 'undefined';
8
+
9
+ while ((elem = elem.parentElement) !== null) {
10
+ if (elem.nodeType !== Node.ELEMENT_NODE) {
11
+ continue;
12
+ }
13
+
14
+ if (!ishaveselector || elem.matches(selector)) {
15
+ elements.push(elem);
16
+ }
17
+ }
18
+
19
+ return elements;
20
+ };
21
+
22
+ Element.prototype.parent = function (selector) {
23
+ 'use strict';
24
+ var elements = this.parents(selector);
25
+
26
+ if (elements.length > 0) {
27
+ return elements[0];
28
+ } else {
29
+ return null;
30
+ }
31
+ };
32
+
33
+ (function () {
34
+ 'use strict';
35
+ var simpleFormPasswordWithHints = {
36
+ init: function () {
37
+ var inputs = document.querySelectorAll('.js-sfpwh-hints-input'),
38
+ togglers = document.querySelectorAll('.js-sfpwh-password-toggle'),
39
+ syncInputs = document.querySelectorAll('.js-sfpwh-sync-input'),
40
+ i = 0;
41
+
42
+ this.listen(inputs, 'input', this.onInput.bind(this));
43
+ this.listen(togglers, 'click', this.onClickToggler);
44
+
45
+ for (i = 0; i < syncInputs.length; i +=1) {
46
+ this.bindEquality(syncInputs[i]);
47
+ }
48
+ },
49
+
50
+ listen: function (elements, action, callback) {
51
+ var i;
52
+ for (i = 0; i < elements.length; i +=1) {
53
+ elements[i].addEventListener(action, callback.bind(this, elements[i]));
54
+ }
55
+ },
56
+
57
+ onInput: function (element) {
58
+ var container = element.parent('.password_with_hints'),
59
+ checks = ['length', 'uppercase', 'lowercase', 'number', 'special'],
60
+ i;
61
+ for (i = 0; i < checks.length; i += 1) {
62
+ this.check(container, checks[i], element.value);
63
+ }
64
+ },
65
+
66
+ check: function (container, key, value) {
67
+ var check = container.querySelector('.js-sfpwh-hint-' + key),
68
+ regexKey = this.getRegex(key, check),
69
+ regex = new RegExp(regexKey);
70
+
71
+ if (check) {
72
+ if (value.match(regex)) {
73
+ check.classList.remove('sfpwh-hint--invalid');
74
+ } else {
75
+ check.classList.add('sfpwh-hint--invalid');
76
+ }
77
+ }
78
+ },
79
+
80
+ bindEquality: function (input) {
81
+ var form = input.parent('form'),
82
+ target = form.querySelector('input[name="' + input.getAttribute('data-link-to') + '"]');
83
+ input.addEventListener('input', this.compare.bind(this, input, target));
84
+ target.addEventListener('input', this.compare.bind(this, input, target));
85
+ },
86
+
87
+ compare: function (field, target) {
88
+ var container = field.parent('.password_with_sync'),
89
+ check;
90
+
91
+ if (container) {
92
+ check = container.querySelector('.js-sfpwh-hint-match');
93
+ }
94
+
95
+ if (field.value !== '' && field.value === target.value) {
96
+ check.classList.remove('sfpwh-hint--invalid');
97
+ } else {
98
+ check.classList.add('sfpwh-hint--invalid');
99
+ }
100
+ },
101
+
102
+ onClickToggler: function (element) {
103
+ var container = element.parent('.password_with_hints, .password_with_sync'),
104
+ input = container.querySelector('.js-sfpwh-input');
105
+
106
+ element.classList.toggle('sfpwh-password-toggle-revealed');
107
+
108
+ input.type = input.type === 'text' ? 'password' : 'text';
109
+ },
110
+
111
+ // HELPERS
112
+
113
+ getRegex: function (key, element) {
5
114
  var regex,
6
115
  min,
7
116
  chars;
117
+
8
118
  switch (key) {
9
119
  case 'uppercase':
10
120
  regex = '[A-Z]';
@@ -17,71 +127,20 @@ $(function () {
17
127
  break;
18
128
  case 'special':
19
129
  // hyphen must be at the end
20
- chars = $check.data('chars');
130
+ chars = element.getAttribute('data-chars');
21
131
  regex = '[' + chars + ']';
22
132
  break;
23
133
  case 'length':
24
- min = $check.data('length');
134
+ min = element.getAttribute('data-length');
25
135
  regex = '.{' + min + ',128}';
26
136
  break;
27
137
  default:
28
138
  break;
29
139
  }
30
140
  return regex;
31
- },
32
- performCheck = function ($container, key, password) {
33
- var $check = $('.js-sfpwh-hint-' + key, $container),
34
- regexKey = getCheckRegex(key, $check),
35
- regex = new RegExp(regexKey);
36
- if ($check.length) {
37
- if (password.match(regex)) {
38
- $check.removeClass('sfpwh-hint--invalid');
39
- } else {
40
- $check.addClass('sfpwh-hint--invalid');
41
- }
42
- }
43
- },
44
- compareFields = function (e) {
45
- var $field = $(e.data.$field),
46
- $target = $(e.data.$target),
47
- $container = $field.parents('.password_with_sync'),
48
- $check = $('.js-sfpwh-hint-match', $container);
49
-
50
- if ($field.val() !== '' && $field.val() === $target.val()) {
51
- $check.removeClass('sfpwh-hint--invalid');
52
- } else {
53
- $check.addClass('sfpwh-hint--invalid');
54
- }
55
- };
56
-
57
- $('.js-sfpwh-hints-input').on('input', function () {
58
- var $container = $(this).parents('.password_with_hints'),
59
- checks = ['length', 'uppercase', 'lowercase', 'number', 'special'],
60
- password = $(this).val(),
61
- i;
62
- for (i = 0; i < checks.length; i += 1) {
63
- performCheck($container, checks[i], password);
64
- }
65
- });
66
-
67
- $('.js-sfpwh-password-toggle').on('click', function () {
68
- var $container = $(this).parents('.password_with_hints, .password_with_sync'),
69
- $input = $('.js-sfpwh-input', $container),
70
- type = $input.attr('type');
71
- $(this).toggleClass('sfpwh-password-toggle-revealed');
72
- if (type === 'text') {
73
- $input.attr('type', 'password');
74
- } else {
75
- $input.attr('type', 'text');
76
141
  }
142
+ };
143
+ document.addEventListener('DOMContentLoaded', function () {
144
+ simpleFormPasswordWithHints.init();
77
145
  });
78
-
79
- $('.js-sfpwh-sync-input').each(function (index, value) {
80
- var $field = $(value),
81
- $form = $field.parents('form'),
82
- $target = $('input[name="' + $field.data('link-to') + '"]', $form);
83
-
84
- $field.on('input', { $field: $field, $target: $target }, compareFields);
85
- $target.on('input', { $field: $field, $target: $target }, compareFields);
86
- });
87
- });
146
+ }());
@@ -1,3 +1,3 @@
1
1
  module SimpleFormPasswordWithHints
2
- VERSION = "0.0.3".freeze
2
+ VERSION = "0.0.4".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_form_password_with_hints
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pierre-andré Boissinot
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-26 00:00:00.000000000 Z
11
+ date: 2021-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails