MediaWiki

Difference between revisions of "Gadget-Bibliography.js"

From ACES

Line 1: Line 1:
(function(g, $, mw) {
(function ($, mw) {
   "use strict";
   "use strict";
   var allowedPages = ["Publications"];
   var allowedPages = ["Publications"];
   if (!allowedPages.includes(mw.config.get("wgPageName"))) return;
   if (!allowedPages.includes(mw.config.get("wgPageName"))) return;
  console.log("!");
   if (window.BibLoaded) return;
   if (window.BibLoaded) {
    return;
  }
   // window.BibLoaded = true;
   // window.BibLoaded = true;


  console.log("[Bibliography] 0.0.1");
   var scriptLoaded = 0;
   var scriptLoaded = 0;
   function run() {
   function run() {
    debugger;
     var Cite = window.require("citation-js");
     var Cite = window.require("citation-js");
    console.log("!");
    console.log($(".bibliography"));
     $(".bibliography").each(function (i, e) {
     $(".bibliography").each(function (i, e) {
       var rawBib = e.textContent;
       var rawBib = e.textContent;
Line 31: Line 26:
       console.log("!");
       console.log("!");


       var tb = $(".bibliography-table");
       var tb = $("<table></table>")[0];
      e.replaceWith(tb);
      tb = $(tb);
       tb.append($("<thead>"));
       tb.append($("<thead>"));
       tb.append($("<tbody>"));
       tb.append($("<tbody>"));
Line 64: Line 61:
     "text/css"
     "text/css"
   );
   );
})(this, jQuery, mediaWiki);
})(jQuery, mediaWiki);

Revision as of 17:45, 3 September 2021

(function ($, mw) {
  "use strict";
  var allowedPages = ["Publications"];
  if (!allowedPages.includes(mw.config.get("wgPageName"))) return;
  if (window.BibLoaded) return;
  // window.BibLoaded = true;

  console.log("[Bibliography] 0.0.1");
  var scriptLoaded = 0;
  function run() {
    var Cite = window.require("citation-js");
    $(".bibliography").each(function (i, e) {
      var rawBib = e.textContent;
      var bib = Cite(rawBib);
      console.log(bib);
      var data = [];
      bib.data.forEach(function (e) {
        e.render = Cite(e).format("bibliography", {
          format: "html",
          template: "apa",
          lang: "en-US",
        });
        console.log(e);
        data.push(e);
      });
      console.log("!");

      var tb = $("<table></table>")[0];
      e.replaceWith(tb);
      tb = $(tb);
      tb.append($("<thead>"));
      tb.append($("<tbody>"));
      tb.dataTable({
        data: data,
        columns: [{ data: "render" }],
      });
    });
  }

  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/1.11.0/js/jquery.dataTables.min.js",
    ],
    run
  );

  mw.loader.load(
    "https:////cdn.datatables.net/1.11.0/css/jquery.dataTables.min.css",
    "text/css"
  );
})(jQuery, mediaWiki);