@pushword/js-helper 0.0.110 → 0.0.112

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pushword/js-helper",
3
- "version": "0.0.110",
3
+ "version": "0.0.112",
4
4
  "description": "Pushword front end helpers. ",
5
5
  "author": "Robin@PiedWeb <contact@piedweb.com>",
6
6
  "license": "MIT",
@@ -207,6 +207,7 @@ class ScrollXEnhancer {
207
207
  manageScrollXControllerVisibility(element) {
208
208
  const scrollLeftElement = element.parentNode.querySelector('.scroll-left')
209
209
  const scrollRightElement = element.parentNode.querySelector('.scroll-right')
210
+ if (!scrollLeftElement || !scrollRightElement) return
210
211
  scrollRightElement.classList.remove('opacity-30')
211
212
  scrollLeftElement.classList.remove('opacity-30')
212
213
 
package/src/helpers.js CHANGED
@@ -1,5 +1,5 @@
1
- import 'core-js/stable';
2
- import 'regenerator-runtime/runtime';
1
+ import 'core-js/stable'
2
+ import 'regenerator-runtime/runtime'
3
3
  /**
4
4
  * List of all functions
5
5
  *
@@ -23,98 +23,97 @@ import 'regenerator-runtime/runtime';
23
23
  */
24
24
  export function liveBlock(liveBlockAttribute = 'data-live', liveFormSelector = '.live-form') {
25
25
  var btnToBlock = function (event, btn) {
26
- btn.setAttribute(liveBlockAttribute, btn.getAttribute('src-' + liveBlockAttribute));
27
- getLiveBlock(btn);
28
- };
26
+ btn.setAttribute(liveBlockAttribute, btn.getAttribute('src-' + liveBlockAttribute))
27
+ getLiveBlock(btn)
28
+ }
29
29
 
30
30
  var getLiveBlock = function (item) {
31
- var url = item.getAttribute(liveBlockAttribute);
32
- url = url.startsWith('e:') ? convertShortchutForLink(rot13ToText(url.substring(2))) : url;
31
+ var url = item.getAttribute(liveBlockAttribute)
32
+ url = url.startsWith('e:') ? convertShortchutForLink(rot13ToText(url.substring(2))) : url
33
33
  fetch(url, {
34
34
  //headers: { "Content-Type": "application/json", Accept: "text/plain" },
35
35
  method: 'POST',
36
36
  credentials: 'include',
37
37
  })
38
38
  .then(function (response) {
39
- return response.text();
39
+ return response.text()
40
40
  })
41
41
  .then(function (body) {
42
- item.removeAttribute(liveBlockAttribute);
43
- item.outerHTML = body;
42
+ item.removeAttribute(liveBlockAttribute)
43
+ item.outerHTML = body
44
44
  })
45
45
  .then(function () {
46
- document.dispatchEvent(new Event('DOMChanged'));
47
- });
48
- };
46
+ document.dispatchEvent(new Event('DOMChanged'))
47
+ })
48
+ }
49
49
 
50
- const spinner =
51
- '<span style="border-top-color: transparent" class="inline-block w-5 h-5 border-4 border-gray-50 border-solid rounded-full animate-spin"></span>';
52
- const htmlLoader = '<div>' + spinner.replace('border-gray-50', 'border-gray-800') + '</div>';
50
+ const spinner = '<span style="border-top-color: transparent" class="inline-block w-5 h-5 border-4 border-gray-50 border-solid rounded-full animate-spin"></span>'
51
+ const htmlLoader = '<div>' + spinner.replace('border-gray-50', 'border-gray-800') + '</div>'
53
52
 
54
53
  var setLoader = function (form) {
55
- var $submitButton = getSubmitButton(form);
54
+ var $submitButton = getSubmitButton(form)
56
55
  if ($submitButton !== undefined) {
57
56
  //var initialButton = $submitButton.outerHTML;
58
- $submitButton.innerHTML = '';
59
- $submitButton.outerHTML = htmlLoader;
57
+ $submitButton.innerHTML = ''
58
+ $submitButton.outerHTML = htmlLoader
60
59
  }
61
- };
60
+ }
62
61
 
