UNPKG

luna-data-grid

Version:

Grid for displaying datasets

154 lines (153 loc) 4.66 kB
import map from 'licia/map'; import trim from 'licia/trim'; import root from 'licia/root'; import html from 'licia/html'; import isNum from 'licia/isNum'; import contain from 'licia/contain'; import toNum from 'licia/toNum'; import detectOs from 'licia/detectOs'; import loadImg from 'licia/loadImg'; import isHidden from 'licia/isHidden'; import durationFormat from 'licia/durationFormat'; export function exportCjs(module, clazz) { try { module.exports = clazz; module.exports.default = clazz; } catch (e) { } } export function classPrefix(name) { const prefix = `luna-${name}-`; function processClass(str) { return map(trim(str).split(/\s+/), (singleClass) => { if (contain(singleClass, prefix)) { return singleClass; } return singleClass.replace(/[\w-]+/, (match) => `${prefix}${match}`); }).join(' '); } return function (str) { if (/<[^>]*>/g.test(str)) { try { const tree = html.parse(str); traverseTree(tree, (node) => { if (node.attrs && node.attrs.class) { node.attrs.class = processClass(node.attrs.class); } }); return html.stringify(tree); } catch (e) { return processClass(str); } } return processClass(str); }; } function traverseTree(tree, handler) { for (let i = 0, len = tree.length; i < len; i++) { const node = tree[i]; handler(node); if (node.content) { traverseTree(node.content, handler); } } } export const hasTouchSupport = 'ontouchstart' in root; export function eventClient(type, e) { const name = type === 'x' ? 'clientX' : 'clientY'; if (e[name]) { return e[name]; } if (e.changedTouches) { return e.changedTouches[0][name]; } return 0; } export function eventPage(type, e) { const name = type === 'x' ? 'pageX' : 'pageY'; if (e[name]) { return e[name]; } if (e.changedTouches) { return e.changedTouches[0][name]; } return 0; } let scrollbarWidth; export function measuredScrollbarWidth() { if (isNum(scrollbarWidth)) { return scrollbarWidth; } if (!document) { return 16; } const scrollDiv = document.createElement('div'); const innerDiv = document.createElement('div'); scrollDiv.setAttribute('style', 'display: block; width: 100px; height: 100px; overflow: scroll;'); innerDiv.setAttribute('style', 'height: 200px'); scrollDiv.appendChild(innerDiv); const container = document.body || document.documentElement; container.appendChild(scrollDiv); scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth; container.removeChild(scrollDiv); return scrollbarWidth; } export function hasVerticalScrollbar(el) { return el.scrollHeight > el.offsetHeight; } export function executeAfterTransition(el, callback) { if (isHidden(el)) { return callback(); } const handler = (e) => { const target = e.target; if (target !== el) { return; } el.removeEventListener('transitionend', handler); callback(); }; el.addEventListener('transitionend', handler); } export function pxToNum(str) { return toNum(str.replace('px', '')); } export function getPlatform() { const os = detectOs(); if (os === 'os x') { return 'mac'; } return os; } export function resetCanvasSize(canvas) { canvas.width = Math.round(canvas.offsetWidth * window.devicePixelRatio); canvas.height = Math.round(canvas.offsetHeight * window.devicePixelRatio); } export function loadImage(url) { return new Promise((resolve, reject) => { loadImg(url, function (err, img) { if (err) { return reject(err); } resolve(img); }); }); } export function hasSelection(node) { const selection = window.getSelection(); if (!selection || selection.type !== 'Range' || selection.toString() === '') { return false; } const { anchorNode, focusNode } = selection; return (selection.containsNode(node, true) || (anchorNode && node.contains(anchorNode)) || (focusNode && node.contains(focusNode))); } export function mediaDurationFormat(seconds) { if (seconds > 3600) { return durationFormat(Math.round(seconds * 1000), 'hh:mm:ss'); } return durationFormat(Math.round(seconds * 1000), 'mm:ss'); }