Không có tóm lược sửa đổi
Không có tóm lược sửa đổi
 
Dòng 180: Dòng 180:
             container.textContent = 'Không thể tải bảng xếp hạng meme lúc này.';
             container.textContent = 'Không thể tải bảng xếp hạng meme lúc này.';
         });
         });
});
mw.loader.using(['mediawiki.api', 'mediawiki.util']).then(function () {
    var newPagesBox = document.getElementById('home-new-pages');
    if (!newPagesBox) return;
    new mw.Api().get({
        action: 'query',
        list: 'recentchanges',
        rctype: 'new',
        rcnamespace: 0, // Chỉ lấy bài viết nội dung chính, bỏ qua trang hệ thống/template
        rclimit: 5,
        rcprop: 'title|timestamp'
    }).done(function (data) {
        var list = (data.query && data.query.recentchanges) || [];
        if (!list.length) {
            newPagesBox.textContent = 'Chưa có bài viết mới nào.';
            return;
        }
        newPagesBox.textContent = '';
        list.forEach(function (item) {
            var row = document.createElement('div');
            row.className = 'new-page-item';
            var link = document.createElement('a');
            link.className = 'new-page-title';
            link.href = mw.util.getUrl(item.title);
            link.textContent = '📄 ' + item.title;
            var date = new Date(item.timestamp);
            var dateSpan = document.createElement('span');
            dateSpan.className = 'new-page-date';
            dateSpan.textContent = date.toLocaleDateString('vi-VN');
            row.appendChild(link);
            row.appendChild(dateSpan);
            newPagesBox.appendChild(row);
        });
    }).fail(function () {
        newPagesBox.textContent = 'Không thể tải danh sách bài viết mới.';
    });
});
});

Bản mới nhất lúc 07:45, ngày 20 tháng 11 năm 2026

