MediaWiki

Difference between revisions of "Gadget-Bibliography.js"

From ACES

Line 28: Line 28:
       tb.append($("<thead>"));
       tb.append($("<thead>"));
       tb.append($("<tbody>"));
       tb.append($("<tbody>"));
       var dataTable = tb.dataTable({
       console.log(data);
      tb.dataTable({
         data: data,
         data: data,
         columns: [{ data: "render" }],
         columns: [
          { data: "render" },
          // { data: "type" },
        ],
        // rowGroup: {
        //  dataSrc: "type",
        // },
        "paging": false
       });
       });
      console.log(dataTable)
     });
     });
   }
   }
Line 50: Line 57:
     [
     [
       "https://cdn.jsdelivr.net/npm/citation-js@0.5.1",
       "https://cdn.jsdelivr.net/npm/citation-js@0.5.1",
       "https://cdn.datatables.net/1.11.0/js/jquery.dataTables.min.js",
       "https://cdn.datatables.net/v/bs5/dt-1.11.1/rg-1.1.3/datatables.min.js",
     ],
     ],
     run
     run
Line 56: Line 63:


   mw.loader.load(
   mw.loader.load(
     "https:////cdn.datatables.net/1.11.0/css/jquery.dataTables.min.css",
     "https://cdn.datatables.net/v/bs5/dt-1.11.1/rg-1.1.3/datatables.min.css",
     "text/css"
     "text/css"
   );
   );
})(jQuery, mediaWiki);
})(jQuery, mediaWiki);

Revision as of 05:24, 4 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:not(.test)").each(function (i, e) {
      var rawBib = $(e).find(".bibliography-data")[0].textContent;
      var bib = Cite(rawBib);
      var data = [];
      bib.data.forEach(function (entry) {
        entry.render = Cite(entry).format("bibliography", {
          format: "html",
          template: "apa",
          lang: "en-US",
        });
        data.push(entry);
      });

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

  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/bs5/dt-1.11.1/rg-1.1.3/datatables.min.js",
    ],
    run
  );

  mw.loader.load(
    "https://cdn.datatables.net/v/bs5/dt-1.11.1/rg-1.1.3/datatables.min.css",
    "text/css"
  );
})(jQuery, mediaWiki);