summaryrefslogtreecommitdiff
path: root/web/public/js
diff options
context:
space:
mode:
Diffstat (limited to 'web/public/js')
-rw-r--r--web/public/js/lib.js66
-rw-r--r--web/public/js/routes/home.js0
-rw-r--r--web/public/js/shared/modal.js (renamed from web/public/js/modal.js)0
-rw-r--r--web/public/js/shared/post.js (renamed from web/public/js/post.js)2
4 files changed, 66 insertions, 2 deletions
diff --git a/web/public/js/lib.js b/web/public/js/lib.js
index d822b02..7647208 100644
--- a/web/public/js/lib.js
+++ b/web/public/js/lib.js
@@ -1,4 +1,8 @@
+///
+/// document ready functions
+///
+
let ready = false;
$(function() {
@@ -15,9 +19,14 @@ var r$ = function(callback) {
}
}
+///
+/// dom observer
+/// checks for elements on the DOM now and added later
+///
+
function observe(containerSelector, elementSelector, callback) {
- r$(() => {
+ r$(() => {
$(containerSelector + ' ' + elementSelector).each(function (_, e) {
let me = $(e);
callback(me);
@@ -37,9 +46,64 @@ function observe(containerSelector, elementSelector, callback) {
};
var target = $(containerSelector)[0];
+
+ if (!target) {
+ console.warn('[observe] didnt find container: ', containerSelector);
+ return;
+ }
+
var config = { childList: true, subtree: true };
var MutationObserver = window.MutationObserver;
var observer = new MutationObserver(onMutationsObserved);
observer.observe(target, config);
});
}
+
+///
+/// ajax setup
+///
+
+let ajaxHeaders = {};
+ajaxHeaders['Content-Type'] = 'application/json';
+if (jwtStr) {
+ ajaxHeaders['Authorization'] = 'Bearer ' + jwtStr
+}
+
+$.ajaxSetup({
+ headers: ajaxHeaders
+})
+
+///
+/// ajax error handle
+///
+
+var errorToast = (xhr) => {
+ let data = xhr.responseJSON;
+ let msg = data.message;
+ let detail = data.details;
+ let hint = data.hint;
+
+ let query = '?msg=' + msg;
+ if (detail) {
+ query += '&detail=' + detail;
+ }
+ if (hint) {
+ query += '&hint=' + hint;
+ }
+ let url = '/template/toast' + query;
+ $.get(url, function (data) {
+ $('#toast-container').prepend(data);
+ })
+}
+
+observe('#toast-container', '.action-close-toast', function(el) {
+ el.on('click', function() {
+ el.parent().remove();
+ });
+
+ setTimeout(function() {
+ el.parent().remove();
+ }, 5000);
+});
+
+
diff --git a/web/public/js/routes/home.js b/web/public/js/routes/home.js
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/web/public/js/routes/home.js
diff --git a/web/public/js/modal.js b/web/public/js/shared/modal.js
index 792cd85..792cd85 100644
--- a/web/public/js/modal.js
+++ b/web/public/js/shared/modal.js
diff --git a/web/public/js/post.js b/web/public/js/shared/post.js
index 736fa2b..afbeaf0 100644
--- a/web/public/js/post.js
+++ b/web/public/js/shared/post.js
@@ -1,4 +1,4 @@
-observe('.post', '.action-load-comments', function(me) {
+observe('#main-content', '.action-load-comments', function(me) {
me.on('click', function() {
let page = me.attr('page');
if (!page) {