From 02f8d5cc4e42e5a61cf54232ab525917fa9cf3b1 Mon Sep 17 00:00:00 2001 From: alexdbondoc17 Date: Thu, 24 Feb 2022 04:46:58 +0000 Subject: [PATCH] Fix bug for `accordion` --- ...doctrines_offcanvas_accordion_component.rb | 39 +++++++++++++ ...nes_offcanvas_accordion_component.html.erb | 25 +++++++++ .../sidenav_component.html.erb | 8 +-- app/components/subject_accordion_component.rb | 38 ------------- .../subject_accordion_component.html.erb | 56 ------------------- .../subjects_accordion_component.rb | 38 +++++++++++++ .../subjects_accordion_component.html.erb | 27 +++++++++ .../subjects_offcanvas_accordion_component.rb | 39 +++++++++++++ ...cts_offcanvas_accordion_component.html.erb | 25 +++++++++ .../controllers/application_controller.js | 18 +++++- .../controllers/subjects_controller.js | 43 +++++++++++++- app/javascript/packs/application.js | 32 ----------- app/reflexes/subjects_reflex.rb | 8 +++ .../_doctrines_offcanvas_accordion.html.erb | 1 + app/views/shared/_subjects_accordion.html.erb | 2 +- .../_subjects_offcanvas_accordion.html.erb | 1 + app/views/subject_indexes/index.html.erb | 11 ++-- 17 files changed, 271 insertions(+), 140 deletions(-) create mode 100644 app/components/doctrines_offcanvas_accordion_component.rb create mode 100644 app/components/doctrines_offcanvas_accordion_component/doctrines_offcanvas_accordion_component.html.erb delete mode 100644 app/components/subject_accordion_component.rb delete mode 100644 app/components/subject_accordion_component/subject_accordion_component.html.erb create mode 100644 app/components/subjects_accordion_component.rb create mode 100644 app/components/subjects_accordion_component/subjects_accordion_component.html.erb create mode 100644 app/components/subjects_offcanvas_accordion_component.rb create mode 100644 app/components/subjects_offcanvas_accordion_component/subjects_offcanvas_accordion_component.html.erb create mode 100644 app/views/shared/_doctrines_offcanvas_accordion.html.erb create mode 100644 app/views/shared/_subjects_offcanvas_accordion.html.erb diff --git a/app/components/doctrines_offcanvas_accordion_component.rb b/app/components/doctrines_offcanvas_accordion_component.rb new file mode 100644 index 0000000..7dd24ad --- /dev/null +++ b/app/components/doctrines_offcanvas_accordion_component.rb @@ -0,0 +1,39 @@ +class DoctrinesOffcanvasAccordionComponent < BaseComponent + with_collection_parameter :search_result + attr_reader :search_result, :opts + + def initialize(search_result:, current_user:, opts: {}) + @search_result = search_result + @opts = opts + end + + def accordion_name + "doctrinesOffCanvasAccordion" + end + + def accordion_body_name + "doctrinesOffCanvasAccordionBody" + end + + def parent_accordion_id + return accordion_body_name if search_result["parent_id"].blank? + + [accordion_name, search_result["parent_id"]].join + end + + def children_accordion_id + [accordion_name, search_result["heirarchy_level"], search_result["id"]].join + end + + def accordion_id + [accordion_name, search_result["id"]].join + end + + def accordion_body_id + [accordion_body_name, search_result["id"]].join + end + + def index_url + search_doctrines_path(subject_ids: [search_result["id"]], is_index_table: true) + end +end diff --git a/app/components/doctrines_offcanvas_accordion_component/doctrines_offcanvas_accordion_component.html.erb b/app/components/doctrines_offcanvas_accordion_component/doctrines_offcanvas_accordion_component.html.erb new file mode 100644 index 0000000..89905da --- /dev/null +++ b/app/components/doctrines_offcanvas_accordion_component/doctrines_offcanvas_accordion_component.html.erb @@ -0,0 +1,25 @@ +
+
+
+ <% if search_result["has_children?"].present? %> + + <% else %> + <%= link_to search_result["name"], index_url, class: "accordion-link text-decoration-none text-dark d-block sub#{search_result["heirarchy_level"] + 1} clickable-link" %> + <% end %> +
+ +
+
pb-1" id="<%= accordion_body_id %>"> +
+
+ Loading... +
+
+
+
+
+
diff --git a/app/components/sidenav_component/sidenav_component.html.erb b/app/components/sidenav_component/sidenav_component.html.erb index 0c26096..41def0c 100644 --- a/app/components/sidenav_component/sidenav_component.html.erb +++ b/app/components/sidenav_component/sidenav_component.html.erb @@ -89,15 +89,15 @@ diff --git a/app/components/subject_accordion_component.rb b/app/components/subject_accordion_component.rb deleted file mode 100644 index ecd2501..0000000 --- a/app/components/subject_accordion_component.rb +++ /dev/null @@ -1,38 +0,0 @@ -class SubjectAccordionComponent < BaseComponent - with_collection_parameter :search_result - attr_reader :search_result, :opts - - def initialize(search_result:, current_user:, opts: {}) - @search_result = search_result - @opts = opts - end - - def parent_accordion_id - return "subjectsAccordion" if search_result["parent_id"].blank? - - ["subjectsAccordion", search_result["parent_id"]].join - end - - def children_accordion_id - ["subjectsAccordion", search_result["id"]].join - end - - def accordion_id - ["subjectsAccordion", search_result["id"]].join - end - - def accordion_body_id - return ["subjectsAccordionBody", search_result["id"]].join if opts[:accordionFor].blank? - - accordion_body = opts[:accordionFor].to_s.eql?("subjects") ? "subjectsOffCanvasAccordionBody" : "doctrinesOffCanvasAccordionBody" - [accordion_body, search_result["id"]].join - end - - def custom_url - return search_doctrines_path(subject_ids: [search_result["id"]], is_index_table: true) if opts[:accordionFor].present? && opts[:accordionFor].to_s.eql?("subjects") - return search_doctrines_path(subject_ids: [search_result["id"]], is_index_table: false) if opts[:accordionFor].present? && opts[:accordionFor].to_s.eql?("doctrines") - - subject_index_path(search_result["id"]) - end -end - \ No newline at end of file diff --git a/app/components/subject_accordion_component/subject_accordion_component.html.erb b/app/components/subject_accordion_component/subject_accordion_component.html.erb deleted file mode 100644 index 1500230..0000000 --- a/app/components/subject_accordion_component/subject_accordion_component.html.erb +++ /dev/null @@ -1,56 +0,0 @@ -<% if opts[:accordionFor].present? %> -
-
-
- <% if search_result["has_children?"].present? %> - - <% else %> - <%= link_to search_result["name"], custom_url, class: "accordion-link text-decoration-none text-dark d-block sub#{search_result["heirarchy_level"] + 1} clickable-link" %> - <% end %> -
- -
-
pb-1" id="<%= accordion_body_id %>"> -
-
- Loading... -
-
-
-
-
-
-<% else %> -
-
-
- <% if search_result["has_children?"].present? %> - - <% else %> - <%= link_to search_result["name"], custom_url, class: "accordion-link text-decoration-none text-dark d-block sub#{search_result["heirarchy_level"] + 1} clickable-link" %> - <%= link_to ''.html_safe, new_subject_index_path(parent_id: search_result["id"]), class: "text-decoration-none d-block sub#{search_result["heirarchy_level"]} clickable-link position-absolute end-0 non-toggle" %> - <% end %> -
- -
-
pb-0" id="<%= accordion_body_id %>"> -
-
- Loading... -
-
-
-
-
-
-<% end %> diff --git a/app/components/subjects_accordion_component.rb b/app/components/subjects_accordion_component.rb new file mode 100644 index 0000000..8f1ac7e --- /dev/null +++ b/app/components/subjects_accordion_component.rb @@ -0,0 +1,38 @@ +class SubjectsAccordionComponent < BaseComponent + with_collection_parameter :search_result + attr_reader :search_result, :opts + + def initialize(search_result:, current_user:, opts: {}) + @search_result = search_result + @opts = opts + end + + def accordion_name + "subjectsAccordion" + end + + def accordion_body_name + "subjectsAccordionBody" + end + + def parent_accordion_id + [accordion_name, search_result["parent_id"]].join + end + + def children_accordion_id + [accordion_name, search_result["heirarchy_level"], search_result["id"]].join + end + + def accordion_id + [accordion_name, search_result["id"]].join + end + + def accordion_body_id + [accordion_body_name, search_result["id"]].join + end + + def custom_url + subject_index_path(search_result["id"]) + end +end + \ No newline at end of file diff --git a/app/components/subjects_accordion_component/subjects_accordion_component.html.erb b/app/components/subjects_accordion_component/subjects_accordion_component.html.erb new file mode 100644 index 0000000..cea499a --- /dev/null +++ b/app/components/subjects_accordion_component/subjects_accordion_component.html.erb @@ -0,0 +1,27 @@ +
+
+
+ <% if search_result["has_children?"].present? %> + + <% else %> + <%= link_to search_result["name"], custom_url, class: "accordion-link text-decoration-none text-dark d-block sub#{search_result["heirarchy_level"] + 1} clickable-link" %> + <%= link_to ''.html_safe, new_subject_index_path(parent_id: search_result["id"]), class: "text-decoration-none d-block sub#{search_result["heirarchy_level"]} clickable-link position-absolute end-0 non-toggle" %> + <% end %> +
+ +
+
pb-0" id="<%= accordion_body_id %>"> +
+
+ Loading... +
+
+
+
+
+
diff --git a/app/components/subjects_offcanvas_accordion_component.rb b/app/components/subjects_offcanvas_accordion_component.rb new file mode 100644 index 0000000..2f445bc --- /dev/null +++ b/app/components/subjects_offcanvas_accordion_component.rb @@ -0,0 +1,39 @@ +class SubjectsOffcanvasAccordionComponent < BaseComponent + with_collection_parameter :search_result + attr_reader :search_result, :opts + + def initialize(search_result:, current_user:, opts: {}) + @search_result = search_result + @opts = opts + end + + def accordion_name + "subjectsOffCanvasAccordion" + end + + def accordion_body_name + "subjectsOffCanvasAccordionBody" + end + + def parent_accordion_id + return accordion_body_name if search_result["parent_id"].blank? + + [accordion_name, search_result["parent_id"]].join + end + + def children_accordion_id + [accordion_name, search_result["heirarchy_level"], search_result["id"]].join + end + + def accordion_id + [accordion_name, search_result["id"]].join + end + + def accordion_body_id + [accordion_body_name, search_result["id"]].join + end + + def index_url + search_doctrines_path(subject_ids: [search_result["id"]], is_index_table: true) + end +end diff --git a/app/components/subjects_offcanvas_accordion_component/subjects_offcanvas_accordion_component.html.erb b/app/components/subjects_offcanvas_accordion_component/subjects_offcanvas_accordion_component.html.erb new file mode 100644 index 0000000..89905da --- /dev/null +++ b/app/components/subjects_offcanvas_accordion_component/subjects_offcanvas_accordion_component.html.erb @@ -0,0 +1,25 @@ +
+
+
+ <% if search_result["has_children?"].present? %> + + <% else %> + <%= link_to search_result["name"], index_url, class: "accordion-link text-decoration-none text-dark d-block sub#{search_result["heirarchy_level"] + 1} clickable-link" %> + <% end %> +
+ +
+
pb-1" id="<%= accordion_body_id %>"> +
+
+ Loading... +
+
+
+
+
+
diff --git a/app/javascript/controllers/application_controller.js b/app/javascript/controllers/application_controller.js index d72ce9d..6142c36 100644 --- a/app/javascript/controllers/application_controller.js +++ b/app/javascript/controllers/application_controller.js @@ -5,10 +5,8 @@ import { end } from '@popperjs/core' require("selectize/dist/js/selectize.min"); require("selectize/dist/css/selectize"); -require("bootstrap/dist/js/bootstrap") import Selectize from "selectize"; -import Bootstrap from 'bootstrap'; /* This is your ApplicationController. * All StimulusReflex controllers should inherit from this class. @@ -23,6 +21,22 @@ import Bootstrap from 'bootstrap'; */ export default class extends Controller { connect () { + $(".clickable-link").on("click", function (e) { + e.preventDefault(); + + let $href = $(this).attr("href"); + if ($href !== undefined) { + window.location.href = $href; + } + }); + + $(".clickable-tr").on("click", function () { + let $href = $(this).attr("href"); + if ($href !== undefined) { + window.open($href); + } + }) + StimulusReflex.register(this) } diff --git a/app/javascript/controllers/subjects_controller.js b/app/javascript/controllers/subjects_controller.js index c505d17..1370b72 100644 --- a/app/javascript/controllers/subjects_controller.js +++ b/app/javascript/controllers/subjects_controller.js @@ -9,13 +9,14 @@ export default class extends ApplicationController { opts = { parent_accordion_id: $this.element.dataset["accordionId"], accordionTarget: $this.element.dataset["accordionTarget"], accordionFor: $this.element.dataset["accordionFor"] } + console.log(opts) if (parent_id !== null && parent_id !== undefined && parent_id !== "") { params.parent_id = parent_id } else { params.is_root = true } - if ($(opts.accordionTarget).find(".accordion-flash").length === 0) { + if ($(opts.accordionTarget).find(".accordion-flush").length === 0) { $.get("/api/subjects.json", params, function (search_results, status) { if (status === "success") { $this.stimulate("SubjectsReflex#render_subjects_accordion", search_results, opts) @@ -23,4 +24,44 @@ export default class extends ApplicationController { }) } } + + loadSubjectsOffcanvasAccordion() { + var $this = this, params = { per_page: 10000 }, parent_id = $this.element.dataset["subjectParentId"], + opts = { accordionTarget: $this.element.dataset["accordionTarget"] } + + console.log(opts) + if (parent_id !== null && parent_id !== undefined && parent_id !== "") { + params.parent_id = parent_id + } else { + params.is_root = true + } + + if ($(opts.accordionTarget).find(".accordion-flush").length === 0) { + $.get("/api/subjects.json", params, function (search_results, status) { + if (status === "success") { + $this.stimulate("SubjectsReflex#render_subjects_offcanvas_accordion", search_results, opts) + } + }) + } + } + + loadDoctrinesOffcanvasAccordion() { + var $this = this, params = { per_page: 10000 }, parent_id = $this.element.dataset["subjectParentId"], + opts = { accordionTarget: $this.element.dataset["accordionTarget"] } + + console.log(opts) + if (parent_id !== null && parent_id !== undefined && parent_id !== "") { + params.parent_id = parent_id + } else { + params.is_root = true + } + + if ($(opts.accordionTarget).find(".accordion-flush").length === 0) { + $.get("/api/subjects.json", params, function (search_results, status) { + if (status === "success") { + $this.stimulate("SubjectsReflex#render_doctrines_offcanvas_accordion", search_results, opts) + } + }) + } + } } diff --git a/app/javascript/packs/application.js b/app/javascript/packs/application.js index 46dd728..3578f1b 100644 --- a/app/javascript/packs/application.js +++ b/app/javascript/packs/application.js @@ -36,44 +36,12 @@ $(document).on("ready turbolinks:load", function () { $('#sidenav').toggleClass('active'); }); - $(".clickable-link").on("click", function (e) { - e.preventDefault(); - - let $href = $(this).attr("href"); - if ($href !== undefined) { - window.location.href = $href; - } - }); - - $(".clickable-tr").on("click", function () { - let $href = $(this).attr("href"); - if ($href !== undefined) { - window.open($href); - } - }) - var default_config = { altFormat: "m/d/Y", altInput: true, enableTime: false, dateFormat: "Y-m-d", isMobile: true }; $('.default-flatpickr').flatpickr(default_config); if ($.fn.footnotes) { $('#documentTabContent').footnotes() } - - $(document).on('click', 'a[href="#footnotes"]', function(event){ - event.preventDefault(); - $(this).each(function(){ - var a, b, c, d; - a = $(this), b = a.attr('href'), c = a.text(), d = '#footnote' + c - a.attr('href', d), a.attr('id', 'fn' + c) - a.trigger('click') - }) - }).on('click', 'a[href^="#footnote"], a[href^="#fn"]', function(event){ - var a, b; - a = $(this), b = $(a.attr('href')) - if(b.length<=0)return - event.preventDefault() - $(document).wrap('') - }) }); import "channels" diff --git a/app/reflexes/subjects_reflex.rb b/app/reflexes/subjects_reflex.rb index 96a619f..fdd41bf 100644 --- a/app/reflexes/subjects_reflex.rb +++ b/app/reflexes/subjects_reflex.rb @@ -4,4 +4,12 @@ class SubjectsReflex < ApplicationReflex def render_subjects_accordion(search_results, opts = {}) morph opts[:accordionTarget], render(partial: "/shared/subjects_accordion", locals: { search_results: search_results, opts: opts }) end + + def render_subjects_offcanvas_accordion(search_results, opts) + morph opts[:accordionTarget], render(partial: "/shared/subjects_offcanvas_accordion", locals: { search_results: search_results, opts: opts }) + end + + def render_doctrines_offcanvas_accordion(search_results, opts) + morph opts[:accordionTarget], render(partial: "/shared/doctrines_offcanvas_accordion", locals: { search_results: search_results, opts: opts }) + end end diff --git a/app/views/shared/_doctrines_offcanvas_accordion.html.erb b/app/views/shared/_doctrines_offcanvas_accordion.html.erb new file mode 100644 index 0000000..82d25d3 --- /dev/null +++ b/app/views/shared/_doctrines_offcanvas_accordion.html.erb @@ -0,0 +1 @@ +<%= render(DoctrinesOffcanvasAccordionComponent.with_collection(search_results, current_user: current_user, opts: opts)) %> diff --git a/app/views/shared/_subjects_accordion.html.erb b/app/views/shared/_subjects_accordion.html.erb index f94df35..9cced20 100644 --- a/app/views/shared/_subjects_accordion.html.erb +++ b/app/views/shared/_subjects_accordion.html.erb @@ -1 +1 @@ -<%= render(SubjectAccordionComponent.with_collection(search_results, current_user: current_user, opts: opts)) %> +<%= render(SubjectsAccordionComponent.with_collection(search_results, current_user: current_user, opts: opts)) %> diff --git a/app/views/shared/_subjects_offcanvas_accordion.html.erb b/app/views/shared/_subjects_offcanvas_accordion.html.erb new file mode 100644 index 0000000..f23bfd7 --- /dev/null +++ b/app/views/shared/_subjects_offcanvas_accordion.html.erb @@ -0,0 +1 @@ +<%= render(SubjectsOffcanvasAccordionComponent.with_collection(search_results, current_user: current_user, opts: opts)) %> diff --git a/app/views/subject_indexes/index.html.erb b/app/views/subject_indexes/index.html.erb index e6788ed..00deaa1 100644 --- a/app/views/subject_indexes/index.html.erb +++ b/app/views/subject_indexes/index.html.erb @@ -2,14 +2,13 @@

Subjects

-
- <% Cdao::Subject.roots.order(name: :asc).each do |root| %> +
+ <% Cdao::Subject.roots.order(name: :asc).each do |root| %>
-
"> +
<% if root.children.present? %>
- <% end %> + <% end %>