var baseUrl = "/wcs";
|
|
var WCS_I18N = (function (window, document) {
|
"use strict";
|
|
var STORAGE_KEY = "wcs_lang";
|
var COOKIE_KEY = "wcs_lang";
|
var I18N_PATH = "/i18n/messages";
|
var TRANSLATABLE_RE = /[\u3400-\u9fff]/;
|
var state = {
|
ready: false,
|
loading: false,
|
locale: null,
|
defaultLocale: "zh-CN",
|
supportedLocales: ["zh-CN", "en-US"],
|
localeOptions: [],
|
messages: {},
|
legacy: {},
|
legacyEntries: [],
|
legacyRegexEntries: [],
|
builtinRegexEntries: [],
|
translateCache: {},
|
translateCacheSize: 0,
|
callbacks: [],
|
observer: null,
|
bridgeAttempts: 0,
|
applying: false,
|
pendingNodes: [],
|
flushTimer: 0
|
};
|
|
patchXmlHttpRequest();
|
patchFetch();
|
scheduleBridgeWrap();
|
|
if (document && document.documentElement) {
|
document.documentElement.lang = getLocale();
|
}
|
|
if (document.readyState === "loading") {
|
document.addEventListener("DOMContentLoaded", function () {
|
load();
|
});
|
} else {
|
load();
|
}
|
|
function normalizeLocale(locale) {
|
var value = (locale || "").replace(/_/g, "-").trim();
|
if (!value) {
|
return state.defaultLocale;
|
}
|
if (/^en/i.test(value)) {
|
return "en-US";
|
}
|
if (/^zh/i.test(value)) {
|
return "zh-CN";
|
}
|
return value;
|
}
|
|
function getBasePath() {
|
return typeof baseUrl === "string" && baseUrl ? baseUrl : "";
|
}
|
|
function isExternalUrl(url) {
|
return /^(?:[a-z]+:)?\/\//i.test(url || "");
|
}
|
|
function appendNonceUrl(url) {
|
var value = (url || "").trim();
|
if (!value || isExternalUrl(value) || value.charAt(0) === "<") {
|
return url;
|
}
|
if (/[?&]_wcsI18nNonce=/.test(value)) {
|
return value;
|
}
|
return value + (value.indexOf("?") === -1 ? "?" : "&") + "_wcsI18nNonce=" + new Date().getTime();
|
}
|
|
function getCookie(name) {
|
var pattern = new RegExp("(?:^|; )" + name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + "=([^;]*)");
|
var matches = document.cookie.match(pattern);
|
return matches ? decodeURIComponent(matches[1]) : "";
|
}
|
|
function setCookie(name, value) {
|
var path = getBasePath() || "/";
|
document.cookie = name + "=" + encodeURIComponent(value) + "; path=" + path + "; max-age=31536000";
|
}
|
|
function getStoredLocale() {
|
var locale = "";
|
try {
|
locale = window.localStorage ? window.localStorage.getItem(STORAGE_KEY) : "";
|
} catch (e) {
|
locale = "";
|
}
|
if (!locale) {
|
locale = getCookie(COOKIE_KEY);
|
}
|
if (!locale && window.navigator) {
|
locale = window.navigator.language || (window.navigator.languages && window.navigator.languages[0]) || "";
|
}
|
return normalizeLocale(locale || state.defaultLocale);
|
}
|
|
function setStoredLocale(locale) {
|
var normalized = normalizeLocale(locale);
|
try {
|
if (window.localStorage) {
|
window.localStorage.setItem(STORAGE_KEY, normalized);
|
}
|
} catch (e) {
|
}
|
setCookie(COOKIE_KEY, normalized);
|
}
|
|
function getLocale() {
|
if (!state.locale) {
|
state.locale = getStoredLocale();
|
}
|
return state.locale;
|
}
|
|
function setLocale(locale, reload) {
|
var normalized = normalizeLocale(locale);
|
state.locale = normalized;
|
setStoredLocale(normalized);
|
if (document && document.documentElement) {
|
document.documentElement.lang = normalized;
|
}
|
if (reload === false) {
|
state.ready = false;
|
load();
|
return;
|
}
|
window.location.reload();
|
}
|
|
function onReady(callback) {
|
if (typeof callback !== "function") {
|
return;
|
}
|
if (state.ready) {
|
callback(api);
|
return;
|
}
|
state.callbacks.push(callback);
|
}
|
|
function flushCallbacks() {
|
var queue = state.callbacks.slice();
|
var i;
|
state.callbacks = [];
|
for (i = 0; i < queue.length; i++) {
|
try {
|
queue[i](api);
|
} catch (e) {
|
}
|
}
|
}
|
|
function parseJson(text) {
|
try {
|
return JSON.parse(text);
|
} catch (e) {
|
return null;
|
}
|
}
|
|
function request(url, success, failure) {
|
var xhr = new XMLHttpRequest();
|
xhr.open("GET", url, true);
|
xhr.setRequestHeader("X-Lang", getLocale());
|
xhr.onreadystatechange = function () {
|
if (xhr.readyState !== 4) {
|
return;
|
}
|
if (xhr.status >= 200 && xhr.status < 300) {
|
success(parseJson(xhr.responseText) || {});
|
} else if (typeof failure === "function") {
|
failure(xhr);
|
}
|
};
|
xhr.send(null);
|
}
|
|
function load(callback) {
|
if (typeof callback === "function") {
|
onReady(callback);
|
}
|
if (state.loading || state.ready) {
|
return;
|
}
|
state.loading = true;
|
request(getBasePath() + I18N_PATH + "?lang=" + encodeURIComponent(getLocale()) + "&_t=" + new Date().getTime(), function (res) {
|
var data = res && res.code === 200 ? (res.data || {}) : {};
|
state.loading = false;
|
state.ready = true;
|
state.locale = normalizeLocale(data.locale || getLocale());
|
state.defaultLocale = normalizeLocale(data.defaultLocale || state.defaultLocale);
|
state.supportedLocales = readLocaleTags(data.supportedLocales);
|
state.localeOptions = readLocaleOptions(data.supportedLocales);
|
state.messages = data.messages || {};
|
state.legacy = data.legacy || {};
|
state.legacyRegexEntries = readLegacyRegexEntries(state.legacy);
|
state.legacyEntries = sortedLegacyEntries(state.legacy);
|
state.builtinRegexEntries = buildBuiltinRegexEntries();
|
resetTranslateCache();
|
if (document && document.documentElement) {
|
document.documentElement.lang = state.locale;
|
}
|
wrapLibraries();
|
observeDom();
|
apply(document.body || document.documentElement);
|
if (document.title) {
|
document.title = translate(document.title);
|
}
|
flushCallbacks();
|
}, function () {
|
state.loading = false;
|
state.ready = true;
|
state.locale = getLocale();
|
state.builtinRegexEntries = buildBuiltinRegexEntries();
|
resetTranslateCache();
|
wrapLibraries();
|
flushCallbacks();
|
});
|
}
|
|
function readLocaleTags(options) {
|
var tags = [];
|
var i;
|
if (!options || !options.length) {
|
return state.supportedLocales.slice();
|
}
|
for (i = 0; i < options.length; i++) {
|
if (typeof options[i] === "string") {
|
tags.push(normalizeLocale(options[i]));
|
} else if (options[i] && options[i].tag) {
|
tags.push(normalizeLocale(options[i].tag));
|
}
|
}
|
return tags.length ? tags : state.supportedLocales.slice();
|
}
|
|
function readLocaleOptions(options) {
|
var list = [];
|
var i;
|
if (!options || !options.length) {
|
options = state.supportedLocales.slice();
|
}
|
for (i = 0; i < options.length; i++) {
|
if (typeof options[i] === "string") {
|
list.push({ tag: normalizeLocale(options[i]), label: options[i] });
|
} else if (options[i] && options[i].tag) {
|
list.push({
|
tag: normalizeLocale(options[i].tag),
|
label: options[i].label || options[i].tag
|
});
|
}
|
}
|
return list;
|
}
|
|
function sortedLegacyEntries(map) {
|
var entries = [];
|
var key;
|
for (key in map) {
|
if (map.hasOwnProperty(key) && key.indexOf("regex:") !== 0) {
|
entries.push([key, map[key]]);
|
}
|
}
|
entries.sort(function (left, right) {
|
return right[0].length - left[0].length;
|
});
|
return entries;
|
}
|
|
function readLegacyRegexEntries(map) {
|
var entries = [];
|
var key;
|
var source;
|
for (key in map) {
|
if (!map.hasOwnProperty(key) || key.indexOf("regex:") !== 0) {
|
continue;
|
}
|
source = key.substring(6);
|
if (!source) {
|
continue;
|
}
|
if (isBrokenRegexEntry(source, map[key])) {
|
continue;
|
}
|
try {
|
entries.push({
|
source: source,
|
regex: new RegExp(source),
|
value: map[key]
|
});
|
} catch (e) {
|
}
|
}
|
entries.sort(function (left, right) {
|
return right.source.length - left.source.length;
|
});
|
return entries;
|
}
|
|
function isBrokenRegexEntry(source, value) {
|
if (!source || !value) {
|
return false;
|
}
|
if (value.indexOf("$=") >= 0) {
|
return true;
|
}
|
return /\\s\*|\\d\+|\\\||\\:|\(\?:/.test(value);
|
}
|
|
function format(template, params) {
|
var result = template || "";
|
var index;
|
if (!params) {
|
return result;
|
}
|
if (Object.prototype.toString.call(params) === "[object Array]") {
|
for (index = 0; index < params.length; index++) {
|
result = result.replace(new RegExp("\\{" + index + "\\}", "g"), params[index]);
|
}
|
return result;
|
}
|
for (index in params) {
|
if (params.hasOwnProperty(index)) {
|
result = result.replace(new RegExp("\\{" + index + "\\}", "g"), params[index]);
|
}
|
}
|
return result;
|
}
|
|
function resetTranslateCache() {
|
state.translateCache = {};
|
state.translateCacheSize = 0;
|
}
|
|
function rememberTranslateCache(text, translated) {
|
if (state.translateCacheSize > 4000) {
|
resetTranslateCache();
|
}
|
if (!Object.prototype.hasOwnProperty.call(state.translateCache, text)) {
|
state.translateCacheSize += 1;
|
}
|
state.translateCache[text] = translated;
|
return translated;
|
}
|
|
function canTranslateText(text) {
|
return !!(text && TRANSLATABLE_RE.test(text));
|
}
|
|
function buildBuiltinRegexEntries() {
|
return [
|
{
|
regex: /^圈(\d+)\s*\|\s*站点:\s*(\d+)\s*\|\s*(?:任务|Task):\s*(\d+)\s*\|\s*承载:\s*([\d.]+%)$/,
|
key: "legacy.regex.loopStatus",
|
fallback: "Zone {0} | Stations: {1} | Tasks: {2} | Load: {3}"
|
},
|
{
|
regex: /^点击(?:删除|Delete)关联:\s*站点\s*(\d+)\s*->\s*设备\s*(\d+)$/,
|
key: "legacy.regex.stationDeviceLink",
|
fallback: "Click to remove mapping: Station {0} -> Device {1}"
|
}
|
];
|
}
|
|
function formatBuiltinRegex(entry, args) {
|
var value = t(entry.key, args);
|
if (value === entry.key && entry.fallback) {
|
return format(entry.fallback, args);
|
}
|
return value;
|
}
|
|
function t(key, params) {
|
var value = state.messages[key];
|
if (!value) {
|
return key;
|
}
|
return format(value, params);
|
}
|
|
function preserveSpaces(original, translated) {
|
var match = original.match(/^(\s*)([\s\S]*?)(\s*)$/);
|
if (!match) {
|
return translated;
|
}
|
return match[1] + translated + match[3];
|
}
|
|
function exactLegacy(text) {
|
var trimmed = (text || "").trim();
|
var translated = state.legacy[trimmed];
|
if (!translated) {
|
return text;
|
}
|
return preserveSpaces(text, translated);
|
}
|
|
function fragmentLegacy(text) {
|
var result = text;
|
var i;
|
var entry;
|
for (i = 0; i < state.legacyEntries.length; i++) {
|
entry = state.legacyEntries[i];
|
if (!entry[0] || entry[0].length < 2 || entry[0] === entry[1]) {
|
continue;
|
}
|
result = result.split(entry[0]).join(entry[1]);
|
}
|
return result;
|
}
|
|
function regexLegacy(text) {
|
var trimmed = (text || "").trim();
|
var i;
|
var entry;
|
var translated;
|
for (i = 0; i < state.legacyRegexEntries.length; i++) {
|
entry = state.legacyRegexEntries[i];
|
if (!entry || !entry.regex) {
|
continue;
|
}
|
if (trimmed) {
|
translated = trimmed.replace(entry.regex, entry.value);
|
if (translated !== trimmed) {
|
return preserveSpaces(text, translated);
|
}
|
}
|
translated = text.replace(entry.regex, entry.value);
|
if (translated !== text) {
|
return translated;
|
}
|
}
|
for (i = 0; i < state.builtinRegexEntries.length; i++) {
|
entry = state.builtinRegexEntries[i];
|
if (!entry || !entry.regex) {
|
continue;
|
}
|
if (trimmed) {
|
translated = trimmed.replace(entry.regex, function () {
|
return formatBuiltinRegex(entry, Array.prototype.slice.call(arguments, 1, -2));
|
});
|
if (translated !== trimmed) {
|
return preserveSpaces(text, translated);
|
}
|
}
|
translated = text.replace(entry.regex, function () {
|
return formatBuiltinRegex(entry, Array.prototype.slice.call(arguments, 1, -2));
|
});
|
if (translated !== text) {
|
return translated;
|
}
|
}
|
return text;
|
}
|
|
function translate(text) {
|
if (!text || !state.ready || state.locale === state.defaultLocale) {
|
return text;
|
}
|
if (Object.prototype.hasOwnProperty.call(state.translateCache, text)) {
|
return state.translateCache[text];
|
}
|
if (!canTranslateText(text)) {
|
return rememberTranslateCache(text, text);
|
}
|
var exact = exactLegacy(text);
|
if (exact !== text) {
|
return rememberTranslateCache(text, exact);
|
}
|
var regex = regexLegacy(text);
|
if (regex !== text) {
|
return rememberTranslateCache(text, regex);
|
}
|
return rememberTranslateCache(text, fragmentLegacy(text));
|
}
|
|
function translateAttribute(element, attrName) {
|
var value = element.getAttribute(attrName);
|
var translated = translate(value);
|
if (translated && translated !== value) {
|
element.setAttribute(attrName, translated);
|
}
|
}
|
|
function applyI18nKeys(element) {
|
var textKey = element.getAttribute("data-i18n-key");
|
var placeholderKey = element.getAttribute("data-i18n-placeholder-key");
|
var titleKey = element.getAttribute("data-i18n-title-key");
|
var params = element.getAttribute("data-i18n-params");
|
var parsedParams = params ? parseJson(params) : null;
|
if (textKey) {
|
if (/^(INPUT|TEXTAREA)$/i.test(element.tagName)) {
|
if (element.type === "button" || element.type === "submit" || element.type === "reset") {
|
var buttonText = t(textKey, parsedParams);
|
if (element.value !== buttonText) {
|
element.value = buttonText;
|
}
|
}
|
} else {
|
var textValue = t(textKey, parsedParams);
|
if (element.textContent !== textValue) {
|
element.textContent = textValue;
|
}
|
}
|
}
|
if (placeholderKey) {
|
var placeholderValue = t(placeholderKey, parsedParams);
|
if (element.getAttribute("placeholder") !== placeholderValue) {
|
element.setAttribute("placeholder", placeholderValue);
|
}
|
}
|
if (titleKey) {
|
var titleValue = t(titleKey, parsedParams);
|
if (element.getAttribute("title") !== titleValue) {
|
element.setAttribute("title", titleValue);
|
}
|
}
|
}
|
|
function shouldSkipElement(element) {
|
if (!element || element.nodeType !== 1) {
|
return true;
|
}
|
return /^(SCRIPT|STYLE|TEXTAREA|CODE|PRE)$/i.test(element.tagName);
|
}
|
|
function traverse(node) {
|
var child;
|
if (!node) {
|
return;
|
}
|
if (node.nodeType === 3) {
|
var translatedText = translate(node.nodeValue);
|
if (translatedText !== node.nodeValue) {
|
node.nodeValue = translatedText;
|
}
|
return;
|
}
|
if (node.nodeType !== 1 || shouldSkipElement(node)) {
|
return;
|
}
|
applyI18nKeys(node);
|
translateAttribute(node, "placeholder");
|
translateAttribute(node, "title");
|
translateAttribute(node, "aria-label");
|
translateAttribute(node, "alt");
|
if (/^(INPUT)$/i.test(node.tagName)) {
|
if (/^(button|submit|reset)$/i.test(node.type || "")) {
|
if (node.value) {
|
var translatedValue = translate(node.value);
|
if (translatedValue !== node.value) {
|
node.value = translatedValue;
|
}
|
}
|
} else if ((node.readOnly || node.disabled || node.getAttribute("readonly") !== null) && node.value) {
|
var translatedReadonlyValue = translate(node.value);
|
if (translatedReadonlyValue !== node.value) {
|
node.value = translatedReadonlyValue;
|
}
|
}
|
}
|
child = node.firstChild;
|
while (child) {
|
traverse(child);
|
child = child.nextSibling;
|
}
|
}
|
|
function apply(root) {
|
if (!root || !state.ready) {
|
return;
|
}
|
state.applying = true;
|
try {
|
traverse(root);
|
} finally {
|
state.applying = false;
|
}
|
}
|
|
function scheduleFlush() {
|
if (state.flushTimer) {
|
return;
|
}
|
var scheduler = window.requestAnimationFrame || function (callback) {
|
return window.setTimeout(callback, 16);
|
};
|
state.flushTimer = scheduler(flushPendingNodes);
|
}
|
|
function enqueueNode(node) {
|
if (!node || node.__wcsI18nQueued) {
|
return;
|
}
|
node.__wcsI18nQueued = true;
|
state.pendingNodes.push(node);
|
scheduleFlush();
|
}
|
|
function flushPendingNodes() {
|
var queue;
|
var i;
|
state.flushTimer = 0;
|
if (!state.pendingNodes.length) {
|
return;
|
}
|
if (state.applying) {
|
scheduleFlush();
|
return;
|
}
|
queue = state.pendingNodes.slice();
|
state.pendingNodes = [];
|
state.applying = true;
|
try {
|
for (i = 0; i < queue.length; i++) {
|
if (!queue[i]) {
|
continue;
|
}
|
queue[i].__wcsI18nQueued = false;
|
if (queue[i].nodeType === 3) {
|
var translatedText = translate(queue[i].nodeValue);
|
if (translatedText !== queue[i].nodeValue) {
|
queue[i].nodeValue = translatedText;
|
}
|
} else if (queue[i].nodeType === 1) {
|
traverse(queue[i]);
|
}
|
}
|
} finally {
|
state.applying = false;
|
}
|
if (state.pendingNodes.length) {
|
scheduleFlush();
|
}
|
}
|
|
function observeDom() {
|
if (state.observer || !window.MutationObserver || !document || !document.documentElement) {
|
return;
|
}
|
state.observer = new MutationObserver(function (mutations) {
|
var i;
|
if (state.applying) {
|
return;
|
}
|
for (i = 0; i < mutations.length; i++) {
|
if (mutations[i].type === "childList") {
|
for (var j = 0; j < mutations[i].addedNodes.length; j++) {
|
enqueueNode(mutations[i].addedNodes[j]);
|
}
|
} else if (mutations[i].type === "characterData" && mutations[i].target) {
|
enqueueNode(mutations[i].target);
|
} else if (mutations[i].type === "attributes" && mutations[i].target) {
|
enqueueNode(mutations[i].target);
|
}
|
}
|
});
|
state.observer.observe(document.documentElement, {
|
childList: true,
|
subtree: true,
|
characterData: true,
|
attributes: true,
|
attributeFilter: ["placeholder", "title", "aria-label", "alt"]
|
});
|
}
|
|
function wrapLayer() {
|
if (!window.layer || window.layer.__wcsI18nWrapped) {
|
return;
|
}
|
var layer = window.layer;
|
var rawMsg = layer.msg;
|
var rawAlert = layer.alert;
|
var rawConfirm = layer.confirm;
|
var rawTips = layer.tips;
|
var rawOpen = layer.open;
|
|
if (typeof rawMsg === "function") {
|
layer.msg = function (content, options, end) {
|
return rawMsg.call(layer, translate(content), options, end);
|
};
|
}
|
if (typeof rawAlert === "function") {
|
layer.alert = function (content, options, yes) {
|
if (options && typeof options === "object" && typeof options.title === "string") {
|
options.title = translate(options.title);
|
}
|
return rawAlert.call(layer, translate(content), options, yes);
|
};
|
}
|
if (typeof rawConfirm === "function") {
|
layer.confirm = function (content, options, yes, cancel) {
|
if (typeof options === "string") {
|
options = translate(options);
|
} else if (options && typeof options === "object") {
|
if (typeof options.title === "string") {
|
options.title = translate(options.title);
|
} else if (!options.title) {
|
options.title = translate("信息");
|
}
|
if (Object.prototype.toString.call(options.btn) === "[object Array]") {
|
for (var i = 0; i < options.btn.length; i++) {
|
options.btn[i] = translate(options.btn[i]);
|
}
|
}
|
}
|
return rawConfirm.call(layer, translate(content), options, yes, cancel);
|
};
|
}
|
if (typeof rawTips === "function") {
|
layer.tips = function (content, follow, options) {
|
return rawTips.call(layer, translate(content), follow, options);
|
};
|
}
|
if (typeof rawOpen === "function") {
|
layer.open = function (options) {
|
if (options && typeof options === "object") {
|
if (typeof options.title === "string") {
|
options.title = translate(options.title);
|
} else if (Object.prototype.toString.call(options.title) === "[object Array]" && typeof options.title[0] === "string") {
|
options.title[0] = translate(options.title[0]);
|
}
|
if (typeof options.content === "string") {
|
if (options.type === 2) {
|
options.content = appendNonceUrl(options.content);
|
} else {
|
options.content = translate(options.content);
|
}
|
}
|
if (Object.prototype.toString.call(options.btn) === "[object Array]") {
|
for (var i = 0; i < options.btn.length; i++) {
|
options.btn[i] = translate(options.btn[i]);
|
}
|
}
|
}
|
return rawOpen.call(layer, options);
|
};
|
}
|
window.layer.__wcsI18nWrapped = true;
|
}
|
|
function wrapElement() {
|
if (!window.ELEMENT || window.ELEMENT.__wcsI18nWrapped) {
|
return;
|
}
|
var element = window.ELEMENT;
|
var methods = ["success", "warning", "info", "error"];
|
var i;
|
if (typeof element.i18n === "function") {
|
element.i18n(function (key, value) {
|
return t(key, value);
|
});
|
}
|
if (typeof element.Message === "function") {
|
var rawMessage = element.Message;
|
element.Message = function (options) {
|
if (typeof options === "string") {
|
return rawMessage.call(element, translate(options));
|
}
|
if (options && typeof options === "object" && typeof options.message === "string") {
|
options.message = translate(options.message);
|
}
|
return rawMessage.call(element, options);
|
};
|
for (i = 0; i < methods.length; i++) {
|
if (typeof rawMessage[methods[i]] === "function") {
|
(function (methodName) {
|
element.Message[methodName] = function (options) {
|
if (typeof options === "string") {
|
return rawMessage[methodName].call(rawMessage, translate(options));
|
}
|
if (options && typeof options === "object" && typeof options.message === "string") {
|
options.message = translate(options.message);
|
}
|
return rawMessage[methodName].call(rawMessage, options);
|
};
|
})(methods[i]);
|
}
|
}
|
}
|
if (element.MessageBox) {
|
wrapMessageBoxMethod(element.MessageBox, "alert");
|
wrapMessageBoxMethod(element.MessageBox, "confirm");
|
wrapMessageBoxMethod(element.MessageBox, "prompt");
|
}
|
window.ELEMENT.__wcsI18nWrapped = true;
|
}
|
|
function wrapMessageBoxMethod(box, methodName) {
|
if (typeof box[methodName] !== "function") {
|
return;
|
}
|
var raw = box[methodName];
|
box[methodName] = function (message, title, options) {
|
if (typeof message === "string") {
|
message = translate(message);
|
}
|
if (typeof title === "string") {
|
title = translate(title);
|
} else if (title && typeof title === "object" && typeof title.message === "string") {
|
title.message = translate(title.message);
|
}
|
if (options && typeof options === "object") {
|
if (typeof options.confirmButtonText === "string") {
|
options.confirmButtonText = translate(options.confirmButtonText);
|
}
|
if (typeof options.cancelButtonText === "string") {
|
options.cancelButtonText = translate(options.cancelButtonText);
|
}
|
if (typeof options.title === "string") {
|
options.title = translate(options.title);
|
}
|
}
|
return raw.call(box, message, title, options);
|
};
|
}
|
|
function wrapLibraries() {
|
wrapLayer();
|
wrapElement();
|
}
|
|
function scheduleBridgeWrap() {
|
var timer = window.setInterval(function () {
|
state.bridgeAttempts += 1;
|
wrapLibraries();
|
if ((window.layer || !window.ELEMENT) && state.bridgeAttempts > 20) {
|
window.clearInterval(timer);
|
}
|
}, 300);
|
}
|
|
function patchXmlHttpRequest() {
|
if (!window.XMLHttpRequest || window.XMLHttpRequest.__wcsI18nPatched) {
|
return;
|
}
|
var rawOpen = window.XMLHttpRequest.prototype.open;
|
var rawSend = window.XMLHttpRequest.prototype.send;
|
var rawSetHeader = window.XMLHttpRequest.prototype.setRequestHeader;
|
|
window.XMLHttpRequest.prototype.open = function (method, url) {
|
this.__wcsI18nUrl = url;
|
this.__wcsI18nHeaderSet = false;
|
return rawOpen.apply(this, arguments);
|
};
|
window.XMLHttpRequest.prototype.setRequestHeader = function (name, value) {
|
if (String(name).toLowerCase() === "x-lang") {
|
this.__wcsI18nHeaderSet = true;
|
}
|
return rawSetHeader.apply(this, arguments);
|
};
|
window.XMLHttpRequest.prototype.send = function () {
|
try {
|
if (!this.__wcsI18nHeaderSet && shouldAttachLangHeader(this.__wcsI18nUrl)) {
|
rawSetHeader.call(this, "X-Lang", getLocale());
|
this.__wcsI18nHeaderSet = true;
|
}
|
} catch (e) {
|
}
|
return rawSend.apply(this, arguments);
|
};
|
window.XMLHttpRequest.__wcsI18nPatched = true;
|
}
|
|
function patchFetch() {
|
if (!window.fetch || window.fetch.__wcsI18nPatched) {
|
return;
|
}
|
var rawFetch = window.fetch;
|
window.fetch = function (input, init) {
|
var requestUrl = typeof input === "string" ? input : (input && input.url);
|
init = init || {};
|
if (shouldAttachLangHeader(requestUrl)) {
|
init.headers = init.headers || {};
|
if (typeof Headers !== "undefined" && init.headers instanceof Headers) {
|
if (!init.headers.has("X-Lang")) {
|
init.headers.set("X-Lang", getLocale());
|
}
|
} else if (!init.headers["X-Lang"] && !init.headers["x-lang"]) {
|
init.headers["X-Lang"] = getLocale();
|
}
|
}
|
return rawFetch.call(window, input, init);
|
};
|
window.fetch.__wcsI18nPatched = true;
|
}
|
|
function shouldAttachLangHeader(url) {
|
if (!url) {
|
return true;
|
}
|
if (typeof url !== "string") {
|
return true;
|
}
|
return url.indexOf("http://") !== 0 && url.indexOf("https://") !== 0 || url.indexOf(window.location.origin) === 0;
|
}
|
|
var api = {
|
load: load,
|
onReady: onReady,
|
t: t,
|
tl: translate,
|
apply: apply,
|
getLocale: getLocale,
|
setLocale: setLocale,
|
getLocaleOptions: function () {
|
return state.localeOptions.slice();
|
},
|
getSupportedLocales: function () {
|
return state.supportedLocales.slice();
|
},
|
getDefaultLocale: function () {
|
return state.defaultLocale;
|
},
|
addNonceUrl: appendNonceUrl,
|
isReady: function () {
|
return state.ready;
|
}
|
};
|
|
return api;
|
})(window, document);
|
|
// 赋值
|
function setVal(el, val) {
|
if (el.text() !== val){
|
el.html(val);
|
}
|
}
|
|
// 详情窗口-高度
|
var detailHeight = '80%';
|
// 详情窗口-宽度
|
var detailWidth = '90%';
|
|
// 非空判断
|
function isEmpty(obj){
|
return typeof obj == "undefined" || obj == null || obj === "";
|
}
|
|
// 时间 ==>> 字符串
|
function dateToStr(date) {
|
var time = new Date(date);
|
var y = time.getFullYear();
|
var M = time.getMonth() + 1;
|
M = M < 10 ? ("0" + M) : M;
|
var d = time.getDate();
|
d = d < 10 ? ("0" + d) : d;
|
var h = time.getHours();
|
h = h < 10 ? ("0" + h) : h;
|
var m = time.getMinutes();
|
m = m < 10 ? ("0" + m) : m;
|
var s = time.getSeconds();
|
s = s < 10 ? ("0" + s) : s;
|
return y + "-" + M + "-" + d + " " + h + ":" + m + ":" + s;
|
}
|
|
// 字符串 ===>> 时间
|
function strToDate(str) {
|
var t = Date.parse(str);
|
if (!isNaN(t)) {
|
return new Date(Date.parse(str.replace(/-/g, "/")));
|
} else {
|
return null;
|
}
|
}
|
|
// 清理对象null值
|
function reObject(data) {
|
for (var obj in data) {
|
if (data[obj]===null){
|
delete data[obj];
|
}
|
}
|
return data;
|
}
|
|
/**
|
* disabled 属性转换
|
*/
|
function convertDisabled(el, param) {
|
el.each(function () {
|
$(this).attr("disabled", param);
|
});
|
}
|
|
// 权限
|
function limit(child){
|
if (child == null){
|
child = false;
|
}
|
var targetWindow = child ? parent.window : window;
|
var search = targetWindow.location.search || "";
|
var resourceId = null;
|
|
if (search) {
|
if (typeof URLSearchParams !== "undefined") {
|
resourceId = new URLSearchParams(search).get("resourceId");
|
}
|
if (resourceId == null || resourceId === "") {
|
var match = search.replace(/^\?/, "").match(/(?:^|&)resourceId=([^&]*)/i);
|
resourceId = match ? decodeURIComponent(match[1]) : null;
|
}
|
}
|
|
if (resourceId != null && resourceId !== "") {
|
$.ajax({
|
url: baseUrl+"/power/menu/"+resourceId+"/auth",
|
headers: {'token': localStorage.getItem('token')},
|
method: 'GET',
|
async: false,
|
success: function (res) {
|
if (res.code === 200){
|
for(var i = 0, len = res.data.length; i < len; i++) {
|
(child?parent:window).$('#'+res.data[i].code).css("display", "inline-block");
|
(child?parent:window).$('.'+res.data[i].code).css("display", "inline-block");
|
}
|
} else if (res.code === 403){
|
window.location.href = baseUrl;
|
} else {
|
layer.msg(res.msg)
|
}
|
}
|
});
|
}
|
|
}
|
|
function clearFormVal(el) {
|
$(':input', el)
|
.val('')
|
.removeAttr('checked')
|
.removeAttr('selected');
|
}
|
|
|
|
// http请求
|
!function (n) {
|
"use strict";
|
|
var http = {
|
toAjax: function (params) {
|
$.ajax(params);
|
},
|
get: function (url, data, callback) {
|
http.toAjax({
|
method: 'GET',
|
url: url,
|
data: data,
|
dataType: 'json',
|
header: {'Content-Type': 'application/json'},
|
timeout: 10000,
|
cache: false,
|
success: function (result) {
|
callback(result);
|
},
|
error: function (res, type) {
|
|
}
|
})
|
},
|
post: function (url, param, callback, type) {
|
var headerType;
|
if (type === 'json') {
|
headerType = {'Content-Type': 'application/json'}
|
} else {
|
headerType = {'Content-Type': 'application/x-www-form-urlencoded'}
|
}
|
headerType['token'] = localStorage.getItem('token');
|
http.toAjax({
|
method: 'POST',
|
url: url,
|
data: param,
|
dataType: 'json',
|
headers: headerType,
|
timeout: 10000,
|
cache: false,
|
success: function (res) {
|
if (res.code === 200){
|
callback(res);
|
} else if (res.code === 403){
|
parent.location.href = baseUrl+"/login";
|
} else {
|
layer.msg(res.msg, {icon: 2});
|
}
|
},
|
error: function (res, type) {
|
|
}
|
})
|
},
|
};
|
|
|
"function" == typeof define && define.amd ? define(function () {
|
return http
|
}) : "object" == typeof module && module.exports ? module.exports = http : n.http = http
|
}(this);
|
|
/**
|
* 获取url键值对
|
*/
|
function getUrlVal(key) {
|
var reg = new RegExp('(^|&)' + key + '=([^&]*)(&|$)', 'i');
|
var r = window.location.search.substr(1).match(reg);
|
if (r != null) {
|
return unescape(r[2]);
|
}
|
return null;
|
}
|
|
function getDateFormat(value){
|
var date = new Date();// 获取当前时间
|
date.setDate(date.getDate() + value);// 设置天数 -1 天
|
return date.Format("MM-dd");
|
}
|
/**
|
* 日期格式化
|
*/
|
Date.prototype.Format = function (fmt) {
|
var o = {
|
"M+": this.getMonth() + 1, //月份
|
"d+": this.getDate(), //日
|
"h+": this.getHours(), //小时
|
"m+": this.getMinutes(), //分
|
"s+": this.getSeconds(), //秒
|
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
|
"S": this.getMilliseconds() //毫秒
|
};
|
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
|
for (var k in o)
|
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
|
return fmt;
|
}
|
|
/**
|
* 获取AI助手SVG图标HTML
|
* @param {number} width 宽度,默认48
|
* @param {number} height 高度,默认48
|
* @returns {string} SVG HTML字符串
|
*/
|
function getAiIconHtml(width, height) {
|
width = width || 48;
|
height = height || 48;
|
// 生成唯一ID防止冲突
|
var uniqueId = 'ai_icon_' + Math.random().toString(36).substr(2, 9);
|
var textGradientId = 'textGradient_' + uniqueId;
|
var glowId = 'glow_' + uniqueId;
|
var spinName = 'spin_' + uniqueId;
|
|
return '<svg width="' + width + '" height="' + height + '" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" fill="none">' +
|
'<defs>' +
|
'<linearGradient id="' + textGradientId + '" gradientUnits="userSpaceOnUse" x1="25" y1="50" x2="75" y2="50">' +
|
'<stop offset="0%" stop-color="#8b5cf6"/>' +
|
'<stop offset="20%" stop-color="#f472b6"/>' +
|
'<stop offset="40%" stop-color="#fb923c"/>' +
|
'<stop offset="60%" stop-color="#fbbf24"/>' +
|
'<stop offset="80%" stop-color="#22d3ee"/>' +
|
'<stop offset="100%" stop-color="#3b82f6"/>' +
|
'<animateTransform attributeName="gradientTransform" type="rotate" from="0 50 50" to="360 50 50" dur="5s" repeatCount="indefinite" />' +
|
'</linearGradient>' +
|
'<filter id="' + glowId + '" x="-50%" y="-50%" width="200%" height="200%">' +
|
'<feGaussianBlur stdDeviation="1.6" result="blur"/>' +
|
'<feMerge>' +
|
'<feMergeNode in="blur"/>' +
|
'<feMergeNode in="SourceGraphic"/>' +
|
'</feMerge>' +
|
'</filter>' +
|
'<style>' +
|
'.' + spinName + ' { animation: ' + spinName + ' 5s linear infinite; transform-origin: 50px 50px; }' +
|
'@keyframes ' + spinName + ' { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }' +
|
'.geo-text-' + uniqueId + ' { fill: url(#' + textGradientId + '); stroke: rgba(0,0,0,0.40); stroke-width: 0.45; paint-order: stroke fill; }' +
|
'</style>' +
|
'</defs>' +
|
'<g class="' + spinName + '" filter="url(#' + glowId + ')">' +
|
'<g transform="rotate(0 50 50)"><rect x="48" y="18" width="4.5" height="10" rx="2" fill="#8b5cf6"/></g>' +
|
'<g transform="rotate(60 50 50)"><rect x="48" y="18" width="4.5" height="10" rx="2" fill="#f472b6"/></g>' +
|
'<g transform="rotate(120 50 50)"><rect x="48" y="18" width="4.5" height="10" rx="2" fill="#fb923c"/></g>' +
|
'<g transform="rotate(180 50 50)"><rect x="48" y="18" width="4.5" height="10" rx="2" fill="#fbbf24"/></g>' +
|
'<g transform="rotate(240 50 50)"><rect x="48" y="18" width="4.5" height="10" rx="2" fill="#22d3ee"/></g>' +
|
'<g transform="rotate(300 50 50)"><rect x="48" y="18" width="4.5" height="10" rx="2" fill="#3b82f6"/></g>' +
|
'</g>' +
|
'<g transform="translate(50 50) scale(0.35) translate(-32.5 -20)" class="geo-text-' + uniqueId + '">' +
|
'<path d="M0 40 L20 0 L40 40 Z"/><rect x="12" y="22" width="16" height="4" rx="1"/>' +
|
'<rect x="50" y="0" width="15" height="4" rx="1"/><rect x="55" y="4" width="5" height="32" rx="1.5"/><rect x="50" y="36" width="15" height="4" rx="1"/>' +
|
'</g>' +
|
'</svg>';
|
}
|