diff options
Diffstat (limited to 'activestorage/app/javascript/activestorage/helpers.js')
-rw-r--r-- | activestorage/app/javascript/activestorage/helpers.js | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/activestorage/app/javascript/activestorage/helpers.js b/activestorage/app/javascript/activestorage/helpers.js new file mode 100644 index 0000000000..52fec8f6f1 --- /dev/null +++ b/activestorage/app/javascript/activestorage/helpers.js @@ -0,0 +1,42 @@ +export function getMetaValue(name) { + const element = findElement(document.head, `meta[name="${name}"]`) + if (element) { + return element.getAttribute("content") + } +} + +export function findElements(root, selector) { + if (typeof root == "string") { + selector = root + root = document + } + const elements = root.querySelectorAll(selector) + return toArray(elements) +} + +export function findElement(root, selector) { + if (typeof root == "string") { + selector = root + root = document + } + return root.querySelector(selector) +} + +export function dispatchEvent(element, type, eventInit = {}) { + const { bubbles, cancelable, detail } = eventInit + const event = document.createEvent("Event") + event.initEvent(type, bubbles || true, cancelable || true) + event.detail = detail || {} + element.dispatchEvent(event) + return event +} + +export function toArray(value) { + if (Array.isArray(value)) { + return value + } else if (Array.from) { + return Array.from(value) + } else { + return [].slice.call(value) + } +} |