@symbo.ls/utils 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +26 -0
- package/src/detectHeight.js +31 -0
- package/src/index.js +23 -0
- package/src/navigation.js +66 -0
- package/src/scaling.js +29 -0
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@symbo.ls/utils",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "",
|
|
5
|
+
"source": "src/index.js",
|
|
6
|
+
"main": "src/index.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"watch": "parcel watch --no-cache",
|
|
9
|
+
"build": "parcel build --no-cache"
|
|
10
|
+
},
|
|
11
|
+
"author": "",
|
|
12
|
+
"license": "ISC",
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@symbo.ls/scratch": "latest",
|
|
15
|
+
"@symbo.ls/icons": "^0.2.26"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@babel/core": "^7.11.5",
|
|
19
|
+
"babel-eslint": "^10.0.3",
|
|
20
|
+
"eslint": "^6.1.0",
|
|
21
|
+
"standard": "^13.1.0"
|
|
22
|
+
},
|
|
23
|
+
"standard": {
|
|
24
|
+
"parser": "babel-eslint"
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
export const detectHeightOnInit = (element, state) => {
|
|
4
|
+
const heightTimeout = setTimeout(() => {
|
|
5
|
+
const { props } = element
|
|
6
|
+
if (!state.clientHeight) {
|
|
7
|
+
const { node: { clientHeight } } = element
|
|
8
|
+
if (clientHeight) {
|
|
9
|
+
state.clientHeight = clientHeight
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
if (state.active) {
|
|
14
|
+
if (props.height === 'auto') return
|
|
15
|
+
element.update({
|
|
16
|
+
props: { height: state.clientHeight }
|
|
17
|
+
}, { ignoreInitUpdate: true, preventChildrenUpdate: true })
|
|
18
|
+
const setAutoTimeout = setTimeout(() => {
|
|
19
|
+
element.update({
|
|
20
|
+
props: { height: 'auto' }
|
|
21
|
+
}, { ignoreInitUpdate: true, preventChildrenUpdate: true })
|
|
22
|
+
clearTimeout(setAutoTimeout)
|
|
23
|
+
}, 450)
|
|
24
|
+
} else {
|
|
25
|
+
element.update({
|
|
26
|
+
props: { height: '0' }
|
|
27
|
+
}, { ignoreInitUpdate: true, preventChildrenUpdate: true })
|
|
28
|
+
}
|
|
29
|
+
clearTimeout(heightTimeout)
|
|
30
|
+
})
|
|
31
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
export * from './navigation'
|
|
4
|
+
export * from './scaling'
|
|
5
|
+
|
|
6
|
+
export const debounce = (func, timeout = 300) => {
|
|
7
|
+
let timer
|
|
8
|
+
return (...args) => {
|
|
9
|
+
clearTimeout(timer)
|
|
10
|
+
timer = setTimeout(() => { func.apply(this, args) }, timeout)
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const copyStringToClipboard = str => {
|
|
15
|
+
var el = document.createElement('textarea')
|
|
16
|
+
el.value = str
|
|
17
|
+
el.setAttribute('readonly', '')
|
|
18
|
+
el.style = { position: 'absolute', left: '-9999px' }
|
|
19
|
+
document.body.appendChild(el)
|
|
20
|
+
el.select()
|
|
21
|
+
document.execCommand('copy')
|
|
22
|
+
document.body.removeChild(el)
|
|
23
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
import { SITEMAP } from '../data'
|
|
4
|
+
|
|
5
|
+
export const filterHrefs = () => {
|
|
6
|
+
return Object.values(SITEMAP)
|
|
7
|
+
.map(({ href, sub_links }) => // eslint-disable-line
|
|
8
|
+
[href, sub_links.map(({ href }) => href).filter(v => v.indexOf('#') === -1)].filter(v => v.length > 0)
|
|
9
|
+
)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const findCurrentIndex = () => { // eslint-disable-line
|
|
13
|
+
const { pathname } = window.location
|
|
14
|
+
const hrefArray = filterHrefs()
|
|
15
|
+
let index
|
|
16
|
+
|
|
17
|
+
hrefArray.map((v, k) => {
|
|
18
|
+
if (v[0] === pathname) index = k
|
|
19
|
+
if (v[1] && v[1].indexOf(pathname) > -1) {
|
|
20
|
+
const i = v[1].indexOf(pathname)
|
|
21
|
+
index = `${k}.${i}`
|
|
22
|
+
}
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
return { active: index }
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Parses sitemap and detects current, active, and previous pages from current url path */
|
|
29
|
+
export const setPaginationNames = () => {
|
|
30
|
+
const { pathname } = window.location
|
|
31
|
+
const hrefArray = filterHrefs()
|
|
32
|
+
const state = {}
|
|
33
|
+
|
|
34
|
+
hrefArray.map((v, k) => {
|
|
35
|
+
const categoryRoute = v[0]
|
|
36
|
+
const subcategoryRoutes = v[1]
|
|
37
|
+
const previousCat = SITEMAP[k - 1]
|
|
38
|
+
const activeCat = SITEMAP[k]
|
|
39
|
+
const nextCat = SITEMAP[k + 1]
|
|
40
|
+
|
|
41
|
+
const isRootCategory = categoryRoute === pathname
|
|
42
|
+
const isSubcategory = subcategoryRoutes && subcategoryRoutes.indexOf(pathname) > -1
|
|
43
|
+
|
|
44
|
+
if (isRootCategory) {
|
|
45
|
+
const previousSubcat = hrefArray[k - 1] && hrefArray[k - 1][1] && previousCat.sub_links[previousCat.sub_links.length - 1]
|
|
46
|
+
const nextSubcat = subcategoryRoutes && activeCat.sub_links[0]
|
|
47
|
+
|
|
48
|
+
state.previous = previousSubcat || previousCat
|
|
49
|
+
state.active = activeCat
|
|
50
|
+
state.next = nextSubcat || nextCat
|
|
51
|
+
state.moveOn = nextSubcat && nextCat
|
|
52
|
+
} else if (isSubcategory) {
|
|
53
|
+
const i = subcategoryRoutes.indexOf(pathname)
|
|
54
|
+
const activeSubcat = activeCat.sub_links[i]
|
|
55
|
+
const previousSubcat = subcategoryRoutes[i - 1] && activeCat.sub_links[i - 1]
|
|
56
|
+
const nextSubcat = subcategoryRoutes[i + 1] && activeCat.sub_links[i + 1]
|
|
57
|
+
|
|
58
|
+
state.previous = previousSubcat || activeCat
|
|
59
|
+
state.active = activeSubcat
|
|
60
|
+
state.next = nextSubcat || nextCat
|
|
61
|
+
state.moveOn = nextSubcat && nextCat
|
|
62
|
+
}
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
return state
|
|
66
|
+
}
|
package/src/scaling.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
import { generateSequence } from '@symbo.ls/scratch'
|
|
4
|
+
|
|
5
|
+
import { isObject, isArray } from '@domql/utils'
|
|
6
|
+
|
|
7
|
+
export const sortSequence = (a, b) => a.index - b.index
|
|
8
|
+
|
|
9
|
+
export const mapSequence = (state, sort) => {
|
|
10
|
+
generateSequence(state)
|
|
11
|
+
const { sequence } = state
|
|
12
|
+
if (sort) {
|
|
13
|
+
const values = Object.values(sequence)
|
|
14
|
+
return values.sort(sort)
|
|
15
|
+
}
|
|
16
|
+
return sequence
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const findClosestNumber = (number, arr) => {
|
|
20
|
+
return (isArray(arr) ? arr : Object.values(arr)).reduce((prev, curr) => {
|
|
21
|
+
return (Math.abs(curr - number) < Math.abs(prev - number) ? curr : prev)
|
|
22
|
+
})
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const findClosestNumberInFactory = (val, factory) => {
|
|
26
|
+
val = parseFloat(val)
|
|
27
|
+
if (isObject(factory)) factory = Object.values(factory)
|
|
28
|
+
return findClosestNumber(val, factory)
|
|
29
|
+
}
|