From 02715158d39e15ea5262b4d766896c823a5349b3 Mon Sep 17 00:00:00 2001
From: Floorb <132411956+Neetpone@users.noreply.github.com>
Date: Sat, 26 Mar 2022 23:48:19 -0400
Subject: [PATCH] Deduplicate some JS
---
js/archive.js | 20 ++------------
js/user_profile.js | 53 ++++++++----------------------------
js/utils.js | 23 ++++++++++++++++
theme/bulma/user_profile.php | 4 ++-
4 files changed, 39 insertions(+), 61 deletions(-)
create mode 100644 js/utils.js
diff --git a/js/archive.js b/js/archive.js
index 2a50605..097881a 100644
--- a/js/archive.js
+++ b/js/archive.js
@@ -1,5 +1,6 @@
import { escape, whenReady } from './dom';
import { DataTable, dumbFilterCallback } from './data_tables';
+import { tagsToHtml } from "./utils";
import { globalSetup } from './main';
whenReady(() => {
@@ -16,27 +17,10 @@ whenReady(() => {
.then(resolve);
},
rowCallback: (rowData) => {
- const tags = rowData.tags.map((tagData) => {
- let tagColorClass;
- if (tagData.name.indexOf('nsfw') !== -1) {
- tagColorClass = 'is-danger';
- } else if (tagData.name.indexOf('safe') !== -1) {
- tagColorClass = 'is-success';
- } else if (tagData.name.indexOf('/') !== -1) {
- tagColorClass = 'is-primary';
- } else {
- tagColorClass = 'is-info';
- }
-
- return `
- ${escape(tagData.name)}
- `;
- }).join('');
-
return `
${escape(rowData.title)} |
${escape(rowData.author)} |
- ${tags} |
+ ${tagsToHtml(rowData.tags)} |
`;
},
filterCallback: dumbFilterCallback,
diff --git a/js/user_profile.js b/js/user_profile.js
index 3134047..613fc66 100644
--- a/js/user_profile.js
+++ b/js/user_profile.js
@@ -1,5 +1,6 @@
import { escape, whenReady } from './dom';
import { DataTable, dumbFilterCallback } from './data_tables';
+import { tagsToHtml } from "./utils";
import { globalSetup } from './main';
const getUserInfo = () => {
@@ -33,39 +34,23 @@ whenReady(() => {
});
},
rowCallback: (rowData) => {
- const tags = rowData.tags.map((tagData) => {
- let tagColorClass;
- if (tagData.name.indexOf('nsfw') !== -1) {
- tagColorClass = 'is-danger';
- } else if (tagData.name.indexOf('safe') !== -1) {
- tagColorClass = 'is-success';
- } else if (tagData.name.indexOf('/') !== -1) {
- tagColorClass = 'is-primary';
- } else {
- tagColorClass = 'is-info';
- }
-
- return `
- ${escape(tagData.name)}
- `;
- }).join('');
-
const userData = getUserInfo();
- const deleteElem = true ? `
+ const deleteElem = true ? ` |
| ` : '';
+ const pasteCreatedAt = new Date(rowData.created_at).toLocaleString();
return `
${escape(rowData.title)} |
- ${rowData.created_at} |
- ${rowData.visibility} |
- ${rowData.views || 0} |
- ${tags} |
+ ${pasteCreatedAt} |
+ ${rowData.visibility} |
+ ${rowData.views || 0} |
+ ${tagsToHtml(rowData.tags)} |
${deleteElem}
`;
},
@@ -87,33 +72,17 @@ whenReady(() => {
});
},
rowCallback: (rowData) => {
- const tags = rowData.tags.map((tagData) => {
- let tagColorClass;
- if (tagData.name.indexOf('nsfw') !== -1) {
- tagColorClass = 'is-danger';
- } else if (tagData.name.indexOf('safe') !== -1) {
- tagColorClass = 'is-success';
- } else if (tagData.name.indexOf('/') !== -1) {
- tagColorClass = 'is-primary';
- } else {
- tagColorClass = 'is-info';
- }
-
- return `
- ${escape(tagData.name)}
- `;
- }).join('');
-
const recentUpdate = rowData.recently_updated ?
`` :
``;
+ const pasteFavedAt = new Date(rowData.favourited_at).toLocaleString();
// ${escape(rowData.author)} |
return `
${escape(rowData.title)} |
- ${rowData.favourited_at} |
- ${recentUpdate} |
- ${tags} |
+ ${pasteFavedAt} |
+ ${recentUpdate} |
+ ${tagsToHtml(rowData.tags)} |
`;
},
filterCallback: dumbFilterCallback
diff --git a/js/utils.js b/js/utils.js
new file mode 100644
index 0000000..cbadd73
--- /dev/null
+++ b/js/utils.js
@@ -0,0 +1,23 @@
+import { escape } from "./dom";
+
+const tagsToHtml = (tags) => {
+
+ return tags.map(tagData => {
+ let tagColorClass;
+ if (tagData.name.indexOf('nsfw') !== -1) {
+ tagColorClass = 'is-danger';
+ } else if (tagData.name.indexOf('safe') !== -1) {
+ tagColorClass = 'is-success';
+ } else if (tagData.name.indexOf('/') !== -1) {
+ tagColorClass = 'is-primary';
+ } else {
+ tagColorClass = 'is-info';
+ }
+
+ return `
+ ${escape(tagData.name)}
+ `;
+ }).join('');
+};
+
+export { tagsToHtml };
diff --git a/theme/bulma/user_profile.php b/theme/bulma/user_profile.php
index 6fce0ec..c939ffe 100644
--- a/theme/bulma/user_profile.php
+++ b/theme/bulma/user_profile.php
@@ -132,7 +132,9 @@
visible == Paste::VISIBILITY_PUBLIC): ?>
= $escaped_title ?> |
- = $p_date->format('d F Y') ?> |
+
+ = $p_date->format('d F Y') ?>
+ |
= $p_visible; ?> |
= $paste->views ?> |
= tagsToHtmlUser($paste->tags, $profile_username); ?> |