@springio/utils
Advanced tools
+35
-29
@@ -17,10 +17,7 @@ /* | ||
| (function() { | ||
| "use strict"; | ||
| class BlockSwitch { | ||
| window.addEventListener("load", onWindowLoad); | ||
| function addBlockSwitches() { | ||
| addBlockSwitches() { | ||
| for (var primary of document.querySelectorAll(".primary")) { | ||
| var switchItem = createSwitchItem(primary, createBlockSwitch(primary)); | ||
| var switchItem = this.createSwitchItem(primary, this.createBlockSwitch(primary)); | ||
| switchItem.item.classList.add("selected"); | ||
@@ -31,7 +28,7 @@ var title = primary.querySelector(".title"); | ||
| for (var secondary of document.querySelectorAll(".secondary")) { | ||
| var primary = findPrimary(secondary); | ||
| var primary = this.findPrimary(secondary); | ||
| if (primary === null) { | ||
| console.error("Found secondary block with no primary sibling"); | ||
| } else { | ||
| var switchItem = createSwitchItem( | ||
| var switchItem = this.createSwitchItem( | ||
| secondary, | ||
@@ -47,3 +44,3 @@ primary.querySelector(".switch") | ||
| function createElementFromHtml(html) { | ||
| createElementFromHtml(html) { | ||
| var template = document.createElement("template"); | ||
@@ -54,4 +51,4 @@ template.innerHTML = html; | ||
| function createBlockSwitch(primary) { | ||
| var blockSwitch = createElementFromHtml('<div class="switch"></div>'); | ||
| createBlockSwitch(primary) { | ||
| var blockSwitch = this.createElementFromHtml('<div class="switch"></div>'); | ||
| primary.prepend(blockSwitch); | ||
@@ -61,3 +58,3 @@ return blockSwitch; | ||
| function findPrimary(secondary) { | ||
| findPrimary(secondary) { | ||
| var candidate = secondary.previousElementSibling; | ||
@@ -70,10 +67,10 @@ while (candidate != null && !candidate.classList.contains("primary")) { | ||
| function createSwitchItem(block, blockSwitch) { | ||
| createSwitchItem(block, blockSwitch) { | ||
| var blockName = block.querySelector(".title").textContent; | ||
| var content = block.querySelectorAll(".content").item(0); | ||
| var colist = nextSibling(block, ".colist"); | ||
| var colist = this.nextSibling(block, ".colist"); | ||
| if (colist != null) { | ||
| content.append(colist); | ||
| } | ||
| var item = createElementFromHtml( | ||
| var item = this.createElementFromHtml( | ||
| '<div class="switch--item">' + blockName + "</div>" | ||
@@ -87,3 +84,3 @@ ); | ||
| function nextSibling(element, selector) { | ||
| nextSibling(element, selector) { | ||
| var sibling = element.nextElementSibling; | ||
@@ -98,5 +95,6 @@ while (sibling) { | ||
| function globalSwitch() { | ||
| document.querySelectorAll(".switch--item").forEach(function(item) { | ||
| var blockId = blockIdForSwitchItem(item); | ||
| globalSwitch() { | ||
| const blocks = this; | ||
| document.querySelectorAll(".switch--item").forEach(item => { | ||
| var blockId = blocks.blockIdForSwitchItem(item); | ||
| var handler = function(event) { | ||
@@ -107,6 +105,6 @@ var selectedText = event.target.textContent; | ||
| if ( | ||
| blockIdForSwitchItem(switchItem) === blockId && | ||
| blocks.blockIdForSwitchItem(switchItem) === blockId && | ||
| switchItem.textContent === selectedText | ||
| ) { | ||
| select(switchItem); | ||
| blocks.select(switchItem); | ||
| } | ||
@@ -117,3 +115,3 @@ } | ||
| if (item.textContent === window.localStorage.getItem(blockId)) { | ||
| select(item); | ||
| blocks.select(item); | ||
| } | ||
@@ -123,3 +121,3 @@ }); | ||
| function select(selected) { | ||
| select(selected) { | ||
| for (var child of selected.parentNode.children) { | ||
@@ -140,3 +138,3 @@ child.classList.remove("selected"); | ||
| function blockIdForSwitchItem(item) { | ||
| blockIdForSwitchItem(item) { | ||
| var idComponents = []; | ||
@@ -149,6 +147,14 @@ for (var switchItem of item.parentNode.querySelectorAll(".switch--item")) { | ||
| function onWindowLoad() { | ||
| addBlockSwitches(); | ||
| globalSwitch(); | ||
| }; | ||
| })(); | ||
| static onWindowLoad() { | ||
| if (document) { | ||
| const blocks = new BlockSwitch(); | ||
| blocks.addBlockSwitches(); | ||
| blocks.globalSwitch(); | ||
| } | ||
| } | ||
| }; | ||
| window.addEventListener("load", BlockSwitch.onWindowLoad); | ||
| export {}; |
+1
-1
| { | ||
| "name": "@springio/utils", | ||
| "version": "1.0.5", | ||
| "version": "2.0.0", | ||
| "description": "Utilities for Spring documentation and websites", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+10
-1
@@ -57,2 +57,11 @@ Javascript library with utilities for [Spring](https://spring.io) documentation and websites. | ||
| The script scans for this kind of structure and re-organizes it so that only one snippet is visible and the others are selectable by a click. It works for all blocks with "Maven|Gradle" choices in the whole document, on the assumption that the user only wants to select this once. It's a bit like classic navigation tabs, but applied to the whole content, not just navigation. | ||
| The script scans for this kind of structure and re-organizes it so that only one snippet is visible and the others are selectable by a click. It works for all blocks with "Maven|Gradle" choices in the whole document, on the assumption that the user only wants to select this once. It's a bit like classic navigation tabs, but applied to the whole content, not just navigation. | ||
| ## Publishing | ||
| This library is published in NPMJS (following instructions [here](https://docs.npmjs.com/creating-and-publishing-scoped-public-packages)). That's how the `unpkg.com` links work above: | ||
| ``` | ||
| $ npm login | ||
| $ npm publish --access public | ||
| ``` |
8202
4.59%160
2.56%66
15.79%