MediaWiki:Common.js: Difference between revisions

Jump to navigation Jump to search
No edit summary
Tag: Reverted
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 51: Line 51:
       var numberA = parseInt(
       var numberA = parseInt(
         $(a).find(".entry-number").text().replace(/\[|\]/g, ""),
         $(a).find(".entry-number").text().replace(/\[|\]/g, ""),
         10,
         10
       );
       );
       var numberB = parseInt(
       var numberB = parseInt(
         $(b).find(".entry-number").text().replace(/\[|\]/g, ""),
         $(b).find(".entry-number").text().replace(/\[|\]/g, ""),
         10,
         10
       );
       );
       return numberB - numberA; // Descending order
       return numberB - numberA; // Descending order
Line 109: Line 109:


           var titleImagesContainer = $(
           var titleImagesContainer = $(
             '<div class="title-images"></div>',
             '<div class="title-images"></div>'
           ).append(images, title);
           ).append(images, title);
           $(this).find(".people").after(titleImagesContainer);
           $(this).find(".people").after(titleImagesContainer);
         }
         }
       },
       }
     );
     );


Line 133: Line 133:
           $(this).find(".type").after(images);
           $(this).find(".type").after(images);
         }
         }
       },
       }
     );
     );
   }
   }
Line 179: Line 179:
     $(".home-block-view").show();
     $(".home-block-view").show();
     $(".home-chronicle-block-button, .home-block-view-button").addClass(
     $(".home-chronicle-block-button, .home-block-view-button").addClass(
       "active-view-button",
       "active-view-button"
     );
     );


     // Initially hide list view sorting buttons and set the default sorted view for block
     // Initially hide list view sorting buttons and set the default sorted view for block
     $(
     $(
       ".home-chronicle-list-button, .home-random-list-button, .home-alphabetical-list-button",
       ".home-chronicle-list-button, .home-random-list-button, .home-alphabetical-list-button"
     ).hide();
     ).hide();
     sortChronologically(); // Sort the block view chronologically by default
     sortChronologically(); // Sort the block view chronologically by default
Line 205: Line 205:
       // Toggling visibility of buttons
       // Toggling visibility of buttons
       $(
       $(
         ".home-chronicle-block-button, .home-random-block-button, .home-alphabetical-block-button",
         ".home-chronicle-block-button, .home-random-block-button, .home-alphabetical-block-button"
       ).hide();
       ).hide();
       $(
       $(
         ".home-chronicle-list-button, .home-random-list-button, .home-alphabetical-list-button",
         ".home-chronicle-list-button, .home-random-list-button, .home-alphabetical-list-button"
       ).show();
       ).show();
       updateLastVisibleCard();
       updateLastVisibleCard();
Line 246: Line 246:
         .addClass("home-chronicle-block");
         .addClass("home-chronicle-block");
       $(
       $(
         ".home-chronicle-block-button, .home-random-block-button, .home-alphabetical-block-button",
         ".home-chronicle-block-button, .home-random-block-button, .home-alphabetical-block-button"
       ).show();
       ).show();
       $(
       $(
         ".home-chronicle-list-button, .home-random-list-button, .home-alphabetical-list-button",
         ".home-chronicle-list-button, .home-random-list-button, .home-alphabetical-list-button"
       ).hide();
       ).hide();
       updateLastVisibleCard();
       updateLastVisibleCard();
Line 350: Line 350:
     $(".home-list-view").show();
     $(".home-list-view").show();
     $(".chronicle-list-button, .list-view-button").addClass(
     $(".chronicle-list-button, .list-view-button").addClass(
       "active-view-button",
       "active-view-button"
     );
     );


Line 356: Line 356:
     // Initially hide block view sorting buttons and set the default sorted view for list
     // Initially hide block view sorting buttons and set the default sorted view for list
     $(
     $(
       ".chronicle-block-button, .random-block-button, .alphabetical-block-button",
       ".chronicle-block-button, .random-block-button, .alphabetical-block-button"
     ).hide();
     ).hide();
     sortChronologically(); // Sort the block view chronologically by default
     sortChronologically(); // Sort the block view chronologically by default
Line 378: Line 378:
       // Toggling visibility of buttons
       // Toggling visibility of buttons
       $(
       $(
         ".chronicle-block-button, .random-block-button, .alphabetical-block-button",
         ".chronicle-block-button, .random-block-button, .alphabetical-block-button"
       ).hide();
       ).hide();
       $(
       $(
         ".chronicle-list-button, .random-list-button, .alphabetical-list-button",
         ".chronicle-list-button, .random-list-button, .alphabetical-list-button"
       ).show();
       ).show();
       updateLastVisibleCard();
       updateLastVisibleCard();