/* Bất kỳ mã JavaScript ở đây sẽ được tải cho tất cả các thành viên khi tải một trang nào đó lên. */
mw.hook('wikipage.content').add(function ($content) {
    var $widgets = $content.find('.meme-goodreads-widget');
    if (!$widgets.length) return;

    var pageId = mw.config.get('wgArticleId');
    var apiUrl = mw.config.get('wgScriptPath') + '/meme_api.php';
    var loginUrl = mw.util.getUrl('Special:UserLogin', { returnto: mw.config.get('wgPageName') });

    $widgets.each(function () {
        var $root = $(this);
        if (!pageId) {
            $root.html('<div style="padding:10px;background:#f8f9fa;border:1px solid #eaecf0;color:#54595d;">Lưu bài viết để kích hoạt mục đóng góp và bình chọn Meme.</div>');
            return;
        }

        function loadMemes() {
            $.getJSON(apiUrl, { action: 'list', page_id: pageId }, function (res) {
                renderWidget(res.user, res.memes || []);
            });
        }

        function renderWidget(user, memes) {
            $root.empty();

            // Thanh công cụ phía trên
            var $toolbar = $('<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:12px;flex-wrap:wrap;gap:8px;"></div>');
            $toolbar.append($('<span style="font-size:90%;color:#54595d;"></span>').text('Tổng cộng: ' + memes.length + ' meme được đóng góp'));

            var $formBox = $('<div style="display:none;background:#f8f9fa;border:1px solid #a2a9b1;border-radius:4px;padding:12px;margin-bottom:15px;"></div>');

            if (user) {
                var $toggleBtn = $('<button type="button" style="cursor:pointer;background:#2a4b8d;color:#fff;border:none;border-radius:4px;padding:6px 12px;font-weight:bold;">+ Đóng góp Meme mới</button>');
                $toggleBtn.on('click', function () {
                    $formBox.slideToggle(150);
                });
                $toolbar.append($toggleBtn);

                // Form thêm meme mới
                var $contentInput = $('<textarea rows="3" placeholder="Nhập câu nói viral, copypasta hoặc mô tả meme..." style="width:100%;box-sizing:border-box;padding:8px;margin-bottom:8px;border:1px solid #a2a9b1;border-radius:4px;font-family:inherit;"></textarea>');
                var $contextInput = $('<input type="text" placeholder="Bối cảnh / Nguồn gốc (ví dụ: Livestream ngày 12/5, Video ABC...)" style="width:100%;box-sizing:border-box;padding:8px;margin-bottom:8px;border:1px solid #a2a9b1;border-radius:4px;">');
                var $submitBtn = $('<button type="button" style="cursor:pointer;background:#14866d;color:#fff;border:none;border-radius:4px;padding:6px 14px;font-weight:bold;">Đăng Meme</button>');

                $submitBtn.on('click', function () {
                    var contentVal = $.trim($contentInput.val());
                    if (!contentVal) {
                        alert('Vui lòng nhập nội dung meme!');
                        return;
                    }
                    $submitBtn.prop('disabled', true).text('Đang gửi...');
                    $.post(apiUrl + '?action=add', {
                        page_id: pageId,
                        content: contentVal,
                        context: $.trim($contextInput.val())
                    }, function () {
                        loadMemes();
                    }, 'json').fail(function (xhr) {
                        alert((xhr.responseJSON && xhr.responseJSON.error) || 'Có lỗi xảy ra.');
                        $submitBtn.prop('disabled', false).text('Đăng Meme');
                    });
                });

                $formBox.append($contentInput, $contextInput, $submitBtn);
            } else {
                var $loginNotice = $('<a style="font-size:90%;font-weight:bold;color:#2a4b8d;">Đăng nhập để bình chọn hoặc thêm Meme mới →</a>').attr('href', loginUrl);
                $toolbar.append($loginNotice);
            }

            $root.append($toolbar, $formBox);

            // Danh sách các thẻ Meme
            if (!memes.length) {
                $root.append('<div style="padding:15px;text-align:center;background:#f8f9fa;border:1px dashed #a2a9b1;border-radius:4px;color:#54595d;">Chưa có meme nào. Hãy là người đầu tiên đóng góp meme cho nhân vật này!</div>');
                return;
            }

            $.each(memes, function (_, m) {
                var $card = $('<div style="display:flex;justify-content:space-between;align-items:flex-start;gap:15px;background:#f8f9fa;border:1px solid #eaecf0;border-left:4px solid #2a4b8d;border-radius:4px;padding:12px 15px;margin-bottom:10px;"></div>');
                var $left = $('<div style="flex:1;"></div>');
                var $quote = $('<div style="font-size:105%;font-style:italic;color:#202122;margin-bottom:6px;white-space:pre-line;"></div>').text('“' + m.content + '”');

                var metaText = (m.context ? '— ' + m.context + ' • ' : '— ') + 'Đóng góp bởi ' + m.author_name;
                var $meta = $('<div style="font-size:85%;color:#54595d;"></div>').text(metaText);

                if (user && user.is_admin) {
                    var $delBtn = $('<a href="#" style="color:#d33;margin-left:10px;text-decoration:none;">[Xóa]</a>');
                    $delBtn.on('click', function (e) {
                        e.preventDefault();
                        if (confirm('Xóa meme này?')) {
                            $.post(apiUrl + '?action=delete', { meme_id: m.id }, loadMemes, 'json');
                        }
                    });
                    $meta.append($delBtn);
                }

                $left.append($quote, $meta);

                var isVoted = parseInt(m.user_voted, 10) === 1;
                var $voteBtn = $('<button type="button" style="cursor:pointer;border:1px solid #a2a9b1;border-radius:4px;padding:6px 12px;font-weight:bold;white-space:nowrap;"></button>')
                    .text('▲ Vote (' + m.vote_count + ')')
                    .css(isVoted ? { background: '#2a4b8d', color: '#fff', borderColor: '#2a4b8d' } : { background: '#eaecf0', color: '#202122' });

                $voteBtn.on('click', function () {
                    if (!user) {
                        if (confirm('Bạn cần đăng nhập để bình chọn. Chuyển đến trang đăng nhập ngay?')) {
                            window.location.href = loginUrl;
                        }
                        return;
                    }
                    $.post(apiUrl + '?action=vote', { meme_id: m.id }, loadMemes, 'json');
                });

                $card.append($left, $voteBtn);
                $root.append($card);
            });
        }

        loadMemes();
    });
});


