Difference between revisions of "Gadget-Bibliography.js"
From ACES
(add button filter) |
|||
Line 2: | Line 2: | ||
"use strict"; | "use strict"; | ||
var allowedPages = ["Publications"]; | var allowedPages = ["Publications"]; | ||
var nYearButtons = 5; | |||
if (!allowedPages.includes(mw.config.get("wgPageName"))) return; | if (!allowedPages.includes(mw.config.get("wgPageName"))) return; | ||
if (window.BibLoaded) return; | if (window.BibLoaded) return; | ||
Line 8: | Line 9: | ||
console.log("[Bibliography] 0.0.1"); | console.log("[Bibliography] 0.0.1"); | ||
var scriptLoaded = 0; | var scriptLoaded = 0; | ||
var mwConfig = mw.config.get(["wgUserName", "wgServer", "wgArticlePath"]); | |||
var isAnon = mwConfig.wgUserName === null; | |||
var articlePath = mwConfig.wgServer + mwConfig.wgArticlePath; | |||
function run() { | function run() { | ||
var Cite = window.require("citation-js"); | var Cite = window.require("citation-js"); | ||
$(".bibliography:not(.test)").each(function (i, e) { | $(".bibliography:not(.test)").each(function (i, e) { | ||
var yearGroup = {}; | |||
var dataTable = undefined; | |||
var rawBib = $(e).find(".bibliography-data")[0].textContent; | var rawBib = $(e).find(".bibliography-data")[0].textContent; | ||
console.log(rawBib); | |||
var bib = Cite(rawBib); | var bib = Cite(rawBib); | ||
var data = []; | var data = []; | ||
console.log(bib); | |||
var pageTitleIter = rawBib.matchAll(/^_pageName = \{(.+)\}\s*,?\s*$/gm); | |||
bib.data.forEach(function (entry) { | bib.data.forEach(function (entry) { | ||
var linkTarget = new URL( | |||
articlePath.replace("$1", pageTitleIter.next().value[1]) | |||
); | |||
var linkEle = $("<a>").text(entry.id); | |||
linkEle.attr("href", linkTarget.toString()); | |||
var editLink = $("<a>").html( | |||
'<span class="material-icons-outlined">edit</span>' | |||
); | |||
linkTarget.searchParams.append("action", "formedit"); | |||
editLink.attr("href", linkTarget.toString()); | |||
editLink.attr("target", "_blank"); | |||
entry.render = Cite(entry).format("bibliography", { | entry.render = Cite(entry).format("bibliography", { | ||
format: "html", | format: "html", | ||
Line 20: | Line 45: | ||
lang: "en-US", | lang: "en-US", | ||
}); | }); | ||
entry.pageTitle = editLink[0].outerHTML + linkEle[0].outerHTML; | |||
entry.year = entry.issued["date-parts"][0][0] || 1970; | |||
yearGroup[parseInt(entry.year)] = false; | |||
data.push(entry); | data.push(entry); | ||
}); | }); | ||
Line 28: | Line 57: | ||
tb.append($("<thead>")); | tb.append($("<thead>")); | ||
tb.append($("<tbody>")); | tb.append($("<tbody>")); | ||
tb.addClass("display") | tb.addClass("display"); | ||
console.log(data); | console.log(data); | ||
// Generate buttons | |||
var orderedYears = Object.keys(yearGroup).sort(function (x, y) { | |||
if (x < y) return 1; | |||
if (x > y) return -1; | |||
return 0; | |||
}); | |||
function draw(dt) { | |||
dt.column("year:name").search( | |||
orderedYears | |||
.filter(function (e) { | |||
return yearGroup[e]; | |||
}) | |||
.join("|"), | |||
true, | |||
false | |||
); | |||
dt.draw(); | |||
} | |||
var buttons = orderedYears.slice(0, nYearButtons).map(function (year) { | |||
return { | |||
text: year, | |||
action: function (e, dt, node, config) { | |||
yearGroup[year] = !yearGroup[year]; | |||
node.toggleClass("active", yearGroup[year]); | |||
draw(dt); | |||
console.log(e, dt, node, config); | |||
}, | |||
}; | |||
}); | |||
if (orderedYears.length > nYearButtons) { | |||
var statusRest = false; | |||
var restYears = orderedYears.slice(nYearButtons); | |||
buttons.push({ | |||
text: "Rest", | |||
action: function (e, dt, node, config) { | |||
statusRest = !statusRest; | |||
restYears.forEach(function (el) { | |||
yearGroup[el] = !yearGroup[el]; | |||
}); | |||
node.toggleClass("active", statusRest); | |||
draw(dt); | |||
console.log(e, dt, node, config); | |||
}, | |||
}); | |||
} | |||
var options = { | |||
data: data, | data: data, | ||
columns: [ | columns: [ | ||
{ data: "render" }, | { | ||
data: "render", | |||
title: "APA", | |||
render: function (data, type, row) { | |||
// If display or filter data is requested, format the date | |||
if (type === "sort") return row.year; | |||
return data; | |||
}, | |||
}, | |||
{ | |||
name: "year", | |||
data: "year", | |||
visible: false, | |||
}, | |||
], | ], | ||
paging: false, | |||
order: [[0, "desc"]], | |||
rowGroup: { | |||
" | dataSrc: "year", | ||
}); | }, | ||
dom: "fBrtip", | |||
buttons: buttons, | |||
}; | |||
if (!isAnon) { | |||
options.columns.unshift({ | |||
data: "pageTitle", | |||
title: "In-site Page", | |||
}); | |||
options.order[0][0] += 1; | |||
options.order.push([0, "asc"]); | |||
} | |||
dataTable = tb.dataTable(options); | |||
dataTable = dataTable.DataTable(); | |||
console.log(dataTable); | |||
}); | }); | ||
} | } | ||
Line 58: | Line 161: | ||
[ | [ | ||
"https://cdn.jsdelivr.net/npm/citation-js@0.5.1", | "https://cdn.jsdelivr.net/npm/citation-js@0.5.1", | ||
"https://cdn.datatables.net/v/dt/dt-1.11.1/rg-1.1.3/datatables.min.js", | "https://cdn.datatables.net/v/dt/dt-1.11.1/b-2.0.0/rg-1.1.3/datatables.min.js", | ||
], | ], | ||
run | run | ||
); | ); | ||
mw.loader.load( | mw.loader.load( | ||
"https://cdn.datatables.net/v/dt/dt-1.11.1/rg-1.1.3/datatables.min.css", | "https://cdn.datatables.net/v/dt/dt-1.11.1/b-2.0.0/rg-1.1.3/datatables.min.css", | ||
"text/css" | "text/css" | ||
); | ); | ||
})(jQuery, mediaWiki); | })(jQuery, mediaWiki); |
Revision as of 23:09, 20 September 2021
(function ($, mw) {
"use strict";
var allowedPages = ["Publications"];
var nYearButtons = 5;
if (!allowedPages.includes(mw.config.get("wgPageName"))) return;
if (window.BibLoaded) return;
// window.BibLoaded = true;
console.log("[Bibliography] 0.0.1");
var scriptLoaded = 0;
var mwConfig = mw.config.get(["wgUserName", "wgServer", "wgArticlePath"]);
var isAnon = mwConfig.wgUserName === null;
var articlePath = mwConfig.wgServer + mwConfig.wgArticlePath;
function run() {
var Cite = window.require("citation-js");
$(".bibliography:not(.test)").each(function (i, e) {
var yearGroup = {};
var dataTable = undefined;
var rawBib = $(e).find(".bibliography-data")[0].textContent;
console.log(rawBib);
var bib = Cite(rawBib);
var data = [];
console.log(bib);
var pageTitleIter = rawBib.matchAll(/^_pageName = \{(.+)\}\s*,?\s*$/gm);
bib.data.forEach(function (entry) {
var linkTarget = new URL(
articlePath.replace("$1", pageTitleIter.next().value[1])
);
var linkEle = $("<a>").text(entry.id);
linkEle.attr("href", linkTarget.toString());
var editLink = $("<a>").html(
'<span class="material-icons-outlined">edit</span>'
);
linkTarget.searchParams.append("action", "formedit");
editLink.attr("href", linkTarget.toString());
editLink.attr("target", "_blank");
entry.render = Cite(entry).format("bibliography", {
format: "html",
template: "apa",
lang: "en-US",
});
entry.pageTitle = editLink[0].outerHTML + linkEle[0].outerHTML;
entry.year = entry.issued["date-parts"][0][0] || 1970;
yearGroup[parseInt(entry.year)] = false;
data.push(entry);
});
var tb = $("<table></table>")[0];
e.replaceWith(tb);
tb = $(tb);
tb.append($("<thead>"));
tb.append($("<tbody>"));
tb.addClass("display");
console.log(data);
// Generate buttons
var orderedYears = Object.keys(yearGroup).sort(function (x, y) {
if (x < y) return 1;
if (x > y) return -1;
return 0;
});
function draw(dt) {
dt.column("year:name").search(
orderedYears
.filter(function (e) {
return yearGroup[e];
})
.join("|"),
true,
false
);
dt.draw();
}
var buttons = orderedYears.slice(0, nYearButtons).map(function (year) {
return {
text: year,
action: function (e, dt, node, config) {
yearGroup[year] = !yearGroup[year];
node.toggleClass("active", yearGroup[year]);
draw(dt);
console.log(e, dt, node, config);
},
};
});
if (orderedYears.length > nYearButtons) {
var statusRest = false;
var restYears = orderedYears.slice(nYearButtons);
buttons.push({
text: "Rest",
action: function (e, dt, node, config) {
statusRest = !statusRest;
restYears.forEach(function (el) {
yearGroup[el] = !yearGroup[el];
});
node.toggleClass("active", statusRest);
draw(dt);
console.log(e, dt, node, config);
},
});
}
var options = {
data: data,
columns: [
{
data: "render",
title: "APA",
render: function (data, type, row) {
// If display or filter data is requested, format the date
if (type === "sort") return row.year;
return data;
},
},
{
name: "year",
data: "year",
visible: false,
},
],
paging: false,
order: [[0, "desc"]],
rowGroup: {
dataSrc: "year",
},
dom: "fBrtip",
buttons: buttons,
};
if (!isAnon) {
options.columns.unshift({
data: "pageTitle",
title: "In-site Page",
});
options.order[0][0] += 1;
options.order.push([0, "asc"]);
}
dataTable = tb.dataTable(options);
dataTable = dataTable.DataTable();
console.log(dataTable);
});
}
function loadScripts(scripts, callback) {
function onLoaded() {
scriptLoaded++;
if (scriptLoaded === scripts.length)
mw.hook("wikipage.content").add(callback);
}
scripts.forEach(function (s) {
mw.loader.getScript(s).then(onLoaded);
});
}
loadScripts(
[
"https://cdn.jsdelivr.net/npm/citation-js@0.5.1",
"https://cdn.datatables.net/v/dt/dt-1.11.1/b-2.0.0/rg-1.1.3/datatables.min.js",
],
run
);
mw.loader.load(
"https://cdn.datatables.net/v/dt/dt-1.11.1/b-2.0.0/rg-1.1.3/datatables.min.css",
"text/css"
);
})(jQuery, mediaWiki);