@pushword/js-helper 0.0.77 → 0.0.78

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.78",
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,69 @@ 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
+ });
209
+ }
210
+ });
211
+ }
197
212
  /**
198
213
  * Convert elements wich contain attribute (data-href) in normal link (a href)
199
214
  * You can use a callback function to decrypt the link (eg: rot13ToText ;-))
200
215
  *
201
216
  * @param {string} attribute
202
217
  */
203
- export async function uncloakLinks(attribute = "data-rot", when = "onEvent") {
218
+ export async function uncloakLinks(attribute = 'data-rot', when = 'onEvent') {
204
219
  var convertLink = function (element) {
205
220
  // fix "bug" with img
206
221
  if (element.getAttribute(attribute) === null) {
207
- var element = element.closest("[" + attribute + "]");
222
+ var element = element.closest('[' + attribute + ']');
208
223
  }
209
224
  if (element === null || element.getAttribute(attribute) === null) return;
210
- var link = document.createElement("a");
225
+ var link = document.createElement('a');
211
226
  var href = element.getAttribute(attribute);
212
227
  element.removeAttribute(attribute);
213
228
  for (var i = 0, n = element.attributes.length; i < n; i++) {
214
229
  link.setAttribute(element.attributes[i].nodeName, element.attributes[i].nodeValue);
215
230
  }
216
231
  link.innerHTML = element.innerHTML;
217
- link.setAttribute("href", responsiveImage(convertShortchutForLink(rot13ToText(href))));
232
+ link.setAttribute('href', responsiveImage(convertShortchutForLink(rot13ToText(href))));
218
233
  element.parentNode.replaceChild(link, element);
219
234
  return link;
220
235
  };
221
236
 
222
237
  var convertThemAll = function (attribute) {
223
- [].forEach.call(document.querySelectorAll("[" + attribute + "]"), function (element) {
238
+ [].forEach.call(document.querySelectorAll('[' + attribute + ']'), function (element) {
224
239
  convertLink(element);
225
240
  });
226
241
  };
227
242
 
228
243
  var fireEventLinksBuilt = async function (element, event) {
229
- await document.dispatchEvent(new Event("DOMChanged"));
244
+ await document.dispatchEvent(new Event('DOMChanged'));
230
245
 
231
246
  var clickEvent = new Event(event.type);
232
247
  element.dispatchEvent(clickEvent);
@@ -234,7 +249,7 @@ export async function uncloakLinks(attribute = "data-rot", when = "onEvent") {
234
249
 
235
250
  var convertLinkOnEvent = async function (event) {
236
251
  // convert them all if it's an image (thanks this bug), permit to use gallery (baguetteBox)
237
- if (event.target.tagName == "IMG") {
252
+ if (event.target.tagName == 'IMG') {
238
253
  await convertThemAll(attribute);
239
254
  var element = event.target;
240
255
  } else {
@@ -243,24 +258,24 @@ export async function uncloakLinks(attribute = "data-rot", when = "onEvent") {
243
258
  if (element) fireEventLinksBuilt(element, event);
244
259
  };
245
260
 
246
- if (when == "onEvent") {
247
- [].forEach.call(document.querySelectorAll("[" + attribute + "]"), function (element) {
261
+ if (when == 'onEvent') {
262
+ [].forEach.call(document.querySelectorAll('[' + attribute + ']'), function (element) {
248
263
  element.addEventListener(
249
- "touchstart",
264
+ 'touchstart',
250
265
  function (e) {
251
266
  convertLinkOnEvent(e);
252
267
  },
253
268
  { once: true, passive: true }
254
269
  );
255
270
  element.addEventListener(
256
- "click",
271
+ 'click',
257
272
  function (e) {
258
273
  convertLinkOnEvent(e);
259
274
  },
260
275
  { once: true }
261
276
  );
262
277
  element.addEventListener(
263
- "mouseover",
278
+ 'mouseover',
264
279
  function (e) {
265
280
  convertLinkOnEvent(e);
266
281
  },
@@ -275,23 +290,23 @@ export async function uncloakLinks(attribute = "data-rot", when = "onEvent") {
275
290
  *
276
291
  * @param {string} attribute
277
292
  */
278
- export function convertFormFromRot13(attribute = "data-frot") {
279
- [].forEach.call(document.querySelectorAll("[" + attribute + "]"), function (element) {
293
+ export function convertFormFromRot13(attribute = 'data-frot') {
294
+ [].forEach.call(document.querySelectorAll('[' + attribute + ']'), function (element) {
280
295
  var action = element.getAttribute(attribute);
281
296
  element.removeAttribute(attribute);
282
- element.setAttribute("action", convertShortchutForLink(rot13ToText(action)));
297
+ element.setAttribute('action', convertShortchutForLink(rot13ToText(action)));
283
298
  });
284
299
  }
285
300
 
286
301
  export function convertShortchutForLink(str) {
287
- if (str.charAt(0) == "-") {
288
- return "http://" + str.substring(1);
302
+ if (str.charAt(0) == '-') {
303
+ return 'http://' + str.substring(1);
289
304
  }
290
- if (str.charAt(0) == "_") {
291
- return "https://" + str.substring(1);
305
+ if (str.charAt(0) == '_') {
306
+ return 'https://' + str.substring(1);
292
307
  }
293
- if (str.charAt(0) == "@") {
294
- return "mailto:" + str.substring(1);
308
+ if (str.charAt(0) == '@') {
309
+ return 'mailto:' + str.substring(1);
295
310
  }
296
311
  return str;
297
312
  }
@@ -304,9 +319,9 @@ export function convertShortchutForLink(str) {
304
319
  export function readableEmail(selector) {
305
320
  document.querySelectorAll(selector).forEach(function (item) {
306
321
  var mail = rot13ToText(item.textContent);
307
- item.classList.remove("hidden");
308
- item.innerHTML = '<a href="mailto:' + mail + '">' + mail + "</a>";
309
- if (selector.charAt(0) == ".") {
322
+ item.classList.remove('hidden');
323
+ item.innerHTML = '<a href="mailto:' + mail + '">' + mail + '</a>';
324
+ if (selector.charAt(0) == '.') {
310
325
  item.classList.remove(selector.substring(1));
311
326
  }
312
327
  });
@@ -319,15 +334,17 @@ export function readableEmail(selector) {
319
334
  */
320
335
  export function rot13ToText(str) {
321
336
  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);
337
+ return String.fromCharCode(
338
+ (c <= 'Z' ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26
339
+ );
323
340
  });
324
341
  }
325
342
 
326
343
  export function testWebPSupport() {
327
- var elem = document.createElement("canvas");
344
+ var elem = document.createElement('canvas');
328
345
 
329
- if (elem.getContext && elem.getContext("2d")) {
330
- return elem.toDataURL("image/webp").indexOf("data:image/webp") == 0;
346
+ if (elem.getContext && elem.getContext('2d')) {
347
+ return elem.toDataURL('image/webp').indexOf('data:image/webp') == 0;
331
348
  }
332
349
 
333
350
  return false;
@@ -338,10 +355,10 @@ export function testWebPSupport() {
338
355
  */
339
356
  export function convertImageLinkToWebPLink() {
340
357
  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");
358
+ [].forEach.call(document.querySelectorAll('a[dwl]'), function (element) {
359
+ var href = responsiveImage(element.getAttribute('dwl'));
360
+ element.setAttribute('href', href);
361
+ element.removeAttribute('dwl');
345
362
  });
346
363
  };
347
364
 
@@ -364,18 +381,21 @@ export function convertImageLinkToWebPLink() {
364
381
  *
365
382
  * still used in piedvert. To remove ?!
366
383
  */
367
- export function imgLazyLoad(attribute = "data-img") {
368
- [].forEach.call(document.querySelectorAll("[" + attribute + "]"), function (img) {
369
- var newDomImg = document.createElement("img");
384
+ export function imgLazyLoad(attribute = 'data-img') {
385
+ [].forEach.call(document.querySelectorAll('[' + attribute + ']'), function (img) {
386
+ var newDomImg = document.createElement('img');
370
387
  var src = img.getAttribute(attribute);
371
388
  img.removeAttribute(attribute);
372
389
  for (var i = 0, n = img.attributes.length; i < n; i++) {
373
390
  newDomImg.setAttribute(img.attributes[i].nodeName, img.attributes[i].nodeValue);
374
391
  }
375
- if (newDomImg.getAttribute("alt") === null && img.textContent != "") {
376
- newDomImg.setAttribute("alt", img.textContent);
392
+ if (newDomImg.getAttribute('alt') === null && img.textContent != '') {
393
+ newDomImg.setAttribute('alt', img.textContent);
377
394
  }
378
- newDomImg.setAttribute("src", typeof responsiveImage === "function" ? responsiveImage(src) : src);
395
+ newDomImg.setAttribute(
396
+ 'src',
397
+ typeof responsiveImage === 'function' ? responsiveImage(src) : src
398
+ );
379
399
  img.outerHTML = newDomImg.outerHTML;
380
400
  });
381
401
  }