미디어위키:CategoryNav.js: 두 판 사이의 차이

(새 문서: →‎========================================= COASTLINE: BLACK ICE - CategoryNav 대문 카테고리 네비 DOM 생성기 =========================================: /* 설계 목적 ----------------------------------------- 이 파일은 대문 카테고리 네비를 MediaWiki 본문 위키문법이 아니라 JavaScript로 생성한다. 왜 JS를 쓰는가 ----------------------------------------- 테스트 단계에서는 다음 방식들을 시도했다. 1. 본문에 <span...)
 
편집 요약 없음
 
(같은 사용자의 중간 판 5개는 보이지 않습니다)
1번째 줄: 1번째 줄:
/* =========================================
/* =========================================
COASTLINE: BLACK ICE - CategoryNav
COASTLINE: BLACK ICE - CategoryNav
대문 카테고리 네비 DOM 생성기
대문 카테고리 네비 SVG 생성기
========================================= */
========================================= */


8번째 줄: 8번째 줄:
-----------------------------------------
-----------------------------------------
이 파일은 대문 카테고리 네비를 MediaWiki 본문 위키문법이 아니라
이 파일은 대문 카테고리 네비를 MediaWiki 본문 위키문법이 아니라
JavaScript로 생성한다.
JavaScript + SVG로 생성한다.


JS를 쓰는가
SVG를 쓰는가
-----------------------------------------
-----------------------------------------
테스트 단계에서는 다음 방식들을 시도했다.
CSS 박스 기반 구현에서 다음 문제가 반복되었다.


1. 본문에 <span class="dir-link">[[문서|라벨]]</span>직접 작성
1. span 안에 [[문서|라벨]]을 넣는 방식
   - MediaWiki가 내부에 <a>를 자동 생성한다.
   - MediaWiki가 내부에 <a>를 자동 생성한다.
   - span은 모양을 담당하고, a는 클릭을 담당하게 된다.
   - span은 모양을 담당하고, a는 클릭을 담당하게 된다.
   - 결과적으로 보이는 면, 호버 , 클릭 면이 서로 분리되었다.
   - 실제 클릭 영역, 호버 영역, 시각 형태가 서로 분리되었다.


2. span에 clip-path를 주고, 검은 획은 span::after로 처리
2. <a class="...">를 본문에 직접 넣는 방식
   - clip-path가 ::after까지 같이 잘라먹었다.
   - MediaWiki 본문에서 안정적으로 먹히지 않았다.
   - 왼쪽 항목의 구분 획이 사라지거나, 일부만 보였다.
   - 위키 문법 처리와 충돌해 코드가 무효화되는 문제가 있었다.


3. 검은 획과 호버를 별도 overlay로 분리
3. clip-path + ::after 방식
   - 호버 좌표와 구분선 좌표를 직접 계산해야 했다.
   - clip-path가 검은 획 pseudo-element까지 잘라먹었다.
  - 프로젝트 버튼은 가운데 남은 공간을 차지하므로 폭이 유동적이다.
   - 왼쪽 항목의 획이 사라지거나 일부만 보였다.
   - 수동 좌표는 화면 폭이 바뀔 때 다시 틀어질 가능성이 높았다.


4. 버튼들을 음수 margin으로 겹침
4. 버튼을 음수 margin으로 겹치는 방식
   - 잘린 사선 영역의 공간은 메웠다.
   - 사선 빈 영역은 메웠지만, 호버가 옆 칸으로 넘어갔다.
  - 대신 호버 배경이 옆 칸으로 살짝 넘어갔다.


결론
5. hover overlay / divider overlay 방식
  - CSS grid, absolute 좌표, 프로젝트 가변폭이 서로 다른 기준을 가져서
    호버 면과 검은 획이 계속 어긋났다.
 
SVG 방식의 장점
-----------------------------------------
-----------------------------------------
카테고리 항목 하나가 다음 역할을 모두 맡아야 한다.
SVG에서는 각 항목을 polygon으로 만든다.
따라서 다음 요소들이 모두 같은 좌표계를 사용한다.


- 실제 링크
- 보이는 버튼 면
- 실제 클릭 영역
- 실제 hover 면
- 실제 호버 영역
- 실제 click 면
- 실제 사선 형태
- 검은 구분선


그러려면 JS가 <a class="portal-cat-link ...">를 직접 만들어야 한다.
즉, 이전처럼 사각형 박스를 사선처럼 보이게 흉내 내는 방식이 아니라,
CSS는 이 실제 a 요소에 clip-path와 hover를 적용한다.
처음부터 사선 polygon을 생성한다.


JS의 역할
15% 확장 처리
-----------------------------------------
-----------------------------------------
JS는 구조만 만든다.
프로젝트를 제외한 6개 카테고리 버튼의 폭은 이 파일의 idealCell 값으로 정한다.
, 표면, 호버, 사선 모양은 MainPage.css가 담당한다.
기존 기준은 163px이었고, 여기서 약 15% 확대한 값은 다음과 같다.
JS에서 좌표 계산이나 시각 보정은 하지 않는다.
 
163 * 1.15 = 187.45
 
렌더링 기준값은 188px로 둔다.
 
중요:
MainPage.css는 SVG 표시 규칙만 담당한다.
좌표성 변수나 버튼 폭 계산은 MainPage.css에 두지 않는다.
*/
*/


54번째 줄: 64번째 줄:
     'use strict';
     'use strict';


     /*
     var SVG_NS = 'http://www.w3.org/2000/svg';
    기본 라벨과 링크
     var XLINK_NS = 'http://www.w3.org/1999/xlink';
    -----------------------------------------
    여기서는 한국어를 기본값으로 둔다.
 
    나중에 다국어 구조가 안정되면
    window.LANG, window.CAT_LINKS 또는 별도 MainPageConfig.js에서
    라벨과 링크를 받아오게 확장할 수 있다.
 
    현재 Lang.js에는 일부 카테고리 라벨과 CAT_LINKS가 있지만,
     '설정'과 '프로젝트'처럼 이번 대문 네비에 새로 들어간 항목은
    기존 CAT_LINKS에 완전히 대응되어 있지 않을 수 있다.
 
    따라서 이 파일은 자체 fallback을 가진다.
    */


     var CATEGORY_NAV_ITEMS = [
     var CATEGORY_NAV_ITEMS = [
74번째 줄: 71번째 줄:
             key: 'history',
             key: 'history',
             label: '역사적 사건',
             label: '역사적 사건',
             title: '역사적_사건',
             title: '역사적_사건'
            className: 'portal-cat-left-start'
         },
         },
         {
         {
             key: 'settings',
             key: 'settings',
             label: '설정',
             label: '설정',
             title: '설정',
             title: '설정'
            className: 'portal-cat-left-mid'
         },
         },
         {
         {
             key: 'nations',
             key: 'nations',
             label: '국가 및 조합',
             label: '국가 및 조합',
             title: '국가_및_조합',
             title: '국가_및_조합'
            className: 'portal-cat-left-end'
         },
         },
         {
         {
             key: 'project',
             key: 'project',
             label: '프로젝트',
             label: '프로젝트',
             title: '프로젝트:소개',
             title: '프로젝트:소개'
            className: 'portal-cat-project'
         },
         },
         {
         {
             key: 'corporations',
             key: 'corporations',
             label: '기업 및 공동체',
             label: '기업 및 공동체',
             title: '기업_및_공동체',
             title: '기업_및_공동체'
            className: 'portal-cat-right-start'
         },
         },
         {
         {
             key: 'military',
             key: 'military',
             label: '군, 정치집단',
             label: '군, 정치집단',
             title: '군_정치집단',
             title: '군_정치집단'
            className: 'portal-cat-right-mid'
         },
         },
         {
         {
             key: 'personnel',
             key: 'personnel',
             label: '인물',
             label: '인물',
             title: '인물',
             title: '인물'
            className: 'portal-cat-right-end'
         }
         }
     ];
     ];
    /*
    언어 감지
    -----------------------------------------
    기존 Common.js에 getCurrentLang() 함수가 있을 수 있다.
    하지만 이 파일은 단독으로 로드되어도 깨지지 않아야 하므로
    함수 존재 여부를 확인하고 fallback을 둔다.
    */


     function getCurrentLanguageSafe() {
     function getCurrentLanguageSafe() {
135번째 줄: 117번째 줄:


         return 'ko';
         return 'ko';
    }
    function buildPath(title) {
        if (typeof window.buildWikiPath === 'function') {
            return window.buildWikiPath(title);
        }
        return '/index.php/' + encodeURI(String(title || '').replace(/ /g, '_'));
    }
    function getLabel(item, lang) {
        /*
        현재는 한국어 대문 테스트를 기준으로 한다.
        다국어 구조가 확정되기 전까지는 한국어 fallback을 유지한다.
        일부 항목만 번역하면 네비 폭과 의미가 언어별로 흔들릴 수 있다.
        */
        return item.label;
    }
    function readWidth(el) {
        if (!el) return 0;
        var rect = el.getBoundingClientRect ? el.getBoundingClientRect() : null;
        var width = rect ? Math.floor(rect.width || 0) : 0;
        if (!width && el.clientWidth) {
            width = Math.floor(el.clientWidth);
        }
        return width || 0;
     }
     }


     /*
     /*
     링크 생성
     폭 측정
     -----------------------------------------
     -----------------------------------------
     MediaWiki 내부 링크는 /index.php/문서명 형식으로 맞춘다.
     nav 자체 폭이 0이면 상위 요소들을 차례로 확인한다.
 
    마지막에는 viewport 폭을 사용한다.
    이 fallback은 nav 배경만 보이고 SVG가 생성되지 않는 상태를 막기 위한 안전장치다.
    실제 레이아웃 폭이 나중에 잡히면 ResizeObserver가 다시 렌더링한다.
    */


     buildWikiPath()가 이미 전역에 있으면 그것을 사용한다.
     function getRenderableWidth(nav, mount) {
    기존 Common.js에는 buildWikiPath(title) 함수가 존재할 수 있으므로
        var candidates = [
    중복 구현을 피한다.
            nav,
            mount,
            mount ? mount.parentElement : null,
            mount ? mount.closest('.main-portal') : null,
            mount ? mount.closest('.stylelab') : null,
            document.querySelector('.liberty-content-main .mw-parser-output'),
            document.querySelector('.liberty-content-main'),
            document.querySelector('.liberty-content')
        ];


    단, 이 파일은 단독 테스트도 가능해야 하므로 fallback도 제공한다.
        for (var i = 0; i < candidates.length; i++) {
    */
            var width = readWidth(candidates[i]);
 
            if (width > 0) {
                return width;
            }
        }


    function buildPath(title) {
         if (document.documentElement && document.documentElement.clientWidth) {
         if (typeof window.buildWikiPath === 'function') {
             return Math.max(320, Math.floor(document.documentElement.clientWidth - 48));
             return window.buildWikiPath(title);
         }
         }


         return '/index.php/' + encodeURI(String(title || '').replace(/ /g, '_'));
         return 1200;
     }
     }


     /*
     /*
     라벨 선택
     좌표 계산
     -----------------------------------------
     -----------------------------------------
     지금은 한국어 대문 테스트를 우선한다.
     idealCell = 188
    프로젝트를 제외한 6개 버튼의 기준 폭이다.
 
    계산 원칙:
    - 좌측 3개와 우측 3개는 같은 폭이다.
    - 프로젝트는 남은 중앙 폭을 차지한다.
    - 화면이 너무 좁으면 cellW를 줄인다.
    - cut 값은 지나치게 크거나 작아지지 않도록 제한한다.
    */


     향후 다국어 확장 시에는 이 함수에서
     function calculateGeometry(width) {
    window.LANG[lang][key] 또는 별도 navLabels를 읽으면 된다.
        var h = 30;
        var idealCell = 188;
        var minCell = 96;
        var minProject = 180;
        var usableWidth = Math.max(320, width);


    현재는 기존 LANG 키와 완전히 맞지 않는 항목이 있으므로,
        var cellW = idealCell;
    섣불리 LANG만 신뢰하지 않는다.
 
    */
        if ((cellW * 6) + minProject > usableWidth) {
            cellW = Math.floor((usableWidth - minProject) / 6);
            cellW = Math.max(minCell, cellW);
        }
 
        var projectW = usableWidth - (cellW * 6);


    function getLabel(item, lang) {
         if (projectW < 100) {
         if (lang === 'ko') {
             projectW = 100;
             return item.label;
         }
         }


         /*
         var totalW = (cellW * 6) + projectW;
         다국어 임시 fallback.
 
         실서비스 다국어 도입 전까지는 한국어 라벨을 유지한다.
         var cut = Math.round(cellW * 0.061);
         cut = Math.max(6, Math.min(10, cut));
 
        var x0 = 0;
        var x1 = cellW;
        var x2 = cellW * 2;
        var x3 = cellW * 3;
        var x4 = x3 + projectW;
        var x5 = x4 + cellW;
        var x6 = x4 + (cellW * 2);
        var x7 = x4 + (cellW * 3);


         이유:
         return {
        - 라벨을 일부만 번역하면 네비 구조가 언어별로 흔들린다.
            width: totalW,
        - 오른쪽 보조 라벨에 영어를 넣었다가 다국어 일관성 문제가 생겼던 것처럼,
            height: h,
          아직 전체 번역 체계가 확정되지 않은 상태에서는 부분 번역을 피한다.
            cellW: cellW,
        */
            projectW: projectW,
         return item.label;
            cut: cut,
            x: [x0, x1, x2, x3, x4, x5, x6, x7]
         };
     }
     }


     /*
     /*
     단일 mount 렌더링
     polygon 좌표
     -----------------------------------------
     -----------------------------------------
     data-component="category-nav"가 붙은 요소를 찾아
     요구 형태:
     내부를 비우고 nav를 생성한다.
     왼쪽군:  \ \ \
    프로젝트: \ PROJECT /
    오른쪽군: / / /


     같은 페이지에서 SPA 이동이나 wikipage.content 훅으로
     좌표 설명:
     여러 번 호출될 수 있으므로 매번 내부를 재생성해도 안전하게 한다.
    - \ 경계는 위쪽 x가 왼쪽, 아래쪽 x가 오른쪽이다.
     - / 경계는 위쪽 x가 오른쪽, 아래쪽 x가 왼쪽이다.
     */
     */


     function renderCategoryNavMount(mount) {
     function getSegmentPoints(index, g) {
         if (!mount) return;
        var x = g.x;
        var c = g.cut;
        var h = g.height;
 
         if (index === 0) {
            return [
                [x[0], 0],
                [x[1] - c, 0],
                [x[1], h],
                [x[0], h]
            ];
        }
 
        if (index === 1) {
            return [
                [x[1] - c, 0],
                [x[2] - c, 0],
                [x[2], h],
                [x[1], h]
            ];
        }
 
        if (index === 2) {
            return [
                [x[2] - c, 0],
                [x[3] - c, 0],
                [x[3], h],
                [x[2], h]
            ];
        }
 
        if (index === 3) {
            return [
                [x[3] - c, 0],
                [x[4], 0],
                [x[4] - c, h],
                [x[3], h]
            ];
        }
 
        if (index === 4) {
            return [
                [x[4], 0],
                [x[5], 0],
                [x[5] - c, h],
                [x[4] - c, h]
            ];
        }
 
        if (index === 5) {
            return [
                [x[5], 0],
                [x[6], 0],
                [x[6] - c, h],
                [x[5] - c, h]
            ];
        }
 
        return [
            [x[6], 0],
            [x[7], 0],
            [x[7], h],
            [x[6] - c, h]
        ];
    }
 
    function pointsToString(points) {
        return points.map(function (point) {
            return point[0] + ',' + point[1];
        }).join(' ');
    }
 
    function getTextX(index, g) {
        var x = g.x;
 
        if (index === 0) return (x[0] + x[1]) / 2;
        if (index === 1) return (x[1] + x[2]) / 2;
        if (index === 2) return (x[2] + x[3]) / 2;
        if (index === 3) return (x[3] + x[4]) / 2;
        if (index === 4) return (x[4] + x[5]) / 2;
        if (index === 5) return (x[5] + x[6]) / 2;
        return (x[6] + x[7]) / 2;
    }
 
    function createSvgElement(name) {
        return document.createElementNS(SVG_NS, name);
    }
 
    function createSegment(item, index, g, lang) {
        var anchor = createSvgElement('a');
        var polygon = createSvgElement('polygon');
        var text = createSvgElement('text');
        var href = buildPath(item.title);
        var label = getLabel(item, lang);
 
        anchor.setAttribute('class', 'portal-cat-anchor');
        anchor.setAttribute('href', href);
        anchor.setAttributeNS(XLINK_NS, 'xlink:href', href);
        anchor.setAttribute('aria-label', label);
        anchor.setAttribute('data-category-key', item.key);
 
        polygon.setAttribute('class', 'portal-cat-shape');
        polygon.setAttribute('points', pointsToString(getSegmentPoints(index, g)));
        polygon.setAttribute('fill', 'transparent');
        polygon.setAttribute('pointer-events', 'all');
 
        text.setAttribute(
            'class',
            item.key === 'project'
                ? 'portal-cat-label portal-cat-project-label'
                : 'portal-cat-label'
        );
 
        text.setAttribute('x', getTextX(index, g));
        text.setAttribute('y', g.height / 2 + 1);
        text.setAttribute('fill', '#e2e2e2');
        text.setAttribute('font-size', item.key === 'project' ? '11' : '10');
        text.setAttribute('font-weight', '700');
        text.setAttribute('text-anchor', 'middle');
        text.setAttribute('dominant-baseline', 'middle');
        text.setAttribute('pointer-events', 'none');
        text.setAttribute('paint-order', 'stroke');
        text.setAttribute('stroke', '#000000');
        text.setAttribute('stroke-width', '2');
        text.setAttribute('stroke-linejoin', 'round');
        text.textContent = label;
 
        anchor.appendChild(polygon);
        anchor.appendChild(text);
 
        return anchor;
    }
 
    function createDivider(x1, y1, x2, y2, side) {
        var line = createSvgElement('line');
 
        line.setAttribute('class', 'portal-cat-divider portal-cat-divider-' + side);
        line.setAttribute('x1', x1);
        line.setAttribute('y1', y1);
        line.setAttribute('x2', x2);
        line.setAttribute('y2', y2);
        line.setAttribute('stroke', '#050505');
        line.setAttribute('stroke-width', '1');
        line.setAttribute('pointer-events', 'none');
        line.setAttribute('vector-effect', 'non-scaling-stroke');
 
        return line;
    }
 
    function appendDividers(svg, g) {
        var x = g.x;
        var c = g.cut;
        var h = g.height;
 
        /*
        왼쪽 3개 구분선: \ 방향
        */
        svg.appendChild(createDivider(x[1] - c, 0, x[1], h, 'left'));
        svg.appendChild(createDivider(x[2] - c, 0, x[2], h, 'left'));
        svg.appendChild(createDivider(x[3] - c, 0, x[3], h, 'left'));
 
        /*
        오른쪽 3개 구분선: / 방향
        */
        svg.appendChild(createDivider(x[4], 0, x[4] - c, h, 'right'));
        svg.appendChild(createDivider(x[5], 0, x[5] - c, h, 'right'));
        svg.appendChild(createDivider(x[6], 0, x[6] - c, h, 'right'));
    }
 
    function clearNode(node) {
        while (node.firstChild) {
            node.removeChild(node.firstChild);
        }
    }


    function buildSvg(width) {
         var lang = getCurrentLanguageSafe();
         var lang = getCurrentLanguageSafe();
         var nav = document.createElement('nav');
         var g = calculateGeometry(width);
        var svg = createSvgElement('svg');
 
        svg.setAttribute('class', 'portal-category-svg');
        svg.setAttribute('viewBox', '0 0 ' + g.width + ' ' + g.height);
        svg.setAttribute('width', '100%');
        svg.setAttribute('height', '100%');
        svg.setAttribute('preserveAspectRatio', 'none');
        svg.setAttribute('aria-hidden', 'false');
 
        CATEGORY_NAV_ITEMS.forEach(function (item, index) {
            svg.appendChild(createSegment(item, index, g, lang));
        });
 
        appendDividers(svg, g);
 
        return {
            svg: svg,
            key: width + '|' + g.width + '|' + g.height + '|' + g.cellW + '|' + g.projectW + '|' + g.cut
        };
    }


         nav.className = 'portal-category-nav';
    function renderSvgIntoNav(nav, mount) {
         nav.setAttribute('aria-label', 'Main categories');
         var measuredWidth = getRenderableWidth(nav, mount);
         var result = buildSvg(measuredWidth);


         CATEGORY_NAV_ITEMS.forEach(function (item) {
         if (nav.__categoryNavRenderKey === result.key && nav.querySelector('svg')) {
            var link = document.createElement('a');
            return;
        }


            link.className = 'portal-cat-link ' + item.className;
        nav.__categoryNavRenderKey = result.key;
            link.href = buildPath(item.title);
        clearNode(nav);
            link.textContent = getLabel(item, lang);
        nav.appendChild(result.svg);
    }


            /*
    function scheduleRender(nav, mount) {
            data-key는 CSS가 아니라 디버깅과 향후 다국어 갱신용이다.
        if (!nav || nav.__categoryNavFrame) {
             나중에 라벨만 다시 바꿔야 할 때 이 key를 기준으로 찾을 수 있다.
             return;
            */
        }
            link.setAttribute('data-category-key', item.key);


             nav.appendChild(link);
        nav.__categoryNavFrame = window.requestAnimationFrame(function () {
             nav.__categoryNavFrame = null;
            renderSvgIntoNav(nav, mount);
         });
         });
    }
    function renderCategoryNavMount(mount) {
        if (!mount) return;
        if (mount.__categoryNavObserver) {
            mount.__categoryNavObserver.disconnect();
            mount.__categoryNavObserver = null;
        }
        clearNode(mount);
        var nav = document.createElement('nav');
        nav.className = 'portal-category-nav';
        nav.setAttribute('aria-label', 'Main categories');


        mount.innerHTML = '';
         mount.appendChild(nav);
         mount.appendChild(nav);
         mount.setAttribute('data-category-nav-rendered', '1');
         mount.setAttribute('data-category-nav-rendered', '1');
    }


    /*
        scheduleRender(nav, mount);
    전체 렌더링
 
    -----------------------------------------
        /*
    대문 본문에는 아래처럼 자리만 남긴다.
        첫 렌더 직후 한 번 더 예약한다.
        MediaWiki/Liberty/SPA 환경에서는 본문이 삽입된 직후와
        실제 레이아웃 폭이 확정된 시점이 다를 수 있다.
        */
        window.setTimeout(function () {
            scheduleRender(nav, mount);
        }, 80);
 
        window.setTimeout(function () {
            scheduleRender(nav, mount);
        }, 250);


    <div data-component="category-nav"></div>
        if (window.ResizeObserver) {
            var observer = new ResizeObserver(function () {
                scheduleRender(nav, mount);
            });


    이 함수는 그 mount를 모두 찾아 렌더링한다.
            observer.observe(mount);
     */
            observer.observe(nav);
            mount.__categoryNavObserver = observer;
        } else {
            window.addEventListener('resize', function () {
                scheduleRender(nav, mount);
            });
        }
     }


     function renderAllCategoryNavs(root) {
     function renderAllCategoryNavs(root) {
244번째 줄: 536번째 줄:
         });
         });
     }
     }
    /*
    초기화
    -----------------------------------------
    1. 일반 페이지 로드 시 DOMContentLoaded에서 실행한다.
    2. MediaWiki SPA/동적 본문 갱신을 고려해 wikipage.content 훅에서도 실행한다.
    3. 전역 객체에도 init 함수를 노출해 디버깅이나 수동 재실행이 가능하게 한다.
    */


     window.CLBI = window.CLBI || {};
     window.CLBI = window.CLBI || {};