63
62
  var sendForm = function (form, liveFormBlock) {
64
- setLoader(form);
63
+ setLoader(form)
65
64
 
66
- var formData = new FormData(form.srcElement);
65
+ var formData = new FormData(form.srcElement)
67
66
  fetch(form.srcElement.action, {
68
67
  method: 'POST',
69
68
  body: formData,
70
69
  credentials: 'include',
71
70
  })
72
71
  .then(function (response) {
73
- return response.text();
72
+ return response.text()
74
73
  })
75
74
  .then(function (body) {
76
- liveFormBlock.outerHTML = body;
75
+ liveFormBlock.outerHTML = body
77
76
  })
78
77
  .then(function () {
79
- document.dispatchEvent(new Event('DOMChanged'));
80
- });
81
- };
78
+ document.dispatchEvent(new Event('DOMChanged'))
79
+ })
80
+ }
82
81
 
83
82
  var getSubmitButton = function (form) {
84
83
  if (form.srcElement.querySelector('[type=submit]') !== null) {
85
- return form.srcElement.querySelector('[type=submit]');
84
+ return form.srcElement.querySelector('[type=submit]')
86
85
  }
87
86
  if (form.srcElement.getElementsByTagName('button') !== null) {
88
- return form.srcElement.getElementsByTagName('button')[0];
87
+ return form.srcElement.getElementsByTagName('button')[0]
89
88
  }
90
- return null;
91
- };
89
+ return null
90
+ }
92
91
 
93
92
  // Listen data-live
94
93
  document.querySelectorAll('[' + liveBlockAttribute + ']').forEach((item) => {
95
- getLiveBlock(item);
96
- });
94
+ getLiveBlock(item)
95
+ })
97
96
 
98
97
  // Listen button src-data-live
99
98
  document.querySelectorAll('[src-' + liveBlockAttribute + ']').forEach((item) => {
100
99
  item.addEventListener('click', (event) => {
101
100
  if (item.tagName == 'BUTTON') {
102
- item.innerHTML = spinner;
103
- item.setAttribute('disabled', true);
101
+ item.innerHTML = spinner
102
+ item.setAttribute('disabled', true)
104
103
  }
105
- btnToBlock(event, item);
106
- });
107
- });
104
+ btnToBlock(event, item)
105
+ })
106
+ })
108
107
 
109
108
  // Listen live-form
110
109
  document.querySelectorAll(liveFormSelector).forEach((item) => {
111
110
  if (item.querySelector('form') !== null) {
112
111
  item.querySelector('form').addEventListener('submit', (e) => {
113
- e.preventDefault();
114
- sendForm(e, item);
115
- });
112
+ e.preventDefault()
113
+ sendForm(e, item)
114
+ })
116
115
  }
117
- });
116
+ })
118
117
  }
119
118
 
120
119
  /**
@@ -123,35 +122,31 @@ export function liveBlock(liveBlockAttribute = 'data-live', liveFormSelector = '
123
122
  */
124
123
  export function replaceOn(attribute = 'replaceBy', eventName = 'click') {
125
124
  var loadVideo = function (element) {
126
- var content = element.getAttribute(attribute);
127
- if (
128
- element.classList.contains('hero-banner-overlay-lg') &&
129
- element.querySelector('picture') &&
130
- window.innerWidth < 992
131
- ) {
132
- element.querySelector('picture').outerHTML = content;
133
- element.querySelector('.btn-play').outerHTML = ' ';
125
+ var content = element.getAttribute(attribute)
126
+ if (element.classList.contains('hero-banner-overlay-lg') && element.querySelector('picture') && window.innerWidth < 992) {
127
+ element.querySelector('picture').outerHTML = content
128
+ element.querySelector('.btn-play').outerHTML = ' '
134
129
  } else {
135
- element.innerHTML = content;
130
+ element.innerHTML = content
136
131
  }
137
132
  if (element.classList.contains('hero-banner-overlay-lg')) {
138
- element.style.zIndex = '2000';
133
+ element.style.zIndex = '2000'
139
134
  }
140
- element.removeAttribute(attribute);
141
- document.dispatchEvent(new Event('DOMChanged'));
142
- };
135
+ element.removeAttribute(attribute)
136
+ document.dispatchEvent(new Event('DOMChanged'))
137
+ }
143
138
 
144
139
  document.querySelectorAll('[' + attribute + ']:not([listen])').forEach(function (element) {
145
- element.setAttribute('listen', '');
140
+ element.setAttribute('listen', '')
146
141
  element.addEventListener(
147
142
  eventName,
148
143
  function (event) {
149
- loadVideo(event.currentTarget); //event.currentTarget;
150
- element.removeAttribute('listen');
144
+ loadVideo(event.currentTarget) //event.currentTarget;
145
+ element.removeAttribute('listen')
151
146
  },
152
- { once: true }
153
- );
154
- });
147
+ { once: true },
148
+ )
149
+ })
155
150
  }
156
151
 