Line 418: Line 418:
         .addClass("home-chronicle-block");
         .addClass("home-chronicle-block");
       $(
       $(
         ".chronicle-block-button, .random-block-button, .alphabetical-block-button",
         ".chronicle-block-button, .random-block-button, .alphabetical-block-button"
       ).show();
       ).show();
       $(
       $(
         ".chronicle-list-button, .random-list-button, .alphabetical-list-button",
         ".chronicle-list-button, .random-list-button, .alphabetical-list-button"
       ).hide();
       ).hide();
       updateLastVisibleCard();
       updateLastVisibleCard();
Line 673: Line 673:
     // Target only the list view container for updating the last visible card
     // Target only the list view container for updating the last visible card
     $(".home-chronicle-list div.list-container div.card").removeClass(
     $(".home-chronicle-list div.list-container div.card").removeClass(
       "last-visible",
       "last-visible"
     );
     );


     // Find the last visible card within the list view and add the class
     // Find the last visible card within the list view and add the class
     var lastVisibleCard = $(
     var lastVisibleCard = $(
       ".home-chronicle-list div.list-container div.card:visible:last",
       ".home-chronicle-list div.list-container div.card:visible:last"
     );
     );
     lastVisibleCard.addClass("last-visible");
     lastVisibleCard.addClass("last-visible");
Line 688: Line 688:
     $(".home-chronicle-block div.list-container div.card").css(
     $(".home-chronicle-block div.list-container div.card").css(
       "width",
       "width",
       "calc(20% - 0px)",
       "calc(20% - 0px)"
     );
     );
     $(
     $(
       ".home-chronicle-block div.list-container div.card:nth-child(5n + 1)",
       ".home-chronicle-block div.list-container div.card:nth-child(5n + 1)"
     ).css("width", "calc(20% + 4px)");
     ).css("width", "calc(20% + 4px)");
   }
   }
Line 768: Line 768:
         .find(".article-images")
         .find(".article-images")
         .html(
         .html(
           getImageHtml(image, currentIndex, images.length, enableNavigation),
           getImageHtml(image, currentIndex, images.length, enableNavigation)
         );
         );
     }
     }
Line 936: Line 936:
           '</p><p class="article-people">' +
           '</p><p class="article-people">' +
           peopleHtml +
           peopleHtml +
           "</p>",
           "</p>"
       );
       );
       var articleContentHtml = '<div class="article-title-link">';
       var articleContentHtml = '<div class="article-title-link">';
Line 1,071: Line 1,071:
           '</p><p class="article-people">' +
           '</p><p class="article-people">' +
           peopleHtml +
           peopleHtml +
           "</p>",
           "</p>"
       );
       );


Line 1,168: Line 1,168:
             '<div class="related-articles-label">Related Articles</div><div class="related-articles-container">' +
             '<div class="related-articles-label">Related Articles</div><div class="related-articles-container">' +
               relatedArticlesHtml +
               relatedArticlesHtml +
               "</div>",
               "</div>"
           )
           )
           .show();
           .show();
Line 1,211: Line 1,211:
       $(".home-chronicle-list div.list-container div.card div.type").css(
       $(".home-chronicle-list div.list-container div.card div.type").css(
         "left",
         "left",
         "90%",
         "90%"
       );
       );
     } else if ($(".home-chronicle-block").is(":visible")) {
     } else if ($(".home-chronicle-block").is(":visible")) {
Line 1,269: Line 1,269:
         '<a href="#" id="print-with-border" class="print-choice">show border</a> ' +
         '<a href="#" id="print-with-border" class="print-choice">show border</a> ' +
         '<a href="#" id="print-no-border" class="print-choice">hide border</a>' +
         '<a href="#" id="print-no-border" class="print-choice">hide border</a>' +
         "</div>",
         "</div>"
     );
     );
     jQuery("#print-button").after($chooser);
     jQuery("#print-button").after($chooser);
