@glitchr/transparent 1.0.30 → 1.0.32

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/js/transparent.js +135 -130
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@glitchr/transparent",
3
- "version": "1.0.30",
3
+ "version": "1.0.32",
4
4
  "description": "Transparent SPA Application",
5
5
  "main": "src/index.js",
6
6
  "access": "public",
@@ -15,36 +15,36 @@
15
15
  if(!newHash) newHash = "";
16
16
  if (newHash !== "" && (''+newHash).charAt(0) !== '#')
17
17
  newHash = '#' + newHash;
18
-
18
+
19
19
  var oldURL = location.origin+location.pathname+location.hash;
20
20
  var newURL = location.origin+location.pathname+newHash;
21
-
21
+
22
22
  var fallback = $(newHash).length === 0;
23
-
23
+
24
24
  var hashElement = $(newHash)[0] ?? undefined;
25
- if (hashElement !== undefined) // Update hash only if element is displayed
26
- fallback |= window.getComputedStyle(hashElement)["display"] == "none";
27
-
25
+ // if (hashElement !== undefined) // Update hash only if element is displayed
26
+ // fallback |= window.getComputedStyle(hashElement)["display"] == "none";
27
+
28
28
  if((skipIfEmptyIdentifier && !newHash) || fallback){
29
-
29
+
30
30
  dispatchEvent(new HashChangeEvent("hashfallback", {oldURL:oldURL, newURL:newURL}));
31
31
  newHash = "";
32
-
32
+
33
33
  oldURL = location.origin+location.pathname+location.hash;
34
34
  newURL = location.origin+location.pathname+newHash;
35
35
  }
36
-
36
+
37
37
  if(oldURL == newURL) return false;
38
-
38
+
39
39
  var state = Object.assign({}, history.state, {href: newURL});
40
40
  history.replaceState(state, '', newURL);
41
-
41
+
42
42
  if(triggerHashChange)
43
43
  dispatchEvent(new HashChangeEvent("hashchange", {oldURL:oldURL, newURL:newURL}));
44
-
44
+
45
45
  return true;
46
46
  };
47
-
47
+
48
48
  $.fn.serializeObject = function() {
49
49
  var o = {};
50
50
  var a = this.serializeArray();
@@ -60,100 +60,100 @@
60
60
  });
61
61
  return o;
62
62
  };
63
-
63
+
64
64
  $.fn.isScrollable = function()
65
65
  {
66
66
  for (let el of $(this).isScrollableX())
67
67
  if(el) return true;
68
-
68
+
69
69
  for (let el of $(this).isScrollableY())
70
70
  if(el) return true;
71
-
71
+
72
72
  return false;
73
73
  }
74
-
74
+
75
75
  $.fn.isScrollableX = function() {
76
-
76
+
77
77
  return $(this).map(function(i) {
78
-
78
+
79
79
  var el = this[i] === window ? document.documentElement : this[i];
80
80
  var isDom = el == document.documentElement;
81
-
81
+
82
82
  var hasScrollableContent = el.scrollWidth > el.clientWidth;
83
-
83
+
84
84
  var overflowStyle = window.getComputedStyle(el).overflowX;
85
85
  var isOverflowScroll = overflowStyle.indexOf('scroll') !== -1;
86
-
86
+
87
87
  return hasScrollableContent && (isOverflowScroll || isDom);
88
-
88
+
89
89
  }.bind(this));
90
90
  }
91
-
91
+
92
92
  $.fn.isScrollableY = function() {
93
-
93
+
94
94
  return $(this).map(function(i) {
95
-
95
+
96
96
  var el = this[i] === window ? document.documentElement : this[i];
97
97
  var isDom = el == document.documentElement;
98
-
98
+
99
99
  var hasScrollableContent = el.scrollHeight > el.clientHeight;
100
-
100
+
101
101
  var overflowStyle = window.getComputedStyle(el).overflowY;
102
102
  var isOverflowScroll = overflowStyle.indexOf('scroll') !== -1;
103
-
103
+
104
104
  return hasScrollableContent && (isOverflowScroll || isDom);
105
-
105
+
106
106
  }.bind(this));
107
107
  }
108
-
108
+
109
109
  $.fn.closestScrollable = function()
