UNPKG

svelte-daum-postcode

Version:
645 lines (616 loc) 22.4 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global['daum-postcode'] = factory()); }(this, (function () { 'use strict'; function noop() { } function run(fn) { return fn(); } function blank_object() { return Object.create(null); } function run_all(fns) { fns.forEach(run); } function is_function(thing) { return typeof thing === 'function'; } function safe_not_equal(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } function is_empty(obj) { return Object.keys(obj).length === 0; } // Track which nodes are claimed during hydration. Unclaimed nodes can then be removed from the DOM // at the end of hydration without touching the remaining nodes. let is_hydrating = false; function start_hydrating() { is_hydrating = true; } function end_hydrating() { is_hydrating = false; } function upper_bound(low, high, key, value) { // Return first index of value larger than input value in the range [low, high) while (low < high) { const mid = low + ((high - low) >> 1); if (key(mid) <= value) { low = mid + 1; } else { high = mid; } } return low; } function init_hydrate(target) { if (target.hydrate_init) return; target.hydrate_init = true; // We know that all children have claim_order values since the unclaimed have been detached const children = target.childNodes; /* * Reorder claimed children optimally. * We can reorder claimed children optimally by finding the longest subsequence of * nodes that are already claimed in order and only moving the rest. The longest * subsequence subsequence of nodes that are claimed in order can be found by * computing the longest increasing subsequence of .claim_order values. * * This algorithm is optimal in generating the least amount of reorder operations * possible. * * Proof: * We know that, given a set of reordering operations, the nodes that do not move * always form an increasing subsequence, since they do not move among each other * meaning that they must be already ordered among each other. Thus, the maximal * set of nodes that do not move form a longest increasing subsequence. */ // Compute longest increasing subsequence // m: subsequence length j => index k of smallest value that ends an increasing subsequence of length j const m = new Int32Array(children.length + 1); // Predecessor indices + 1 const p = new Int32Array(children.length); m[0] = -1; let longest = 0; for (let i = 0; i < children.length; i++) { const current = children[i].claim_order; // Find the largest subsequence length such that it ends in a value less than our current value // upper_bound returns first greater value, so we subtract one const seqLen = upper_bound(1, longest + 1, idx => children[m[idx]].claim_order, current) - 1; p[i] = m[seqLen] + 1; const newLen = seqLen + 1; // We can guarantee that current is the smallest value. Otherwise, we would have generated a longer sequence. m[newLen] = i; longest = Math.max(newLen, longest); } // The longest increasing subsequence of nodes (initially reversed) const lis = []; // The rest of the nodes, nodes that will be moved const toMove = []; let last = children.length - 1; for (let cur = m[longest] + 1; cur != 0; cur = p[cur - 1]) { lis.push(children[cur - 1]); for (; last >= cur; last--) { toMove.push(children[last]); } last--; } for (; last >= 0; last--) { toMove.push(children[last]); } lis.reverse(); // We sort the nodes being moved to guarantee that their insertion order matches the claim order toMove.sort((a, b) => a.claim_order - b.claim_order); // Finally, we move the nodes for (let i = 0, j = 0; i < toMove.length; i++) { while (j < lis.length && toMove[i].claim_order >= lis[j].claim_order) { j++; } const anchor = j < lis.length ? lis[j] : null; target.insertBefore(toMove[i], anchor); } } function append(target, node) { if (is_hydrating) { init_hydrate(target); if ((target.actual_end_child === undefined) || ((target.actual_end_child !== null) && (target.actual_end_child.parentElement !== target))) { target.actual_end_child = target.firstChild; } if (node !== target.actual_end_child) { target.insertBefore(node, target.actual_end_child); } else { target.actual_end_child = node.nextSibling; } } else if (node.parentNode !== target) { target.appendChild(node); } } function insert(target, node, anchor) { if (is_hydrating && !anchor) { append(target, node); } else if (node.parentNode !== target || (anchor && node.nextSibling !== anchor)) { target.insertBefore(node, anchor || null); } } function detach(node) { node.parentNode.removeChild(node); } function element(name) { return document.createElement(name); } function attr(node, attribute, value) { if (value == null) node.removeAttribute(attribute); else if (node.getAttribute(attribute) !== value) node.setAttribute(attribute, value); } function children(element) { return Array.from(element.childNodes); } function custom_event(type, detail) { const e = document.createEvent('CustomEvent'); e.initCustomEvent(type, false, false, detail); return e; } let current_component; function set_current_component(component) { current_component = component; } function get_current_component() { if (!current_component) throw new Error('Function called outside component initialization'); return current_component; } function onMount(fn) { get_current_component().$$.on_mount.push(fn); } function createEventDispatcher() { const component = get_current_component(); return (type, detail) => { const callbacks = component.$$.callbacks[type]; if (callbacks) { // TODO are there situations where events could be dispatched // in a server (non-DOM) environment? const event = custom_event(type, detail); callbacks.slice().forEach(fn => { fn.call(component, event); }); } }; } const dirty_components = []; const binding_callbacks = []; const render_callbacks = []; const flush_callbacks = []; const resolved_promise = Promise.resolve(); let update_scheduled = false; function schedule_update() { if (!update_scheduled) { update_scheduled = true; resolved_promise.then(flush); } } function add_render_callback(fn) { render_callbacks.push(fn); } let flushing = false; const seen_callbacks = new Set(); function flush() { if (flushing) return; flushing = true; do { // first, call beforeUpdate functions // and update components for (let i = 0; i < dirty_components.length; i += 1) { const component = dirty_components[i]; set_current_component(component); update(component.$$); } set_current_component(null); dirty_components.length = 0; while (binding_callbacks.length) binding_callbacks.pop()(); // then, once components are updated, call // afterUpdate functions. This may cause // subsequent updates... for (let i = 0; i < render_callbacks.length; i += 1) { const callback = render_callbacks[i]; if (!seen_callbacks.has(callback)) { // ...so guard against infinite loops seen_callbacks.add(callback); callback(); } } render_callbacks.length = 0; } while (dirty_components.length); while (flush_callbacks.length) { flush_callbacks.pop()(); } update_scheduled = false; flushing = false; seen_callbacks.clear(); } function update($$) { if ($$.fragment !== null) { $$.update(); run_all($$.before_update); const dirty = $$.dirty; $$.dirty = [-1]; $$.fragment && $$.fragment.p($$.ctx, dirty); $$.after_update.forEach(add_render_callback); } } const outroing = new Set(); function transition_in(block, local) { if (block && block.i) { outroing.delete(block); block.i(local); } } function mount_component(component, target, anchor, customElement) { const { fragment, on_mount, on_destroy, after_update } = component.$$; fragment && fragment.m(target, anchor); if (!customElement) { // onMount happens before the initial afterUpdate add_render_callback(() => { const new_on_destroy = on_mount.map(run).filter(is_function); if (on_destroy) { on_destroy.push(...new_on_destroy); } else { // Edge case - component was destroyed immediately, // most likely as a result of a binding initialising run_all(new_on_destroy); } component.$$.on_mount = []; }); } after_update.forEach(add_render_callback); } function destroy_component(component, detaching) { const $$ = component.$$; if ($$.fragment !== null) { run_all($$.on_destroy); $$.fragment && $$.fragment.d(detaching); // TODO null out other refs, including component.$$ (but need to // preserve final state?) $$.on_destroy = $$.fragment = null; $$.ctx = []; } } function make_dirty(component, i) { if (component.$$.dirty[0] === -1) { dirty_components.push(component); schedule_update(); component.$$.dirty.fill(0); } component.$$.dirty[(i / 31) | 0] |= (1 << (i % 31)); } function init(component, options, instance, create_fragment, not_equal, props, dirty = [-1]) { const parent_component = current_component; set_current_component(component); const $$ = component.$$ = { fragment: null, ctx: null, // state props, update: noop, not_equal, bound: blank_object(), // lifecycle on_mount: [], on_destroy: [], on_disconnect: [], before_update: [], after_update: [], context: new Map(parent_component ? parent_component.$$.context : options.context || []), // everything else callbacks: blank_object(), dirty, skip_bound: false }; let ready = false; $$.ctx = instance ? instance(component, options.props || {}, (i, ret, ...rest) => { const value = rest.length ? rest[0] : ret; if ($$.ctx && not_equal($$.ctx[i], $$.ctx[i] = value)) { if (!$$.skip_bound && $$.bound[i]) $$.bound[i](value); if (ready) make_dirty(component, i); } return ret; }) : []; $$.update(); ready = true; run_all($$.before_update); // `false` as a special case of no DOM component $$.fragment = create_fragment ? create_fragment($$.ctx) : false; if (options.target) { if (options.hydrate) { start_hydrating(); const nodes = children(options.target); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion $$.fragment && $$.fragment.l(nodes); nodes.forEach(detach); } else { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion $$.fragment && $$.fragment.c(); } if (options.intro) transition_in(component.$$.fragment); mount_component(component, options.target, options.anchor, options.customElement); end_hydrating(); flush(); } set_current_component(parent_component); } /** * Base class for Svelte components. Used when dev=false. */ class SvelteComponent { $destroy() { destroy_component(this, 1); this.$destroy = noop; } $on(type, callback) { const callbacks = (this.$$.callbacks[type] || (this.$$.callbacks[type] = [])); callbacks.push(callback); return () => { const index = callbacks.indexOf(callback); if (index !== -1) callbacks.splice(index, 1); }; } $set($$props) { if (this.$$set && !is_empty($$props)) { this.$$.skip_bound = true; this.$$set($$props); this.$$.skip_bound = false; } } } /* src/index.svelte generated by Svelte v3.38.3 */ function create_fragment(ctx) { let div; let div_style_value; return { c() { div = element("div"); attr(div, "style", div_style_value = ` display: ${display}; width: ${/*daumWidth*/ ctx[3]}; height: ${/*daumHeight*/ ctx[4]}; ${/*style*/ ctx[1]} `); }, m(target, anchor) { insert(target, div, anchor); /*div_binding*/ ctx[27](div); }, p(ctx, dirty) { if (dirty[0] & /*daumWidth, daumHeight, style*/ 26 && div_style_value !== (div_style_value = ` display: ${display}; width: ${/*daumWidth*/ ctx[3]}; height: ${/*daumHeight*/ ctx[4]}; ${/*style*/ ctx[1]} `)) { attr(div, "style", div_style_value); } }, i: noop, o: noop, d(detaching) { if (detaching) detach(div); /*div_binding*/ ctx[27](null); } }; } let display = "block"; function instance($$self, $$props, $$invalidate) { let daumWidth; let daumHeight; let { alwaysShowEngAddr = false } = $$props; let { animation = false } = $$props; let { autoClose = false } = $$props; let { autoMapping = true } = $$props; let { autoResize = false } = $$props; let { defaultQuery = null } = $$props; let { errorMessage = "<p>현재 Daum 우편번호 서비스를 이용할 수 없습니다. 잠시 후 다시 시도해주세요.</p>" } = $$props; let { height = "400px" } = $$props; let { hideEngBtn = false } = $$props; let { hideMapBtn = false } = $$props; let { maxSuggestItems = 10 } = $$props; let { pleaseReadGuide = 0 } = $$props; let { pleaseReadGuideTimer = 1.5 } = $$props; let { scriptUrl = "https://t1.daumcdn.net/mapjsapi/bundle/postcode/prod/postcode.v2.js" } = $$props; let { shorthand = true } = $$props; let { showMoreHName = false } = $$props; let { style = "" } = $$props; let { submitMode = true } = $$props; let { theme = null } = $$props; let { useSuggest = true } = $$props; let { useBannerLink = true } = $$props; let { width = "100%" } = $$props; let { focusInput = true } = $$props; let { focusContent = true } = $$props; let daumRef = null; const dispatch = createEventDispatcher(); onMount(() => { const scriptId = "daum_postcode"; const isExist = !!document.getElementById(scriptId); if (!isExist) { const script = document.createElement("script"); script.src = scriptUrl; script.onload = () => onLoad(); script.onerror = error => onError(error); script.id = scriptId; document.body.appendChild(script); } else { onLoad(); } }); const onLoad = () => { window.daum.postcode.load(() => { const postCode = new window.daum.Postcode({ oncomplete: onComplete, onsearch: onSearch, onresize: onResize, alwaysShowEngAddr, animation, autoMapping, autoResize, height: "100%", hideEngBtn, hideMapBtn, maxSuggestItems, pleaseReadGuide, pleaseReadGuideTimer, shorthand, showMoreHName, submitMode, theme, useSuggest, useBannerLink, width: "100%", focusInput, focusContent }); postCode.embed(daumRef, { q: defaultQuery, autoClose }); }); }; const onError = error => { error.target.remove(); error = true; dispatch("error"); }; const onComplete = data => { dispatch("complete", { data }); }; const onSearch = data => { dispatch("search", { data }); }; const onResize = size => { if (autoResize) { $$invalidate(5, height = size.height); } }; function div_binding($$value) { binding_callbacks[$$value ? "unshift" : "push"](() => { daumRef = $$value; $$invalidate(2, daumRef); }); } $$self.$$set = $$props => { if ("alwaysShowEngAddr" in $$props) $$invalidate(6, alwaysShowEngAddr = $$props.alwaysShowEngAddr); if ("animation" in $$props) $$invalidate(7, animation = $$props.animation); if ("autoClose" in $$props) $$invalidate(8, autoClose = $$props.autoClose); if ("autoMapping" in $$props) $$invalidate(9, autoMapping = $$props.autoMapping); if ("autoResize" in $$props) $$invalidate(10, autoResize = $$props.autoResize); if ("defaultQuery" in $$props) $$invalidate(11, defaultQuery = $$props.defaultQuery); if ("errorMessage" in $$props) $$invalidate(0, errorMessage = $$props.errorMessage); if ("height" in $$props) $$invalidate(5, height = $$props.height); if ("hideEngBtn" in $$props) $$invalidate(12, hideEngBtn = $$props.hideEngBtn); if ("hideMapBtn" in $$props) $$invalidate(13, hideMapBtn = $$props.hideMapBtn); if ("maxSuggestItems" in $$props) $$invalidate(14, maxSuggestItems = $$props.maxSuggestItems); if ("pleaseReadGuide" in $$props) $$invalidate(15, pleaseReadGuide = $$props.pleaseReadGuide); if ("pleaseReadGuideTimer" in $$props) $$invalidate(16, pleaseReadGuideTimer = $$props.pleaseReadGuideTimer); if ("scriptUrl" in $$props) $$invalidate(17, scriptUrl = $$props.scriptUrl); if ("shorthand" in $$props) $$invalidate(18, shorthand = $$props.shorthand); if ("showMoreHName" in $$props) $$invalidate(19, showMoreHName = $$props.showMoreHName); if ("style" in $$props) $$invalidate(1, style = $$props.style); if ("submitMode" in $$props) $$invalidate(20, submitMode = $$props.submitMode); if ("theme" in $$props) $$invalidate(21, theme = $$props.theme); if ("useSuggest" in $$props) $$invalidate(22, useSuggest = $$props.useSuggest); if ("useBannerLink" in $$props) $$invalidate(23, useBannerLink = $$props.useBannerLink); if ("width" in $$props) $$invalidate(24, width = $$props.width); if ("focusInput" in $$props) $$invalidate(25, focusInput = $$props.focusInput); if ("focusContent" in $$props) $$invalidate(26, focusContent = $$props.focusContent); }; $$self.$$.update = () => { if ($$self.$$.dirty[0] & /*width*/ 16777216) { $$invalidate(3, daumWidth = width); } if ($$self.$$.dirty[0] & /*height*/ 32) { $$invalidate(4, daumHeight = height); } }; return [ errorMessage, style, daumRef, daumWidth, daumHeight, height, alwaysShowEngAddr, animation, autoClose, autoMapping, autoResize, defaultQuery, hideEngBtn, hideMapBtn, maxSuggestItems, pleaseReadGuide, pleaseReadGuideTimer, scriptUrl, shorthand, showMoreHName, submitMode, theme, useSuggest, useBannerLink, width, focusInput, focusContent, div_binding ]; } class Src extends SvelteComponent { constructor(options) { super(); init( this, options, instance, create_fragment, safe_not_equal, { alwaysShowEngAddr: 6, animation: 7, autoClose: 8, autoMapping: 9, autoResize: 10, defaultQuery: 11, errorMessage: 0, height: 5, hideEngBtn: 12, hideMapBtn: 13, maxSuggestItems: 14, pleaseReadGuide: 15, pleaseReadGuideTimer: 16, scriptUrl: 17, shorthand: 18, showMoreHName: 19, style: 1, submitMode: 20, theme: 21, useSuggest: 22, useBannerLink: 23, width: 24, focusInput: 25, focusContent: 26 }, [-1, -1] ); } } return Src; })));