UNPKG

ivue-material-plus

Version:

A high quality UI components Library with Vue.js

287 lines (282 loc) 7.32 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var vue = require('vue'); var validate = require('./validate.js'); function readFileContent(file, resultType) { return new Promise((resolve) => { if (resultType === "file") { resolve(); return; } if (resultType === "url") { const url = URL.createObjectURL(file); resolve(url); return; } const reader = new FileReader(); reader.onload = (event) => { resolve(event.target.result); }; if (resultType === "dataUrl") { reader.readAsDataURL(file); } else if (resultType === "text") { reader.readAsText(file); } }); } function isOversize(items, maxSize) { return toArray(items).some((item) => { if (item.file) { if (isFunction(maxSize)) { return maxSize(item.file); } return item.file.size > maxSize; } return false; }); } function toArray(item) { if (Array.isArray(item)) { return item; } return [item]; } function isFunction(val) { return typeof val === "function"; } function filterFiles(items, maxSize) { const valid = []; const invalid = []; items.forEach((item) => { if (isOversize(item, maxSize)) { invalid.push(item); } else { valid.push(item); } }); return { valid, invalid }; } const getRandomStr = (len = 32) => { const $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; const length = $chars.length; let str = ""; for (let i = 0; i < len; i++) { str += $chars.charAt(Math.floor(Math.random() * length)); } return str; }; const IMAGE_REGEXP = /\.(jpeg|jpg|gif|png|svg|webp|jfif|bmp|dpg)/i; function isImageUrl(url) { return IMAGE_REGEXP.test(url); } function isImageFile(item) { if (item.isImage) { return true; } if (item.file && item.file.type) { return item.file.type.indexOf("image") === 0; } if (item.url) { return isImageUrl(item.url); } if (typeof item.content === "string") { return item.content.indexOf("data:image") === 0; } return false; } function addUnit(value) { if (!validate.isDef(value)) { return void 0; } return validate.isNumeric(value) ? `${value}px` : String(value); } function getSizeStyle(originSize) { if (validate.isDef(originSize)) { const size = addUnit(originSize); return { width: size, height: size }; } } const EVENT_CODE = { tab: "Tab", enter: "Enter", space: "Space", left: "ArrowLeft", up: "ArrowUp", right: "ArrowRight", down: "ArrowDown", esc: "Escape", delete: "Delete", backspace: "Backspace" }; function createRange(length) { return Array.from({ length }, (v, k) => k); } function isCssColor(color) { return !!color && !!color.match(/^(#|(rgb|hsl)a?\()/); } function setTextColor(color) { let style = {}; if (Array.isArray(color)) { style = { color: `${color[0]}` }; } else if (isCssColor(color)) { style = { color: `${color}` }; } else if (color) { const [colorName] = color.toString().trim().split(" ", 2); style = { color: `${colorName}--text` }; } return style; } function isValueNumber(value) { return /^[0-9][0-9]*$/.test(value + ""); } let cached; function getScrollBarSize(fresh) { if (!isClient) { return 0; } if (fresh || cached === void 0) { const inner = document.createElement("div"); inner.style.width = "100%"; inner.style.height = "200px"; const outer = document.createElement("div"); const outerStyle = outer.style; outerStyle.position = "absolute"; outerStyle.top = "0px"; outerStyle.left = "0px"; outerStyle.pointerEvents = "none"; outerStyle.visibility = "hidden"; outerStyle.width = "200px"; outerStyle.height = "150px"; outerStyle.overflow = "hidden"; outer.appendChild(inner); document.body.appendChild(outer); const widthContained = inner.offsetWidth; outer.style.overflow = "scroll"; let widthScroll = inner.offsetWidth; if (widthContained === widthScroll) { widthScroll = outer.clientWidth; } document.body.removeChild(outer); cached = widthContained - widthScroll; } return cached; } const zIndex = vue.ref(0); const useZIndex = () => { const currentZIndex = vue.computed(() => 2e3 + zIndex.value); const nextZIndex = () => { zIndex.value++; return currentZIndex.value; }; return { currentZIndex, nextZIndex }; }; const isClient = typeof window !== "undefined"; const isObject = (obj) => { return obj !== null && typeof obj === "object"; }; const isElement = (el) => { return typeof HTMLElement === "object" && el instanceof HTMLElement; }; async function downloadFile(url, name = "") { if (!isClient) { return Promise.reject(); } try { const res = await fetch(url); const blob = await res.blob(); const split = res.url.split("/"); const fileName = split[split.length - 1]; if (!blob) { return Promise.reject(); } const localUrl = URL.createObjectURL(blob); const a = document.createElement("a"); a.setAttribute("href", localUrl); a.setAttribute("download", name || fileName); a.click(); URL.revokeObjectURL(localUrl); return Promise.resolve(); } catch (e) { return Promise.reject(e); } } function raf(fn) { return isClient ? requestAnimationFrame(fn) : -1; } function cancelRaf(id) { if (isClient) { cancelAnimationFrame(id); } } function doubleRaf(fn) { raf(() => raf(fn)); } const isUndefined = (val) => val === void 0; function findComponentsUpward(context, componentName) { const parents = []; const parent = context.$parent; if (parent) { if (parent.$options.name === componentName) { parents.push(parent); } return parents.concat(findComponentsUpward(parent, componentName)); } else { return []; } } function findComponentUpward(context, componentName, componentNames) { if (typeof componentName === "string") { componentNames = [componentName]; } else { componentNames = componentName; } let parent = context.$parent; let name = parent.$options.name; while (parent && (!name || componentNames.indexOf(name) < 0)) { parent = parent.$parent; if (parent) { name = parent.$options.name; } } return parent; } exports.EVENT_CODE = EVENT_CODE; exports.addUnit = addUnit; exports.cancelRaf = cancelRaf; exports.createRange = createRange; exports.doubleRaf = doubleRaf; exports.downloadFile = downloadFile; exports.filterFiles = filterFiles; exports.findComponentUpward = findComponentUpward; exports.findComponentsUpward = findComponentsUpward; exports.getRandomStr = getRandomStr; exports.getScrollBarSize = getScrollBarSize; exports.getSizeStyle = getSizeStyle; exports.isClient = isClient; exports.isCssColor = isCssColor; exports.isElement = isElement; exports.isFunction = isFunction; exports.isImageFile = isImageFile; exports.isImageUrl = isImageUrl; exports.isObject = isObject; exports.isOversize = isOversize; exports.isUndefined = isUndefined; exports.isValueNumber = isValueNumber; exports.raf = raf; exports.readFileContent = readFileContent; exports.setTextColor = setTextColor; exports.toArray = toArray; exports.useZIndex = useZIndex; //# sourceMappingURL=helpers.js.map