110
110
  {
111
111
  return $(this).map((i) => {
112
-
112
+
113
113
  var target = this[i] === window ? document.documentElement : this[i];
114
114
  if (target === undefined) target = document.documentElement;
115
-
115
+
116
116
  while (target !== document.documentElement) {
117
-
117
+
118
118
  if($(target).isScrollable()) return target;
119
-
119
+
120
120
  if(target.parentElement === undefined) return undefined;
121
121
  if(target.parentElement === null) return null;
122
-
122
+
123
123
  target = target.parentElement;
124
124
  }
125
-
125
+
126
126
  return $(target).isScrollable() ? target : undefined;
127
127
  });
128
128
  }
129
-
129
+
130
130
  $.fn.repaint = function(duration = 1000, reiteration=5) {
131
-
131
+
132
132
  var time = 0;
133
133
  var interval = undefined;
134
134
  var fn = function () {
135
-
135
+
136
136
  $(this).each(function (_, el) {
137
-
137
+
138
138
  var displayBak = el.style.display;
139
-
139
+
140
140
  el.style.display = "none";
141
141
  el.style.display = displayBak;
142
142
  el.offsetHeight;
143
143
  });
144
-
144
+
145
145
  if (time > duration) clearInterval(interval);
146
- time += duration/reiteration;
147
-
146
+ time += duration/reiteration;
147
+
148
148
  }.bind(this);
149
-
149
+
150
150
  fn();
151
151
  if(reiteration > 0)
152
152
  interval = setInterval(fn, duration/reiteration);
153
153
  };
154
-
154
+
155
155
  var Transparent = window.Transparent = {};
156
- Transparent.version = '0.1.0';
156
+ Transparent.version = '0.1.0';
157
157
 
158
158
  var Settings = Transparent.settings = {
159
159
  "headers": {},
@@ -276,7 +276,7 @@
276
276
  responseText = responseText.outerHTML;
277
277
 
278
278
  var array = JSON.parse(sessionStorage.getItem('transparent')) || [];
279
- array.push(uuid);
279
+ array.push(uuid);
280
280
 
281
281
  while(array.length > Settings["response_limit"])
282
282
  sessionStorage.removeItem('transparent['+array.shift()+']');
@@ -533,7 +533,7 @@
533
533
  if (href.endsWith ("#")) href = href.slice(0, -1);
534
534
 
535
535
  var method = el.target.getAttribute("method") || "GET";
536
- method = method.toUpperCase();
536
+ method = method.toUpperCase();
537
537
 
538
538
  var form = Transparent.findNearestForm(el);
539
539
  if (form == null) {
@@ -650,20 +650,20 @@
650
650
  if(url === null || url === "") continue;
651
651
 
652
652
  var preload = document.createElement("link");
653
- preloads.push(preload);
653
+ preloads.push(preload);
654
654
 
655
- preload.onload = function () {
655
+ preload.onload = function () {
656
656
 
657
- if (++nPreloaded == nPreload)
658
- return callback(preloads);
659
- };
657
+ if (++nPreloaded == nPreload)
658
+ return callback(preloads);
659
+ };
660
660
 
661
- preload.setAttribute("rel", "preload");
662
- preload.setAttribute("as", as);
663
- preload.setAttribute("crossorigin","");
664
- preload.href = url;
661
+ preload.setAttribute("rel", "preload");
662
+ preload.setAttribute("as", as);
663
+ preload.setAttribute("crossorigin","");
664
+ preload.href = url;
665
665
 
666
- document.head.append(preload);
666
+ document.head.append(preload);
667
667
  }
668
668
  }
669
669
  }
@@ -725,11 +725,11 @@
725
725
  Transparent.parseDuration = function(str) {
726
726
 
727
727
  var array = String(str).split(", ");
728
- array = array.map(function(t) {
728
+ array = array.map(function(t) {
729
729
 
730
- if(String(t).endsWith("ms")) return parseFloat(String(t))/1000;
731
- return parseFloat(String(t));
732
- });
730
+ if(String(t).endsWith("ms")) return parseFloat(String(t))/1000;
731
+ return parseFloat(String(t));
732
+ });
733
733
 
734
734
  return Math.max(...array);
735
735
  }