157
152
  /**
@@ -163,12 +158,12 @@ export function seasonedBackground() {
163
158
  if (window.location.hash) {
164
159
  if (element.getAttribute('x-hash') == window.location.hash.substring(1)) {
165
160
  element.parentNode.parentNode.querySelectorAll('img').forEach(function (img) {
166
- img.style = 'display:none';
167
- });
168
- element.style = 'display:block';
161
+ img.style = 'display:none'
162
+ })
163
+ element.style = 'display:block'
169
164
  }
170
165
  }
171
- });
166
+ })
172
167
  }
173
168
 
174
169
  /**
@@ -177,40 +172,51 @@ export function seasonedBackground() {
177
172
  * @param {string} src
178
173
  */
179
174
  export function responsiveImage(src) {
180
- var screenWidth = window.innerWidth;
175
+ var screenWidth = window.innerWidth
181
176
  if (screenWidth <= 576) {
182
- src = src.replace('/default/', '/xs/');
177
+ src = src.replace('/default/', '/xs/')
183
178
  } else if (screenWidth <= 768) {
184
- src = src.replace('/default/', '/sm/');
179
+ src = src.replace('/default/', '/sm/')
185
180
  } else if (screenWidth <= 992) {
186
- src = src.replace('/default/', '/md/');
181
+ src = src.replace('/default/', '/md/')
187
182
  } else if (screenWidth <= 1200) {
188
- src = src.replace('/default/', '/lg/');
183
+ src = src.replace('/default/', '/lg/')
189
184
  } else {
190
185
  // 1200+
191
- src = src.replace('/default/', '/xl/');
186
+ src = src.replace('/default/', '/xl/')
192
187
  }
193
188
 
194
- return src;
189
+ return src
195
190
  }
196
191
 
197
192
  export function addClassForNormalUser(attribute = 'data-acinb') {
198
- var startScrolling = 0;
199
- document.addEventListener('scroll', function () {
193
+ var startScrolling = 0
194
+ function scrollEventAddClassHandler() {
200
195
  if (startScrolling < 5) {
201
- startScrolling++;
196
+ startScrolling++
202
197
  }
203
198
  if (startScrolling === 4) {
204
- [].forEach.call(document.querySelectorAll('[' + attribute + ']'), function (element) {
205
- var classToAdd = element.getAttribute(attribute).split(' ');
206
- element.removeAttribute(attribute);
207
- element.classList.add(...classToAdd);
199
+ document.removeEventListener('scroll', scrollEventAddClassHandler)
200
+ ;[].forEach.call(document.querySelectorAll('[' + attribute + ']'), function (element) {
201
+ var classToAddRaw = element.getAttribute(attribute)
202
+ var classToAdd = classToAddRaw.split(' ')
203
+ element.removeAttribute(attribute)
204
+ element.classList.add(...classToAdd)
208
205
  if (classToAdd.includes('block')) {
209
- element.classList.remove('hidden');
206
+ element.classList.remove('hidden')
207
+ }
208
+ })
209
+ if (window.location.hash) {
210
+ const targetElement = document.querySelector(window.location.hash)
211
+ if (targetElement) {
212
+ targetElement.scrollIntoView({
213
+ behavior: 'smooth',
214
+ })
210
215
  }
211
- });
216
+ }
212
217
  }
213
- });
218
+ }
219
+ document.addEventListener('scroll', scrollEventAddClassHandler)
214
220
  }