Line 1,304: Line 1,304:
   function updatePrintSelectionUI() {
   function updatePrintSelectionUI() {
     requestAnimationFrame(function () {
     requestAnimationFrame(function () {
      var activeCount = jQuery("#filters .values button.active").length;
        var activeCount = jQuery("#filters .values button.active").length;


      if (activeCount > 0) {
        if (activeCount > 0) {
         jQuery("#print-selection-wrapper").show();
         jQuery("#print-selection-wrapper").show();
      } else {
        } else {
         jQuery("#print-selection-wrapper").hide();
         jQuery("#print-selection-wrapper").hide();
         jQuery("#print-selection-options").hide();
         jQuery("#print-selection-options").hide();
      }
        }
     });
     });
   }
   }


   function hidePrintSelectionOptions() {
   function hidePrintSelectionOptions() {
    jQuery("#print-selection-options").hide();
        jQuery("#print-selection-options").hide();
  }
    }


   function swHandleBatchPrint(borderPref) {
   function swHandleBatchPrint(borderPref) {
Line 1,325: Line 1,325:


     if (!$cards.length) {
     if (!$cards.length) {
      alert("No entries to print.");
        alert("No entries to print.");
      return;
        return;
     }
     }


Line 1,332: Line 1,332:


     $cards.each(function () {
     $cards.each(function () {
      var $card = jQuery(this);
        var $card = jQuery(this);
      var title = $card.data("page");
        var title = $card.data("page");


      if (!title) return;
        if (!title) return;


      var url =
        var url =
         window.mw && mw.util && mw.util.getUrl
         window.mw && mw.util && mw.util.getUrl
          ? swPrintCacheBust(mw.util.getUrl(title))
            ? swPrintCacheBust(mw.util.getUrl(title))
          : swPrintCacheBust("/wiki/" + String(title));
            : swPrintCacheBust("/wiki/" + String(title));


      requests.push(
        requests.push(
         jQuery.get(url).then(function (html) {
         jQuery.get(url).then(function (html) {
          var $tmp = jQuery("<div>").html(html);
            var $tmp = jQuery("<div>").html(html);
          var $print = $tmp.find(".print-only").first();
            var $print = $tmp.find(".print-only").first();
          return $print.length ? $print.prop("outerHTML") : "";
            return $print.length ? $print.prop("outerHTML") : "";
         }),
         })
      );
        );
     });
     });


     Promise.all(requests)
     Promise.all(requests)
      .then(function (results) {
        .then(function (results) {
         var combinedHtml = results.join("");
         var combinedHtml = results.join("");


         if (!combinedHtml.trim()) {
         if (!combinedHtml.trim()) {
          alert("Could not generate print content.");
            alert("Could not generate print content.");
          return;
            return;
         }
         }


         swBuildIframeAndPrint(
         swBuildIframeAndPrint(
          combinedHtml,
            combinedHtml,
          borderPref,
            borderPref,
          null,
            null,
          "filtered.softwear.directory",
            "filtered.softwear.directory"
         );
         );
      })
        })
      .catch(function () {
        .catch(function () {
         alert("There was a problem preparing the print selection.");
         alert("There was a problem preparing the print selection.");
      });
        });
   }
   }


Line 1,399: Line 1,399:
     doc.open();
     doc.open();
     doc.write(
     doc.write(
       '<!doctype html><html><head><meta charset="utf-8"><title>Print</title></head><body></body></html>',
       '<!doctype html><html><head><meta charset="utf-8"><title>Print</title></head><body></body></html>'
     );
     );
     doc.close();
     doc.close();
Line 1,412: Line 1,412:
     linkCss.rel = "stylesheet";
     linkCss.rel = "stylesheet";
     linkCss.href = swPrintCacheBust(
     linkCss.href = swPrintCacheBust(
       "/index.php?title=MediaWiki:Print.css&action=raw&ctype=text/css",
       "/index.php?title=MediaWiki:Print.css&action=raw&ctype=text/css"
     );
     );


Line 1,435: Line 1,435:


     (function () {
     (function () {
      var pres = doc.querySelectorAll(".link-pdf pre");
        var pres = doc.querySelectorAll(".link-pdf pre");


      Array.prototype.forEach.call(pres, function (pre) {
        Array.prototype.forEach.call(pres, function (pre) {
        // move its children out
            // move its children out
        while (pre.firstChild) {
            while (pre.firstChild) {
          pre.parentNode.insertBefore(pre.firstChild, pre);
            pre.parentNode.insertBefore(pre.firstChild, pre);
        }
            }
        // remove the <pre>
            // remove the <pre>
        pre.parentNode.removeChild(pre);
            pre.parentNode.removeChild(pre);
      });
        });
     })();
     })();