@@ -760,7 +760,8 @@
760
760
 
761
761
  return {delay:delay, duration:duration};
762
762
  }
763
-
763
+ var activeInTime = 0;
764
+ var activeInRemainingTime = 0;
764
765
  Transparent.activeIn = function(activeCallback = function() {}) {
765
766
 
766
767
  if(!Transparent.html.hasClass(Transparent.state.PREACTIVE)) {
@@ -769,6 +770,8 @@
769
770
  }
770
771
 
771
772
  var active = Transparent.activeTime();
773
+ activeInTime = Date.now();
774
+ activeInRemainingTime = active.delay+active.duration;
772
775
 
773
776
  Transparent.html.removeClass(Transparent.state.PREACTIVE);
774
777
  if(!Transparent.html.hasClass(Transparent.state.ACTIVEIN)) {
@@ -788,6 +791,7 @@
788
791
  Transparent.callback(function() {
789
792
 
790
793
  activeCallback();
794
+ activeInRemainingTime = 0;
791
795
 
792
796
  }.bind(this), active.duration);
793
797
 
@@ -908,7 +912,7 @@
908
912
 
909
913
  console.log(head, body);
910
914
  if(head == undefined || body == "undefined") {
911
-
915
+
912
916
  $(Settings.identifier).html("<div class='error'></div>");
913
917
 
914
918
  setTimeout(function() { window.location.reload(); }, Transparent.parseDuration(Settings["rescue_reload"]));
@@ -920,7 +924,7 @@
920
924
  Transparent.evalScript($("head")[0]);
921
925
  Transparent.evalScript($("body")[0]);
922
926
  }
923
-
927
+
924
928
  Transparent.activeOut();
925
929
  }
926
930
 
@@ -1023,22 +1027,22 @@
1023
1027
  return Array.from(doc.querySelectorAll('*'))
1024
1028
  .reduce((collection, node) => {
1025
1029
 
1026
- let prop = window.getComputedStyle(node, null).getPropertyValue('background-image')
1027
- let match = srcChecker.exec(prop);
1028
- if (match) collection.add(match[1]);
1030
+ let prop = window.getComputedStyle(node, null).getPropertyValue('background-image')
1031
+ let match = srcChecker.exec(prop);
1032
+ if (match) collection.add(match[1]);
1029
1033
 
1030
- if (/^img$/i.test(node.tagName)) collection.add(node.src)
1031
- else if (/^frame$/i.test(node.tagName)) {
1034
+ if (/^img$/i.test(node.tagName)) collection.add(node.src)
1035
+ else if (/^frame$/i.test(node.tagName)) {
1032
1036
 
1033
- try {
1034
- searchDOM(node.contentDocument || node.contentWindow.document)
1035
- .forEach(img => { if (img) collection.add(img); })
1036
- } catch (e) {}
1037
- }
1037
+ try {
1038
+ searchDOM(node.contentDocument || node.contentWindow.document)
1039
+ .forEach(img => { if (img) collection.add(img); })
1040
+ } catch (e) {}
1041
+ }
1038
1042
 
1039
- return collection;
1043
+ return collection;
1040
1044
 
1041
- }, new Set());
1045
+ }, new Set());
1042
1046
  }
1043
1047
 
1044
1048
  Transparent.lazyLoad = function (lazyloadImages = undefined)
@@ -1046,25 +1050,25 @@
1046
1050
  lazyloadImages = lazyloadImages || document.querySelectorAll("img[data-src]:not(.loaded)");
