Skip to content

Commit

Permalink
Convert Tooltip from Coffeescript to Javascript
Browse files Browse the repository at this point in the history
Move the tooltip also to the app/javascript folder and import the file only in the GUI init - function. It isn't used anywhere else. Also remove jQuery usage in that tooltips.js.
  • Loading branch information
sascha-karnatz committed Aug 3, 2023
1 parent 826e538 commit 0adce11
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
1 change: 0 additions & 1 deletion app/assets/javascripts/alchemy/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,5 @@
//= require alchemy/alchemy.uploader
//= require alchemy/alchemy.preview_window
//= require alchemy/alchemy.spinner
//= require alchemy/alchemy.tooltips
//= require alchemy/page_select
//= require alchemy/node_select
10 changes: 0 additions & 10 deletions app/assets/javascripts/alchemy/alchemy.tooltips.coffee

This file was deleted.

18 changes: 17 additions & 1 deletion app/javascript/alchemy_admin/gui.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
import TagsAutocomplete from "alchemy_admin/tags_autocomplete"
import Tooltips from "alchemy_admin/tooltips"

/**
* translate the jQuery scope into a native Element
* @param scope
* @returns {Element|Document}
*/
function currentElement(scope) {
if (scope && scope.length > 0) {
return scope[0]
} else {
return document
}
}

function init(scope) {
const element = currentElement(scope)

Alchemy.SelectBox(scope)
Alchemy.Datepicker(scope && scope.selector)
Alchemy.Tooltips(scope)
Tooltips(element)
Alchemy.Buttons.observe(scope)
if (!scope) {
Alchemy.watchForDialogs()
Expand Down
34 changes: 34 additions & 0 deletions app/javascript/alchemy_admin/tooltips.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* small helper to create html elements
* @param {String} tag
* @param {String} className
* @param {String|undefined} content
* @returns {HTMLElement}
*/
function createHtmlElement(tag, className, content = undefined) {
const element = document.createElement(tag)
element.classList.add(className)
if (content) {
element.appendChild(document.createTextNode(content))
}
return element
}

/**
* show tooltips on fixed inputs
* @param {Element|Document} baseElement
* @constructor
*/
export default function Tooltips(baseElement) {
baseElement.querySelectorAll("[data-alchemy-tooltip]").forEach((element) => {
const text = element.dataset.alchemyTooltip

// wrap element with an div.with-hint - tag
const wrapper = createHtmlElement("div", "with-hint")
element.parentElement.insertBefore(wrapper, element)
wrapper.appendChild(element)

// add hint bubble
wrapper.appendChild(createHtmlElement("span", "hint-bubble", text))
})
}

0 comments on commit 0adce11

Please sign in to comment.