@pushword/js-helper 0.0.77 → 0.0.80

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