215
221
  /**
216
222
  * Convert elements wich contain attribute (data-href) in normal link (a href)
@@ -222,70 +228,70 @@ export async function uncloakLinks(attribute = 'data-rot', when = 'onEvent') {
222
228
  var convertLink = function (element) {
223
229
  // fix "bug" with img
224
230
  if (element.getAttribute(attribute) === null) {
225
- var element = element.closest('[' + attribute + ']');
231
+ var element = element.closest('[' + attribute + ']')
226
232
  }
227
- if (element === null || element.getAttribute(attribute) === null) return;
228
- var link = document.createElement('a');
229
- var href = element.getAttribute(attribute);
230
- element.removeAttribute(attribute);
233
+ if (element === null || element.getAttribute(attribute) === null) return
234
+ var link = document.createElement('a')
235
+ var href = element.getAttribute(attribute)
236
+ element.removeAttribute(attribute)
231
237
  for (var i = 0, n = element.attributes.length; i < n; i++) {
232
- link.setAttribute(element.attributes[i].nodeName, element.attributes[i].nodeValue);
238
+ link.setAttribute(element.attributes[i].nodeName, element.attributes[i].nodeValue)
233
239
  }
234
- link.innerHTML = element.innerHTML;
235
- link.setAttribute('href', responsiveImage(convertShortchutForLink(rot13ToText(href))));
236
- element.parentNode.replaceChild(link, element);
237
- return link;
238
- };
240
+ link.innerHTML = element.innerHTML
241
+ link.setAttribute('href', responsiveImage(convertShortchutForLink(rot13ToText(href))))
242
+ element.parentNode.replaceChild(link, element)
243
+ return link
244
+ }
239
245
 
240
246
  var convertThemAll = function (attribute) {
241
- [].forEach.call(document.querySelectorAll('[' + attribute + ']'), function (element) {
242
- convertLink(element);
243
- });
244
- };
247
+ ;[].forEach.call(document.querySelectorAll('[' + attribute + ']'), function (element) {
248
+ convertLink(element)
249
+ })
250
+ }
245
251
 
246
252
  var fireEventLinksBuilt = async function (element, event) {
247
- await document.dispatchEvent(new Event('DOMChanged'));
253
+ await document.dispatchEvent(new Event('DOMChanged'))
248
254
 
249
- var clickEvent = new Event(event.type);
250
- element.dispatchEvent(clickEvent);
251
- };
255
+ var clickEvent = new Event(event.type)
256
+ element.dispatchEvent(clickEvent)
257
+ }
252
258
 
253
259
  var convertLinkOnEvent = async function (event) {
254
260
  // convert them all if it's an image (thanks this bug), permit to use gallery (baguetteBox)
255
261
  if (event.target.tagName == 'IMG') {
256
- await convertThemAll(attribute);
257
- var element = event.target;
262
+ await convertThemAll(attribute)
263
+ var element = event.target
258
264
  } else {
259
- var element = convertLink(event.target);
265
+ var element = convertLink(event.target)
260
266
  }
261
- if (element) fireEventLinksBuilt(element, event);
262
- };
267
+ if (element) fireEventLinksBuilt(element, event)
268
+ }
263
269
 
264
270
  if (when == 'onEvent') {
265
- [].forEach.call(document.querySelectorAll('[' + attribute + ']'), function (element) {
271
+ ;[].forEach.call(document.querySelectorAll('[' + attribute + ']'), function (element) {
266
272
  element.addEventListener(
267
273
  'touchstart',
268
274
  function (e) {
269
- convertLinkOnEvent(e);
275
+ convertLinkOnEvent(e)
270
276
  },
271
- { once: true, passive: true }
272
- );
277
+ { once: true, passive: true },
278
+ )
273
279
  element.addEventListener(
274
280
  'click',
275
281
  function (e) {
276
- convertLinkOnEvent(e);
282
+ convertLinkOnEvent(e)
277
283
  },
278
- { once: true }
279
- );
284
+ { once: true },
285
+ )
280
286
  element.addEventListener(
281
287
  'mouseover',
282
288
  function (e) {
283
- convertLinkOnEvent(e);
289
+ convertLinkOnEvent(e)
284
290
  },
285
- { once: true }
286
- );
287
- });
288
- } else convertThemAll(attribute);
291
+ { once: true },
292
+ )
293
+ })
294
+ } else convertThemAll(attribute)
289
295
  }
290
296
 
291
297
  /**
@@ -294,24 +300,24 @@ export async function uncloakLinks(attribute = 'data-rot', when = 'onEvent') {
294
300
  * @param {string} attribute
295
301
  */
296
302
  export function convertFormFromRot13(attribute = 'data-frot') {
297
- [].forEach.call(document.querySelectorAll('[' + attribute + ']'), function (element) {
298
- var action = element.getAttribute(attribute);
299
- element.removeAttribute(attribute);
300
- element.setAttribute('action', convertShortchutForLink(rot13ToText(action)));
301
- });
303
+ ;[].forEach.call(document.querySelectorAll('[' + attribute + ']'), function (element) {
304
+ var action = element.getAttribute(attribute)
305
+ element.removeAttribute(attribute)
306
+ element.setAttribute('action', convertShortchutForLink(rot13ToText(action)))
307
+ })
302
308
  }
303
309
 
304
310
  export function convertShortchutForLink(str) {
305
311
  if (str.charAt(0) == '-') {
306
- return 'http://' + str.substring(1);
312
+ return 'http://' + str.substring(1)
307
313
  }
308
314
  if (str.charAt(0) == '_') {
309
- return 'https://' + str.substring(1);
315
+ return 'https://' + str.substring(1)
310
316
  }
311
317
  if (str.charAt(0) == '@') {
312
- return 'mailto:' + str.substring(1);
318
+ return 'mailto:' + str.substring(1)
313
319
  }
314
- return str;
320
+ return str
315
321
  }