Line 1,474: Line 1,474:
           htmlEl.className = (htmlEl.className || "").replace(
           htmlEl.className = (htmlEl.className || "").replace(
             /\bprint-no-border\b/g,
             /\bprint-no-border\b/g,
             "",
             ""
           );
           );
       }
       }
Line 1,610: Line 1,610:
             };
             };
           });
           });
         }),
         })
       );
       );
     }
     }
Line 1,650: Line 1,650:


         if (filenameOverride) {
         if (filenameOverride) {
          desiredTitle = filenameOverride;
        desiredTitle = filenameOverride;
         } else {
         } else {
          var entryNum = "";
        var entryNum = "";
          var numEl = doc.querySelector(".article-entry-number");
        var numEl = doc.querySelector(".article-entry-number");
          if (numEl) {
        if (numEl) {
             var m = (numEl.textContent || "").match(/\d+/);
             var m = (numEl.textContent || "").match(/\d+/);
             entryNum = m ? m[0] : "";
             entryNum = m ? m[0] : "";
          }
        }
          desiredTitle =
        desiredTitle =
             (entryNum ? entryNum + "." : "") + "softwear.directory";
             (entryNum ? entryNum + "." : "") + "softwear.directory";
         }
         }
         var oldIframeTitle = doc.title;
         var oldIframeTitle = doc.title;
         var oldParentTitle = document.title;
         var oldParentTitle = document.title;
Line 1,791: Line 1,792:
             if (k === "Enter" || k === 13 || k === " " || k === 32) fire(e);
             if (k === "Enter" || k === 13 || k === " " || k === 32) fire(e);
           },
           },
           true,
           true
         );
         );
       })(els[i]);
       })(els[i]);
Line 1,841: Line 1,842:
       // click directly on a choice link (fallback path)
       // click directly on a choice link (fallback path)
       var $choice = jQuery(e.target).closest(
       var $choice = jQuery(e.target).closest(
         "a#print-with-border, a#print-no-border",
         "a#print-with-border, a#print-no-border"
       );
       );
       if (!$choice.length) return;
       if (!$choice.length) return;
       e.preventDefault();
       e.preventDefault();
       swHandlePrintChoice($choice.attr("id"), $choice);
       swHandlePrintChoice($choice.attr("id"), $choice);
     },
     }
   );
   );


Line 1,855: Line 1,856:
     function (e) {
     function (e) {
       var host = this.closest(
       var host = this.closest(
         '[id="print-with-border"], [id="print-no-border"]',
         '[id="print-with-border"], [id="print-no-border"]'
       );
       );
       if (!host) return;
       if (!host) return;
       e.preventDefault();
       e.preventDefault();
       swHandlePrintChoice(host.id, (window.jQuery && jQuery(host)) || null);
       swHandlePrintChoice(host.id, (window.jQuery && jQuery(host)) || null);
     },
     }
   );
   );


Line 1,866: Line 1,867:
   jQuery(document).on("keydown.swprint", function (e) {
   jQuery(document).on("keydown.swprint", function (e) {
     if (e && e.keyCode === 27) {
     if (e && e.keyCode === 27) {
      swHidePrintUI();
        swHidePrintUI();
      hidePrintSelectionOptions();
        hidePrintSelectionOptions();
     }
     }
   });
   });
Line 1,873: Line 1,874:
   // toggle filtered print options
   // toggle filtered print options
   jQuery(document).on("click", ".print-selection-toggle", function (e) {
   jQuery(document).on("click", ".print-selection-toggle", function (e) {
    e.preventDefault();
        e.preventDefault();
    jQuery("#print-selection-options").toggle();
        jQuery("#print-selection-options").toggle();
  });
    });


   // run filtered batch print
   // run filtered batch print
