-
-
Notifications
You must be signed in to change notification settings - Fork 104
Fix pat-autotoc fieldset ID and refactor TOC navigation
#1479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
petschki
wants to merge
4
commits into
master
Choose a base branch
from
autotoc-fix-sectionid
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
46c8dae
feat(pat autotoc): make markup compatible with the bootstrap and clea…
cihanandac 7983bcf
fix(pat autotoc): Fix fieldset ID and refactor TOC navigation.
petschki 4f5898f
fix(pat schemaeditor): changed autotoc selector
petschki 103fa15
fix(pat autotoc): Add note how to handle activating nested autotoc tabs.
petschki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import $ from "jquery"; | ||
| import Base from "@patternslib/patternslib/src/core/base"; | ||
| import utils from "@patternslib/patternslib/src/core/utils"; | ||
| import Tab from "bootstrap/js/dist/tab"; | ||
|
|
||
| export default Base.extend({ | ||
| name: "autotoc", | ||
|
|
@@ -10,8 +11,9 @@ export default Base.extend({ | |
| section: "section", | ||
| levels: "h1,h2,h3", | ||
| IDPrefix: "autotoc-item-", | ||
| classTOCName: "autotoc-nav", | ||
| classSectionName: "autotoc-section", | ||
| classTOCName: "autotoc-nav nav", | ||
| classContentAreaName: "autotoc-content tab-content", | ||
| classSectionName: "autotoc-section tab-pane fade", | ||
| classLevelPrefixName: "autotoc-level-", | ||
| classActiveName: "active", | ||
| scrollDuration: "slow", | ||
|
|
@@ -26,67 +28,108 @@ export default Base.extend({ | |
|
|
||
| var self = this; | ||
|
|
||
| self.$toc = $("<nav/>").addClass(self.options.classTOCName); | ||
| const $nav = $("<nav/>") | ||
| .attr("aria-label", "Tab Navigation"); | ||
|
|
||
| const $toc = $("<ul/>") | ||
| .addClass(self.options.classTOCName) | ||
| .appendTo($nav); | ||
|
|
||
| if (self.options.prependTo) { | ||
| self.$toc.prependTo(self.options.prependTo); | ||
| $nav.prependTo(self.options.prependTo); | ||
| } else if (self.options.appendTo) { | ||
| self.$toc.appendTo(self.options.appendTo); | ||
| $nav.appendTo(self.options.appendTo); | ||
| } else { | ||
| self.$toc.prependTo(self.$el); | ||
| $nav.prependTo(self.$el); | ||
| } | ||
|
|
||
| if (self.options.className) { | ||
| self.$el.addClass(self.options.className); | ||
| } | ||
|
|
||
| $(self.options.section, self.$el).addClass(self.options.classSectionName); | ||
| $(self.options.section, self.$el) | ||
| .addClass(self.options.classSectionName) | ||
| .attr("role", "tabpanel") | ||
| .attr("tabindex", "0"); | ||
|
|
||
| var asTabs = self.$el.hasClass("autotabs"); | ||
|
|
||
| if (asTabs) { | ||
| $toc.addClass("nav-tabs"); | ||
| self.$contentArea = $("<div/>").addClass(self.options.classContentAreaName); | ||
| self.$contentArea.insertAfter($nav); | ||
| $(self.options.section, self.$el).appendTo(self.$contentArea); | ||
| $(self.options.section, self.$el).find("legend").hide(); | ||
| } else { | ||
| $toc.addClass([ | ||
| "flex-column", | ||
| "float-end", | ||
| "border", | ||
| "mt-0", | ||
| "me-0", | ||
| "mb-3", | ||
| "ms-3", | ||
| "py-2", | ||
| "px-0" | ||
| ]); | ||
| } | ||
|
|
||
| var activeId = null; | ||
|
|
||
| $(self.options.levels, self.$el).each(function (i) { | ||
| const section = this.closest(self.options.section); | ||
| const $level = $(this); | ||
| let id = $level.prop("id") ? $level.prop("id") : $(section).prop("id"); | ||
|
|
||
| if (!id || $("#" + id).length > 0) { | ||
| id = self.options.IDPrefix + self.name + "-" + i; | ||
| } | ||
| if (window.location.hash === "#" + id) { | ||
| activeId = id; | ||
| const $section = $level.closest(self.options.section); | ||
| let sectionId = $section.prop("id"); | ||
| let sectionHash = `#${sectionId}`; | ||
| const tabId = `${self.options.IDPrefix}${self.name}-${i}`; | ||
| const tabHash = `#${tabId}`; | ||
| const levelId = `${tabId}-pane`; | ||
| const levelHash = `#${levelId}`; | ||
|
|
||
| if (!asTabs) { | ||
| $level.attr("id", levelId).attr("aria-labelledby", tabId); | ||
| } else { | ||
| $section.attr("aria-labelledby", tabId); | ||
| if (!sectionId) { | ||
| // sections without ID get auto generated "<tabId>-pane" | ||
| sectionId = levelId; | ||
| sectionHash = `#${levelId}`; | ||
| $section.attr("id", levelId); | ||
| } | ||
| } | ||
| if (activeId === null && $level.hasClass(self.options.classActiveName)) { | ||
| activeId = id; | ||
|
|
||
| // NOTE: if you have nested autotocs then you have to add the | ||
| // parent autotoc tabId to `options.IDPrefix` of the sub autotoc | ||
| // in order to mark parent and sub tab as active. | ||
| if (activeId === null && (window.location.hash.indexOf(tabHash) == 0 || $level.hasClass(self.options.classActiveName))) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cihanandac this solves the nested tab activation problem. however, we need to fix the |
||
| activeId = tabId; | ||
| } | ||
| $level.data("navref", id); | ||
|
|
||
| const $navItem = $("<li/>"); | ||
| $navItem | ||
| .addClass("nav-item") | ||
| .attr("role", "presentation") | ||
| .appendTo($toc); | ||
|
|
||
| const $nav = $("<a/>"); | ||
| $nav.appendTo(self.$toc) | ||
| .text($level.text()) | ||
| .attr("id", id) | ||
| .attr("href", "#" + id) | ||
| .addClass(self.options.classLevelPrefixName + self.getLevel($level)) | ||
| $nav.appendTo($navItem) | ||
| .html($level.html()) | ||
| .attr("id", tabId) | ||
| .attr("href", asTabs ? sectionHash : levelHash) | ||
| .attr("aria-controls", asTabs ? sectionId : levelId) | ||
| .attr("data-bs-toggle", "tab") | ||
| .attr("data-bs-target", asTabs ? sectionHash : levelHash) | ||
| .addClass([ | ||
| "nav-link", | ||
| self.options.classLevelPrefixName + self.getLevel($level), | ||
| ]) | ||
| .on("click", function (e, options) { | ||
| e.stopPropagation(); | ||
| e.preventDefault(); | ||
| if (!options) { | ||
| options = { | ||
| doScroll: true, | ||
| skipHash: false, | ||
| }; | ||
| } | ||
| var $el = $(this); | ||
| self.$toc | ||
| .children("." + self.options.classActiveName) | ||
| .removeClass(self.options.classActiveName); | ||
| self.$el | ||
| .children("." + self.options.classActiveName) | ||
| .removeClass(self.options.classActiveName); | ||
| $(e.target).addClass(self.options.classActiveName); | ||
| $level | ||
| .parents(self.options.section) | ||
| .addClass(self.options.classActiveName); | ||
| if ( | ||
| options.doScroll !== false && | ||
| self.options.scrollDuration && | ||
|
|
@@ -107,37 +150,36 @@ export default Base.extend({ | |
| $(this).trigger("clicked"); | ||
| if (!options.skipHash) { | ||
| if (window.history && window.history.pushState) { | ||
| window.history.pushState({}, "", "#" + $el.attr("id")); | ||
| window.history.pushState({}, "", tabHash); | ||
| } | ||
| } | ||
| }); | ||
| $level.data("autotoc-trigger-id", id); | ||
|
|
||
| if (!asTabs) { | ||
| $nav.addClass([ | ||
| "text-decoration-underline", | ||
| "p-0", | ||
| "mx-3", | ||
| ]); | ||
| } | ||
|
|
||
| self.tabs.push({ | ||
| section: section, | ||
| id: id, | ||
| section: $section[0], | ||
| id: levelId, | ||
| nav: $nav[0], | ||
| }); | ||
| }); | ||
|
|
||
| if (activeId) { | ||
| $("a#" + activeId).trigger("click", { | ||
| doScroll: true, | ||
| skipHash: true, | ||
| }); | ||
| } else { | ||
| self.$toc.find("a").first().trigger("click", { | ||
| doScroll: false, | ||
| skipHash: true, | ||
| }); | ||
| } | ||
| const activeTabButton = activeId ? $toc.find("a#" + activeId)[0] : $toc.find("a").first()[0]; | ||
| const tab = Tab.getOrCreateInstance(activeTabButton); | ||
| tab.show(); | ||
|
|
||
| // After DOM tree is built, initialize eventual validation | ||
| this.initialize_validation(self.$el); | ||
| this.initialize_validation(); | ||
| }, | ||
|
|
||
| initialize_validation: function ($el) { | ||
| const el = $el[0]; | ||
| initialize_validation: function () { | ||
| const el = this.el | ||
|
|
||
| // Initialize only on forms | ||
| const form = el.closest("form"); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,69 +1,10 @@ | ||
| .pat-autotoc { | ||
| .autotoc-nav { | ||
| float: right; | ||
| border: 1px solid #ddd; | ||
| padding: 0.5em 0; | ||
| margin: 0 0 1em 1em; | ||
| a { | ||
| display: block; | ||
| } | ||
| a:focus { | ||
| outline-style: none; | ||
| } | ||
| .autotoc-level-1 { | ||
| margin: 0 1em 0 1em; | ||
| } | ||
| .autotoc-level-2 { | ||
| margin: 0 1em 0 2em; | ||
| margin-left: 2em; | ||
| } | ||
| .autotoc-level-3 { | ||
| margin: 0 1em 0 3em; | ||
| } | ||
| } | ||
| &.autotabs { | ||
| .autotoc-nav { | ||
| float: none; | ||
| padding: 0; | ||
| margin: 0 0 0.3em 0; | ||
| border: 0; | ||
| border-bottom: 1px solid #ddd; | ||
| &:after { | ||
| content: ""; | ||
| display: table; | ||
| line-height: 0; | ||
| } | ||
| a { | ||
| display: inline-block; | ||
| margin: 0 0.5em -1px 0.5em; | ||
| line-height: 1.5em; | ||
| padding: 0.4em 0.8em; | ||
| text-decoration: none; | ||
| border-radius: 4px 4px 0 0; | ||
| &.active { | ||
| border: 1px solid #ddd; | ||
| border-bottom-color: var(--bs-body-bg); | ||
| color: var(--bs-secondary-text); | ||
| cursor: default; | ||
| } | ||
| &.active:hover { | ||
| background-color: transparent; | ||
| } | ||
| &:hover { | ||
| // background-color: #eee; | ||
| background-color: var(--bs-secondary-bg); | ||
| border-color: #eee; | ||
| border-bottom-color: var(--bs-body-bg); | ||
| } | ||
| } | ||
| } | ||
| .autotoc-section { | ||
| display: none; | ||
| &.active { | ||
| display: block; | ||
| legend { | ||
| display: none; | ||
| } | ||
| } | ||
| margin-left: 3em; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.