2020-08-12 09:55:02 +00:00
|
|
|
/**
|
|
|
|
* Climbs the DOM until the root is reached, storing every node passed.
|
|
|
|
* @param {HTMLElement} el
|
|
|
|
* @return {Array} Contains all the nodes between el and the root
|
|
|
|
*/
|
|
|
|
export function generatePath(el) {
|
|
|
|
if (!el) {
|
|
|
|
throw new Error('No element passed to the generatePath function!')
|
|
|
|
}
|
|
|
|
|
|
|
|
let path = [el]
|
|
|
|
|
|
|
|
while ((el = el.parentNode) && el !== document) {
|
|
|
|
path.push(el)
|
|
|
|
}
|
|
|
|
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2020-07-06 19:55:28 +00:00
|
|
|
export function isValidURL(text) {
|
2020-04-28 18:51:14 +00:00
|
|
|
let lowerCaseText = text.toLowerCase()
|
|
|
|
|
|
|
|
if (lowerCaseText.startsWith('http')) {
|
2020-09-19 12:20:15 +00:00
|
|
|
if (
|
|
|
|
lowerCaseText.indexOf('deezer.com') >= 0 ||
|
|
|
|
lowerCaseText.indexOf('deezer.page.link') >= 0 ||
|
|
|
|
lowerCaseText.indexOf('open.spotify.com') >= 0
|
|
|
|
) {
|
2020-04-08 17:01:50 +00:00
|
|
|
return true
|
2020-04-28 18:51:14 +00:00
|
|
|
}
|
|
|
|
} else if (lowerCaseText.startsWith('spotify:')) {
|
|
|
|
return true
|
|
|
|
}
|
2020-04-08 17:01:50 +00:00
|
|
|
return false
|
|
|
|
}
|
2020-04-14 14:48:13 +00:00
|
|
|
|
2020-07-06 19:55:28 +00:00
|
|
|
export function convertDuration(duration) {
|
2020-06-02 14:40:12 +00:00
|
|
|
// Convert from seconds only to mm:ss format
|
2020-04-16 17:57:34 +00:00
|
|
|
let mm, ss
|
2020-04-09 10:24:49 +00:00
|
|
|
mm = Math.floor(duration / 60)
|
2020-04-16 17:57:34 +00:00
|
|
|
ss = duration - mm * 60
|
2020-06-02 14:40:12 +00:00
|
|
|
// Add leading zero if ss < 0
|
2020-04-09 10:24:49 +00:00
|
|
|
if (ss < 10) {
|
2020-04-16 17:57:34 +00:00
|
|
|
ss = '0' + ss
|
2020-04-09 10:24:49 +00:00
|
|
|
}
|
2020-04-16 17:57:34 +00:00
|
|
|
return mm + ':' + ss
|
2020-04-09 10:24:49 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 19:55:28 +00:00
|
|
|
export function convertDurationSeparated(duration) {
|
2020-04-16 17:57:34 +00:00
|
|
|
let hh, mm, ss
|
2020-04-09 10:24:49 +00:00
|
|
|
mm = Math.floor(duration / 60)
|
|
|
|
hh = Math.floor(mm / 60)
|
2020-04-16 17:57:34 +00:00
|
|
|
ss = duration - mm * 60
|
|
|
|
mm -= hh * 60
|
2020-04-09 10:24:49 +00:00
|
|
|
return [hh, mm, ss]
|
|
|
|
}
|
|
|
|
|
2020-07-06 19:55:28 +00:00
|
|
|
export function numberWithDots(x) {
|
2020-04-26 17:33:09 +00:00
|
|
|
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, '.')
|
2020-04-26 12:27:54 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 19:03:12 +00:00
|
|
|
// On scroll event, returns currentTarget = null
|
|
|
|
// Probably on other events too
|
2020-07-06 19:55:28 +00:00
|
|
|
export function debounce(func, wait, immediate) {
|
2020-04-19 20:02:06 +00:00
|
|
|
var timeout
|
2020-07-06 19:55:28 +00:00
|
|
|
return function() {
|
2020-04-19 20:02:06 +00:00
|
|
|
var context = this
|
|
|
|
var args = arguments
|
2020-07-06 19:55:28 +00:00
|
|
|
var later = function() {
|
2020-04-19 20:02:06 +00:00
|
|
|
timeout = null
|
|
|
|
if (!immediate) func.apply(context, args)
|
|
|
|
}
|
|
|
|
var callNow = immediate && !timeout
|
|
|
|
clearTimeout(timeout)
|
|
|
|
timeout = setTimeout(later, wait)
|
|
|
|
if (callNow) func.apply(context, args)
|
|
|
|
}
|
2020-04-09 10:24:49 +00:00
|
|
|
}
|
2020-04-23 19:26:47 +00:00
|
|
|
|
2020-08-19 15:35:25 +00:00
|
|
|
/**
|
|
|
|
* Workaround to copy to the clipboard cross-OS by generating a
|
|
|
|
* ghost input and copying the passed String
|
|
|
|
*
|
|
|
|
* @param {string} text Text to copy
|
|
|
|
*/
|
|
|
|
export function copyToClipboard(text) {
|
|
|
|
const ghostInput = document.createElement('input')
|
|
|
|
|
|
|
|
document.body.appendChild(ghostInput)
|
|
|
|
ghostInput.setAttribute('type', 'text')
|
|
|
|
ghostInput.setAttribute('value', text)
|
|
|
|
ghostInput.select()
|
|
|
|
ghostInput.setSelectionRange(0, 99999)
|
|
|
|
document.execCommand('copy')
|
|
|
|
ghostInput.remove()
|
|
|
|
}
|
|
|
|
|
2020-04-23 19:26:47 +00:00
|
|
|
export default {
|
|
|
|
isValidURL,
|
|
|
|
convertDuration,
|
|
|
|
convertDurationSeparated,
|
2020-04-26 17:33:09 +00:00
|
|
|
numberWithDots,
|
2020-09-19 12:20:15 +00:00
|
|
|
debounce
|
2020-04-23 19:26:47 +00:00
|
|
|
}
|