1047
1051
  if ("IntersectionObserver" in window) {
1048
1052
 
1049
- var imageObserver = new IntersectionObserver(function (entries, observer) {
1050
- entries.forEach(function (entry) {
1051
- if (entry.isIntersecting) {
1052
- var image = entry.target;
1053
- var lazybox = image.closest(".lazybox");
1053
+ var imageObserver = new IntersectionObserver(function (entries, observer) {
1054
+ entries.forEach(function (entry) {
1055
+ if (entry.isIntersecting) {
1056
+ var image = entry.target;
1057
+ var lazybox = image.closest(".lazybox");
1054
1058
 
1055
- image.onload = function() {
1056
- this.classList.add("loaded");
1057
- this.classList.remove("loading");
1058
- if(lazybox) lazybox.classList.add("loaded");
1059
- if(lazybox) lazybox.classList.remove("loading");
1060
- };
1059
+ image.onload = function() {
1060
+ this.classList.add("loaded");
1061
+ this.classList.remove("loading");
1062
+ if(lazybox) lazybox.classList.add("loaded");
1063
+ if(lazybox) lazybox.classList.remove("loading");
1064
+ };
1061
1065
 
1062
- if(lazybox) lazybox.classList.add("loading");
1063
- image.classList.add("loading");
1064
- image.src = image.dataset.src;
1066
+ if(lazybox) lazybox.classList.add("loading");
1067
+ image.classList.add("loading");
1068
+ image.src = image.dataset.src;
1065
1069
 
1066
- imageObserver.unobserve(image);
1067
- }
1070
+ imageObserver.unobserve(image);
1071
+ }
1068
1072
  });
1069
1073
  });
1070
1074
 
@@ -1074,7 +1078,7 @@
1074
1078
 
1075
1079
  } else {
1076
1080
 
1077
- var lazyloadThrottleTimeout;
1081
+ var lazyloadThrottleTimeout;
1078
1082
 
1079
1083
  function lazyload() {
1080
1084
  if (lazyloadThrottleTimeout) {
@@ -1109,7 +1113,7 @@
1109
1113
  var imgPromise = new Promise((resolve, reject) => {
1110
1114
 
1111
1115
  let img = new Image()
1112
- img.onload = () => {
1116
+ img.onload = () => {
1113
1117
  resolve({
1114
1118
  src: src,
1115
1119
  width: img.naturalWidth,
@@ -1209,14 +1213,14 @@
1209
1213
  });
1210
1214
 
1211
1215
  var bodyScript = $(dom).find("body > script");
1212
- bodyScript.each(function() {
1216
+ bodyScript.each(function() {
1213
1217
 
1214
- var el = this;
1215
- var found = false;
1218
+ var el = this;
1219
+ var found = false;
1216
1220
 
1217
- $("body").children().each(function() { found |= this.isEqualNode(el); });
1218
- if(!found) $("body").append(this);
1219
- });
1221
+ $("body").children().each(function() { found |= this.isEqualNode(el); });
1222
+ if(!found) $("body").append(this);
1223
+ });
1220
1224
 
1221
1225
  // Replace canvases..
1222
1226
  Transparent.replaceCanvases(dom);
@@ -1282,6 +1286,7 @@
1282
1286
 
1283
1287
  $(Settings.identifier).append(function() {
1284
1288
 
1289
+ activeInRemainingTime = activeInRemainingTime - (Date.now() - activeInTime);
1285
1290
  setTimeout(function() {
1286
1291
 
1287
1292
  // Callback if needed, or any other actions
@@ -1291,15 +1296,15 @@
1291
1296
  dispatchEvent(new Event('transparent:load'));
1292
1297
  dispatchEvent(new Event('load'));
1293
1298
 
1294
- }.bind(this), 1);
1299
+ }.bind(this), activeInRemainingTime > 0 ? activeInRemainingTime : 1);
1295
1300
  });
1296
1301
  });
1297
1302
  }
1298
1303
 
1299
1304
  function uuidv4() {
1300
1305
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
1301
- var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
1302
- return v.toString(16);
1306
+ var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
1307
+ return v.toString(16);
1303
1308
  });
1304
1309
  }
1305
1310
 
@@ -1323,13 +1328,13 @@
1323
1328
  if(str === undefined) return undefined;
1324
1329
 
1325
1330
  var array = String(str).split(", ");
1326
- array = array.map(function(s) {
1331
+ array = array.map(function(s) {
1327
1332
 
1328
- if(s.endsWith("rem")) return Transparent.remToPixel (s);
1329
- else if(s.endsWith("em") ) return Transparent.emToPixel (s, el);
1330
- else if(s.endsWith("%") ) return Transparent.percentToPixel(s, el);
1331
- return parseFloat(s);
1332
- });
1333
+ if(s.endsWith("rem")) return Transparent.remToPixel (s);
1334
+ else if(s.endsWith("em") ) return Transparent.emToPixel (s, el);
1335
+ else if(s.endsWith("%") ) return Transparent.percentToPixel(s, el);
1336
+ return parseFloat(s);
1337
+ });
1333
1338
 
1334
1339
  return Math.max(...array);
1335
1340
  }
@@ -1338,10 +1343,10 @@
1338
1343
 
1339
1344
  var style = window.getComputedStyle(el);
1340
1345
  var dict = {};
1341
- dict["top" ] = Transparent.parseToPixel(style["scroll-padding-top" ] || 0, el);
1342
- dict["left" ] = Transparent.parseToPixel(style["scroll-padding-left" ] || 0, el);
1343
- dict["right" ] = Transparent.parseToPixel(style["scroll-padding-right" ] || 0, el);
1344
- dict["bottom"] = Transparent.parseToPixel(style["scroll-padding-bottom"] || 0, el);
1346
+ dict["top" ] = Transparent.parseToPixel(style["scroll-padding-top" ] || 0, el);
1347
+ dict["left" ] = Transparent.parseToPixel(style["scroll-padding-left" ] || 0, el);
1348
+ dict["right" ] = Transparent.parseToPixel(style["scroll-padding-right" ] || 0, el);
1349
+ dict["bottom"] = Transparent.parseToPixel(style["scroll-padding-bottom"] || 0, el);
1345
1350
 
1346
1351
  if(isNaN(dict["top" ])) dict["top"] = 0;
1347
1352
  if(isNaN(dict["left" ])) dict["left"] = 0;
@@ -1468,7 +1473,7 @@
1468
1473
 
1469
1474
  dispatchEvent(new Event('transparent:onbeforeunload'));
1470
1475
  dispatchEvent(new Event('onbeforeunload'));
1471
-
1476
+
1472
1477
  $(Transparent.html).prop("user-scroll", true);
1473
1478
  $(Transparent.html).stop();
1474
1479
 
@@ -1483,7 +1488,7 @@
1483
1488
  function handleResponse(uuid, status = 200, method = null, data = null, xhr = null, request = null) {
1484
1489
 
1485
1490
  var responseURL;
1486
- responseURL = xhr !== null ? xhr.responseURL : url.href;
1491
+ responseURL = xhr !== null ? xhr.responseURL : url.href;
1487
1492
 
1488
1493
  responseText = Transparent.getResponseText(uuid);
1489
1494
 
@@ -1590,21 +1595,21 @@
1590
1595
  return window.location.reload();
1591
1596
 
1592
1597
  return Transparent.onLoad(uuid, dom, function() {
1593
-
1594
- Transparent.activeOut(function() {
1595
-
1596
- Transparent.html
1597
- .removeClass(switchLayout)
1598
- .removeClass(Transparent.state.SUBMIT)
1599
- .removeClass(Transparent.state.POPSTATE)
1600
- .removeClass(Transparent.state.NEW);
1601
- });
1602
1598
 
1603
- }, type != "POST");
1599
+ Transparent.activeOut(function() {
1600
+
1601
+ Transparent.html
1602
+ .removeClass(switchLayout)
1603
+ .removeClass(Transparent.state.SUBMIT)
1604
+ .removeClass(Transparent.state.POPSTATE)
1605
+ .removeClass(Transparent.state.NEW);
1606
+ });
1607
+
1608
+ }, type != "POST");
1604
1609
  }
1605
1610
 
1606
1611
  if(history.state && !Transparent.hasResponse(history.state.uuid))
1607
- Transparent.setResponse(history.state.uuid, Transparent.html[0], Transparent.getScrollableElementXY());
1612
+ Transparent.setResponse(history.state.uuid, Transparent.html[0], Transparent.getScrollableElementXY());
1608
1613
 
1609
1614
  // This append on user click (e.g. when user push a link)
1610
1615
  // It is null when dev is pushing or replacing state
@@ -1652,7 +1657,7 @@
1652
1657
  window.onhashchange = __main__;
1653
1658
  document.addEventListener('click', __main__, false);
1654
1659
 
1655
- $("form").submit(__main__);
1660
+ $("form").on("submit", __main__);
1656
1661
  }
1657
1662
 
1658
1663
  return Transparent;