316
322
 
317
323
  /**
@@ -321,13 +327,13 @@ export function convertShortchutForLink(str) {
321
327
  */
322
328
  export function readableEmail(selector) {
323
329
  document.querySelectorAll(selector).forEach(function (item) {
324
- var mail = rot13ToText(item.textContent).trim();
325
- item.classList.remove('hidden');
326
- item.innerHTML = '<a href="mailto:' + mail + '">' + mail + '</a>';
330
+ var mail = rot13ToText(item.textContent).trim()
331
+ item.classList.remove('hidden')
332
+ item.innerHTML = '<a href="mailto:' + mail + '">' + mail + '</a>'
327
333
  if (selector.charAt(0) == '.') {
328
- item.classList.remove(selector.substring(1));
334
+ item.classList.remove(selector.substring(1))
329
335
  }
330
- });
336
+ })
331
337
  }
332
338
 
333
339
  /**
@@ -337,20 +343,18 @@ export function readableEmail(selector) {
337
343
  */
338
344
  export function rot13ToText(str) {
339
345
  return str.replace(/[a-zA-Z]/g, function (c) {
340
- return String.fromCharCode(
341
- (c <= 'Z' ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26
342
- );
343
- });
346
+ return String.fromCharCode((c <= 'Z' ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26)
347
+ })
344
348
  }
345
349
 
346
350
  export function testWebPSupport() {
347
- var elem = document.createElement('canvas');
351
+ var elem = document.createElement('canvas')
348
352
 
349
353
  if (elem.getContext && elem.getContext('2d')) {
350
- return elem.toDataURL('image/webp').indexOf('data:image/webp') == 0;
354
+ return elem.toDataURL('image/webp').indexOf('data:image/webp') == 0
351
355
  }
352
356
 
353
- return false;
357
+ return false
354
358
  }
355
359
 
356
360
  /**
@@ -358,14 +362,14 @@ export function testWebPSupport() {
358
362
  */
359
363
  export function convertImageLinkToWebPLink() {
360
364
  var switchToWebP = function () {
361
- [].forEach.call(document.querySelectorAll('a[dwl]'), function (element) {
362
- var href = responsiveImage(element.getAttribute('dwl'));
363
- element.setAttribute('href', href);
364
- element.removeAttribute('dwl');
365
- });
366
- };
365
+ ;[].forEach.call(document.querySelectorAll('a[dwl]'), function (element) {
366
+ var href = responsiveImage(element.getAttribute('dwl'))
367
+ element.setAttribute('href', href)
368
+ element.removeAttribute('dwl')
369
+ })
370
+ }
367
371
 
368
- if (testWebPSupport()) switchToWebP();
372
+ if (testWebPSupport()) switchToWebP()
369
373
  }
370
374
 
371
375
  /**
@@ -385,20 +389,17 @@ export function convertImageLinkToWebPLink() {
385
389
  * still used in piedvert. To remove ?!
386
390
  */
387
391
  export function imgLazyLoad(attribute = 'data-img') {
388
- [].forEach.call(document.querySelectorAll('[' + attribute + ']'), function (img) {
389
- var newDomImg = document.createElement('img');
390
- var src = img.getAttribute(attribute);
391
- img.removeAttribute(attribute);
392
+ ;[].forEach.call(document.querySelectorAll('[' + attribute + ']'), function (img) {
393
+ var newDomImg = document.createElement('img')
394
+ var src = img.getAttribute(attribute)
395
+ img.removeAttribute(attribute)
392
396
  for (var i = 0, n = img.attributes.length; i < n; i++) {
393
- newDomImg.setAttribute(img.attributes[i].nodeName, img.attributes[i].nodeValue);
397
+ newDomImg.setAttribute(img.attributes[i].nodeName, img.attributes[i].nodeValue)
394
398
  }
395
399
  if (newDomImg.getAttribute('alt') === null && img.textContent != '') {
396
- newDomImg.setAttribute('alt', img.textContent);
400
+ newDomImg.setAttribute('alt', img.textContent)
397
401
  }
398
- newDomImg.setAttribute(
399
- 'src',
400
- typeof responsiveImage === 'function' ? responsiveImage(src) : src
401
- );
402
- img.outerHTML = newDomImg.outerHTML;
403
- });
402
+ newDomImg.setAttribute('src', typeof responsiveImage === 'function' ? responsiveImage(src) : src)
403
+ img.outerHTML = newDomImg.outerHTML
404
+ })
404
405
  }