aboutsummaryrefslogtreecommitdiff
path: root/app/javascript
diff options
context:
space:
mode:
authorMiquel Sabaté Solà <msabate@suse.com>2024-03-13 18:03:43 +0100
committerMiquel Sabaté Solà <msabate@suse.com>2024-03-13 18:03:43 +0100
commita7791635aa98f012d35f97c3c5288494bbb28e38 (patch)
treebad987e6b4d10ad2f7cd02e40658d464907595ee /app/javascript
downloadoperum-a7791635aa98f012d35f97c3c5288494bbb28e38.tar.gz
operum-a7791635aa98f012d35f97c3c5288494bbb28e38.zip
Initial commit
Signed-off-by: Miquel Sabaté Solà <msabate@suse.com>
Diffstat (limited to 'app/javascript')
-rw-r--r--app/javascript/application.js6
-rw-r--r--app/javascript/controllers/application.js9
-rw-r--r--app/javascript/controllers/comment_controller.js36
-rw-r--r--app/javascript/controllers/export_controller.js19
-rw-r--r--app/javascript/controllers/header_controller.js21
-rw-r--r--app/javascript/controllers/index.js11
-rw-r--r--app/javascript/controllers/search_controller.js36
7 files changed, 138 insertions, 0 deletions
diff --git a/app/javascript/application.js b/app/javascript/application.js
new file mode 100644
index 0000000..9ae56c5
--- /dev/null
+++ b/app/javascript/application.js
@@ -0,0 +1,6 @@
+// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
+import "@hotwired/turbo-rails"
+import "controllers"
+
+import "trix"
+import "@rails/actiontext"
diff --git a/app/javascript/controllers/application.js b/app/javascript/controllers/application.js
new file mode 100644
index 0000000..1213e85
--- /dev/null
+++ b/app/javascript/controllers/application.js
@@ -0,0 +1,9 @@
+import { Application } from "@hotwired/stimulus"
+
+const application = Application.start()
+
+// Configure Stimulus development experience
+application.debug = false
+window.Stimulus = application
+
+export { application }
diff --git a/app/javascript/controllers/comment_controller.js b/app/javascript/controllers/comment_controller.js
new file mode 100644
index 0000000..22167c8
--- /dev/null
+++ b/app/javascript/controllers/comment_controller.js
@@ -0,0 +1,36 @@
+import { Controller } from "@hotwired/stimulus"
+
+// Controller that handles actions for adding some dynamism like showing/hiding
+// comments.
+export default class extends Controller {
+ // Cancel the current operation for comments.
+ toggle(event) {
+ event.preventDefault();
+
+ const parent = event.target.closest('.comment');
+ const regl_body = parent.getElementsByClassName('comment-body');
+ const edit_body = parent.getElementsByClassName('comment-edit-body');
+ if (regl_body.length !== 1 || edit_body.length !== 1) {
+ return;
+ }
+
+ regl_body[0].classList.toggle('hidden');
+ edit_body[0].classList.toggle('hidden');
+ }
+
+ // Hide/show the "New comment" button & form properly, while also focusing on
+ // the text area when shown.
+ toggleNewComment(event) {
+ event.preventDefault();
+
+ document.getElementById("comment-new-button").classList.toggle('hidden');
+
+ let body = document.getElementById("comment-new-body");
+ body.classList.toggle('hidden');
+
+ // BUG (minor): this is not working! (bug in Trix?)
+ if (!body.classList.contains("hidden")) {
+ document.getElementById("comment_content").focus();
+ }
+ }
+}
diff --git a/app/javascript/controllers/export_controller.js b/app/javascript/controllers/export_controller.js
new file mode 100644
index 0000000..1807799
--- /dev/null
+++ b/app/javascript/controllers/export_controller.js
@@ -0,0 +1,19 @@
+import { Controller } from "@hotwired/stimulus"
+
+// Controller used in the exports#new page. That is, it allows us to change the
+// format to be picked up by the exporter.
+export default class extends Controller {
+ static targets = ["id"]
+
+ connect() {
+ const url = `/searches/${this.idTarget.value}/exports.csv`;
+ document.getElementById("export-format-button").setAttribute('href', url);
+ }
+
+ // Change the format to be used.
+ change() {
+ const format = document.getElementById("export-select-format").value;
+ const url = `/searches/${this.idTarget.value}/exports.${format}`;
+ document.getElementById("export-format-button").setAttribute('href', url);
+ }
+}
diff --git a/app/javascript/controllers/header_controller.js b/app/javascript/controllers/header_controller.js
new file mode 100644
index 0000000..ceb53a6
--- /dev/null
+++ b/app/javascript/controllers/header_controller.js
@@ -0,0 +1,21 @@
+import { Controller } from "@hotwired/stimulus"
+
+// Controller being used by the top application header. It allows the user to
+// show/hide the hidden menu with the different options.
+export default class extends Controller {
+ // Hide/show the hidden navigation menu from the header.
+ toggle(event) {
+ event.preventDefault();
+
+ // Toggle the up/down arrow.
+ const el = document.getElementById("header-chevron");
+ if (el.classList[0] === "gg-chevron-down") {
+ el.classList.replace("gg-chevron-down", "gg-chevron-up")
+ } else {
+ el.classList.replace("gg-chevron-up", "gg-chevron-down")
+ }
+
+ // Hide/show the hidden navigation menu.
+ document.getElementById("hidden-nav").classList.toggle('hidden');
+ }
+}
diff --git a/app/javascript/controllers/index.js b/app/javascript/controllers/index.js
new file mode 100644
index 0000000..54ad4ca
--- /dev/null
+++ b/app/javascript/controllers/index.js
@@ -0,0 +1,11 @@
+// Import and register all your controllers from the importmap under controllers/*
+
+import { application } from "controllers/application"
+
+// Eager load all controllers defined in the import map under controllers/**/*_controller
+import { eagerLoadControllersFrom } from "@hotwired/stimulus-loading"
+eagerLoadControllersFrom("controllers", application)
+
+// Lazy load controllers as they appear in the DOM (remember not to preload controllers in import map!)
+// import { lazyLoadControllersFrom } from "@hotwired/stimulus-loading"
+// lazyLoadControllersFrom("controllers", application)
diff --git a/app/javascript/controllers/search_controller.js b/app/javascript/controllers/search_controller.js
new file mode 100644
index 0000000..b35593a
--- /dev/null
+++ b/app/javascript/controllers/search_controller.js
@@ -0,0 +1,36 @@
+import { Controller } from "@hotwired/stimulus"
+
+// Controller that dynamically fetches search results from the body as
+// introduced on an input. Then it fills the `results` elements with them.
+export default class extends Controller {
+ static targets = ["body"]
+
+ connect() {
+ document.getElementById("search_body").focus();
+ }
+
+ load(event) {
+ event.preventDefault();
+
+ try {
+ fetch("/searches/search?" + new URLSearchParams({body: this.bodyTarget.value}))
+ .then(response => response.text())
+ .then(html => {
+ document.getElementById("results").innerHTML = html;
+ document.getElementById("save-search").classList.toggle("hidden");
+ })
+ } catch (error) {
+ document.getElementById("results").innerHTML = '<div class="notice">Something went wrong!</div>'
+ }
+ }
+
+ save(event) {
+ event.preventDefault();
+
+ document.getElementById("search-form-submit").classList.toggle("hidden");
+ document.getElementById("save-search").classList.toggle("hidden");
+ document.getElementById("search-form-name").classList.toggle("hidden");
+ document.getElementById("search-form-shared").classList.toggle("hidden");
+ document.getElementById("search_name").focus();
+ }
+}