wickes-css2 2.106.0-develop.5 → 2.106.0-develop.6

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.
@@ -1,57 +1,175 @@
1
1
  import { copyToClipboard } from './utils/copy-to-clipboard';
2
2
 
3
- var Wick = Wick || {};
3
+ var Wick = window.Wick || {};
4
+
4
5
  Wick.ShareProjectList = (function () {
5
- function init() {
6
- var $input = $('#share-list-modal #shopping-list-link');
7
- var linkContainer = $('#copy-link');
8
- var $actualLink = linkContainer.find('a');
9
- var copyClass = 'copy-link_hovered';
6
+ const SELECTORS = {
7
+ input: '#share-list-modal #shopping-list-link',
8
+ linkContainer: '#copy-link',
9
+ link: 'a',
10
+ };
10
11
 
11
- $actualLink.on('click', function (e) {
12
- e.preventDefault();
13
- });
12
+ const CLASSES = {
13
+ copied: 'copy-link_hovered',
14
+ };
15
+
16
+ function logActiveElement() {
17
+ const active = document.activeElement;
18
+
19
+ if (!active) {
20
+ return;
21
+ }
22
+ }
23
+
24
+ function getElements() {
25
+ const $input = $(SELECTORS.input);
26
+ const $linkContainer = $(SELECTORS.linkContainer);
27
+ const $link = $linkContainer.find(SELECTORS.link);
28
+
29
+ return {
30
+ $input: $input,
31
+ $linkContainer: $linkContainer,
32
+ $link: $link,
33
+ };
34
+ }
35
+
36
+ function enableFocusAndSelect($input, source) {
37
+ if (!$input.length) {
38
+ return;
39
+ }
14
40
 
15
- function focusAndSelectInput() {
16
- if (!$input.length) return;
41
+ const input = $input.get(0);
42
+ const value = input.value || '';
43
+ const valueLength = value.length;
17
44
 
18
- var el = $input.get(0);
45
+ logActiveElement('[ShareProjectList] before removeAttr disabled');
19
46
 
20
- $input.removeAttr('disabled');
47
+ $input.removeAttr('disabled');
21
48
 
22
- el.focus();
49
+ try {
50
+ input.focus();
51
+ } catch (error) {
52
+ console.error('[ShareProjectList] input.focus() failed', {
53
+ source: source,
54
+ error: error,
55
+ });
56
+ }
57
+
58
+ logActiveElement('[ShareProjectList] after input.focus()');
23
59
 
24
- var len = (el.value || '').length;
25
- if (typeof el.setSelectionRange === 'function') {
26
- el.setSelectionRange(0, len);
27
- } else if (typeof el.select === 'function') {
28
- el.select();
60
+ try {
61
+ if (typeof input.setSelectionRange === 'function') {
62
+ input.setSelectionRange(0, valueLength);
63
+ } else if (typeof input.select === 'function') {
64
+ input.select();
29
65
  }
66
+ } catch (error) {
67
+ console.error('[ShareProjectList] selection failed', {
68
+ source: source,
69
+ error: error,
70
+ });
30
71
  }
31
72
 
32
- linkContainer.on('click', function (e) {
33
- e.preventDefault();
73
+ logActiveElement('[ShareProjectList] after selection');
74
+
75
+ setTimeout(function () {
76
+ logActiveElement('[ShareProjectList] after 0ms timeout');
77
+ }, 0);
78
+
79
+ setTimeout(function () {
80
+ logActiveElement('[ShareProjectList] after 50ms timeout');
81
+ }, 50);
82
+
83
+ setTimeout(function () {
84
+ logActiveElement('[ShareProjectList] after 150ms timeout');
85
+ }, 150);
86
+ }
87
+
88
+ function getInputValue($input) {
89
+ const value = String($input.val() || '').trim();
90
+
91
+ return value;
92
+ }
93
+
94
+ function bindEvents(elements) {
95
+ const $input = elements.$input;
96
+ const $linkContainer = elements.$linkContainer;
97
+ const $link = elements.$link;
34
98
 
35
- focusAndSelectInput();
99
+ $input.on('focus', function () {
100
+ logActiveElement('[ShareProjectList] on input focus');
101
+ });
36
102
 
37
- var value = ($input.val() || '').trim();
38
- if (!value) return;
103
+ $input.on('blur', function () {
104
+ logActiveElement('[ShareProjectList] on input blur');
105
+ });
39
106
 
40
- copyToClipboard(value, linkContainer[0], { noUpperCase: true })
107
+ $link.on('click', function (event) {
108
+ event.preventDefault();
109
+ });
110
+
111
+ $linkContainer.on('mousedown', function () {
112
+ logActiveElement('[ShareProjectList] on mousedown before focus');
113
+ });
114
+
115
+ $linkContainer.on('click', function (event) {
116
+ let value;
117
+
118
+ logActiveElement('[ShareProjectList] before click preventDefault');
119
+
120
+ event.preventDefault();
121
+
122
+ enableFocusAndSelect($input, 'linkContainer click');
123
+
124
+ value = getInputValue($input);
125
+ if (!value) {
126
+ return;
127
+ }
128
+
129
+ copyToClipboard(value, $linkContainer.get(0), { noUpperCase: true })
41
130
  .then(function () {
42
- $actualLink.addClass(copyClass);
131
+ $link.addClass(CLASSES.copied);
132
+
133
+ logActiveElement('[ShareProjectList] after copy success before refocus');
134
+
135
+ enableFocusAndSelect($input, 'copy success refocus');
136
+
137
+ setTimeout(function () {
138
+ enableFocusAndSelect($input, 'copy success refocus timeout 0');
139
+ }, 0);
140
+
141
+ setTimeout(function () {
142
+ enableFocusAndSelect($input, 'copy success refocus timeout 50');
143
+ }, 50);
43
144
  })
44
- .catch(function (e) {
45
- console.error(e);
145
+ .catch(function (error) {
146
+ console.error('[ShareProjectList] Failed to copy shopping list link', error);
147
+
148
+ logActiveElement('[ShareProjectList] after copy error before refocus');
149
+
150
+ enableFocusAndSelect($input, 'copy error refocus');
46
151
  });
47
152
  });
48
153
 
49
- $input.on('click', function (e) {
50
- e.preventDefault();
51
- focusAndSelectInput();
52
- $actualLink.removeClass(copyClass);
154
+ $input.on('click', function () {
155
+ enableFocusAndSelect($input, 'input click');
156
+ $link.removeClass(CLASSES.copied);
53
157
  });
54
158
  }
55
159
 
56
- init();
160
+ function init() {
161
+ var elements = getElements();
162
+
163
+ if (!elements.$input.length || !elements.$linkContainer.length || !elements.$link.length) {
164
+ return;
165
+ }
166
+
167
+ bindEvents(elements);
168
+ }
169
+
170
+ return {
171
+ init: init,
172
+ };
57
173
  })();
174
+
175
+ Wick.ShareProjectList.init();