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

편집 요약 없음
편집 요약 없음
206번째 줄: 206번째 줄:
         if (location.pathname[0] == '/') return location.pathname.substring(1) + location.search;
         if (location.pathname[0] == '/') return location.pathname.substring(1) + location.search;
         else return location.pathname + location.search;
         else return location.pathname + location.search;
    }
}
// 앨범 레이아웃 보정
updateAllAlbum();
window.addEventListener('load', updateAllAlbum);
window.addEventListener('resize', updateAllAlbum);
function updateAllAlbum() {
    var $albums = document.querySelectorAll('.custom-album');
    for (var i = 0; i < $albums.length; i++) {
        updateAlbum($albums[i]);
    }
}
function updateAlbum($elem) {
    var lineMax = 3;
    for (var i = 0; i < $elem.classList.length; i++) {
        if ($elem.classList[i].indexOf('line-max-') == 0) {
            lineMax = Number($elem.classList[i].replace('line-max-', ''));
        }
    }
    if ($elem.children.length > 0) {
        for (var i = $elem.children.length - 1; i > 0; i--) {
            if (!$elem.children[i].classList.contains('albumitem')) {
                $elem.children[i].remove();
                continue;
            }
            $elem.children[i].style.width = '';
            $elem.children[i].style.height = '';
        }
        if (window.innerWidth < 720) return;
        var lineTotalRatio = [];
        for (var i = 0; i < $elem.children.length; i++) {
            var lineIdx = Math.floor(i / lineMax);
            var $img = $elem.children[i].querySelector('img');
            if (!$img || $img.naturalWidth == 0) continue;
            if (lineTotalRatio[lineIdx] == undefined) {
                lineTotalRatio[lineIdx] = 0;
            }
            lineTotalRatio[lineIdx] += $img.naturalWidth / $img.naturalHeight;
        }
        for (var i = 0; i < $elem.children.length; i++) {
            var totalRatio = lineTotalRatio[Math.floor(i / lineMax)];
            var $img = $elem.children[i].querySelector('img');
            if (!$img || $img.naturalWidth == 0) continue;
            var ratio = $img.naturalWidth / $img.naturalHeight;
            $elem.children[i].style.width = (ratio / totalRatio) * 100 + '%';
        }
     }
     }
}
}