MediaWiki:Common.js: Difference between revisions
From Artifacts of Capitalism
No edit summary |
No edit summary |
||
| Line 103: | Line 103: | ||
// Start watching the body for when the form arrives | // Start watching the body for when the form arrives | ||
observer.observe(document.body, { childList: true, subtree: true }); | observer.observe(document.body, { childList: true, subtree: true }); | ||
}); | |||
document.addEventListener("DOMContentLoaded", function () { | |||
// Wait until Leaflet map exists | |||
if (!window.mw || !mw.leafletMap || !mw.leafletMap.map) { | |||
console.log("Leaflet map not ready"); | |||
return; | |||
} | |||
var map = mw.leafletMap.map; | |||
// Get Cargo JSON data | |||
var raw = document.getElementById("artifact-network-data").textContent; | |||
if (!raw) { | |||
console.log("No Cargo data found"); | |||
return; | |||
} | |||
var data = JSON.parse(raw); | |||
// Build lookup table: Title → coordinates | |||
var lookup = {}; | |||
data.forEach(function(item) { | |||
if (item.Title && item.Latitude && item.Longitude) { | |||
lookup[item.Title.trim()] = [ | |||
parseFloat(item.Latitude), | |||
parseFloat(item.Longitude) | |||
]; | |||
} | |||
}); | |||
// Draw connections | |||
data.forEach(function(item) { | |||
if (!item.Related_artifacts) return; | |||
var fromCoords = lookup[item.Title]; | |||
if (!fromCoords) return; | |||
// Split comma-separated list | |||
var relatedList = item.Related_artifacts.split(","); | |||
relatedList.forEach(function(rel) { | |||
var target = rel.trim(); | |||
var toCoords = lookup[target]; | |||
if (toCoords) { | |||
L.polyline([fromCoords, toCoords], { | |||
color: "purple", | |||
weight: 2, | |||
opacity: 0.7, | |||
dashArray: "4,6" | |||
}).addTo(map); | |||
} | |||
}); | |||
}); | |||
console.log("Artifact network loaded"); | |||
}); | }); | ||
Revision as of 02:49, 19 March 2026
// Ensure viewport meta exists (iOS layout fix)
(function () {
if (!document.querySelector('meta[name="viewport"]')) {
var m = document.createElement('meta');
m.name = 'viewport';
m.content = 'width=device-width, initial-scale=1, shrink-to-fit=no';
document.head.appendChild(m);
}
})();
mw.hook('wikipage.content').add(function ($c) {
var groups = mw.config.get('wgUserGroups') || [];
if (groups.indexOf('approver') !== -1) return; // approvers keep the checkbox
// Hide the Draft namespace checkbox in the advanced search pane
$c.find('input[name="ns' + 3000 + '"]').closest('label, .mw-advancedSearch-ns_option').hide();
});
// Load Google CSE only on Advanced_search page
mw.loader.using('mediawiki.util').then(function () {
var page = mw.config.get('wgPageName');
if (page !== 'Artifacts_of_Capitalism:Advanced_search') return;
console.log('[CSE] Loading on:', page);
// Ensure CSE callback setup exists before script load
window.__gcse = {
searchCallbacks: {
ready: function () {
try {
var params = new URLSearchParams(window.location.search);
var q = params.get('q');
if (q) {
var hash = 'gsc.tab=0&gsc.q=' + encodeURIComponent(q);
if (!window.location.hash.includes('gsc.q=')) {
window.location.hash = hash;
console.log('[CSE] Applied query:', q);
}
}
} catch (err) {
console.warn('[CSE] Query error:', err);
}
}
}
};
// Inject Google CSE script
var script = document.createElement('script');
script.async = true;
script.src = 'https://cse.google.com/cse.js?cx=b194aa5e4d64344ad';
script.onload = function () {
console.log('[CSE] Script loaded successfully');
};
script.onerror = function () {
console.error('[CSE] Failed to load cse.js');
};
document.body.appendChild(script);
});
// Open all external links in a new window or tab
$(document).ready(function() {
$('a.external').attr('target', '_blank');
});
// Wait for the Chameleon layout to finish before inserting fields
mw.loader.using('mediawiki.util').then(function () {
if (mw.config.get('wgCanonicalSpecialPageName') !== 'CreateAccount') return;
// Observe the page until the form appears
const observer = new MutationObserver(() => {
const realNameInput = document.getElementById('wpRealName');
if (!realNameInput) return; // Not yet loaded
const form = realNameInput.closest('form');
if (!form) return;
// Prevent duplicate injection
if (document.getElementById('wpAffiliation')) return;
// --- Affiliation field ---
const affDiv = document.createElement('div');
affDiv.className = 'mw-input';
affDiv.innerHTML = `
<label for="wpAffiliation">Affiliation</label>
<input type="text" id="wpAffiliation" name="wpAffiliation"
required class="mw-ui-input form-control"/>
`;
// --- Institutional Email field ---
const instDiv = document.createElement('div');
instDiv.className = 'mw-input';
instDiv.innerHTML = `
<label for="wpInstitutionalEmail">Institutional Email</label>
<input type="email" id="wpInstitutionalEmail" name="wpInstitutionalEmail"
required class="mw-ui-input form-control"/>
`;
// Insert right after Real Name
const realNameDiv =
realNameInput.closest('.mw-input, .form-group, p') || realNameInput.parentNode;
realNameDiv.insertAdjacentElement('afterend', instDiv);
realNameDiv.insertAdjacentElement('afterend', affDiv);
// Once inserted, stop observing
observer.disconnect();
});
// Start watching the body for when the form arrives
observer.observe(document.body, { childList: true, subtree: true });
});
document.addEventListener("DOMContentLoaded", function () {
// Wait until Leaflet map exists
if (!window.mw || !mw.leafletMap || !mw.leafletMap.map) {
console.log("Leaflet map not ready");
return;
}
var map = mw.leafletMap.map;
// Get Cargo JSON data
var raw = document.getElementById("artifact-network-data").textContent;
if (!raw) {
console.log("No Cargo data found");
return;
}
var data = JSON.parse(raw);
// Build lookup table: Title → coordinates
var lookup = {};
data.forEach(function(item) {
if (item.Title && item.Latitude && item.Longitude) {
lookup[item.Title.trim()] = [
parseFloat(item.Latitude),
parseFloat(item.Longitude)
];
}
});
// Draw connections
data.forEach(function(item) {
if (!item.Related_artifacts) return;
var fromCoords = lookup[item.Title];
if (!fromCoords) return;
// Split comma-separated list
var relatedList = item.Related_artifacts.split(",");
relatedList.forEach(function(rel) {
var target = rel.trim();
var toCoords = lookup[target];
if (toCoords) {
L.polyline([fromCoords, toCoords], {
color: "purple",
weight: 2,
opacity: 0.7,
dashArray: "4,6"
}).addTo(map);
}
});
});
console.log("Artifact network loaded");
});