UNPKG

yg-tools

Version:

some convenient APIs and Methods for Youngon

203 lines (202 loc) 9.36 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); var WebContext = /** @class */ (function () { function WebContext(options) { var _this = this; if (options) { Object.keys(options).forEach(function (key) { // @ts-ignore _this[key] = typeof options[key] === 'function' ? options[key].bind(_this) : options[key]; }); } } WebContext.createWebContext = function (options) { return new WebContext(options); }; WebContext.prototype.obj2query = function (obj) { return (typeof Object.entries === 'function' ? Object.entries(obj) : Object.keys(obj).map(function (k) { return [k, obj[k]]; })) .map(function (_a) { var k = _a[0], v = _a[1]; return k + "=" + v; }) .join('&'); }; WebContext.prototype.request = function (options) { return __awaiter(this, void 0, void 0, function () { var url, data, method, otherOpts, handledUrl, handledOptions, querys, rs; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!fetch) { console.error('[yg-tools WebContext error] fetch is not defined'); } url = options.url, data = options.data, method = options.method, otherOpts = __rest(options, ["url", "data", "method"]); handledUrl = url; handledOptions = __assign(__assign({}, otherOpts), { method: method, mode: 'cors' }); if (method === 'GET' && data && typeof data === 'object') { querys = this.obj2query(data); handledUrl = "" + url + (url.includes('?') ? '&' : '?') + querys; } if (method !== 'GET' && data) { handledOptions.body = JSON.stringify(data); } return [4 /*yield*/, fetch(handledUrl, handledOptions)]; case 1: rs = _a.sent(); return [4 /*yield*/, rs.json()]; case 2: return [2 /*return*/, _a.sent()]; } }); }); }; WebContext.prototype.showToast = function (options) { var global = window; var title = options.title, duration = options.duration, _a = options.location, location = _a === void 0 ? 'bottom' : _a; if (global) { var offset = global.__toast__item__ && global.__toast__item__ > 0 ? global.__toast__item__ : 0; var vlocation = location === 'bottom' ? 'bottom: ' + (10 + offset * 7) + '%;' : location === 'top' ? 'top: ' + (10 + offset * 7) + '%;' : 'top: ' + (50 + offset * 7) + '%;'; var toastElement_1 = global.document.createElement('div'); toastElement_1.innerText = title; toastElement_1.style.cssText = vlocation + "\n ;\n width: fit-content;\n max-width:80%;\n min-width:150px;\n padding:0 14px;\n height: 40px;\n color: rgb(255, 255, 255);\n line-height: 40px;\n text-align: center;\n border-radius: 4px;\n position: fixed;\n left: 50%;\n transform: translate(-50%, -50%);\n z-index: 999999;\n background: rgba(0, 0, 0, 0.75);\n font-size: 16px;\n "; global.document.body.appendChild(toastElement_1); global.__toast__item__ = (global.__toast__item__ || 0) + 1; setTimeout(function () { var delay = 0.5; toastElement_1.style.transition = 'transform ' + delay + 's ease-in, opacity ' + delay + 's ease-in'; toastElement_1.style.opacity = '0'; setTimeout(function () { global.document.body.removeChild(toastElement_1); global.__toast__item__ = (global.__toast__item__ || 0) - 1; }, delay * 100); }, duration); } }; WebContext.prototype.login = function () { return { code: Math.random() }; }; WebContext.prototype.switchTab = function (_a) { var url = _a.url; if (location) { location.href = url; } }; WebContext.prototype.getStorageSync = function (key) { var ls = localStorage; if (ls) { var v = ls.getItem(key); if (!v) { return; } if (v.indexOf('obj-') === 0) { v = v.slice(4); return JSON.parse(v); } else if (v.indexOf('str-') === 0) { return v.slice(4); } } }; WebContext.prototype.setStorageSync = function (key, value) { if (arguments.length === 2) { var v = value; if (typeof v === 'object') { v = JSON.stringify(v); v = 'obj-' + v; } else { v = 'str-' + v; } var ls = localStorage; if (ls) { ls.setItem(key, v); } } }; WebContext.prototype.removeStorageSync = function (key) { var ls = localStorage; if (ls && key) { ls.removeItem(key); } }; WebContext.prototype.clearStorageSync = function () { var ls = localStorage; if (ls) { ls.clear(); } }; return WebContext; }()); exports.default = WebContext;