MediaWiki:Common.js: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
No edit summary
Tag: Reverted
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(combinedHtml, borderPref);
         swBuildIframeAndPrint(
        })
          combinedHtml,
        .catch(function () {
          borderPref,
          null,
          "filtered.softwear.directory",
        );
      })
      .catch(function () {
         alert("There was a problem preparing the print selection.");
         alert("There was a problem preparing the print selection.");
        });
      });
   }
   }


Line 1,380: Line 1,385:


   /* core: build iframe and print */
   /* core: build iframe and print */
   function swBuildIframeAndPrint(printHtml, borderPref, $btn) {
   function swBuildIframeAndPrint(
    printHtml,
    borderPref,
    $btn,
    filenameOverride,
  ) {
     // iframe
     // iframe
     var iframe = document.createElement("iframe");
     var iframe = document.createElement("iframe");
Line 1,394: Line 1,404:
     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,407: Line 1,417:
     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,430: Line 1,440:


     (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,469: Line 1,479:
           htmlEl.className = (htmlEl.className || "").replace(
           htmlEl.className = (htmlEl.className || "").replace(
             /\bprint-no-border\b/g,
             /\bprint-no-border\b/g,
             ""
             "",
           );
           );
       }
       }
Line 1,605: Line 1,615:
             };
             };
           });
           });
         })
         }),
       );
       );
     }
     }
Line 1,642: Line 1,652:
       .then(function () {
       .then(function () {
         // filename via document.title
         // filename via document.title
         var entryNum = "";
         var desiredTitle;
        var numEl = doc.querySelector(".article-entry-number");
 
        if (numEl) {
        if (filenameOverride) {
          var m = (numEl.textContent || "").match(/\d+/);
          desiredTitle = filenameOverride;
          entryNum = m ? m[0] : "";
        } else {
          var entryNum = "";
          var numEl = doc.querySelector(".article-entry-number");
          if (numEl) {
            var m = (numEl.textContent || "").match(/\d+/);
            entryNum = m ? m[0] : "";
          }
          desiredTitle =
            (entryNum ? entryNum + "." : "") + "softwear.directory";
         }
         }
        var desiredTitle =
          (entryNum ? entryNum + "." : "") + "softwear.directory";
         var oldIframeTitle = doc.title;
         var oldIframeTitle = doc.title;
         var oldParentTitle = document.title;
         var oldParentTitle = document.title;
Line 1,780: Line 1,796:
             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,830: Line 1,846:
       // 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,844: Line 1,860:
     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,855: Line 1,871:
   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,862: Line 1,878:
   // 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,871: Line 1,887:
     ".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(".print-selection-border, .print-selection-no-border, .print-selection-toggle")
      jQuery(
        ".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(".print-selection-border, .print-selection-no-border, .print-selection-toggle")
         jQuery(
            .prop("disabled", false)
          ".print-selection-border, .print-selection-no-border, .print-selection-toggle",
            .css("opacity", "");
        )
        }, 2000);
          .prop("disabled", false)
     }
          .css("opacity", "");
      }, 2000);
     },
   );
   );


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


Line 1,978: Line 1,998:


     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,082: Line 2,102:
   // 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");


Navigation menu