MediaWiki

Difference between revisions of "Gadget-Bibliography.js"

From ACES

Line 1: Line 1:
(function ($, mw) {
(function ($, mw) {
   "use strict";
   "use strict";
   var allowedPages = ["Publications"];
   var allowedPages = ["Publications", "Home"];
   var nYearButtons = 5;
   var nYearButtons = 5;
   if (!allowedPages.includes(mw.config.get("wgPageName"))) return;
   if (!allowedPages.includes(mw.config.get("wgPageName"))) return;
Line 30: Line 30:
   }
   }


   function run() {
   function genDataTable(e) {
     var Cite = window.require("citation-js");
     var Cite = window.require("citation-js");
     $(".bibliography:not(.test)").each(function (i, e) {
     var yearGroup = {};
      var yearGroup = {};
    var dataTable = undefined;
      var dataTable = undefined;
    var rawBib = $(e).find(".bibliography-data")[0].textContent;
      var rawBib = $(e).find(".bibliography-data")[0].textContent;
    var bib = Cite(rawBib);
      var bib = Cite(rawBib);
    var data = [];
      var data = [];
    var pageTitleIter = rawBib.matchAll(/^_pageName = \{(.+)\}\s*,?\s*$/gm);
      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(
    bib.data.forEach(function (entry) {
          '<span class="material-icons-outlined">edit</span>'
      var linkTarget = new URL(
        );
        articlePath.replace("$1", pageTitleIter.next().value[1])
        linkTarget.searchParams.append("action", "formedit");
      );
        editLink.attr("href", linkTarget.toString());
      var linkEle = $("<a>").text(entry.id);
        editLink.attr("target", "_blank");
      linkEle.attr("href", linkTarget.toString());


        entry.render = Cite(entry).format("bibliography", {
      var editLink = $("<a>").html(
          format: "html",
        '<span class="material-icons-outlined">edit</span>'
          template: "apa",
      );
          lang: "en-US",
      linkTarget.searchParams.append("action", "formedit");
        });
      editLink.attr("href", linkTarget.toString());
        entry.pageTitle = editLink[0].outerHTML + linkEle[0].outerHTML;
      editLink.attr("target", "_blank");
        entry.year = entry.issued["date-parts"][0][0] || 1970;
        yearGroup[parseInt(entry.year)] = false;


        data.push(entry);
      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];
    var tb = $("<table></table>")[0];
      e.replaceWith(tb);
    e.replaceWith(tb);
      tb = $(tb);
    tb = $(tb);
      tb.append($("<thead>"));
    tb.append($("<thead>"));
      tb.append($("<tbody>"));
    tb.append($("<tbody>"));
      tb.addClass("display");
    tb.addClass("display");


      // Generate buttons
    // Generate buttons
      var orderedYears = Object.keys(yearGroup).sort(function (x, y) {
    var orderedYears = Object.keys(yearGroup).sort(function (x, y) {
        if (x < y) return 1;
      if (x < y) return 1;
        if (x > y) return -1;
      if (x > y) return -1;
        return 0;
      return 0;
      });
    });
      function draw(dt) {
    function draw(dt) {
        dt.column("year:name").search(
      dt.column("year:name").search(
          orderedYears
        orderedYears
            .filter(function (e) {
          .filter(function (e) {
              return yearGroup[e];
            return yearGroup[e];
            })
          })
            .join("|"),
          .join("|"),
          true,
        true,
          false
        false
        );
      );
        dt.draw();
      dt.draw();
      }
    }
      var buttons = orderedYears.slice(0, nYearButtons).map(function (year) {
    var buttons = orderedYears.slice(0, nYearButtons).map(function (year) {
        return {
      return {
          text: year,
        text: year,
          action: function (e, dt, node, config) {
        action: function (e, dt, node, config) {
            yearGroup[year] = !yearGroup[year];
          yearGroup[year] = !yearGroup[year];
            node.toggleClass("active", yearGroup[year]);
          node.toggleClass("active", yearGroup[year]);
            draw(dt);
          draw(dt);
          },
        },
        };
      };
      });
    });
      if (orderedYears.length > nYearButtons) {
    if (orderedYears.length > nYearButtons) {
        var statusRest = false;
      var statusRest = false;
        var restYears = orderedYears.slice(nYearButtons);
      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);
          },
        });
      }
      // export BibTeX button
       buttons.push({
       buttons.push({
         text: "Export BibTeX",
         text: "Rest",
         action: function (e, dt, node, config) {
         action: function (e, dt, node, config) {
           var text = Cite(dt.rows({ page: "current" }).data().toArray()).format(
           statusRest = !statusRest;
             "bibtex"
          restYears.forEach(function (el) {
           );
             yearGroup[el] = !yearGroup[el];
           download("bibtex.bib", text);
           });
           node.toggleClass("active", statusRest);
          draw(dt);
         },
         },
        titleAttr: "export",
       });
       });
    }
    // export BibTeX button
    buttons.push({
      text: "Export BibTeX",
      action: function (e, dt, node, config) {
        var text = Cite(dt.rows({ page: "current" }).data().toArray()).format(
          "bibtex"
        );
        download("bibtex.bib", text);
      },
      titleAttr: "export",
    });


      var options = {
    var options = {
        data: data,
      data: data,
        columns: [
      columns: [
          {
        {
            data: "render",
          data: "render",
            title: "APA",
          title: "APA",
            render: function (data, type, row) {
          render: function (data, type, row) {
              // If display or filter data is requested, format the date
            // If display or filter data is requested, format the date
              if (type === "sort") return row.year;
            if (type === "sort") return row.year;
              return data;
            return data;
            },
           },
           },
          {
            name: "year",
            data: "year",
            visible: false,
          },
        ],
        paging: false,
        order: [[0, "desc"]],
        rowGroup: {
          dataSrc: "year",
         },
         },
         dom: "fBrtip",
         {
        buttons: buttons,
          name: "year",
      };
          data: "year",
      if (!isAnon) {
          visible: false,
        options.columns.unshift({
        },
          data: "pageTitle",
      ],
          title: "In-site Page",
      paging: false,
        });
      order: [[0, "desc"]],
        options.order[0][0] += 1;
      rowGroup: {
        options.order.push([0, "asc"]);
        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();
  }
 
  function genList(e) {
    var Cite = window.require("citation-js");
    var rawBib = $(e).find(".bibliography-data")[0].textContent;
    var bib = Cite(rawBib);
 
    var ul = $("<ul  />", { class: "csl-bib-body" });
    bib.data.forEach(function (entry) {
      var li = $("<li/>", { class: "bib-entry" });
      var link = $("<a />").text("[PDF]");
      li.append(link);
      li.append(" ");
      li.append(
        $(
          $(
            Cite(entry).format("bibliography", {
              format: "html",
              template: "apa",
              lang: "en-US",
            })
          ).html()
        ).css("display", "inline")
      );
      ul.append(li);
    });
 
    e.replaceWith(ul[0]);
    return ul;
  }
 
  function run() {
    $(".bibliography:not(.test)").each(function (i, e) {
      var style = e.getAttribute("data-style");
      if (style === "list") {
        return genList(e);
       }
       }
       dataTable = tb.dataTable(options);
       return genDataTable(e);
      dataTable = dataTable.DataTable();
      console.log(dataTable);
     });
     });
   }
   }

Revision as of 22:08, 27 September 2021

(function ($, mw) {
  "use strict";
  var allowedPages = ["Publications", "Home"];
  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 download(filename, text) {
    var el = document.createElement("a");
    el.setAttribute(
      "href",
      "data:text/plain;charset=utf-8," + encodeURIComponent(text)
    );
    el.setAttribute("download", filename);

    el.style.display = "none";
    document.body.appendChild(el);

    el.click();

    document.body.removeChild(el);
  }

  function genDataTable(e) {
    var Cite = window.require("citation-js");
    var yearGroup = {};
    var dataTable = undefined;
    var rawBib = $(e).find(".bibliography-data")[0].textContent;
    var bib = Cite(rawBib);
    var data = [];
    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");

    // 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);
        },
      };
    });
    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);
        },
      });
    }
    // export BibTeX button
    buttons.push({
      text: "Export BibTeX",
      action: function (e, dt, node, config) {
        var text = Cite(dt.rows({ page: "current" }).data().toArray()).format(
          "bibtex"
        );
        download("bibtex.bib", text);
      },
      titleAttr: "export",
    });

    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();
  }

  function genList(e) {
    var Cite = window.require("citation-js");
    var rawBib = $(e).find(".bibliography-data")[0].textContent;
    var bib = Cite(rawBib);

    var ul = $("<ul  />", { class: "csl-bib-body" });
    bib.data.forEach(function (entry) {
      var li = $("<li/>", { class: "bib-entry" });
      var link = $("<a />").text("[PDF]");
      li.append(link);
      li.append(" ");
      li.append(
        $(
          $(
            Cite(entry).format("bibliography", {
              format: "html",
              template: "apa",
              lang: "en-US",
            })
          ).html()
        ).css("display", "inline")
      );
      ul.append(li);
    });

    e.replaceWith(ul[0]);
    return ul;
  }

  function run() {
    $(".bibliography:not(.test)").each(function (i, e) {
      var style = e.getAttribute("data-style");
      if (style === "list") {
        return genList(e);
      }
      return genDataTable(e);
    });
  }

  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);