MediaWiki:Common.js: Perbedaan antara revisi
Tampilan
Tidak ada ringkasan suntingan Tanda: Suntingan perangkat seluler Suntingan peramban seluler |
Tidak ada ringkasan suntingan Tanda: Suntingan perangkat seluler Suntingan peramban seluler |
||
| Baris 1: | Baris 1: | ||
/* ========================================================== | /* ========================================================== | ||
🧠 MIPPEDIA DATA - | 🧠 MIPPEDIA DATA - FINAL STABLE (ALL FEATURES) | ||
========================================================== */ | ========================================================== */ | ||
(function() { | (function() { | ||
| Baris 17: | Baris 16: | ||
]; | ]; | ||
function cleanExtract(text) { | function cleanExtract(text) { | ||
return text.replace(/\[\d+\]/g, '') | return text.replace(/\[\d+\]/g, '').replace(/\{\{[^}]+\}\}/g, '').replace(/\(\s*\)/g, '').replace(/\s\s+/g, ' ').trim(); | ||
} | } | ||
// | // Fungsi Potong Teks (Baca Selengkapnya) | ||
function applySummary(text) { | function applySummary(text) { | ||
var limit = 250; | var limit = 250; | ||
if (text.length <= limit) | if (text.length <= limit) return '<span>' + text + '</span>'; | ||
return '<span>' + text.substring(0, limit) + '</span>' + | |||
'<span class="mip-dots">... </span>' + | |||
'<span class="mip-more" style="display:none;">' + text.substring(limit) + '</span>' + | |||
'<span class="mip-read-btn" style="color: #6a5acd; cursor: pointer; font-weight: bold; margin-left: 5px;">Baca selengkapnya</span>'; | |||
'<span class="mip- | |||
'<span class="mip- | |||
'<span class="mip-read-btn" style="color: #6a5acd; cursor: pointer; font-weight: bold | |||
} | } | ||
function translateText(text, callback) { | function translateText(text, callback) { | ||
var | var url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=id&dt=t&q=" + encodeURIComponent(text); | ||
$.ajax({ | $.ajax({ url: url, success: function(res) { | ||
var translated = ""; | |||
res[0].forEach(function(s) { if (s[0]) translated += s[0]; }); | |||
callback(translated); | |||
}, error: function() { callback(text); } }); | |||
} | } | ||
function | function renderAll(p, currentText, isTranslated, originalText) { | ||
var sourceLink = p.base + encodeURIComponent(pageTitle); | var sourceLink = p.base + encodeURIComponent(pageTitle); | ||
// | // Tampilkan teks dengan sistem potong | ||
$descBox.html( | $descBox.html(applySummary(currentText)); | ||
// | // Tampilkan Sumber & Info | ||
var | var footer = '<div style="font-size: 0.9em; color: #777;">Sumber Dari : <a href="' + sourceLink + '" target="_blank" style="color: #6a5acd; font-weight: bold; text-decoration: none;">' + p.name + '.</a>'; | ||
if (isTranslated) { | if (isTranslated) { | ||
footer += '<br><span style="font-size: 0.85em; font-style: italic;">(Diterjemahkan secara otomatis)</span> ' + | |||
'<span id="mip-toggle-orig" style="color: #6a5acd; cursor: pointer; text-decoration: underline; margin-left: 5px;">' + (currentText === originalText ? 'Tampilkan terjemahan' : 'Tampilkan versi asli') + '</span>'; | |||
} | } | ||
footer += '</div>'; | |||
$sourceInfo.html( | $sourceInfo.html(footer); | ||
// | // Handler Baca Selengkapnya | ||
$(document).off('click', '.mip-read-btn').on('click', '.mip-read-btn', function() { | $(document).off('click', '.mip-read-btn').on('click', '.mip-read-btn', function() { | ||
$(this).hide(); | $(this).hide(); $('.mip-dots').hide(); $('.mip-more').fadeIn(); | ||
}); | }); | ||
// | // Handler Toggle Versi Asli (Logic Perbaikan) | ||
if (isTranslated) { | if (isTranslated) { | ||
$(document).off('click', '#mip-toggle-orig').on('click', '#mip-toggle-orig', function() { | $(document).off('click', '#mip-toggle-orig').on('click', '#mip-toggle-orig', function() { | ||
var isShowingOriginal = ($(this).text() === 'Tampilkan terjemahan'); | |||
var targetText = isShowingOriginal ? currentText : originalText; // currentText di sini adalah hasil translate | |||
// Re-render dengan teks yang ditukar | |||
renderAll(p, originalText, true, currentText); | |||
// Kita balik posisinya biar fungsi toggle-nya bisa switch balik | |||
}); | }); | ||
} | } | ||
} | |||
// PENTING: Fungsi render khusus EN biar gak pusing variabelnya | |||
function handleEN(extract, p) { | |||
translateText(extract, function(translated) { | |||
// p, hasil_translate, isTranslated?, teks_asli | |||
renderAll(p, translated, true, extract); | |||
}); | |||
} | } | ||
| Baris 107: | Baris 97: | ||
var extract = cleanExtract(pages[pageId].extract); | var extract = cleanExtract(pages[pageId].extract); | ||
if (extract !== "") { | if (extract !== "") { | ||
if (p.id === 'en') { | if (p.id === 'en') { handleEN(extract, p); } | ||
else { renderAll(p, extract, false); } | |||
return; | return; | ||
} | } | ||
Revisi per 14 April 2026 23.53
/* ==========================================================
🧠 MIPPEDIA DATA - FINAL STABLE (ALL FEATURES)
========================================================== */
(function() {
$(document).ready(function() {
var $descSection = $('#mip-desc-section');
var $descBox = $('#mip-auto-description');
var $sourceInfo = $('#mip-source-info');
if (!$descBox.length) return;
var pageTitle = mw.config.get('wgPageName');
var projects = [
{ id: 'id', name: 'Mippedia bahasa Indonesia', url: 'https://id.mippedia.org/api.php', base: 'https://id.mippedia.org/wiki/' },
{ id: 'en', name: 'Mippedia bahasa Inggris', url: 'https://en.mippedia.org/api.php', base: 'https://en.mippedia.org/wiki/' },
{ id: 'concise', name: 'Mippedia bahasa Indonesia ringkas', url: 'https://concise.mippedia.org/api.php', base: 'https://concise.mippedia.org/wiki/' }
];
function cleanExtract(text) {
return text.replace(/\[\d+\]/g, '').replace(/\{\{[^}]+\}\}/g, '').replace(/\(\s*\)/g, '').replace(/\s\s+/g, ' ').trim();
}
// Fungsi Potong Teks (Baca Selengkapnya)
function applySummary(text) {
var limit = 250;
if (text.length <= limit) return '<span>' + text + '</span>';
return '<span>' + text.substring(0, limit) + '</span>' +
'<span class="mip-dots">... </span>' +
'<span class="mip-more" style="display:none;">' + text.substring(limit) + '</span>' +
'<span class="mip-read-btn" style="color: #6a5acd; cursor: pointer; font-weight: bold; margin-left: 5px;">Baca selengkapnya</span>';
}
function translateText(text, callback) {
var url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=id&dt=t&q=" + encodeURIComponent(text);
$.ajax({ url: url, success: function(res) {
var translated = "";
res[0].forEach(function(s) { if (s[0]) translated += s[0]; });
callback(translated);
}, error: function() { callback(text); } });
}
function renderAll(p, currentText, isTranslated, originalText) {
var sourceLink = p.base + encodeURIComponent(pageTitle);
// Tampilkan teks dengan sistem potong
$descBox.html(applySummary(currentText));
// Tampilkan Sumber & Info
var footer = '<div style="font-size: 0.9em; color: #777;">Sumber Dari : <a href="' + sourceLink + '" target="_blank" style="color: #6a5acd; font-weight: bold; text-decoration: none;">' + p.name + '.</a>';
if (isTranslated) {
footer += '<br><span style="font-size: 0.85em; font-style: italic;">(Diterjemahkan secara otomatis)</span> ' +
'<span id="mip-toggle-orig" style="color: #6a5acd; cursor: pointer; text-decoration: underline; margin-left: 5px;">' + (currentText === originalText ? 'Tampilkan terjemahan' : 'Tampilkan versi asli') + '</span>';
}
footer += '</div>';
$sourceInfo.html(footer);
// Handler Baca Selengkapnya
$(document).off('click', '.mip-read-btn').on('click', '.mip-read-btn', function() {
$(this).hide(); $('.mip-dots').hide(); $('.mip-more').fadeIn();
});
// Handler Toggle Versi Asli (Logic Perbaikan)
if (isTranslated) {
$(document).off('click', '#mip-toggle-orig').on('click', '#mip-toggle-orig', function() {
var isShowingOriginal = ($(this).text() === 'Tampilkan terjemahan');
var targetText = isShowingOriginal ? currentText : originalText; // currentText di sini adalah hasil translate
// Re-render dengan teks yang ditukar
renderAll(p, originalText, true, currentText);
// Kita balik posisinya biar fungsi toggle-nya bisa switch balik
});
}
}
// PENTING: Fungsi render khusus EN biar gak pusing variabelnya
function handleEN(extract, p) {
translateText(extract, function(translated) {
// p, hasil_translate, isTranslated?, teks_asli
renderAll(p, translated, true, extract);
});
}
function fetchDescription(index) {
if (index >= projects.length) { $descSection.hide(); return; }
var p = projects[index];
$descBox.html('<div class="mip-loading-shimmer"></div>');
$sourceInfo.empty();
$descSection.show();
$.ajax({
url: p.url,
data: { action: 'query', prop: 'extracts', exintro: true, explaintext: true, titles: pageTitle, format: 'json', origin: '*' },
dataType: 'json',
success: function(data) {
var pages = data.query.pages;
var pageId = Object.keys(pages)[0];
if (pageId != "-1") {
var extract = cleanExtract(pages[pageId].extract);
if (extract !== "") {
if (p.id === 'en') { handleEN(extract, p); }
else { renderAll(p, extract, false); }
return;
}
}
fetchDescription(index + 1);
},
error: function() { fetchDescription(index + 1); }
});
}
fetchDescription(0);
if ($('#mip-shimmer-style').length === 0) {
$('<style id="mip-shimmer-style">').text(`
.mip-loading-shimmer { height: 16px; width: 100%; background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%); background-size: 200% 100%; animation: mip-shimmer 1.5s infinite; border-radius: 4px; }
@keyframes mip-shimmer { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }
`).appendTo('head');
}
});
})();