@delon/util
Version:
Universal toolset of ng-alain.
272 lines (264 loc) • 8.02 kB
JavaScript
import { Platform } from '@angular/cdk/platform';
import { DOCUMENT } from '@angular/common';
import * as i0 from '@angular/core';
import { inject, Injectable } from '@angular/core';
/**
* A set of simple Cookie manipulation classes.
*
* 一组简单的 Cookie 操作类。
*/
class CookieService {
_doc = inject(DOCUMENT);
platform = inject(Platform);
get doc() {
return this._doc || document;
}
/**
* Original cookie value
*
* 原始Cookie值
*/
get cookie() {
return this.platform.isBrowser ? this.doc.cookie : '';
}
/**
* Get all cookie key-value pairs
*
* 获取所有Cookie键值对
*/
getAll() {
const ret = {};
const arr = this.cookie.split('; ');
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let i = 0; i < arr.length; i++) {
const cookie = arr[i];
const index = cookie.indexOf('=');
if (index > 0) {
const name = decodeURIComponent(cookie.substring(0, index));
if (ret[name] == null) {
ret[name] = decodeURIComponent(cookie.substring(index + 1));
}
}
}
return ret;
}
/**
* Get the value of given cookie `key`
*
* 获取指定 `key` 的值
*/
get(key) {
return this.getAll()[key];
}
/**
* Sets a value for given cookie key
*
* 设置指定 Cookie 键的值
*/
put(key, value, options) {
if (!this.platform.isBrowser) {
return;
}
const opt = { path: '/', ...options };
if (typeof opt.expires === 'number') {
opt.expires = new Date(+new Date() + opt.expires * 1e3);
}
if (typeof opt.expires !== 'string') {
opt.expires = opt.expires ? opt.expires.toUTCString() : '';
}
const optStr = opt;
const attributes = Object.keys(optStr)
.filter(k => optStr[k] && optStr[k] !== true)
.map(k => `${k}=${optStr[k].split(';')[0]}`)
.join(';');
const keyValue = `${encodeURIComponent(String(key))}=${encodeURIComponent(String(value))}`;
this.doc.cookie = `${keyValue}${attributes ? `; ${attributes}` : ''}`;
}
/**
* Remove given cookie
*
* 移除指定 Cookie
*/
remove(key, options) {
this.put(key, '', options);
}
/**
* Remove all cookies
*
* 移除所有 Cookies
*/
removeAll() {
this.doc.cookie = '';
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.2", ngImport: i0, type: CookieService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.1.2", ngImport: i0, type: CookieService, providedIn: 'root' });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.2", ngImport: i0, type: CookieService, decorators: [{
type: Injectable,
args: [{ providedIn: 'root' }]
}] });
/**
* Copy text to clipboard
*
* 复制字符串文档至剪贴板
*/
function copy(value) {
return new Promise((resolve) => {
let copyTextArea = null;
try {
copyTextArea = document.createElement('textarea');
copyTextArea.style.height = '0px';
copyTextArea.style.opacity = '0';
copyTextArea.style.width = '0px';
document.body.appendChild(copyTextArea);
copyTextArea.value = value;
copyTextArea.select();
// eslint-disable-next-line @typescript-eslint/no-deprecated
document.execCommand('copy');
resolve(value);
}
finally {
if (copyTextArea && copyTextArea.parentNode) {
copyTextArea.parentNode.removeChild(copyTextArea);
}
}
});
}
/**
* Used to verify `<ng-content />` is empty, useful for custom components.
*
* 用于校验 `<ng-content />` 是否为空,自定义组件时蛮有用。
*/
function isEmpty(element) {
const nodes = element.childNodes;
for (let i = 0; i < nodes.length; i++) {
const node = nodes.item(i);
if (node.nodeType === 1 && node.outerHTML.toString().trim().length !== 0) {
return false;
}
else if (node.nodeType === 3 && node.textContent.toString().trim().length !== 0) {
return false;
}
}
return true;
}
class ScrollService {
_doc = inject(DOCUMENT);
platform = inject(Platform);
_getDoc() {
return this._doc || document;
}
_getWin() {
const doc = this._getDoc();
return doc.defaultView || window;
}
/**
* 获取滚动条位置
*
* @param element 指定元素,默认 `window`
*/
getScrollPosition(element) {
if (!this.platform.isBrowser) {
return [0, 0];
}
const win = this._getWin();
if (element && element !== win) {
return [element.scrollLeft, element.scrollTop];
}
else {
return [win.scrollX, win.scrollY];
}
}
/**
* 设置滚动条位置
*
* @param element 指定元素
*/
scrollToPosition(element, position) {
if (!this.platform.isBrowser) {
return;
}
(element || this._getWin()).scrollTo(position[0], position[1]);
}
/**
* 设置滚动条至指定元素
*
* @param element 指定元素,默认 `document.body`
* @param topOffset 偏移值,默认 `0`
*/
scrollToElement(element, topOffset = 0) {
if (!this.platform.isBrowser) {
return;
}
if (!element) {
element = this._getDoc().body;
}
element.scrollIntoView();
const win = this._getWin();
if (win && win.scrollBy) {
win.scrollBy(0, element.getBoundingClientRect().top - topOffset);
if (win.scrollY < 20) {
win.scrollBy(0, -win.scrollY);
}
}
}
/**
* 滚动至顶部
*
* @param topOffset 偏移值,默认 `0`
*/
scrollToTop(topOffset = 0) {
if (!this.platform.isBrowser) {
return;
}
this.scrollToElement(this._getDoc().body, topOffset);
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.2", ngImport: i0, type: ScrollService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.1.2", ngImport: i0, type: ScrollService, providedIn: 'root' });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.2", ngImport: i0, type: ScrollService, decorators: [{
type: Injectable,
args: [{ providedIn: 'root' }]
}] });
function removeClass(el, classMap, renderer) {
Object.keys(classMap).forEach(key => renderer.removeClass(el, key));
}
function addClass(el, classMap, renderer) {
for (const i in classMap) {
if (classMap[i]) {
renderer.addClass(el, i);
}
}
}
/**
* Update host component style `class`
*
* 更新宿主组件样式 `class`
*
* ```ts
* updateHostClass(
* this.el.nativeElement,
* this.renderer,
* {
* [ 'classname' ]: true,
* [ 'classname' ]: this.type === '1',
* [ this.cls ]: true,
* [ `a-${this.cls}` ]: true
* })
* ```
*/
function updateHostClass(el, renderer, classMap, preClean = false) {
if (preClean === true) {
renderer.removeAttribute(el, 'class');
}
else {
removeClass(el, classMap, renderer);
}
classMap = { ...classMap };
addClass(el, classMap, renderer);
}
/**
* Generated bundle index. Do not edit.
*/
export { CookieService, ScrollService, copy, isEmpty, updateHostClass };
//# sourceMappingURL=browser.mjs.map