From de48651ae7dcf07a32ecb8296f602c4f508258af Mon Sep 17 00:00:00 2001 From: hide_d Date: Sat, 28 Aug 2021 17:20:44 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20exportWindow=20-=20global=EC=9D=84=20?= =?UTF-8?q?=EC=A7=80=EC=A0=80=EB=B6=84=ED=95=98=EA=B2=8C=20=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EA=B3=A0,=20window=EC=97=90=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=EB=A7=8C=20=ED=95=B4=EC=A4=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hwe/ts/common_deprecated.ts | 65 ++++++++++--------------------------- hwe/ts/install.ts | 3 +- hwe/ts/map.ts | 4 +-- hwe/ts/util/exportWindow.ts | 4 +++ ts/admin_member.ts | 14 +++----- 5 files changed, 29 insertions(+), 61 deletions(-) create mode 100644 hwe/ts/util/exportWindow.ts diff --git a/hwe/ts/common_deprecated.ts b/hwe/ts/common_deprecated.ts index eee84ca4..569f3a09 100644 --- a/hwe/ts/common_deprecated.ts +++ b/hwe/ts/common_deprecated.ts @@ -4,8 +4,10 @@ import { TemplateEngine } from "./util/TemplateEngine"; import { escapeHtml } from "./legacy/escapeHtml"; import { nl2br } from "./util/nl2br"; import jQuery from "jquery"; -window.jQuery = jQuery; -window.$ = jQuery; +import { exportWindow } from "./util/exportWindow"; + +exportWindow(jQuery, '$'); +exportWindow(jQuery, 'jQuery'); jQuery(function ($) { initTooltip(); @@ -19,55 +21,22 @@ jQuery(function ($) { } }); -declare global { - interface Window { - $: typeof jQuery; - jQuery: typeof jQuery; - /** @deprecated Module 사용할 것 */ - escapeHtml: typeof escapeHtml; - /** @deprecated Module 사용할 것 */ - mb_strwidth: typeof mb_strwidth; - /** @deprecated Module 사용할 것 */ - isBrightColor: typeof isBrightColor; - /** @deprecated Module 사용할 것 */ - TemplateEngine: typeof TemplateEngine; - /** @deprecated Module 사용할 것 */ - getIconPath: typeof getIconPath; - /** @deprecated Module 사용할 것 */ - activeFlip: typeof activeFlip; - /** @deprecated Module 사용할 것 */ - errUnknown: typeof errUnknown; - /** @deprecated Module 사용할 것 */ - errUnknownToast: typeof errUnknownToast; - /** @deprecated Module 사용할 것 */ - quickReject: typeof quickReject; - /** @deprecated Module 사용할 것 */ - nl2br: typeof nl2br; - /** @deprecated Module 사용할 것 */ - initTooltip: typeof initTooltip; - } -} - - /** * {0}, {1}, {2}형태로 포맷해주는 함수 - * NOTE: TypeScript declare 충돌로 인해 우회 정의 */ - (String.prototype as unknown as {format:(...args:(string|number)[])=>string}).format = function(this:string, ...args){ +exportWindow(function(this:string, ...args:(string|number)[]){ return this.replace(/{(\d+)}/g, function (match, number) { return (typeof args[number] != 'undefined') ? args[number].toString() : match; }); -}; - - -window.escapeHtml = escapeHtml; -window.mb_strwidth = mb_strwidth; -window.isBrightColor = isBrightColor; -window.TemplateEngine = TemplateEngine; -window.getIconPath = getIconPath; -window.activeFlip = activeFlip; -window.errUnknown = errUnknown; -window.errUnknownToast = errUnknownToast; -window.quickReject = quickReject; -window.nl2br = nl2br; -window.initTooltip = initTooltip; \ No newline at end of file +}, 'format', String.prototype); +exportWindow(escapeHtml, 'escapeHtml'); +exportWindow(mb_strwidth, 'mb_strwidth'); +exportWindow(isBrightColor, 'isBrightColor'); +exportWindow(TemplateEngine, 'TemplateEngine'); +exportWindow(getIconPath, 'getIconPath'); +exportWindow(activeFlip, 'activeFlip'); +exportWindow(errUnknown, 'errUnknown'); +exportWindow(errUnknownToast, 'errUnknownToast'); +exportWindow(quickReject, 'quickReject'); +exportWindow(nl2br, 'nl2br'); +exportWindow(initTooltip, 'initTooltip'); \ No newline at end of file diff --git a/hwe/ts/install.ts b/hwe/ts/install.ts index 86795fc0..a45502ac 100644 --- a/hwe/ts/install.ts +++ b/hwe/ts/install.ts @@ -7,6 +7,7 @@ import axios from "axios"; import { InvalidResponse } from "./defs"; import { JQValidateForm, NamedRules } from "./util/jqValidateForm"; import { convertFormData } from "./util/convertFormData"; +import { exportWindow } from "./util/exportWindow"; type ResponseScenarioItem = { year?: number, @@ -267,4 +268,4 @@ $(async function () { }) -window.$ = $; \ No newline at end of file +exportWindow($, '$'); \ No newline at end of file diff --git a/hwe/ts/map.ts b/hwe/ts/map.ts index 3cabe2b1..f330dc0b 100644 --- a/hwe/ts/map.ts +++ b/hwe/ts/map.ts @@ -5,6 +5,7 @@ import { convColorValue, convertDictById, convertSet, stringFormat } from './com import { InvalidResponse } from './defs'; import { unwrap } from "./util/unwrap"; import { convertFormData } from './util/convertFormData'; +import { exportWindow } from './util/exportWindow'; declare const serverNick: string; declare const serverID: string; @@ -16,7 +17,6 @@ type CityPositionMap = { declare global { interface Window { sam_toggleSingleTap?: boolean, - reloadWorldMap: (option: loadMapOption, drawTarget?: string) => Promise; getCityPosition: () => CityPositionMap; formatCityInfo: (city: MapCityParsedRaw)=>MapCityParsedRegionLevelText } @@ -782,7 +782,7 @@ export async function reloadWorldMap(option: loadMapOption, drawTarget = '.world } } -window.reloadWorldMap = reloadWorldMap; +exportWindow(reloadWorldMap, 'reloadWorldMap'); $(function ($) { if (is_touch_device()) { $('.map_body .map_toggle_single_tap').show(); diff --git a/hwe/ts/util/exportWindow.ts b/hwe/ts/util/exportWindow.ts new file mode 100644 index 00000000..b768b1c4 --- /dev/null +++ b/hwe/ts/util/exportWindow.ts @@ -0,0 +1,4 @@ +export function exportWindow(obj:unknown, objName: string, targetWindow?: unknown):void{ + const target:unknown = targetWindow ?? window; + (target as {[v: string]: unknown})[objName] = obj; +} \ No newline at end of file diff --git a/ts/admin_member.ts b/ts/admin_member.ts index af445897..fc43382d 100644 --- a/ts/admin_member.ts +++ b/ts/admin_member.ts @@ -6,13 +6,7 @@ import { InvalidResponse } from '../hwe/ts/defs'; import { setAxiosXMLHttpRequest } from '../hwe/ts/util/setAxiosXMLHttpRequest'; import { unwrap_any } from '../hwe/ts/util/unwrap_any'; import { convertFormData } from '../hwe/ts/util/convertFormData'; - -declare global { - interface Window { - changeSystem: (action: string) => Promise; - changeUserStatus: (action: string, userID: Element | number, param?: number) => Promise; - } -} +import { exportWindow } from './util/exportWindow'; type UserEntry = { userID: string, @@ -277,9 +271,6 @@ async function refreshAll() { $(async function () { setAxiosXMLHttpRequest(); - window.changeSystem = changeSystem; - window.changeUserStatus = changeUserStatus; - await refreshAll(); $('input[name=allow_join], input[name=allow_login]').on('change', async function () { @@ -287,3 +278,6 @@ $(async function () { await changeSystem(unwrap_any($this.attr('name')), unwrap_any($this.val())); }) }); + +exportWindow(changeSystem, 'changeSystem'); +exportWindow(changeUserStatus, 'changeUserStatus'); \ No newline at end of file