Line 1,882: Line 1,883:
     ".print-selection-border, .print-selection-no-border",
     ".print-selection-border, .print-selection-no-border",
     function (e) {
     function (e) {
      e.preventDefault();
        e.preventDefault();


      var $btn = jQuery(this);
        var $btn = jQuery(this);
      var borderPref = $btn.hasClass("print-selection-no-border")
        var borderPref = $btn.hasClass("print-selection-no-border")
         ? "without"
         ? "without"
         : "with";
         : "with";


      // disable all related buttons
        // disable all related buttons
      jQuery(
        jQuery(".print-selection-border, .print-selection-no-border, .print-selection-toggle")
        ".print-selection-border, .print-selection-no-border, .print-selection-toggle",
      )
         .prop("disabled", true)
         .prop("disabled", true)
         .css("opacity", "0.5");
         .css("opacity", "0.5");


      // change ONLY clicked button text (native feeling)
        // change ONLY clicked button text (native feeling)
      var originalText = $btn.text();
        var originalText = $btn.text();
      $btn.text("working…");
        $btn.text("working…");


      swHandleBatchPrint(borderPref);
        swHandleBatchPrint(borderPref);


      // optional reset (if user comes back)
        // optional reset (if user comes back)
      setTimeout(function () {
        setTimeout(function () {
         $btn.text(originalText);
         $btn.text(originalText);
         jQuery(
         jQuery(".print-selection-border, .print-selection-no-border, .print-selection-toggle")
          ".print-selection-border, .print-selection-no-border, .print-selection-toggle",
            .prop("disabled", false)
        )
            .css("opacity", "");
          .prop("disabled", false)
        }, 2000);
          .css("opacity", "");
     }
      }, 2000);
     },
   );
   );


Line 1,952: Line 1,949:
       $(this).css("overflow-y", "hidden");
       $(this).css("overflow-y", "hidden");
       $(this).css("overflow-x", "hidden");
       $(this).css("overflow-x", "hidden");
     },
     }
   );
   );


Line 1,993: Line 1,990:


     jQuery(
     jQuery(
       "#show-article .article-description, #show-article .article-reflection",
       "#show-article .article-description, #show-article .article-reflection"
     ).each(function () {
     ).each(function () {
       var $el = jQuery(this);
       var $el = jQuery(this);
Line 2,097: Line 2,094:
   // Tooltip for "wander elsewhere..." on .card.event
   // Tooltip for "wander elsewhere..." on .card.event
   var tooltip = $(
   var tooltip = $(
     '<div class="tooltip-popup">wander elsewhere...</div>',
     '<div class="tooltip-popup">wander elsewhere...</div>'
   ).appendTo("body");
   ).appendTo("body");


Line 2,134: Line 2,131:
   updatePrintSelectionUI();
   updatePrintSelectionUI();
   hidePrintSelectionOptions();
   hidePrintSelectionOptions();
});
/* Load related articles only when a directory card is opened */
jQuery(function ($) {
    $(".card:not(.event)").on("click.softwearRelated", function (event) {
        if ($(event.target).closest(".people a, .type a").length) return;
        const card = $(this);
        const entry = String(card.data("page") || "");
        const subjects = [...new Set(
            card.find(".subject a").map(function () {
                return $(this).attr("title") || $(this).text().trim();
            }).get()
        )];
        const container = $("#related-articles");
        container.empty().hide();
        if (!subjects.length || !entry) return;
        const fields = "Entry number|People|Title|Type|Subject|Pdf|Link|Entity|Discipline|Description|Reflection|Quote|Image|CaptionImage|Image2|CaptionImage2|Image3|CaptionImage3|Image4|CaptionImage4|Image5|CaptionImage5|Date|External reference|Modification date"
            .split("|").map(field => "|?" + field).join("");
        const api = new mw.Api();
        const requests = subjects.map(subject => {
            const query =
                "{{#ask: [[Class::Entry]] [[Subject::" + subject + "]]" +
                " [[Entry number::!" + entry + "]] [[Type::!Event]]" +
                fields +
                "|format=template|template=ShowRelatedArticles" +
                "|limit=4|named args=yes}}";
            return api.get({
                action: "parse",
                title: "Main_Page",
                text: query,
                prop: "text"
            }).then(result => result.parse.text["*"], () => "");
        });
        Promise.all(requests).then(results => {
            // Ignore results if another card was opened meanwhile.
            if (String(window.currentEntryTitle) !== entry) return;
            const seen = new Set();
            const articles = [];
            results.forEach(html => {
                const temporary = $("<div>").html(html);
                temporary.find(".related-article").each(function () {
                    const article = $(this);
                    const number = article.find(".related-article-entry-number").text().trim();
                    if (!number || seen.has(number) || articles.length >= 4) return;
                    seen.add(number);
                    articles.push(article.prop("outerHTML"));
                });
            });
            if (articles.length) {
                container.html(
                    '<div class="related-articles-label">Related Articles</div>' +
                    '<div class="related-articles-container">' +
                    articles.join("") +
                    '</div>'
                ).show();
            }
        });
    });
});
});