Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/jacobsun/edidor.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/js/helpers.js')
-rw-r--r--src/js/helpers.js124
1 files changed, 124 insertions, 0 deletions
diff --git a/src/js/helpers.js b/src/js/helpers.js
new file mode 100644
index 0000000..1152d24
--- /dev/null
+++ b/src/js/helpers.js
@@ -0,0 +1,124 @@
+// dom helper functions
+const throttle = require('lodash.throttle')
+export function qs (sel, parent = document.body) {
+ if (typeof parent === 'string') {
+ parent = document.querySelector(parent)
+ }
+ return parent.querySelector(sel)
+}
+
+export function elt (name, attr = {}, ...text) {
+ const node = document.createElement(name)
+ Object.keys(attr).forEach(k => {
+ node.setAttribute(k, attr[k])
+ })
+ text.forEach(t => {
+ if (typeof t === 'string') {
+ t = document.createTextNode(t)
+ }
+ node.appendChild(t)
+ })
+ return node
+}
+
+export function removeClass (dom, cls) {
+ if (typeof dom === 'string') {
+ dom = qs(dom)
+ }
+ dom.classList.remove(cls)
+ return dom
+}
+export function clearClass (dom, cls) {
+ if (typeof dom === 'string') {
+ dom = qs(dom)
+ }
+ dom.className = ''
+ return dom
+}
+export function addClass (dom, cls) {
+ if (typeof dom === 'string') {
+ dom = qs(dom)
+ }
+ dom.classList.add(cls)
+ return dom
+}
+export function toggleClass (dom, cls) {
+ if (typeof dom === 'string') {
+ dom = qs(dom)
+ }
+ dom.classList.toggle(cls)
+ return dom
+}
+export function hasClass (dom, cls) {
+ if (typeof dom === 'string') {
+ dom = qs(dom)
+ }
+ return dom.classList.contains(cls)
+}
+export function setStyle (dom, styles) {
+ if (typeof dom === 'string') {
+ dom = qs(dom)
+ }
+
+ Object.keys(styles).forEach(key => {
+ dom.style[key] = styles[key]
+ })
+}
+export function onEvent (event, cb, limit = 0, dom = document.body) {
+ if (typeof dom === 'string') {
+ dom = qs(dom)
+ }
+ if (limit > 0) {
+ dom.addEventListener(event, throttle(cb, limit))
+ } else {
+ dom.addEventListener(event, cb)
+ }
+}
+// mousemove and touchmove abstraction
+export function onPointerMove (cb, limit, dom = document.body) {
+ onEvent('mousemove', e => {
+ cb({ // eslint-disable-line
+ clientX: e.clientX,
+ clientY: e.clientY,
+ type: e.type
+ })
+ }, limit, dom)
+
+ onEvent('touchmove', e => {
+ cb({ // eslint-disable-line
+ clientX: e.touches[0].clientX,
+ clientY: e.touches[0].clientY,
+ type: e.type
+ })
+ }, limit, dom)
+}
+// local storage
+export const ls = {}
+ls.get = key => window.localStorage.getItem(key)
+
+ls.set = (key, value) => {
+ if (value === null || value === undefined) {
+ value = ''
+ } else if (typeof value !== 'string') {
+ value = JSON.stringify(value)
+ }
+ window.localStorage.setItem(key, value)
+}
+export function randomInt (min, max) {
+ min = Math.ceil(min)
+ max = Math.floor(max)
+ return Math.floor(Math.random() * (max - min)) + min
+}
+
+export function download (filename, text) {
+ const a = elt('a', {
+ href: 'data:text/plain;charset=utf-8,' + encodeURIComponent(text),
+ download: filename,
+ style: {
+ display: 'none'
+ }
+ })
+ document.body.appendChild(a)
+ a.click()
+ document.body.removeChild(a)
+}