2026년 5월 19일 (화) 08:18 기준 최신판

/* =========================================
COASTLINE: BLACK ICE - CategoryNav
대문 카테고리 네비 SVG 생성기
========================================= */

/*
설계 목적
-----------------------------------------
이 파일은 대문 카테고리 네비를 MediaWiki 본문 위키문법이 아니라
JavaScript + SVG로 생성한다.

왜 SVG를 쓰는가
-----------------------------------------
CSS 박스 기반 구현에서 다음 문제가 반복되었다.

1. span 안에 [[문서|라벨]]을 넣는 방식
   - MediaWiki가 내부에 <a>를 자동 생성한다.
   - span은 모양을 담당하고, a는 클릭을 담당하게 된다.
   - 실제 클릭 영역, 호버 영역, 시각 형태가 서로 분리되었다.

2. <a class="...">를 본문에 직접 넣는 방식
   - MediaWiki 본문에서 안정적으로 먹히지 않았다.
   - 위키 문법 처리와 충돌해 코드가 무효화되는 문제가 있었다.

3. clip-path + ::after 방식
   - clip-path가 검은 획 pseudo-element까지 잘라먹었다.
   - 왼쪽 항목의 획이 사라지거나 일부만 보였다.

4. 버튼을 음수 margin으로 겹치는 방식
   - 사선 빈 영역은 메웠지만, 호버가 옆 칸으로 넘어갔다.

5. hover overlay / divider overlay 방식
   - CSS grid, absolute 좌표, 프로젝트 가변폭이 서로 다른 기준을 가져서
     호버 면과 검은 획이 계속 어긋났다.

SVG 방식의 장점
-----------------------------------------
SVG에서는 각 항목을 polygon으로 만든다.
따라서 다음 요소들이 모두 같은 좌표계를 사용한다.

- 보이는 버튼 면
- 실제 hover 면
- 실제 click 면
- 검은 구분선

즉, 이전처럼 사각형 박스를 사선처럼 보이게 흉내 내는 방식이 아니라,
처음부터 사선 polygon을 생성한다.

15% 확장 처리
-----------------------------------------
프로젝트를 제외한 6개 카테고리 버튼의 폭은 이 파일의 idealCell 값으로 정한다.
기존 기준은 163px이었고, 여기서 약 15% 확대한 값은 다음과 같다.

163 * 1.15 = 187.45

렌더링 기준값은 188px로 둔다.

중요:
MainPage.css는 SVG 표시 규칙만 담당한다.
좌표성 변수나 버튼 폭 계산은 MainPage.css에 두지 않는다.
*/