mw.loader.using(['mediawiki.util']).then(function () {
    var container = document.getElementById('home-hot-memes');
    if (!container) return;

    // Gọi API lấy danh sách meme (điều chỉnh tham số nếu meme_api.php của bạn dùng action riêng)
    fetch('/meme_api.php?action=top')
        .then(function (res) { return res.json(); })
        .then(function (data) {
            // Hỗ trợ cả trường hợp API trả về mảng trực tiếp hoặc bọc trong { memes: [...] }
            var list = Array.isArray(data) ? data : (data.memes || data.data || []);
            if (!list.length) {
                container.textContent = 'Chưa có meme nào được bình chọn.';
                return;
            }

            // Sắp xếp giảm dần theo số vote và lấy top 5
            list.sort(function (a, b) { return (b.votes || 0) - (a.votes || 0); });
            var topMemes = list.slice(0, 5);

            container.textContent = '';
            topMemes.forEach(function (item, index) {
                var row = document.createElement('div');
                row.className = 'meme-hot-item';

                var img = document.createElement('img');
                img.className = 'meme-hot-thumb';
                img.src = item.image_url || item.url || '';
                img.alt = 'Meme #' + (index + 1);

                var info = document.createElement('div');
                info.className = 'meme-hot-info';

                var badge = document.createElement('span');
                badge.className = 'meme-hot-votes';
                badge.textContent = '🔥 ' + (item.votes || 0) + ' votes';

                var title = document.createElement('div');
                title.style.marginTop = '4px';
                title.style.fontWeight = '600';

                if (item.page_title) {
                    var link = document.createElement('a');
                    link.href = mw.util.getUrl(item.page_title);
                    link.textContent = 'Trang: ' + item.page_title.replace(/_/g, ' ');
                    title.appendChild(link);
                } else {
                    title.textContent = 'Top #' + (index + 1);
                }

                info.appendChild(badge);
                info.appendChild(title);
                row.appendChild(img);
                row.appendChild(info);
                container.appendChild(row);
            });
        })
        .catch(function () {
            container.textContent = 'Không thể tải bảng xếp hạng meme lúc này.';
        });
});

mw.loader.using(['mediawiki.api', 'mediawiki.util']).then(function () {
    var newPagesBox = document.getElementById('home-new-pages');
    if (!newPagesBox) return;

    new mw.Api().get({
        action: 'query',
        list: 'recentchanges',
        rctype: 'new',
        rcnamespace: 0, // Chỉ lấy bài viết nội dung chính, bỏ qua trang hệ thống/template
        rclimit: 5,
        rcprop: 'title|timestamp'
    }).done(function (data) {
        var list = (data.query && data.query.recentchanges) || [];
        if (!list.length) {
            newPagesBox.textContent = 'Chưa có bài viết mới nào.';
            return;
        }

        newPagesBox.textContent = '';
        list.forEach(function (item) {
            var row = document.createElement('div');
            row.className = 'new-page-item';

            var link = document.createElement('a');
            link.className = 'new-page-title';
            link.href = mw.util.getUrl(item.title);
            link.textContent = '📄 ' + item.title;

            var date = new Date(item.timestamp);
            var dateSpan = document.createElement('span');
            dateSpan.className = 'new-page-date';
            dateSpan.textContent = date.toLocaleDateString('vi-VN');

            row.appendChild(link);
            row.appendChild(dateSpan);
            newPagesBox.appendChild(row);
        });
    }).fail(function () {
        newPagesBox.textContent = 'Không thể tải danh sách bài viết mới.';
    });
});