@fmdevui/fm-dev
Version:
Page level components developed based on Element Plus.
137 lines (134 loc) • 4.03 kB
JavaScript
import { nextTick } from 'vue';
import { useRouter } from 'vue-router';
import { pinia } from '../../stores/inpinia.mjs';
import { storeToRefs } from 'pinia';
import '../../stores/index.mjs';
import { i18n } from '../../locales/index.mjs';
import { Local } from '../storage/index.mjs';
import { verifyUrl } from '../comm/toolsValidate.mjs';
import { useThemeConfig } from '../../stores/themeConfig.mjs';
function useTitle() {
const stores = useThemeConfig(pinia);
const { themeConfig } = storeToRefs(stores);
const router = useRouter();
nextTick(() => {
let webTitle = "";
let globalTitle = themeConfig.value.globalTitle;
const { path, meta } = router.currentRoute.value;
if (path === "/login") {
webTitle = meta.title;
} else {
webTitle = setTagsViewNameI18n(router.currentRoute.value);
}
document.title = `${webTitle} - ${globalTitle}` || globalTitle;
});
}
function setTagsViewNameI18n(item) {
let tagsViewName = "";
const { query, params, meta } = item;
const pattern = /^\{("(zh-CN|en|zh-TW)":"[^,]+",?){1,3}}$/;
if (query?.tagsViewName || params?.tagsViewName) {
if (pattern.test(query?.tagsViewName) || pattern.test(params?.tagsViewName)) {
const urlTagsParams = query?.tagsViewName && JSON.parse(query?.tagsViewName) || params?.tagsViewName && JSON.parse(params?.tagsViewName);
tagsViewName = urlTagsParams[i18n.global.locale.value];
} else {
tagsViewName = query?.tagsViewName || params?.tagsViewName;
}
} else {
tagsViewName = i18n.global.t(`message.menu.${meta.title}`);
}
return tagsViewName;
}
const lazyImg = (el, arr) => {
const io = new IntersectionObserver((res) => {
res.forEach((v) => {
if (v.isIntersecting) {
const { img, key } = v.target.dataset;
v.target.src = img;
v.target.onload = () => {
io.unobserve(v.target);
arr[key]["loading"] = false;
};
}
});
});
nextTick(() => {
document.querySelectorAll(el).forEach((img) => io.observe(img));
});
};
const globalComponentSize = () => {
const stores = useThemeConfig(pinia);
const { themeConfig } = storeToRefs(stores);
return Local.get("themeConfig")?.globalComponentSize || themeConfig.value?.globalComponentSize;
};
function deepClone(obj) {
let newObj;
try {
newObj = obj.push ? [] : {};
} catch (error) {
newObj = {};
}
for (let attr in obj) {
if (obj[attr] && typeof obj[attr] === "object") {
newObj[attr] = deepClone(obj[attr]);
} else {
newObj[attr] = obj[attr];
}
}
return newObj;
}
function isMobile() {
if (navigator.userAgent.match(/('phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone')/i)) {
return true;
} else {
return false;
}
}
function handleEmpty(list) {
const arr = [];
for (const i in list) {
const d = [];
for (const j in list[i]) {
d.push(list[i][j]);
}
const leng = d.filter((item) => item === "").length;
if (leng !== d.length) {
arr.push(list[i]);
}
}
return arr;
}
function handleOpenLink(val) {
const { origin, pathname } = window.location;
const router = useRouter();
router.push(val.path);
if (verifyUrl(val.meta?.isLink)) window.open(val.meta?.isLink);
else window.open(`${origin}${pathname}#${val.meta?.isLink}`);
}
const other = {
useTitle: () => {
useTitle();
},
setTagsViewNameI18n(route) {
return setTagsViewNameI18n(route);
},
lazyImg: (el, arr) => {
lazyImg(el, arr);
},
globalComponentSize: () => {
return globalComponentSize();
},
deepClone: (obj) => {
return deepClone(obj);
},
isMobile: () => {
return isMobile();
},
handleEmpty: (list) => {
return handleEmpty(list);
},
handleOpenLink: (val) => {
handleOpenLink(val);
}
};
export { deepClone, other as default, globalComponentSize, handleEmpty, handleOpenLink, isMobile, lazyImg, setTagsViewNameI18n, useTitle };