(function () {
    'use strict';

    var SVG_NS = 'http://www.w3.org/2000/svg';
    var XLINK_NS = 'http://www.w3.org/1999/xlink';

    var CATEGORY_NAV_ITEMS = [
        {
            key: 'history',
            label: '역사적 사건',
            title: '역사적_사건'
        },
        {
            key: 'settings',
            label: '설정',
            title: '설정'
        },
        {
            key: 'nations',
            label: '국가 및 조합',
            title: '국가_및_조합'
        },
        {
            key: 'project',
            label: '프로젝트',
            title: '프로젝트:소개'
        },
        {
            key: 'corporations',
            label: '기업 및 공동체',
            title: '기업_및_공동체'
        },
        {
            key: 'military',
            label: '군, 정치집단',
            title: '군_정치집단'
        },
        {
            key: 'personnel',
            label: '인물',
            title: '인물'
        }
    ];

    function getCurrentLanguageSafe() {
        if (typeof window.getCurrentLang === 'function') {
            return window.getCurrentLang();
        }

        var langData = document.getElementById('clbi-lang-data');

        if (langData) {
            return langData.getAttribute('data-lang') || 'ko';
        }

        return 'ko';
    }

    function buildPath(title) {
        if (typeof window.buildWikiPath === 'function') {
            return window.buildWikiPath(title);
        }

        return '/index.php/' + encodeURI(String(title || '').replace(/ /g, '_'));
    }

    function getLabel(item, lang) {
        /*
        현재는 한국어 대문 테스트를 기준으로 한다.

        다국어 구조가 확정되기 전까지는 한국어 fallback을 유지한다.
        일부 항목만 번역하면 네비 폭과 의미가 언어별로 흔들릴 수 있다.
        */
        return item.label;
    }

    function readWidth(el) {
        if (!el) return 0;

        var rect = el.getBoundingClientRect ? el.getBoundingClientRect() : null;
        var width = rect ? Math.floor(rect.width || 0) : 0;

        if (!width && el.clientWidth) {
            width = Math.floor(el.clientWidth);
        }

        return width || 0;
    }

    /*
    폭 측정
    -----------------------------------------
    nav 자체 폭이 0이면 상위 요소들을 차례로 확인한다.

    마지막에는 viewport 폭을 사용한다.
    이 fallback은 nav 배경만 보이고 SVG가 생성되지 않는 상태를 막기 위한 안전장치다.
    실제 레이아웃 폭이 나중에 잡히면 ResizeObserver가 다시 렌더링한다.
    */

    function getRenderableWidth(nav, mount) {
        var candidates = [
            nav,
            mount,
            mount ? mount.parentElement : null,
            mount ? mount.closest('.main-portal') : null,
            mount ? mount.closest('.stylelab') : null,
            document.querySelector('.liberty-content-main .mw-parser-output'),
            document.querySelector('.liberty-content-main'),
            document.querySelector('.liberty-content')
        ];

        for (var i = 0; i < candidates.length; i++) {
            var width = readWidth(candidates[i]);

            if (width > 0) {
                return width;
            }
        }

        if (document.documentElement && document.documentElement.clientWidth) {
            return Math.max(320, Math.floor(document.documentElement.clientWidth - 48));
        }

        return 1200;
    }

    /*
    좌표 계산
    -----------------------------------------
    idealCell = 188
    프로젝트를 제외한 6개 버튼의 기준 폭이다.

    계산 원칙:
    - 좌측 3개와 우측 3개는 같은 폭이다.
    - 프로젝트는 남은 중앙 폭을 차지한다.
    - 화면이 너무 좁으면 cellW를 줄인다.
    - cut 값은 지나치게 크거나 작아지지 않도록 제한한다.
    */

    function calculateGeometry(width) {
        var h = 30;
        var idealCell = 188;
        var minCell = 96;
        var minProject = 180;
        var usableWidth = Math.max(320, width);

        var cellW = idealCell;

        if ((cellW * 6) + minProject > usableWidth) {
            cellW = Math.floor((usableWidth - minProject) / 6);
            cellW = Math.max(minCell, cellW);
        }

        var projectW = usableWidth - (cellW * 6);

        if (projectW < 100) {
            projectW = 100;
        }

        var totalW = (cellW * 6) + projectW;

        var cut = Math.round(cellW * 0.061);
        cut = Math.max(6, Math.min(10, cut));

        var x0 = 0;
        var x1 = cellW;
        var x2 = cellW * 2;
        var x3 = cellW * 3;
        var x4 = x3 + projectW;
        var x5 = x4 + cellW;
        var x6 = x4 + (cellW * 2);
        var x7 = x4 + (cellW * 3);

        return {
            width: totalW,
            height: h,
            cellW: cellW,
            projectW: projectW,
            cut: cut,
            x: [x0, x1, x2, x3, x4, x5, x6, x7]
        };
    }

    /*
    polygon 좌표
    -----------------------------------------
    요구 형태:
    왼쪽군:   \ \ \
    프로젝트: \ PROJECT /
    오른쪽군: / / /

    좌표 설명:
    - \ 경계는 위쪽 x가 왼쪽, 아래쪽 x가 오른쪽이다.
    - / 경계는 위쪽 x가 오른쪽, 아래쪽 x가 왼쪽이다.
    */

    function getSegmentPoints(index, g) {
        var x = g.x;
        var c = g.cut;
        var h = g.height;

        if (index === 0) {
            return [
                [x[0], 0],
                [x[1] - c, 0],
                [x[1], h],
                [x[0], h]
            ];
        }

        if (index === 1) {
            return [
                [x[1] - c, 0],
                [x[2] - c, 0],
                [x[2], h],
                [x[1], h]
            ];
        }

        if (index === 2) {
            return [
                [x[2] - c, 0],
                [x[3] - c, 0],
                [x[3], h],
                [x[2], h]
            ];
        }

        if (index === 3) {
            return [
                [x[3] - c, 0],
                [x[4], 0],
                [x[4] - c, h],
                [x[3], h]
            ];
        }

        if (index === 4) {
            return [
                [x[4], 0],
                [x[5], 0],
                [x[5] - c, h],
                [x[4] - c, h]
            ];
        }

        if (index === 5) {
            return [
                [x[5], 0],
                [x[6], 0],
                [x[6] - c, h],
                [x[5] - c, h]
            ];
        }

        return [
            [x[6], 0],
            [x[7], 0],
            [x[7], h],
            [x[6] - c, h]
        ];
    }

    function pointsToString(points) {
        return points.map(function (point) {
            return point[0] + ',' + point[1];
        }).join(' ');
    }

    function getTextX(index, g) {
        var x = g.x;

        if (index === 0) return (x[0] + x[1]) / 2;
        if (index === 1) return (x[1] + x[2]) / 2;
        if (index === 2) return (x[2] + x[3]) / 2;
        if (index === 3) return (x[3] + x[4]) / 2;
        if (index === 4) return (x[4] + x[5]) / 2;
        if (index === 5) return (x[5] + x[6]) / 2;
        return (x[6] + x[7]) / 2;
    }

    function createSvgElement(name) {
        return document.createElementNS(SVG_NS, name);
    }

    function createSegment(item, index, g, lang) {
        var anchor = createSvgElement('a');
        var polygon = createSvgElement('polygon');
        var text = createSvgElement('text');
        var href = buildPath(item.title);
        var label = getLabel(item, lang);

        anchor.setAttribute('class', 'portal-cat-anchor');
        anchor.setAttribute('href', href);
        anchor.setAttributeNS(XLINK_NS, 'xlink:href', href);
        anchor.setAttribute('aria-label', label);
        anchor.setAttribute('data-category-key', item.key);

        polygon.setAttribute('class', 'portal-cat-shape');
        polygon.setAttribute('points', pointsToString(getSegmentPoints(index, g)));
        polygon.setAttribute('fill', 'transparent');
        polygon.setAttribute('pointer-events', 'all');

        text.setAttribute(
            'class',
            item.key === 'project'
                ? 'portal-cat-label portal-cat-project-label'
                : 'portal-cat-label'
        );

        text.setAttribute('x', getTextX(index, g));
        text.setAttribute('y', g.height / 2 + 1);
        text.setAttribute('fill', '#e2e2e2');
        text.setAttribute('font-size', item.key === 'project' ? '11' : '10');
        text.setAttribute('font-weight', '700');
        text.setAttribute('text-anchor', 'middle');
        text.setAttribute('dominant-baseline', 'middle');
        text.setAttribute('pointer-events', 'none');
        text.setAttribute('paint-order', 'stroke');
        text.setAttribute('stroke', '#000000');
        text.setAttribute('stroke-width', '2');
        text.setAttribute('stroke-linejoin', 'round');
        text.textContent = label;

        anchor.appendChild(polygon);
        anchor.appendChild(text);

        return anchor;
    }

    function createDivider(x1, y1, x2, y2, side) {
        var line = createSvgElement('line');

        line.setAttribute('class', 'portal-cat-divider portal-cat-divider-' + side);
        line.setAttribute('x1', x1);
        line.setAttribute('y1', y1);
        line.setAttribute('x2', x2);
        line.setAttribute('y2', y2);
        line.setAttribute('stroke', '#050505');
        line.setAttribute('stroke-width', '1');
        line.setAttribute('pointer-events', 'none');
        line.setAttribute('vector-effect', 'non-scaling-stroke');

        return line;
    }

    function appendDividers(svg, g) {
        var x = g.x;
        var c = g.cut;
        var h = g.height;

        /*
        왼쪽 3개 구분선: \ 방향
        */
        svg.appendChild(createDivider(x[1] - c, 0, x[1], h, 'left'));
        svg.appendChild(createDivider(x[2] - c, 0, x[2], h, 'left'));
        svg.appendChild(createDivider(x[3] - c, 0, x[3], h, 'left'));

        /*
        오른쪽 3개 구분선: / 방향
        */
        svg.appendChild(createDivider(x[4], 0, x[4] - c, h, 'right'));
        svg.appendChild(createDivider(x[5], 0, x[5] - c, h, 'right'));
        svg.appendChild(createDivider(x[6], 0, x[6] - c, h, 'right'));
    }

    function clearNode(node) {
        while (node.firstChild) {
            node.removeChild(node.firstChild);
        }
    }

    function buildSvg(width) {
        var lang = getCurrentLanguageSafe();
        var g = calculateGeometry(width);
        var svg = createSvgElement('svg');

        svg.setAttribute('class', 'portal-category-svg');
        svg.setAttribute('viewBox', '0 0 ' + g.width + ' ' + g.height);
        svg.setAttribute('width', '100%');
        svg.setAttribute('height', '100%');
        svg.setAttribute('preserveAspectRatio', 'none');
        svg.setAttribute('aria-hidden', 'false');

        CATEGORY_NAV_ITEMS.forEach(function (item, index) {
            svg.appendChild(createSegment(item, index, g, lang));
        });

        appendDividers(svg, g);

        return {
            svg: svg,
            key: width + '|' + g.width + '|' + g.height + '|' + g.cellW + '|' + g.projectW + '|' + g.cut
        };
    }

    function renderSvgIntoNav(nav, mount) {
        var measuredWidth = getRenderableWidth(nav, mount);
        var result = buildSvg(measuredWidth);

        if (nav.__categoryNavRenderKey === result.key && nav.querySelector('svg')) {
            return;
        }

        nav.__categoryNavRenderKey = result.key;
        clearNode(nav);
        nav.appendChild(result.svg);
    }

    function scheduleRender(nav, mount) {
        if (!nav || nav.__categoryNavFrame) {
            return;
        }

        nav.__categoryNavFrame = window.requestAnimationFrame(function () {
            nav.__categoryNavFrame = null;
            renderSvgIntoNav(nav, mount);
        });
    }

    function renderCategoryNavMount(mount) {
        if (!mount) return;

        if (mount.__categoryNavObserver) {
            mount.__categoryNavObserver.disconnect();
            mount.__categoryNavObserver = null;
        }

        clearNode(mount);

        var nav = document.createElement('nav');
        nav.className = 'portal-category-nav';
        nav.setAttribute('aria-label', 'Main categories');

        mount.appendChild(nav);
        mount.setAttribute('data-category-nav-rendered', '1');

        scheduleRender(nav, mount);

        /*
        첫 렌더 직후 한 번 더 예약한다.
        MediaWiki/Liberty/SPA 환경에서는 본문이 삽입된 직후와
        실제 레이아웃 폭이 확정된 시점이 다를 수 있다.
        */
        window.setTimeout(function () {
            scheduleRender(nav, mount);
        }, 80);

        window.setTimeout(function () {
            scheduleRender(nav, mount);
        }, 250);

        if (window.ResizeObserver) {
            var observer = new ResizeObserver(function () {
                scheduleRender(nav, mount);
            });

            observer.observe(mount);
            observer.observe(nav);
            mount.__categoryNavObserver = observer;
        } else {
            window.addEventListener('resize', function () {
                scheduleRender(nav, mount);
            });
        }
    }

    function renderAllCategoryNavs(root) {
        var scope = root || document;
        var mounts = scope.querySelectorAll('[data-component="category-nav"]');

        Array.prototype.forEach.call(mounts, function (mount) {
            renderCategoryNavMount(mount);
        });
    }

    window.CLBI = window.CLBI || {};
    window.CLBI.categoryNav = {
        init: renderAllCategoryNavs,
        renderMount: renderCategoryNavMount
    };

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', function () {
            renderAllCategoryNavs(document);
        });
    } else {
        renderAllCategoryNavs(document);
    }

    if (window.mw && mw.hook) {
        mw.hook('wikipage.content').add(function ($content) {
            var root = $content && $content[0] ? $content[0] : document;
            renderAllCategoryNavs(root);
        });
    }
}());