import { Options } from "./types"; export default class NaverMap { public static NAVER_MAP_ELEMENT_ID: "naver-maps"; /** * option을 인자로 받아 naver-map url을 return한다. * * @static * @private * @return string * @param options */ private static getNaverMapUrl(options: Options) { const category = options.category || "ncp"; const clientId = options.clientId; const subModules = options.subModules || ""; const url = `https://openapi.map.naver.com/openapi/v3/maps.js?${category}ClientId=${clientId}&submodules=${subModules}`; return url; } /** * html tag에 naver map을 불러올 수 있는 script를 생성해 head에 붙인다. * * @static * @public * @return string * @param options */ static createScript(options: Options) { if (typeof document == "undefined") return; const isExist = document.getElementById(this.NAVER_MAP_ELEMENT_ID); if (isExist) return; const script = document.createElement("script"); const url = NaverMap.getNaverMapUrl(options); script.setAttribute("id", this.NAVER_MAP_ELEMENT_ID); script.setAttribute("src", url); script.setAttribute("async", ""); script.setAttribute("defer", ""); script.onerror = () => { new Error("[naver-maps] Failed to load"); }; document.head.appendChild(script); } }