ivue-material-plus
Version:
A high quality UI components Library with Vue.js
257 lines (254 loc) • 6.64 kB
JavaScript
import { ref, computed } from 'vue';
import { isDef, isNumeric } from './validate.mjs';
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 (!isDef(value)) {
return void 0;
}
return isNumeric(value) ? `${value}px` : String(value);
}
function getSizeStyle(originSize) {
if (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 = ref(0);
const useZIndex = () => {
const currentZIndex = 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;
}
export { EVENT_CODE, addUnit, cancelRaf, createRange, doubleRaf, downloadFile, filterFiles, findComponentUpward, findComponentsUpward, getRandomStr, getScrollBarSize, getSizeStyle, isClient, isCssColor, isElement, isFunction, isImageFile, isImageUrl, isObject, isOversize, isUndefined, isValueNumber, raf, readFileContent, setTextColor, toArray, useZIndex };
//# sourceMappingURL=helpers.mjs.map