Skip to content

Commit

Permalink
Trying to Fix the Duplicate Event Listeners Added on Every AJAX Complete
Browse files Browse the repository at this point in the history
  • Loading branch information
taufik-nurrohman committed Feb 18, 2020
1 parent 64daf76 commit 2b4076c
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 98 deletions.
2 changes: 1 addition & 1 deletion panel/engine/r/layout/panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8">
<meta content="width=device-width" name="viewport">
<meta content="noindex" name="robots">
<title><?= w($t->reverse); ?></title>
<title data-loading-text="<?= i('Loading'); ?>&#x2026;"><?= w($t->reverse); ?></title>
<link href="<?= $url; ?>/favicon.ico" rel="shortcut icon">
</head>
<body spellcheck="false">
Expand Down
99 changes: 48 additions & 51 deletions panel/lot/asset/js/panel.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,67 @@
(function(win, doc, _) {

function count(x) {
return Object.keys(x).length;
}

function isSet(x) {
return 'undefined' !== typeof x;
}
var hooks = {};

var hooks = {};

_.on = function(name, fn, id) {
if (!isSet(hooks[name])) {
hooks[name] = {};
}
if (!isSet(id)) {
id = count(hooks[name]);
}
return hooks[name][id] = fn, _;
};
function isSet(x) {
return 'undefined' !== typeof x;
}

_.off = function(name, id) {
if (!isSet(name)) {
return hooks = {}, _;
}
if (!isSet(id)) {
return hooks[name] = {}, _;
}
return delete hooks[name][id], _;
};
_.on = function(name, fn) {
if (!isSet(hooks[name])) {
hooks[name] = [];
}
if (isSet(fn)) {
hooks[name].push(fn);
}
return _;
};

_.fire = function(name, lot, id) {
if (!isSet(hooks[name])) {
return _;
}
if (!isSet(id)) {
for (var i in hooks[name]) {
hooks[name][i].apply(_, lot);
_.off = function(name, fn) {
if (!isSet(name)) {
return (hooks = {}), _;
}
if (isSet(hooks[name])) {
if (isSet(fn)) {
for (var i = 0, j = hooks[name].length; i < j; ++i) {
if (fn === hooks[name][i]) {
hooks[name].splice(i, 1);
}
}
} else {
if (isSet(hooks[name][id])) {
hooks[name][id].apply(_, lot);
}
delete hooks[name];
}
return _;
};

var src = doc.currentScript.src,
a = src.split('/'), i,
// `../`
end = a.pop().split('?')[1] || '0';

// `../`
a.pop();
}
return _;
};

src = a.join('/');
_.fire = function(name, lot) {
if (!isSet(hooks[name])) {
return _;
}
for (var i = 0, j = hooks[name].length; i < j; ++i) {
hooks[name][i].apply(_, lot);
}
return _;
};

_.folder = src;
_.hooks = hooks;

_.on('pop', function() {
// ...
doc.addEventListener('load', function() {
_.fire('get');
});

doc.addEventListener('beforeunload', function() {
_.fire('let');
});

doc.addEventListener('DOMContentLoaded', function() {
_.fire('set');
});

_.on('let', function() {
var title = doc.querySelector('title');
title = title.getAttribute('data-loading-text');
title && (doc.title = title);
});

})(window, document, window._ = window._ || {});
2 changes: 1 addition & 1 deletion panel/lot/asset/js/panel.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions panel/lot/asset/js/panel/alert.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
(function(win, doc, _) {
var timer;
function onChange() {
var bell = doc.querySelector('.js\\:alert');
if (bell) {
var a = bell.querySelector('a'),
svg = a.querySelector('use'),
icon = svg.getAttribute('href'),
url = a.href;
url = url.replace(/\/::\w+::\/.*$/, '/::f::/affd6ded?token=' + _.token);
url = url.replace(/\/::\w+::\/.*$/, '/::f::/affd6ded?token=' + (_.token || ""));
function count() {
fetch(url).then(function(request) {
return request.text();
fetch(url).then(function(response) {
return response.text();
}).then(function(i) {
i = i ? +i : 0;
a.title = i;
svg.setAttribute('href', i ? '#i:e1a64362' : icon);
bell.classList[i ? 'add' : 'remove']('is:active');
});
setTimeout(count, 10000);
timer && clearTimeout(timer);
timer = setTimeout(count, 10000);
}
count();
}
} onChange();
}
_.on('set', onChange);
_.on('change', onChange);
})(window, document, _);
2 changes: 1 addition & 1 deletion panel/lot/asset/js/panel/alert.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 12 additions & 6 deletions panel/lot/asset/js/panel/fetch.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion panel/lot/asset/js/panel/fetch.min.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions panel/lot/asset/js/panel/field/query.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2b4076c

Please sign in to comment.